|
@@ -11,6 +11,9 @@
|
|
|
* - x86-64 version, renamed as salsa20-x86_64-asm_64.S
|
|
|
* available from <http://cr.yp.to/snuffle/salsa20/amd64-3/salsa20.s>
|
|
|
*
|
|
|
+ * Also modified to set up the initial state using the generic C code rather
|
|
|
+ * than in assembly.
|
|
|
+ *
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
@@ -18,93 +21,65 @@
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
-#include <crypto/algapi.h>
|
|
|
+#include <asm/unaligned.h>
|
|
|
+#include <crypto/internal/skcipher.h>
|
|
|
+#include <crypto/salsa20.h>
|
|
|
#include <linux/module.h>
|
|
|
-#include <linux/crypto.h>
|
|
|
-
|
|
|
-#define SALSA20_IV_SIZE 8U
|
|
|
-#define SALSA20_MIN_KEY_SIZE 16U
|
|
|
-#define SALSA20_MAX_KEY_SIZE 32U
|
|
|
-
|
|
|
-struct salsa20_ctx
|
|
|
-{
|
|
|
- u32 input[16];
|
|
|
-};
|
|
|
|
|
|
-asmlinkage void salsa20_keysetup(struct salsa20_ctx *ctx, const u8 *k,
|
|
|
- u32 keysize, u32 ivsize);
|
|
|
-asmlinkage void salsa20_ivsetup(struct salsa20_ctx *ctx, const u8 *iv);
|
|
|
-asmlinkage void salsa20_encrypt_bytes(struct salsa20_ctx *ctx,
|
|
|
- const u8 *src, u8 *dst, u32 bytes);
|
|
|
+asmlinkage void salsa20_encrypt_bytes(u32 state[16], const u8 *src, u8 *dst,
|
|
|
+ u32 bytes);
|
|
|
|
|
|
-static int setkey(struct crypto_tfm *tfm, const u8 *key,
|
|
|
- unsigned int keysize)
|
|
|
+static int salsa20_asm_crypt(struct skcipher_request *req)
|
|
|
{
|
|
|
- struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
|
|
|
- salsa20_keysetup(ctx, key, keysize*8, SALSA20_IV_SIZE*8);
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int encrypt(struct blkcipher_desc *desc,
|
|
|
- struct scatterlist *dst, struct scatterlist *src,
|
|
|
- unsigned int nbytes)
|
|
|
-{
|
|
|
- struct blkcipher_walk walk;
|
|
|
- struct crypto_blkcipher *tfm = desc->tfm;
|
|
|
- struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
|
|
|
+ struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
+ const struct salsa20_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
+ struct skcipher_walk walk;
|
|
|
+ u32 state[16];
|
|
|
int err;
|
|
|
|
|
|
- blkcipher_walk_init(&walk, dst, src, nbytes);
|
|
|
- err = blkcipher_walk_virt_block(desc, &walk, 64);
|
|
|
+ err = skcipher_walk_virt(&walk, req, true);
|
|
|
|
|
|
- salsa20_ivsetup(ctx, walk.iv);
|
|
|
+ crypto_salsa20_init(state, ctx, walk.iv);
|
|
|
|
|
|
- while (walk.nbytes >= 64) {
|
|
|
- salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
|
|
|
- walk.dst.virt.addr,
|
|
|
- walk.nbytes - (walk.nbytes % 64));
|
|
|
- err = blkcipher_walk_done(desc, &walk, walk.nbytes % 64);
|
|
|
- }
|
|
|
+ while (walk.nbytes > 0) {
|
|
|
+ unsigned int nbytes = walk.nbytes;
|
|
|
|
|
|
- if (walk.nbytes) {
|
|
|
- salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
|
|
|
- walk.dst.virt.addr, walk.nbytes);
|
|
|
- err = blkcipher_walk_done(desc, &walk, 0);
|
|
|
+ if (nbytes < walk.total)
|
|
|
+ nbytes = round_down(nbytes, walk.stride);
|
|
|
+
|
|
|
+ salsa20_encrypt_bytes(state, walk.src.virt.addr,
|
|
|
+ walk.dst.virt.addr, nbytes);
|
|
|
+ err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
|
|
|
}
|
|
|
|
|
|
return err;
|
|
|
}
|
|
|
|
|
|
-static struct crypto_alg alg = {
|
|
|
- .cra_name = "salsa20",
|
|
|
- .cra_driver_name = "salsa20-asm",
|
|
|
- .cra_priority = 200,
|
|
|
- .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
|
|
|
- .cra_type = &crypto_blkcipher_type,
|
|
|
- .cra_blocksize = 1,
|
|
|
- .cra_ctxsize = sizeof(struct salsa20_ctx),
|
|
|
- .cra_alignmask = 3,
|
|
|
- .cra_module = THIS_MODULE,
|
|
|
- .cra_u = {
|
|
|
- .blkcipher = {
|
|
|
- .setkey = setkey,
|
|
|
- .encrypt = encrypt,
|
|
|
- .decrypt = encrypt,
|
|
|
- .min_keysize = SALSA20_MIN_KEY_SIZE,
|
|
|
- .max_keysize = SALSA20_MAX_KEY_SIZE,
|
|
|
- .ivsize = SALSA20_IV_SIZE,
|
|
|
- }
|
|
|
- }
|
|
|
+static struct skcipher_alg alg = {
|
|
|
+ .base.cra_name = "salsa20",
|
|
|
+ .base.cra_driver_name = "salsa20-asm",
|
|
|
+ .base.cra_priority = 200,
|
|
|
+ .base.cra_blocksize = 1,
|
|
|
+ .base.cra_ctxsize = sizeof(struct salsa20_ctx),
|
|
|
+ .base.cra_module = THIS_MODULE,
|
|
|
+
|
|
|
+ .min_keysize = SALSA20_MIN_KEY_SIZE,
|
|
|
+ .max_keysize = SALSA20_MAX_KEY_SIZE,
|
|
|
+ .ivsize = SALSA20_IV_SIZE,
|
|
|
+ .chunksize = SALSA20_BLOCK_SIZE,
|
|
|
+ .setkey = crypto_salsa20_setkey,
|
|
|
+ .encrypt = salsa20_asm_crypt,
|
|
|
+ .decrypt = salsa20_asm_crypt,
|
|
|
};
|
|
|
|
|
|
static int __init init(void)
|
|
|
{
|
|
|
- return crypto_register_alg(&alg);
|
|
|
+ return crypto_register_skcipher(&alg);
|
|
|
}
|
|
|
|
|
|
static void __exit fini(void)
|
|
|
{
|
|
|
- crypto_unregister_alg(&alg);
|
|
|
+ crypto_unregister_skcipher(&alg);
|
|
|
}
|
|
|
|
|
|
module_init(init);
|