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