aboutsummaryrefslogtreecommitdiffstats
path: root/include/crypto/internal/chacha.h
blob: c0e40b24543118e51549faed48a342cd11c43937 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* SPDX-License-Identifier: GPL-2.0 */

#ifndef _CRYPTO_INTERNAL_CHACHA_H
#define _CRYPTO_INTERNAL_CHACHA_H

#include <crypto/chacha.h>
#include <crypto/internal/skcipher.h>
#include <linux/crypto.h>

struct chacha_ctx {
	u32 key[8];
	int nrounds;
};

void crypto_chacha_init(u32 *state, const struct chacha_ctx *ctx, const u8 *iv);

static inline int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
				unsigned int keysize, int nrounds)
{
	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
	int i;

	if (keysize != CHACHA_KEY_SIZE)
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
		ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));

	ctx->nrounds = nrounds;
	return 0;
}

static inline int chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
				  unsigned int keysize)
{
	return chacha_setkey(tfm, key, keysize, 20);
}

static int inline chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
				  unsigned int keysize)
{
	return chacha_setkey(tfm, key, keysize, 12);
}

int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
			   unsigned int keysize);
int crypto_chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
			   unsigned int keysize);

int crypto_chacha_crypt(struct skcipher_request *req);
int crypto_xchacha_crypt(struct skcipher_request *req);

#endif /* _CRYPTO_CHACHA_H */