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
chacha20_xform_setkey(void * ctx,const uint8_t * key,int len)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
chacha20_xform_reinit(void * ctx,const uint8_t * iv)21 chacha20_xform_reinit(void *ctx, const uint8_t *iv)
22 {
23
24 chacha_ivsetup(ctx, iv + 8, iv);
25 }
26
27 static void
chacha20_xform_crypt(void * ctx,const uint8_t * in,uint8_t * out)28 chacha20_xform_crypt(void *ctx, const uint8_t *in, uint8_t *out)
29 {
30
31 chacha_encrypt_bytes(ctx, in, out, CHACHA_BLOCKLEN);
32 }
33
34 static void
chacha20_xform_crypt_last(void * ctx,const uint8_t * in,uint8_t * out,size_t len)35 chacha20_xform_crypt_last(void *ctx, const uint8_t *in, uint8_t *out,
36 size_t len)
37 {
38
39 chacha_encrypt_bytes(ctx, in, out, len);
40 }
41
42 struct enc_xform enc_xform_chacha20 = {
43 .type = CRYPTO_CHACHA20,
44 .name = "chacha20",
45 .ctxsize = sizeof(struct chacha_ctx),
46 .blocksize = 1,
47 .native_blocksize = CHACHA_BLOCKLEN,
48 .ivsize = CHACHA_NONCELEN + CHACHA_CTRLEN,
49 .minkey = CHACHA_MINKEYLEN,
50 .maxkey = 32,
51 .encrypt = chacha20_xform_crypt,
52 .decrypt = chacha20_xform_crypt,
53 .setkey = chacha20_xform_setkey,
54 .reinit = chacha20_xform_reinit,
55 .encrypt_last = chacha20_xform_crypt_last,
56 .decrypt_last = chacha20_xform_crypt_last,
57 };
58