2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Netflix Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <opencrypto/xform_auth.h>
29 #include <opencrypto/xform_enc.h>
31 #include <sodium/crypto_onetimeauth_poly1305.h>
32 #include <sodium/crypto_stream_chacha20.h>
34 struct chacha20_poly1305_cipher_ctx {
37 char nonce[CHACHA20_POLY1305_IV_LEN];
41 chacha20_poly1305_setkey(void *vctx, const uint8_t *key, int len)
43 struct chacha20_poly1305_cipher_ctx *ctx = vctx;
45 if (len != CHACHA20_POLY1305_KEY)
53 chacha20_poly1305_reinit(void *vctx, const uint8_t *iv)
55 struct chacha20_poly1305_cipher_ctx *ctx = vctx;
57 /* Block 0 is used for the poly1305 key. */
58 memcpy(ctx->nonce, iv, sizeof(ctx->nonce));
63 chacha20_poly1305_crypt(void *vctx, const uint8_t *in, uint8_t *out)
65 struct chacha20_poly1305_cipher_ctx *ctx = vctx;
68 error = crypto_stream_chacha20_ietf_xor_ic(out, in,
69 CHACHA20_NATIVE_BLOCK_LEN, ctx->nonce, ctx->ic, ctx->key);
70 KASSERT(error == 0, ("%s failed: %d", __func__, error));
75 chacha20_poly1305_crypt_last(void *vctx, const uint8_t *in, uint8_t *out,
78 struct chacha20_poly1305_cipher_ctx *ctx = vctx;
82 error = crypto_stream_chacha20_ietf_xor_ic(out, in, len, ctx->nonce,
84 KASSERT(error == 0, ("%s failed: %d", __func__, error));
87 const struct enc_xform enc_xform_chacha20_poly1305 = {
88 .type = CRYPTO_CHACHA20_POLY1305,
89 .name = "ChaCha20-Poly1305",
90 .ctxsize = sizeof(struct chacha20_poly1305_cipher_ctx),
92 .native_blocksize = CHACHA20_NATIVE_BLOCK_LEN,
93 .ivsize = CHACHA20_POLY1305_IV_LEN,
94 .minkey = CHACHA20_POLY1305_KEY,
95 .maxkey = CHACHA20_POLY1305_KEY,
96 .encrypt = chacha20_poly1305_crypt,
97 .decrypt = chacha20_poly1305_crypt,
98 .setkey = chacha20_poly1305_setkey,
99 .reinit = chacha20_poly1305_reinit,
100 .encrypt_last = chacha20_poly1305_crypt_last,
101 .decrypt_last = chacha20_poly1305_crypt_last,
104 struct chacha20_poly1305_auth_ctx {
105 struct crypto_onetimeauth_poly1305_state state;
108 CTASSERT(sizeof(union authctx) >= sizeof(struct chacha20_poly1305_auth_ctx));
111 chacha20_poly1305_Init(void *vctx)
116 chacha20_poly1305_Setkey(void *vctx, const uint8_t *key, u_int klen)
118 struct chacha20_poly1305_auth_ctx *ctx = vctx;
124 chacha20_poly1305_Reinit(void *vctx, const uint8_t *nonce, u_int noncelen)
126 struct chacha20_poly1305_auth_ctx *ctx = vctx;
127 char block[CHACHA20_NATIVE_BLOCK_LEN];
129 crypto_stream_chacha20_ietf(block, sizeof(block), nonce, ctx->key);
130 crypto_onetimeauth_poly1305_init(&ctx->state, block);
131 explicit_bzero(block, sizeof(block));
135 chacha20_poly1305_Update(void *vctx, const void *data, u_int len)
137 struct chacha20_poly1305_auth_ctx *ctx = vctx;
139 crypto_onetimeauth_poly1305_update(&ctx->state, data, len);
144 chacha20_poly1305_Final(uint8_t *digest, void *vctx)
146 struct chacha20_poly1305_auth_ctx *ctx = vctx;
148 crypto_onetimeauth_poly1305_final(&ctx->state, digest);
151 const struct auth_hash auth_hash_chacha20_poly1305 = {
152 .type = CRYPTO_POLY1305,
153 .name = "ChaCha20-Poly1305",
154 .keysize = POLY1305_KEY_LEN,
155 .hashsize = POLY1305_HASH_LEN,
156 .ctxsize = sizeof(struct chacha20_poly1305_auth_ctx),
157 .blocksize = crypto_onetimeauth_poly1305_BYTES,
158 .Init = chacha20_poly1305_Init,
159 .Setkey = chacha20_poly1305_Setkey,
160 .Reinit = chacha20_poly1305_Reinit,
161 .Update = chacha20_poly1305_Update,
162 .Final = chacha20_poly1305_Final,