]>
Commit | Line | Data |
---|---|---|
c08d0e64 | 1 | /* |
aa762409 | 2 | * ChaCha and XChaCha stream ciphers, including ChaCha20 (RFC7539) |
c08d0e64 MW |
3 | * |
4 | * Copyright (C) 2015 Martin Willi | |
de61d7ae | 5 | * Copyright (C) 2018 Google LLC |
c08d0e64 MW |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | */ | |
12 | ||
dbd872a1 | 13 | #include <asm/unaligned.h> |
c08d0e64 | 14 | #include <crypto/algapi.h> |
1ca1b917 | 15 | #include <crypto/chacha.h> |
9ae433bc AB |
16 | #include <crypto/internal/skcipher.h> |
17 | #include <linux/module.h> | |
c08d0e64 | 18 | |
1ca1b917 EB |
19 | static void chacha_docrypt(u32 *state, u8 *dst, const u8 *src, |
20 | unsigned int bytes, int nrounds) | |
c08d0e64 | 21 | { |
a5e9f557 | 22 | /* aligned to potentially speed up crypto_xor() */ |
1ca1b917 | 23 | u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long)); |
c08d0e64 | 24 | |
1ca1b917 EB |
25 | while (bytes >= CHACHA_BLOCK_SIZE) { |
26 | chacha_block(state, stream, nrounds); | |
29d97dec | 27 | crypto_xor_cpy(dst, src, stream, CHACHA_BLOCK_SIZE); |
1ca1b917 EB |
28 | bytes -= CHACHA_BLOCK_SIZE; |
29 | dst += CHACHA_BLOCK_SIZE; | |
29d97dec | 30 | src += CHACHA_BLOCK_SIZE; |
c08d0e64 MW |
31 | } |
32 | if (bytes) { | |
1ca1b917 | 33 | chacha_block(state, stream, nrounds); |
29d97dec | 34 | crypto_xor_cpy(dst, src, stream, bytes); |
c08d0e64 MW |
35 | } |
36 | } | |
37 | ||
1ca1b917 EB |
38 | static int chacha_stream_xor(struct skcipher_request *req, |
39 | struct chacha_ctx *ctx, u8 *iv) | |
de61d7ae EB |
40 | { |
41 | struct skcipher_walk walk; | |
42 | u32 state[16]; | |
43 | int err; | |
44 | ||
45 | err = skcipher_walk_virt(&walk, req, false); | |
46 | ||
1ca1b917 | 47 | crypto_chacha_init(state, ctx, iv); |
de61d7ae EB |
48 | |
49 | while (walk.nbytes > 0) { | |
50 | unsigned int nbytes = walk.nbytes; | |
51 | ||
52 | if (nbytes < walk.total) | |
7aceaaef | 53 | nbytes = round_down(nbytes, CHACHA_BLOCK_SIZE); |
de61d7ae | 54 | |
1ca1b917 EB |
55 | chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr, |
56 | nbytes, ctx->nrounds); | |
de61d7ae EB |
57 | err = skcipher_walk_done(&walk, walk.nbytes - nbytes); |
58 | } | |
59 | ||
60 | return err; | |
61 | } | |
62 | ||
1ca1b917 | 63 | void crypto_chacha_init(u32 *state, struct chacha_ctx *ctx, u8 *iv) |
c08d0e64 | 64 | { |
ecf3220d EB |
65 | state[0] = 0x61707865; /* "expa" */ |
66 | state[1] = 0x3320646e; /* "nd 3" */ | |
67 | state[2] = 0x79622d32; /* "2-by" */ | |
68 | state[3] = 0x6b206574; /* "te k" */ | |
c08d0e64 MW |
69 | state[4] = ctx->key[0]; |
70 | state[5] = ctx->key[1]; | |
71 | state[6] = ctx->key[2]; | |
72 | state[7] = ctx->key[3]; | |
73 | state[8] = ctx->key[4]; | |
74 | state[9] = ctx->key[5]; | |
75 | state[10] = ctx->key[6]; | |
76 | state[11] = ctx->key[7]; | |
dbd872a1 EB |
77 | state[12] = get_unaligned_le32(iv + 0); |
78 | state[13] = get_unaligned_le32(iv + 4); | |
79 | state[14] = get_unaligned_le32(iv + 8); | |
80 | state[15] = get_unaligned_le32(iv + 12); | |
c08d0e64 | 81 | } |
1ca1b917 | 82 | EXPORT_SYMBOL_GPL(crypto_chacha_init); |
c08d0e64 | 83 | |
1ca1b917 EB |
84 | static int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key, |
85 | unsigned int keysize, int nrounds) | |
c08d0e64 | 86 | { |
1ca1b917 | 87 | struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm); |
c08d0e64 MW |
88 | int i; |
89 | ||
1ca1b917 | 90 | if (keysize != CHACHA_KEY_SIZE) |
c08d0e64 MW |
91 | return -EINVAL; |
92 | ||
93 | for (i = 0; i < ARRAY_SIZE(ctx->key); i++) | |
dbd872a1 | 94 | ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32)); |
c08d0e64 | 95 | |
1ca1b917 | 96 | ctx->nrounds = nrounds; |
c08d0e64 MW |
97 | return 0; |
98 | } | |
1ca1b917 EB |
99 | |
100 | int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key, | |
101 | unsigned int keysize) | |
102 | { | |
103 | return chacha_setkey(tfm, key, keysize, 20); | |
104 | } | |
31d7247d | 105 | EXPORT_SYMBOL_GPL(crypto_chacha20_setkey); |
c08d0e64 | 106 | |
aa762409 EB |
107 | int crypto_chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key, |
108 | unsigned int keysize) | |
109 | { | |
110 | return chacha_setkey(tfm, key, keysize, 12); | |
111 | } | |
112 | EXPORT_SYMBOL_GPL(crypto_chacha12_setkey); | |
113 | ||
1ca1b917 | 114 | int crypto_chacha_crypt(struct skcipher_request *req) |
c08d0e64 | 115 | { |
9ae433bc | 116 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
1ca1b917 | 117 | struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm); |
c08d0e64 | 118 | |
1ca1b917 | 119 | return chacha_stream_xor(req, ctx, req->iv); |
de61d7ae | 120 | } |
1ca1b917 | 121 | EXPORT_SYMBOL_GPL(crypto_chacha_crypt); |
c08d0e64 | 122 | |
1ca1b917 | 123 | int crypto_xchacha_crypt(struct skcipher_request *req) |
de61d7ae EB |
124 | { |
125 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); | |
1ca1b917 EB |
126 | struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm); |
127 | struct chacha_ctx subctx; | |
de61d7ae EB |
128 | u32 state[16]; |
129 | u8 real_iv[16]; | |
4de43726 | 130 | |
de61d7ae | 131 | /* Compute the subkey given the original key and first 128 nonce bits */ |
1ca1b917 EB |
132 | crypto_chacha_init(state, ctx, req->iv); |
133 | hchacha_block(state, subctx.key, ctx->nrounds); | |
134 | subctx.nrounds = ctx->nrounds; | |
4de43726 | 135 | |
de61d7ae EB |
136 | /* Build the real IV */ |
137 | memcpy(&real_iv[0], req->iv + 24, 8); /* stream position */ | |
138 | memcpy(&real_iv[8], req->iv + 16, 8); /* remaining 64 nonce bits */ | |
c08d0e64 | 139 | |
de61d7ae | 140 | /* Generate the stream and XOR it with the data */ |
1ca1b917 | 141 | return chacha_stream_xor(req, &subctx, real_iv); |
c08d0e64 | 142 | } |
1ca1b917 | 143 | EXPORT_SYMBOL_GPL(crypto_xchacha_crypt); |
de61d7ae EB |
144 | |
145 | static struct skcipher_alg algs[] = { | |
146 | { | |
147 | .base.cra_name = "chacha20", | |
148 | .base.cra_driver_name = "chacha20-generic", | |
149 | .base.cra_priority = 100, | |
150 | .base.cra_blocksize = 1, | |
1ca1b917 | 151 | .base.cra_ctxsize = sizeof(struct chacha_ctx), |
de61d7ae EB |
152 | .base.cra_module = THIS_MODULE, |
153 | ||
1ca1b917 EB |
154 | .min_keysize = CHACHA_KEY_SIZE, |
155 | .max_keysize = CHACHA_KEY_SIZE, | |
156 | .ivsize = CHACHA_IV_SIZE, | |
157 | .chunksize = CHACHA_BLOCK_SIZE, | |
de61d7ae | 158 | .setkey = crypto_chacha20_setkey, |
1ca1b917 EB |
159 | .encrypt = crypto_chacha_crypt, |
160 | .decrypt = crypto_chacha_crypt, | |
de61d7ae EB |
161 | }, { |
162 | .base.cra_name = "xchacha20", | |
163 | .base.cra_driver_name = "xchacha20-generic", | |
164 | .base.cra_priority = 100, | |
165 | .base.cra_blocksize = 1, | |
1ca1b917 | 166 | .base.cra_ctxsize = sizeof(struct chacha_ctx), |
de61d7ae EB |
167 | .base.cra_module = THIS_MODULE, |
168 | ||
1ca1b917 EB |
169 | .min_keysize = CHACHA_KEY_SIZE, |
170 | .max_keysize = CHACHA_KEY_SIZE, | |
171 | .ivsize = XCHACHA_IV_SIZE, | |
172 | .chunksize = CHACHA_BLOCK_SIZE, | |
de61d7ae | 173 | .setkey = crypto_chacha20_setkey, |
1ca1b917 EB |
174 | .encrypt = crypto_xchacha_crypt, |
175 | .decrypt = crypto_xchacha_crypt, | |
aa762409 EB |
176 | }, { |
177 | .base.cra_name = "xchacha12", | |
178 | .base.cra_driver_name = "xchacha12-generic", | |
179 | .base.cra_priority = 100, | |
180 | .base.cra_blocksize = 1, | |
181 | .base.cra_ctxsize = sizeof(struct chacha_ctx), | |
182 | .base.cra_module = THIS_MODULE, | |
183 | ||
184 | .min_keysize = CHACHA_KEY_SIZE, | |
185 | .max_keysize = CHACHA_KEY_SIZE, | |
186 | .ivsize = XCHACHA_IV_SIZE, | |
187 | .chunksize = CHACHA_BLOCK_SIZE, | |
188 | .setkey = crypto_chacha12_setkey, | |
189 | .encrypt = crypto_xchacha_crypt, | |
190 | .decrypt = crypto_xchacha_crypt, | |
de61d7ae | 191 | } |
c08d0e64 MW |
192 | }; |
193 | ||
1ca1b917 | 194 | static int __init chacha_generic_mod_init(void) |
c08d0e64 | 195 | { |
de61d7ae | 196 | return crypto_register_skciphers(algs, ARRAY_SIZE(algs)); |
c08d0e64 MW |
197 | } |
198 | ||
1ca1b917 | 199 | static void __exit chacha_generic_mod_fini(void) |
c08d0e64 | 200 | { |
de61d7ae | 201 | crypto_unregister_skciphers(algs, ARRAY_SIZE(algs)); |
c08d0e64 MW |
202 | } |
203 | ||
c4741b23 | 204 | subsys_initcall(chacha_generic_mod_init); |
1ca1b917 | 205 | module_exit(chacha_generic_mod_fini); |
c08d0e64 MW |
206 | |
207 | MODULE_LICENSE("GPL"); | |
208 | MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); | |
1ca1b917 | 209 | MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)"); |
c08d0e64 MW |
210 | MODULE_ALIAS_CRYPTO("chacha20"); |
211 | MODULE_ALIAS_CRYPTO("chacha20-generic"); | |
de61d7ae EB |
212 | MODULE_ALIAS_CRYPTO("xchacha20"); |
213 | MODULE_ALIAS_CRYPTO("xchacha20-generic"); | |
aa762409 EB |
214 | MODULE_ALIAS_CRYPTO("xchacha12"); |
215 | MODULE_ALIAS_CRYPTO("xchacha12-generic"); |