xref: /openbmc/linux/include/crypto/internal/chacha.h (revision 8dd06ef34b6e2f41b29fbf5fc1663780f2524285)
15fb8ef25SArd Biesheuvel /* SPDX-License-Identifier: GPL-2.0 */
25fb8ef25SArd Biesheuvel 
35fb8ef25SArd Biesheuvel #ifndef _CRYPTO_INTERNAL_CHACHA_H
45fb8ef25SArd Biesheuvel #define _CRYPTO_INTERNAL_CHACHA_H
55fb8ef25SArd Biesheuvel 
65fb8ef25SArd Biesheuvel #include <crypto/chacha.h>
75fb8ef25SArd Biesheuvel #include <crypto/internal/skcipher.h>
85fb8ef25SArd Biesheuvel #include <linux/crypto.h>
95fb8ef25SArd Biesheuvel 
105fb8ef25SArd Biesheuvel struct chacha_ctx {
115fb8ef25SArd Biesheuvel 	u32 key[8];
125fb8ef25SArd Biesheuvel 	int nrounds;
135fb8ef25SArd Biesheuvel };
145fb8ef25SArd Biesheuvel 
chacha_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keysize,int nrounds)155fb8ef25SArd Biesheuvel static inline int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
165fb8ef25SArd Biesheuvel 				unsigned int keysize, int nrounds)
175fb8ef25SArd Biesheuvel {
185fb8ef25SArd Biesheuvel 	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
195fb8ef25SArd Biesheuvel 	int i;
205fb8ef25SArd Biesheuvel 
215fb8ef25SArd Biesheuvel 	if (keysize != CHACHA_KEY_SIZE)
225fb8ef25SArd Biesheuvel 		return -EINVAL;
235fb8ef25SArd Biesheuvel 
245fb8ef25SArd Biesheuvel 	for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
255fb8ef25SArd Biesheuvel 		ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
265fb8ef25SArd Biesheuvel 
275fb8ef25SArd Biesheuvel 	ctx->nrounds = nrounds;
285fb8ef25SArd Biesheuvel 	return 0;
295fb8ef25SArd Biesheuvel }
305fb8ef25SArd Biesheuvel 
chacha20_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keysize)315fb8ef25SArd Biesheuvel static inline int chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
325fb8ef25SArd Biesheuvel 				  unsigned int keysize)
335fb8ef25SArd Biesheuvel {
345fb8ef25SArd Biesheuvel 	return chacha_setkey(tfm, key, keysize, 20);
355fb8ef25SArd Biesheuvel }
365fb8ef25SArd Biesheuvel 
chacha12_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keysize)37*579d705cSValdis Klētnieks static inline int chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
385fb8ef25SArd Biesheuvel 				  unsigned int keysize)
395fb8ef25SArd Biesheuvel {
405fb8ef25SArd Biesheuvel 	return chacha_setkey(tfm, key, keysize, 12);
415fb8ef25SArd Biesheuvel }
425fb8ef25SArd Biesheuvel 
435fb8ef25SArd Biesheuvel #endif /* _CRYPTO_CHACHA_H */
44