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>
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>
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)
21 #include <crypto/algapi.h>
22 #include <linux/module.h>
23 #include <linux/crypto.h>
25 #define SALSA20_IV_SIZE 8U
26 #define SALSA20_MIN_KEY_SIZE 16U
27 #define SALSA20_MAX_KEY_SIZE 32U
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
39 asmlinkage void salsa20_keysetup(struct salsa20_ctx *ctx, const u8 *k,
40 u32 keysize, u32 ivsize);
41 asmlinkage void salsa20_ivsetup(struct salsa20_ctx *ctx, const u8 *iv);
42 asmlinkage void salsa20_encrypt_bytes(struct salsa20_ctx *ctx,
43 const u8 *src, u8 *dst, u32 bytes);
45 static int setkey(struct crypto_tfm *tfm, const u8 *key,
48 struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
49 salsa20_keysetup(ctx, key, keysize*8, SALSA20_IV_SIZE*8);
53 static int encrypt(struct blkcipher_desc *desc,
54 struct scatterlist *dst, struct scatterlist *src,
57 struct blkcipher_walk walk;
58 struct crypto_blkcipher *tfm = desc->tfm;
59 struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
62 blkcipher_walk_init(&walk, dst, src, nbytes);
63 err = blkcipher_walk_virt_block(desc, &walk, 64);
65 salsa20_ivsetup(ctx, walk.iv);
67 if (likely(walk.nbytes == nbytes))
69 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
70 walk.dst.virt.addr, nbytes);
71 return blkcipher_walk_done(desc, &walk, 0);
74 while (walk.nbytes >= 64) {
75 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
77 walk.nbytes - (walk.nbytes % 64));
78 err = blkcipher_walk_done(desc, &walk, walk.nbytes % 64);
82 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
83 walk.dst.virt.addr, walk.nbytes);
84 err = blkcipher_walk_done(desc, &walk, 0);
90 static struct crypto_alg alg = {
91 .cra_name = "salsa20",
92 .cra_driver_name = "salsa20-asm",
94 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
95 .cra_type = &crypto_blkcipher_type,
97 .cra_ctxsize = sizeof(struct salsa20_ctx),
99 .cra_module = THIS_MODULE,
105 .min_keysize = SALSA20_MIN_KEY_SIZE,
106 .max_keysize = SALSA20_MAX_KEY_SIZE,
107 .ivsize = SALSA20_IV_SIZE,
112 static int __init init(void)
114 return crypto_register_alg(&alg);
117 static void __exit fini(void)
119 crypto_unregister_alg(&alg);
125 MODULE_LICENSE("GPL");
126 MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm (optimized assembly version)");
127 MODULE_ALIAS("salsa20");
128 MODULE_ALIAS("salsa20-asm");