GNU Linux-libre 4.9.337-gnu1
[releases.git] / crypto / chacha20_generic.c
1 /*
2  * ChaCha20 256-bit cipher algorithm, RFC7539
3  *
4  * Copyright (C) 2015 Martin Willi
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <crypto/algapi.h>
13 #include <linux/crypto.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <crypto/chacha20.h>
17
18 static inline u32 le32_to_cpuvp(const void *p)
19 {
20         return le32_to_cpup(p);
21 }
22
23 static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
24                              unsigned int bytes)
25 {
26         /* aligned to potentially speed up crypto_xor() */
27         u8 stream[CHACHA20_BLOCK_SIZE] __aligned(sizeof(long));
28
29         if (dst != src)
30                 memcpy(dst, src, bytes);
31
32         while (bytes >= CHACHA20_BLOCK_SIZE) {
33                 chacha20_block(state, stream);
34                 crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE);
35                 bytes -= CHACHA20_BLOCK_SIZE;
36                 dst += CHACHA20_BLOCK_SIZE;
37         }
38         if (bytes) {
39                 chacha20_block(state, stream);
40                 crypto_xor(dst, stream, bytes);
41         }
42 }
43
44 void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
45 {
46         static const char constant[16] = "expand 32-byte k";
47
48         state[0]  = le32_to_cpuvp(constant +  0);
49         state[1]  = le32_to_cpuvp(constant +  4);
50         state[2]  = le32_to_cpuvp(constant +  8);
51         state[3]  = le32_to_cpuvp(constant + 12);
52         state[4]  = ctx->key[0];
53         state[5]  = ctx->key[1];
54         state[6]  = ctx->key[2];
55         state[7]  = ctx->key[3];
56         state[8]  = ctx->key[4];
57         state[9]  = ctx->key[5];
58         state[10] = ctx->key[6];
59         state[11] = ctx->key[7];
60         state[12] = le32_to_cpuvp(iv +  0);
61         state[13] = le32_to_cpuvp(iv +  4);
62         state[14] = le32_to_cpuvp(iv +  8);
63         state[15] = le32_to_cpuvp(iv + 12);
64 }
65 EXPORT_SYMBOL_GPL(crypto_chacha20_init);
66
67 int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
68                            unsigned int keysize)
69 {
70         struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm);
71         int i;
72
73         if (keysize != CHACHA20_KEY_SIZE)
74                 return -EINVAL;
75
76         for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
77                 ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32));
78
79         return 0;
80 }
81 EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
82
83 int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
84                           struct scatterlist *src, unsigned int nbytes)
85 {
86         struct blkcipher_walk walk;
87         u32 state[16];
88         int err;
89
90         blkcipher_walk_init(&walk, dst, src, nbytes);
91         err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
92
93         crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
94
95         while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
96                 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
97                                  rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
98                 err = blkcipher_walk_done(desc, &walk,
99                                           walk.nbytes % CHACHA20_BLOCK_SIZE);
100         }
101
102         if (walk.nbytes) {
103                 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
104                                  walk.nbytes);
105                 err = blkcipher_walk_done(desc, &walk, 0);
106         }
107
108         return err;
109 }
110 EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
111
112 static struct crypto_alg alg = {
113         .cra_name               = "chacha20",
114         .cra_driver_name        = "chacha20-generic",
115         .cra_priority           = 100,
116         .cra_flags              = CRYPTO_ALG_TYPE_BLKCIPHER,
117         .cra_blocksize          = 1,
118         .cra_type               = &crypto_blkcipher_type,
119         .cra_ctxsize            = sizeof(struct chacha20_ctx),
120         .cra_alignmask          = sizeof(u32) - 1,
121         .cra_module             = THIS_MODULE,
122         .cra_u                  = {
123                 .blkcipher = {
124                         .min_keysize    = CHACHA20_KEY_SIZE,
125                         .max_keysize    = CHACHA20_KEY_SIZE,
126                         .ivsize         = CHACHA20_IV_SIZE,
127                         .geniv          = "seqiv",
128                         .setkey         = crypto_chacha20_setkey,
129                         .encrypt        = crypto_chacha20_crypt,
130                         .decrypt        = crypto_chacha20_crypt,
131                 },
132         },
133 };
134
135 static int __init chacha20_generic_mod_init(void)
136 {
137         return crypto_register_alg(&alg);
138 }
139
140 static void __exit chacha20_generic_mod_fini(void)
141 {
142         crypto_unregister_alg(&alg);
143 }
144
145 module_init(chacha20_generic_mod_init);
146 module_exit(chacha20_generic_mod_fini);
147
148 MODULE_LICENSE("GPL");
149 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
150 MODULE_DESCRIPTION("chacha20 cipher algorithm");
151 MODULE_ALIAS_CRYPTO("chacha20");
152 MODULE_ALIAS_CRYPTO("chacha20-generic");