1 /* This file is in the public domain. */
6 #include <crypto/chacha20/chacha.h>
7 #include <opencrypto/xform_enc.h>
10 chacha20_xform_setkey(void *ctx, const uint8_t *key, int len)
13 if (len != CHACHA_MINKEYLEN && len != 32)
16 chacha_keysetup(ctx, key, len * 8);
21 chacha20_xform_reinit(void *ctx, const uint8_t *iv, size_t ivlen)
23 KASSERT(ivlen == CHACHA_NONCELEN + CHACHA_CTRLEN,
24 ("%s: invalid IV length", __func__));
25 chacha_ivsetup(ctx, iv + 8, iv);
29 chacha20_xform_crypt(void *ctx, const uint8_t *in, uint8_t *out)
32 chacha_encrypt_bytes(ctx, in, out, CHACHA_BLOCKLEN);
36 chacha20_xform_crypt_multi(void *ctx, const uint8_t *in, uint8_t *out,
39 KASSERT(len % CHACHA_BLOCKLEN == 0, ("%s: invalid length", __func__));
40 chacha_encrypt_bytes(ctx, in, out, len);
44 chacha20_xform_crypt_last(void *ctx, const uint8_t *in, uint8_t *out,
48 chacha_encrypt_bytes(ctx, in, out, len);
51 const struct enc_xform enc_xform_chacha20 = {
52 .type = CRYPTO_CHACHA20,
54 .ctxsize = sizeof(struct chacha_ctx),
56 .native_blocksize = CHACHA_BLOCKLEN,
57 .ivsize = CHACHA_NONCELEN + CHACHA_CTRLEN,
58 .minkey = CHACHA_MINKEYLEN,
60 .setkey = chacha20_xform_setkey,
61 .reinit = chacha20_xform_reinit,
62 .encrypt = chacha20_xform_crypt,
63 .decrypt = chacha20_xform_crypt,
64 .encrypt_multi = chacha20_xform_crypt_multi,
65 .decrypt_multi = chacha20_xform_crypt_multi,
66 .encrypt_last = chacha20_xform_crypt_last,
67 .decrypt_last = chacha20_xform_crypt_last,