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