Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Glue code for optimized assembly version of  Salsa20.
  3 *
  4 * Copyright (c) 2007 Tan Swee Heng <thesweeheng@gmail.com>
  5 *
  6 * The assembly codes are public domain assembly codes written by Daniel. J.
  7 * Bernstein <djb@cr.yp.to>. The codes are modified to include indentation
  8 * and to remove extraneous comments and functions that are not needed.
  9 * - i586 version, renamed as salsa20-i586-asm_32.S
 10 *   available from <http://cr.yp.to/snuffle/salsa20/x86-pm/salsa20.s>
 11 * - x86-64 version, renamed as salsa20-x86_64-asm_64.S
 12 *   available from <http://cr.yp.to/snuffle/salsa20/amd64-3/salsa20.s>
 13 *
 
 
 
 14 * This program is free software; you can redistribute it and/or modify it
 15 * under the terms of the GNU General Public License as published by the Free
 16 * Software Foundation; either version 2 of the License, or (at your option)
 17 * any later version.
 18 *
 19 */
 20
 21#include <crypto/algapi.h>
 
 
 22#include <linux/module.h>
 23#include <linux/crypto.h>
 24
 25#define SALSA20_IV_SIZE        8U
 26#define SALSA20_MIN_KEY_SIZE  16U
 27#define SALSA20_MAX_KEY_SIZE  32U
 28
 29// use the ECRYPT_* function names
 30#define salsa20_keysetup        ECRYPT_keysetup
 31#define salsa20_ivsetup         ECRYPT_ivsetup
 32#define salsa20_encrypt_bytes   ECRYPT_encrypt_bytes
 33
 34struct salsa20_ctx
 35{
 36	u32 input[16];
 37};
 38
 39asmlinkage void salsa20_keysetup(struct salsa20_ctx *ctx, const u8 *k,
 40				 u32 keysize, u32 ivsize);
 41asmlinkage void salsa20_ivsetup(struct salsa20_ctx *ctx, const u8 *iv);
 42asmlinkage void salsa20_encrypt_bytes(struct salsa20_ctx *ctx,
 43				      const u8 *src, u8 *dst, u32 bytes);
 44
 45static int setkey(struct crypto_tfm *tfm, const u8 *key,
 46		  unsigned int keysize)
 47{
 48	struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
 49	salsa20_keysetup(ctx, key, keysize*8, SALSA20_IV_SIZE*8);
 50	return 0;
 51}
 52
 53static int encrypt(struct blkcipher_desc *desc,
 54		   struct scatterlist *dst, struct scatterlist *src,
 55		   unsigned int nbytes)
 56{
 57	struct blkcipher_walk walk;
 58	struct crypto_blkcipher *tfm = desc->tfm;
 59	struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
 60	int err;
 61
 62	blkcipher_walk_init(&walk, dst, src, nbytes);
 63	err = blkcipher_walk_virt_block(desc, &walk, 64);
 64
 65	salsa20_ivsetup(ctx, walk.iv);
 66
 67	if (likely(walk.nbytes == nbytes))
 68	{
 69		salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
 70				      walk.dst.virt.addr, nbytes);
 71		return blkcipher_walk_done(desc, &walk, 0);
 72	}
 73
 74	while (walk.nbytes >= 64) {
 75		salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
 76				      walk.dst.virt.addr,
 77				      walk.nbytes - (walk.nbytes % 64));
 78		err = blkcipher_walk_done(desc, &walk, walk.nbytes % 64);
 79	}
 80
 81	if (walk.nbytes) {
 82		salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
 83				      walk.dst.virt.addr, walk.nbytes);
 84		err = blkcipher_walk_done(desc, &walk, 0);
 85	}
 86
 87	return err;
 88}
 89
 90static struct crypto_alg alg = {
 91	.cra_name           =   "salsa20",
 92	.cra_driver_name    =   "salsa20-asm",
 93	.cra_priority       =   200,
 94	.cra_flags          =   CRYPTO_ALG_TYPE_BLKCIPHER,
 95	.cra_type           =   &crypto_blkcipher_type,
 96	.cra_blocksize      =   1,
 97	.cra_ctxsize        =   sizeof(struct salsa20_ctx),
 98	.cra_alignmask      =	3,
 99	.cra_module         =   THIS_MODULE,
100	.cra_list           =   LIST_HEAD_INIT(alg.cra_list),
101	.cra_u              =   {
102		.blkcipher = {
103			.setkey         =   setkey,
104			.encrypt        =   encrypt,
105			.decrypt        =   encrypt,
106			.min_keysize    =   SALSA20_MIN_KEY_SIZE,
107			.max_keysize    =   SALSA20_MAX_KEY_SIZE,
108			.ivsize         =   SALSA20_IV_SIZE,
109		}
110	}
111};
112
113static int __init init(void)
114{
115	return crypto_register_alg(&alg);
116}
117
118static void __exit fini(void)
119{
120	crypto_unregister_alg(&alg);
121}
122
123module_init(init);
124module_exit(fini);
125
126MODULE_LICENSE("GPL");
127MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm (optimized assembly version)");
128MODULE_ALIAS("salsa20");
129MODULE_ALIAS("salsa20-asm");
v4.17
 1/*
 2 * Glue code for optimized assembly version of  Salsa20.
 3 *
 4 * Copyright (c) 2007 Tan Swee Heng <thesweeheng@gmail.com>
 5 *
 6 * The assembly codes are public domain assembly codes written by Daniel. J.
 7 * Bernstein <djb@cr.yp.to>. The codes are modified to include indentation
 8 * and to remove extraneous comments and functions that are not needed.
 9 * - i586 version, renamed as salsa20-i586-asm_32.S
10 *   available from <http://cr.yp.to/snuffle/salsa20/x86-pm/salsa20.s>
11 * - x86-64 version, renamed as salsa20-x86_64-asm_64.S
12 *   available from <http://cr.yp.to/snuffle/salsa20/amd64-3/salsa20.s>
13 *
14 * Also modified to set up the initial state using the generic C code rather
15 * than in assembly.
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
20 * any later version.
21 *
22 */
23
24#include <asm/unaligned.h>
25#include <crypto/internal/skcipher.h>
26#include <crypto/salsa20.h>
27#include <linux/module.h>
 
28
29asmlinkage void salsa20_encrypt_bytes(u32 state[16], const u8 *src, u8 *dst,
30				      u32 bytes);
 
 
 
 
 
 
31
32static int salsa20_asm_crypt(struct skcipher_request *req)
33{
34	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
35	const struct salsa20_ctx *ctx = crypto_skcipher_ctx(tfm);
36	struct skcipher_walk walk;
37	u32 state[16];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38	int err;
39
40	err = skcipher_walk_virt(&walk, req, true);
 
41
42	crypto_salsa20_init(state, ctx, walk.iv);
43
44	while (walk.nbytes > 0) {
45		unsigned int nbytes = walk.nbytes;
 
 
 
 
46
47		if (nbytes < walk.total)
48			nbytes = round_down(nbytes, walk.stride);
 
 
 
 
49
50		salsa20_encrypt_bytes(state, walk.src.virt.addr,
51				      walk.dst.virt.addr, nbytes);
52		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
 
53	}
54
55	return err;
56}
57
58static struct skcipher_alg alg = {
59	.base.cra_name		= "salsa20",
60	.base.cra_driver_name	= "salsa20-asm",
61	.base.cra_priority	= 200,
62	.base.cra_blocksize	= 1,
63	.base.cra_ctxsize	= sizeof(struct salsa20_ctx),
64	.base.cra_module	= THIS_MODULE,
65
66	.min_keysize		= SALSA20_MIN_KEY_SIZE,
67	.max_keysize		= SALSA20_MAX_KEY_SIZE,
68	.ivsize			= SALSA20_IV_SIZE,
69	.chunksize		= SALSA20_BLOCK_SIZE,
70	.setkey			= crypto_salsa20_setkey,
71	.encrypt		= salsa20_asm_crypt,
72	.decrypt		= salsa20_asm_crypt,
 
 
 
 
 
 
73};
74
75static int __init init(void)
76{
77	return crypto_register_skcipher(&alg);
78}
79
80static void __exit fini(void)
81{
82	crypto_unregister_skcipher(&alg);
83}
84
85module_init(init);
86module_exit(fini);
87
88MODULE_LICENSE("GPL");
89MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm (optimized assembly version)");
90MODULE_ALIAS_CRYPTO("salsa20");
91MODULE_ALIAS_CRYPTO("salsa20-asm");