xref: /freebsd/sys/crypto/chacha20/chacha-sw.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /* This file is in the public domain. */
2 
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5 
6 #include <crypto/chacha20/chacha.h>
7 #include <opencrypto/xform_enc.h>
8 
9 static int
10 chacha20_xform_setkey(void *ctx, const uint8_t *key, int len)
11 {
12 
13 	if (len != CHACHA_MINKEYLEN && len != 32)
14 		return (EINVAL);
15 
16 	chacha_keysetup(ctx, key, len * 8);
17 	return (0);
18 }
19 
20 static void
21 chacha20_xform_reinit(void *ctx, const uint8_t *iv, size_t ivlen)
22 {
23 	KASSERT(ivlen == CHACHA_NONCELEN + CHACHA_CTRLEN,
24 	    ("%s: invalid IV length", __func__));
25 	chacha_ivsetup(ctx, iv + 8, iv);
26 }
27 
28 static void
29 chacha20_xform_crypt(void *ctx, const uint8_t *in, uint8_t *out)
30 {
31 
32 	chacha_encrypt_bytes(ctx, in, out, CHACHA_BLOCKLEN);
33 }
34 
35 static void
36 chacha20_xform_crypt_last(void *ctx, const uint8_t *in, uint8_t *out,
37     size_t len)
38 {
39 
40 	chacha_encrypt_bytes(ctx, in, out, len);
41 }
42 
43 const struct enc_xform enc_xform_chacha20 = {
44 	.type = CRYPTO_CHACHA20,
45 	.name = "chacha20",
46 	.ctxsize = sizeof(struct chacha_ctx),
47 	.blocksize = 1,
48 	.native_blocksize = CHACHA_BLOCKLEN,
49 	.ivsize = CHACHA_NONCELEN + CHACHA_CTRLEN,
50 	.minkey = CHACHA_MINKEYLEN,
51 	.maxkey = 32,
52 	.encrypt = chacha20_xform_crypt,
53 	.decrypt = chacha20_xform_crypt,
54 	.setkey = chacha20_xform_setkey,
55 	.reinit = chacha20_xform_reinit,
56 	.encrypt_last = chacha20_xform_crypt_last,
57 	.decrypt_last = chacha20_xform_crypt_last,
58 };
59