[CRYPTO] salsa20_i586: Salsa20 stream cipher algorithm (i586 version)
[safe/jmp/linux-2.6] / arch / x86 / crypto / salsa20_glue.c
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  *
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)
15  * any later version.
16  *
17  */
18
19 #include <crypto/algapi.h>
20 #include <linux/module.h>
21 #include <linux/crypto.h>
22
23 #define SALSA20_IV_SIZE        8U
24 #define SALSA20_MIN_KEY_SIZE  16U
25 #define SALSA20_MAX_KEY_SIZE  32U
26
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
31
32 struct salsa20_ctx
33 {
34         u32 input[16];
35 };
36
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);
42
43 static int setkey(struct crypto_tfm *tfm, const u8 *key,
44                   unsigned int keysize)
45 {
46         struct salsa20_ctx *ctx = crypto_tfm_ctx(tfm);
47         salsa20_keysetup(ctx, key, keysize*8, SALSA20_IV_SIZE*8);
48         return 0;
49 }
50
51 static int encrypt(struct blkcipher_desc *desc,
52                    struct scatterlist *dst, struct scatterlist *src,
53                    unsigned int nbytes)
54 {
55         struct blkcipher_walk walk;
56         struct crypto_blkcipher *tfm = desc->tfm;
57         struct salsa20_ctx *ctx = crypto_blkcipher_ctx(tfm);
58         int err;
59
60         blkcipher_walk_init(&walk, dst, src, nbytes);
61         err = blkcipher_walk_virt_block(desc, &walk, 64);
62
63         salsa20_ivsetup(ctx, walk.iv);
64
65         if (likely(walk.nbytes == nbytes))
66         {
67                 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
68                                       walk.dst.virt.addr, nbytes);
69                 return blkcipher_walk_done(desc, &walk, 0);
70         }
71
72         while (walk.nbytes >= 64) {
73                 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
74                                       walk.dst.virt.addr,
75                                       walk.nbytes - (walk.nbytes % 64));
76                 err = blkcipher_walk_done(desc, &walk, walk.nbytes % 64);
77         }
78
79         if (walk.nbytes) {
80                 salsa20_encrypt_bytes(ctx, walk.src.virt.addr,
81                                       walk.dst.virt.addr, walk.nbytes);
82                 err = blkcipher_walk_done(desc, &walk, 0);
83         }
84
85         return err;
86 }
87
88 static struct crypto_alg alg = {
89         .cra_name           =   "salsa20",
90         .cra_driver_name    =   "salsa20-asm",
91         .cra_priority       =   200,
92         .cra_flags          =   CRYPTO_ALG_TYPE_BLKCIPHER,
93         .cra_type           =   &crypto_blkcipher_type,
94         .cra_blocksize      =   1,
95         .cra_ctxsize        =   sizeof(struct salsa20_ctx),
96         .cra_alignmask      =   3,
97         .cra_module         =   THIS_MODULE,
98         .cra_list           =   LIST_HEAD_INIT(alg.cra_list),
99         .cra_u              =   {
100                 .blkcipher = {
101                         .setkey         =   setkey,
102                         .encrypt        =   encrypt,
103                         .decrypt        =   encrypt,
104                         .min_keysize    =   SALSA20_MIN_KEY_SIZE,
105                         .max_keysize    =   SALSA20_MAX_KEY_SIZE,
106                         .ivsize         =   SALSA20_IV_SIZE,
107                 }
108         }
109 };
110
111 static int __init init(void)
112 {
113         return crypto_register_alg(&alg);
114 }
115
116 static void __exit fini(void)
117 {
118         crypto_unregister_alg(&alg);
119 }
120
121 module_init(init);
122 module_exit(fini);
123
124 MODULE_LICENSE("GPL");
125 MODULE_DESCRIPTION ("Salsa20 stream cipher algorithm (optimized assembly version)");
126 MODULE_ALIAS("salsa20");
127 MODULE_ALIAS("salsa20-asm");