Loading...
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 <asm/unaligned.h>
13#include <crypto/algapi.h>
14#include <crypto/chacha20.h>
15#include <crypto/internal/skcipher.h>
16#include <linux/module.h>
17
18static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
19 unsigned int bytes)
20{
21 u32 stream[CHACHA20_BLOCK_WORDS];
22
23 if (dst != src)
24 memcpy(dst, src, bytes);
25
26 while (bytes >= CHACHA20_BLOCK_SIZE) {
27 chacha20_block(state, stream);
28 crypto_xor(dst, (const u8 *)stream, CHACHA20_BLOCK_SIZE);
29 bytes -= CHACHA20_BLOCK_SIZE;
30 dst += CHACHA20_BLOCK_SIZE;
31 }
32 if (bytes) {
33 chacha20_block(state, stream);
34 crypto_xor(dst, (const u8 *)stream, bytes);
35 }
36}
37
38void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
39{
40 state[0] = 0x61707865; /* "expa" */
41 state[1] = 0x3320646e; /* "nd 3" */
42 state[2] = 0x79622d32; /* "2-by" */
43 state[3] = 0x6b206574; /* "te k" */
44 state[4] = ctx->key[0];
45 state[5] = ctx->key[1];
46 state[6] = ctx->key[2];
47 state[7] = ctx->key[3];
48 state[8] = ctx->key[4];
49 state[9] = ctx->key[5];
50 state[10] = ctx->key[6];
51 state[11] = ctx->key[7];
52 state[12] = get_unaligned_le32(iv + 0);
53 state[13] = get_unaligned_le32(iv + 4);
54 state[14] = get_unaligned_le32(iv + 8);
55 state[15] = get_unaligned_le32(iv + 12);
56}
57EXPORT_SYMBOL_GPL(crypto_chacha20_init);
58
59int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
60 unsigned int keysize)
61{
62 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
63 int i;
64
65 if (keysize != CHACHA20_KEY_SIZE)
66 return -EINVAL;
67
68 for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
69 ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
70
71 return 0;
72}
73EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
74
75int crypto_chacha20_crypt(struct skcipher_request *req)
76{
77 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
78 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
79 struct skcipher_walk walk;
80 u32 state[16];
81 int err;
82
83 err = skcipher_walk_virt(&walk, req, true);
84
85 crypto_chacha20_init(state, ctx, walk.iv);
86
87 while (walk.nbytes > 0) {
88 unsigned int nbytes = walk.nbytes;
89
90 if (nbytes < walk.total)
91 nbytes = round_down(nbytes, walk.stride);
92
93 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
94 nbytes);
95 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
96 }
97
98 return err;
99}
100EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
101
102static struct skcipher_alg alg = {
103 .base.cra_name = "chacha20",
104 .base.cra_driver_name = "chacha20-generic",
105 .base.cra_priority = 100,
106 .base.cra_blocksize = 1,
107 .base.cra_ctxsize = sizeof(struct chacha20_ctx),
108 .base.cra_module = THIS_MODULE,
109
110 .min_keysize = CHACHA20_KEY_SIZE,
111 .max_keysize = CHACHA20_KEY_SIZE,
112 .ivsize = CHACHA20_IV_SIZE,
113 .chunksize = CHACHA20_BLOCK_SIZE,
114 .setkey = crypto_chacha20_setkey,
115 .encrypt = crypto_chacha20_crypt,
116 .decrypt = crypto_chacha20_crypt,
117};
118
119static int __init chacha20_generic_mod_init(void)
120{
121 return crypto_register_skcipher(&alg);
122}
123
124static void __exit chacha20_generic_mod_fini(void)
125{
126 crypto_unregister_skcipher(&alg);
127}
128
129module_init(chacha20_generic_mod_init);
130module_exit(chacha20_generic_mod_fini);
131
132MODULE_LICENSE("GPL");
133MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
134MODULE_DESCRIPTION("chacha20 cipher algorithm");
135MODULE_ALIAS_CRYPTO("chacha20");
136MODULE_ALIAS_CRYPTO("chacha20-generic");
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
18static inline u32 rotl32(u32 v, u8 n)
19{
20 return (v << n) | (v >> (sizeof(v) * 8 - n));
21}
22
23static inline u32 le32_to_cpuvp(const void *p)
24{
25 return le32_to_cpup(p);
26}
27
28static void chacha20_block(u32 *state, void *stream)
29{
30 u32 x[16], *out = stream;
31 int i;
32
33 for (i = 0; i < ARRAY_SIZE(x); i++)
34 x[i] = state[i];
35
36 for (i = 0; i < 20; i += 2) {
37 x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 16);
38 x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 16);
39 x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 16);
40 x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 16);
41
42 x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 12);
43 x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 12);
44 x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 12);
45 x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 12);
46
47 x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 8);
48 x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 8);
49 x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 8);
50 x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 8);
51
52 x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 7);
53 x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 7);
54 x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 7);
55 x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 7);
56
57 x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 16);
58 x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 16);
59 x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 16);
60 x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 16);
61
62 x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 12);
63 x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 12);
64 x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 12);
65 x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 12);
66
67 x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 8);
68 x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 8);
69 x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 8);
70 x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 8);
71
72 x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 7);
73 x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 7);
74 x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 7);
75 x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 7);
76 }
77
78 for (i = 0; i < ARRAY_SIZE(x); i++)
79 out[i] = cpu_to_le32(x[i] + state[i]);
80
81 state[12]++;
82}
83
84static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
85 unsigned int bytes)
86{
87 u8 stream[CHACHA20_BLOCK_SIZE];
88
89 if (dst != src)
90 memcpy(dst, src, bytes);
91
92 while (bytes >= CHACHA20_BLOCK_SIZE) {
93 chacha20_block(state, stream);
94 crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE);
95 bytes -= CHACHA20_BLOCK_SIZE;
96 dst += CHACHA20_BLOCK_SIZE;
97 }
98 if (bytes) {
99 chacha20_block(state, stream);
100 crypto_xor(dst, stream, bytes);
101 }
102}
103
104void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
105{
106 static const char constant[16] = "expand 32-byte k";
107
108 state[0] = le32_to_cpuvp(constant + 0);
109 state[1] = le32_to_cpuvp(constant + 4);
110 state[2] = le32_to_cpuvp(constant + 8);
111 state[3] = le32_to_cpuvp(constant + 12);
112 state[4] = ctx->key[0];
113 state[5] = ctx->key[1];
114 state[6] = ctx->key[2];
115 state[7] = ctx->key[3];
116 state[8] = ctx->key[4];
117 state[9] = ctx->key[5];
118 state[10] = ctx->key[6];
119 state[11] = ctx->key[7];
120 state[12] = le32_to_cpuvp(iv + 0);
121 state[13] = le32_to_cpuvp(iv + 4);
122 state[14] = le32_to_cpuvp(iv + 8);
123 state[15] = le32_to_cpuvp(iv + 12);
124}
125EXPORT_SYMBOL_GPL(crypto_chacha20_init);
126
127int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
128 unsigned int keysize)
129{
130 struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm);
131 int i;
132
133 if (keysize != CHACHA20_KEY_SIZE)
134 return -EINVAL;
135
136 for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
137 ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32));
138
139 return 0;
140}
141EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
142
143int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
144 struct scatterlist *src, unsigned int nbytes)
145{
146 struct blkcipher_walk walk;
147 u32 state[16];
148 int err;
149
150 blkcipher_walk_init(&walk, dst, src, nbytes);
151 err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
152
153 crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
154
155 while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
156 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
157 rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
158 err = blkcipher_walk_done(desc, &walk,
159 walk.nbytes % CHACHA20_BLOCK_SIZE);
160 }
161
162 if (walk.nbytes) {
163 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
164 walk.nbytes);
165 err = blkcipher_walk_done(desc, &walk, 0);
166 }
167
168 return err;
169}
170EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
171
172static struct crypto_alg alg = {
173 .cra_name = "chacha20",
174 .cra_driver_name = "chacha20-generic",
175 .cra_priority = 100,
176 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
177 .cra_blocksize = 1,
178 .cra_type = &crypto_blkcipher_type,
179 .cra_ctxsize = sizeof(struct chacha20_ctx),
180 .cra_alignmask = sizeof(u32) - 1,
181 .cra_module = THIS_MODULE,
182 .cra_u = {
183 .blkcipher = {
184 .min_keysize = CHACHA20_KEY_SIZE,
185 .max_keysize = CHACHA20_KEY_SIZE,
186 .ivsize = CHACHA20_IV_SIZE,
187 .geniv = "seqiv",
188 .setkey = crypto_chacha20_setkey,
189 .encrypt = crypto_chacha20_crypt,
190 .decrypt = crypto_chacha20_crypt,
191 },
192 },
193};
194
195static int __init chacha20_generic_mod_init(void)
196{
197 return crypto_register_alg(&alg);
198}
199
200static void __exit chacha20_generic_mod_fini(void)
201{
202 crypto_unregister_alg(&alg);
203}
204
205module_init(chacha20_generic_mod_init);
206module_exit(chacha20_generic_mod_fini);
207
208MODULE_LICENSE("GPL");
209MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
210MODULE_DESCRIPTION("chacha20 cipher algorithm");
211MODULE_ALIAS_CRYPTO("chacha20");
212MODULE_ALIAS_CRYPTO("chacha20-generic");