ceph: map snapid to anonymous bdev ID
[linux-2.6-block.git] / crypto / chacha_generic.c
CommitLineData
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
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
aa762409
EB
109int crypto_chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
110 unsigned int keysize)
111{
112 return chacha_setkey(tfm, key, keysize, 12);
113}
114EXPORT_SYMBOL_GPL(crypto_chacha12_setkey);
115
1ca1b917 116int crypto_chacha_crypt(struct skcipher_request *req)
c08d0e64 117{
9ae433bc 118 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1ca1b917 119 struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
c08d0e64 120
1ca1b917 121 return chacha_stream_xor(req, ctx, req->iv);
de61d7ae 122}
1ca1b917 123EXPORT_SYMBOL_GPL(crypto_chacha_crypt);
c08d0e64 124
1ca1b917 125int crypto_xchacha_crypt(struct skcipher_request *req)
de61d7ae
EB
126{
127 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1ca1b917
EB
128 struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
129 struct chacha_ctx subctx;
de61d7ae
EB
130 u32 state[16];
131 u8 real_iv[16];
4de43726 132
de61d7ae 133 /* Compute the subkey given the original key and first 128 nonce bits */
1ca1b917
EB
134 crypto_chacha_init(state, ctx, req->iv);
135 hchacha_block(state, subctx.key, ctx->nrounds);
136 subctx.nrounds = ctx->nrounds;
4de43726 137
de61d7ae
EB
138 /* Build the real IV */
139 memcpy(&real_iv[0], req->iv + 24, 8); /* stream position */
140 memcpy(&real_iv[8], req->iv + 16, 8); /* remaining 64 nonce bits */
c08d0e64 141
de61d7ae 142 /* Generate the stream and XOR it with the data */
1ca1b917 143 return chacha_stream_xor(req, &subctx, real_iv);
c08d0e64 144}
1ca1b917 145EXPORT_SYMBOL_GPL(crypto_xchacha_crypt);
de61d7ae
EB
146
147static struct skcipher_alg algs[] = {
148 {
149 .base.cra_name = "chacha20",
150 .base.cra_driver_name = "chacha20-generic",
151 .base.cra_priority = 100,
152 .base.cra_blocksize = 1,
1ca1b917 153 .base.cra_ctxsize = sizeof(struct chacha_ctx),
de61d7ae
EB
154 .base.cra_module = THIS_MODULE,
155
1ca1b917
EB
156 .min_keysize = CHACHA_KEY_SIZE,
157 .max_keysize = CHACHA_KEY_SIZE,
158 .ivsize = CHACHA_IV_SIZE,
159 .chunksize = CHACHA_BLOCK_SIZE,
de61d7ae 160 .setkey = crypto_chacha20_setkey,
1ca1b917
EB
161 .encrypt = crypto_chacha_crypt,
162 .decrypt = crypto_chacha_crypt,
de61d7ae
EB
163 }, {
164 .base.cra_name = "xchacha20",
165 .base.cra_driver_name = "xchacha20-generic",
166 .base.cra_priority = 100,
167 .base.cra_blocksize = 1,
1ca1b917 168 .base.cra_ctxsize = sizeof(struct chacha_ctx),
de61d7ae
EB
169 .base.cra_module = THIS_MODULE,
170
1ca1b917
EB
171 .min_keysize = CHACHA_KEY_SIZE,
172 .max_keysize = CHACHA_KEY_SIZE,
173 .ivsize = XCHACHA_IV_SIZE,
174 .chunksize = CHACHA_BLOCK_SIZE,
de61d7ae 175 .setkey = crypto_chacha20_setkey,
1ca1b917
EB
176 .encrypt = crypto_xchacha_crypt,
177 .decrypt = crypto_xchacha_crypt,
aa762409
EB
178 }, {
179 .base.cra_name = "xchacha12",
180 .base.cra_driver_name = "xchacha12-generic",
181 .base.cra_priority = 100,
182 .base.cra_blocksize = 1,
183 .base.cra_ctxsize = sizeof(struct chacha_ctx),
184 .base.cra_module = THIS_MODULE,
185
186 .min_keysize = CHACHA_KEY_SIZE,
187 .max_keysize = CHACHA_KEY_SIZE,
188 .ivsize = XCHACHA_IV_SIZE,
189 .chunksize = CHACHA_BLOCK_SIZE,
190 .setkey = crypto_chacha12_setkey,
191 .encrypt = crypto_xchacha_crypt,
192 .decrypt = crypto_xchacha_crypt,
de61d7ae 193 }
c08d0e64
MW
194};
195
1ca1b917 196static int __init chacha_generic_mod_init(void)
c08d0e64 197{
de61d7ae 198 return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
c08d0e64
MW
199}
200
1ca1b917 201static void __exit chacha_generic_mod_fini(void)
c08d0e64 202{
de61d7ae 203 crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
c08d0e64
MW
204}
205
1ca1b917
EB
206module_init(chacha_generic_mod_init);
207module_exit(chacha_generic_mod_fini);
c08d0e64
MW
208
209MODULE_LICENSE("GPL");
210MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
1ca1b917 211MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)");
c08d0e64
MW
212MODULE_ALIAS_CRYPTO("chacha20");
213MODULE_ALIAS_CRYPTO("chacha20-generic");
de61d7ae
EB
214MODULE_ALIAS_CRYPTO("xchacha20");
215MODULE_ALIAS_CRYPTO("xchacha20-generic");
aa762409
EB
216MODULE_ALIAS_CRYPTO("xchacha12");
217MODULE_ALIAS_CRYPTO("xchacha12-generic");