xref: /openbmc/linux/crypto/chacha_generic.c (revision 1ca1b917)
1 /*
2  * ChaCha20 (RFC7539) and XChaCha20 stream cipher algorithms
3  *
4  * Copyright (C) 2015 Martin Willi
5  * Copyright (C) 2018 Google LLC
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 
13 #include <asm/unaligned.h>
14 #include <crypto/algapi.h>
15 #include <crypto/chacha.h>
16 #include <crypto/internal/skcipher.h>
17 #include <linux/module.h>
18 
19 static void chacha_docrypt(u32 *state, u8 *dst, const u8 *src,
20 			   unsigned int bytes, int nrounds)
21 {
22 	/* aligned to potentially speed up crypto_xor() */
23 	u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long));
24 
25 	if (dst != src)
26 		memcpy(dst, src, bytes);
27 
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;
33 	}
34 	if (bytes) {
35 		chacha_block(state, stream, nrounds);
36 		crypto_xor(dst, stream, bytes);
37 	}
38 }
39 
40 static int chacha_stream_xor(struct skcipher_request *req,
41 			     struct chacha_ctx *ctx, u8 *iv)
42 {
43 	struct skcipher_walk walk;
44 	u32 state[16];
45 	int err;
46 
47 	err = skcipher_walk_virt(&walk, req, false);
48 
49 	crypto_chacha_init(state, ctx, iv);
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 
57 		chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
58 			       nbytes, ctx->nrounds);
59 		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
60 	}
61 
62 	return err;
63 }
64 
65 void crypto_chacha_init(u32 *state, struct chacha_ctx *ctx, u8 *iv)
66 {
67 	state[0]  = 0x61707865; /* "expa" */
68 	state[1]  = 0x3320646e; /* "nd 3" */
69 	state[2]  = 0x79622d32; /* "2-by" */
70 	state[3]  = 0x6b206574; /* "te k" */
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];
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);
83 }
84 EXPORT_SYMBOL_GPL(crypto_chacha_init);
85 
86 static int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
87 			 unsigned int keysize, int nrounds)
88 {
89 	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
90 	int i;
91 
92 	if (keysize != CHACHA_KEY_SIZE)
93 		return -EINVAL;
94 
95 	for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
96 		ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
97 
98 	ctx->nrounds = nrounds;
99 	return 0;
100 }
101 
102 int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
103 			   unsigned int keysize)
104 {
105 	return chacha_setkey(tfm, key, keysize, 20);
106 }
107 EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
108 
109 int crypto_chacha_crypt(struct skcipher_request *req)
110 {
111 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
112 	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
113 
114 	return chacha_stream_xor(req, ctx, req->iv);
115 }
116 EXPORT_SYMBOL_GPL(crypto_chacha_crypt);
117 
118 int crypto_xchacha_crypt(struct skcipher_request *req)
119 {
120 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
121 	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
122 	struct chacha_ctx subctx;
123 	u32 state[16];
124 	u8 real_iv[16];
125 
126 	/* Compute the subkey given the original key and first 128 nonce bits */
127 	crypto_chacha_init(state, ctx, req->iv);
128 	hchacha_block(state, subctx.key, ctx->nrounds);
129 	subctx.nrounds = ctx->nrounds;
130 
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 */
134 
135 	/* Generate the stream and XOR it with the data */
136 	return chacha_stream_xor(req, &subctx, real_iv);
137 }
138 EXPORT_SYMBOL_GPL(crypto_xchacha_crypt);
139 
140 static 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,
146 		.base.cra_ctxsize	= sizeof(struct chacha_ctx),
147 		.base.cra_module	= THIS_MODULE,
148 
149 		.min_keysize		= CHACHA_KEY_SIZE,
150 		.max_keysize		= CHACHA_KEY_SIZE,
151 		.ivsize			= CHACHA_IV_SIZE,
152 		.chunksize		= CHACHA_BLOCK_SIZE,
153 		.setkey			= crypto_chacha20_setkey,
154 		.encrypt		= crypto_chacha_crypt,
155 		.decrypt		= crypto_chacha_crypt,
156 	}, {
157 		.base.cra_name		= "xchacha20",
158 		.base.cra_driver_name	= "xchacha20-generic",
159 		.base.cra_priority	= 100,
160 		.base.cra_blocksize	= 1,
161 		.base.cra_ctxsize	= sizeof(struct chacha_ctx),
162 		.base.cra_module	= THIS_MODULE,
163 
164 		.min_keysize		= CHACHA_KEY_SIZE,
165 		.max_keysize		= CHACHA_KEY_SIZE,
166 		.ivsize			= XCHACHA_IV_SIZE,
167 		.chunksize		= CHACHA_BLOCK_SIZE,
168 		.setkey			= crypto_chacha20_setkey,
169 		.encrypt		= crypto_xchacha_crypt,
170 		.decrypt		= crypto_xchacha_crypt,
171 	}
172 };
173 
174 static int __init chacha_generic_mod_init(void)
175 {
176 	return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
177 }
178 
179 static void __exit chacha_generic_mod_fini(void)
180 {
181 	crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
182 }
183 
184 module_init(chacha_generic_mod_init);
185 module_exit(chacha_generic_mod_fini);
186 
187 MODULE_LICENSE("GPL");
188 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
189 MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)");
190 MODULE_ALIAS_CRYPTO("chacha20");
191 MODULE_ALIAS_CRYPTO("chacha20-generic");
192 MODULE_ALIAS_CRYPTO("xchacha20");
193 MODULE_ALIAS_CRYPTO("xchacha20-generic");
194