2 * Glue code for optimized assembly version of Salsa20.
4 * Copyright (c) 2007 Tan Swee Heng <thesweeheng@gmail.com>
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>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
19 #include <crypto/algapi.h>
20 #include <linux/module.h>
21 #include <linux/crypto.h>
23 #define SALSA20_IV_SIZE 8U
24 #define SALSA20_MIN_KEY_SIZE 16U
25 #define SALSA20_MAX_KEY_SIZE 32U
27 // use the ECRYPT_* function names
28 #define salsa20_keysetup ECRYPT_keysetup
29 #define salsa20_ivsetup ECRYPT_ivsetup
30 #define salsa20_encrypt_bytes ECRYPT_encrypt_bytes
37 asmlinkage void salsa20_keysetup(struct salsa20_ctx *ctx, const u8 *k,
38 u32 keysize, u32 ivsize);
39 asmlinkage void salsa20_ivsetup(struct salsa20_ctx *ctx, const u8 *iv);
40 asmlinkage void salsa20_encrypt_bytes(struct salsa20_ctx *ctx,
41 const u8 *src, u8 *dst, u32 bytes);
43 static int setkey(struct crypto_tfm *tfm, const u8 *key,
46 struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
47 salsa20_keysetup(ctx, key, keysize*8, SALSA20_IV_SIZE*8);
51 static int encrypt(struct blkcipher_desc *desc,
52 struct scatterlist *dst, struct scatterlist *src,
55 struct blkcipher_walk walk;
56 struct crypto_blkcipher *tfm = desc->tfm;
57 struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
60 blkcipher_walk_init(&walk, dst, src, nbytes);
61 err = blkcipher_walk_virt_block(desc, &walk, 64);
63 salsa20_ivsetup(ctx, walk.iv);
65 if (likely(walk.nbytes == nbytes))
67 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
68 walk.dst.virt.addr, nbytes);
69 return blkcipher_walk_done(desc, &walk, 0);
72 while (walk.nbytes >= 64) {
73 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
75 walk.nbytes - (walk.nbytes % 64));
76 err = blkcipher_walk_done(desc, &walk, walk.nbytes % 64);
80 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
81 walk.dst.virt.addr, walk.nbytes);
82 err = blkcipher_walk_done(desc, &walk, 0);
88 static struct crypto_alg alg = {
89 .cra_name = "salsa20",
90 .cra_driver_name = "salsa20-asm",
92 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
93 .cra_type = &crypto_blkcipher_type,
95 .cra_ctxsize = sizeof(struct salsa20_ctx),
97 .cra_module = THIS_MODULE,
98 .cra_list = LIST_HEAD_INIT(alg.cra_list),
104 .min_keysize = SALSA20_MIN_KEY_SIZE,
105 .max_keysize = SALSA20_MAX_KEY_SIZE,
106 .ivsize = SALSA20_IV_SIZE,
111 static int __init init(void)
113 return crypto_register_alg(&alg);
116 static void __exit fini(void)
118 crypto_unregister_alg(&alg);
124 MODULE_LICENSE("GPL");
125 MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm (optimized assembly version)");
126 MODULE_ALIAS("salsa20");
127 MODULE_ALIAS("salsa20-asm");