xref: /linux-6.15/include/crypto/internal/chacha.h (revision 5fb8ef25)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef _CRYPTO_INTERNAL_CHACHA_H
4 #define _CRYPTO_INTERNAL_CHACHA_H
5 
6 #include <crypto/chacha.h>
7 #include <crypto/internal/skcipher.h>
8 #include <linux/crypto.h>
9 
10 struct chacha_ctx {
11 	u32 key[8];
12 	int nrounds;
13 };
14 
15 void crypto_chacha_init(u32 *state, const struct chacha_ctx *ctx, const u8 *iv);
16 
17 static inline int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
18 				unsigned int keysize, int nrounds)
19 {
20 	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
21 	int i;
22 
23 	if (keysize != CHACHA_KEY_SIZE)
24 		return -EINVAL;
25 
26 	for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
27 		ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
28 
29 	ctx->nrounds = nrounds;
30 	return 0;
31 }
32 
33 static inline int chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
34 				  unsigned int keysize)
35 {
36 	return chacha_setkey(tfm, key, keysize, 20);
37 }
38 
39 static int inline chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
40 				  unsigned int keysize)
41 {
42 	return chacha_setkey(tfm, key, keysize, 12);
43 }
44 
45 int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
46 			   unsigned int keysize);
47 int crypto_chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
48 			   unsigned int keysize);
49 
50 int crypto_chacha_crypt(struct skcipher_request *req);
51 int crypto_xchacha_crypt(struct skcipher_request *req);
52 
53 #endif /* _CRYPTO_CHACHA_H */
54