2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 The FreeBSD Foundation
6 * This software was developed by Ararat River Consulting, LLC under
7 * sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/endian.h>
32 #include <crypto/chacha20_poly1305.h>
33 #include <opencrypto/xform_enc.h>
35 static const uint8_t zeroes[POLY1305_BLOCK_LEN];
38 chacha20_poly1305_encrypt(uint8_t *dst, const uint8_t *src,
39 const size_t src_len, const uint8_t *aad, const size_t aad_len,
40 const uint8_t *nonce, const size_t nonce_len, const uint8_t *key)
42 const struct enc_xform *exf;
47 exf = &enc_xform_chacha20_poly1305;
48 ctx = __builtin_alloca(exf->ctxsize);
49 exf->setkey(ctx, key, CHACHA20_POLY1305_KEY);
50 exf->reinit(ctx, nonce, nonce_len);
52 exf->update(ctx, aad, aad_len);
53 if (aad_len % POLY1305_BLOCK_LEN != 0)
54 exf->update(ctx, zeroes,
55 POLY1305_BLOCK_LEN - aad_len % POLY1305_BLOCK_LEN);
58 todo = rounddown2(resid, CHACHA20_NATIVE_BLOCK_LEN);
60 exf->encrypt_multi(ctx, src, dst, todo);
61 exf->update(ctx, dst, todo);
67 exf->encrypt_last(ctx, src, dst, resid);
68 exf->update(ctx, dst, resid);
70 if (resid % POLY1305_BLOCK_LEN != 0)
71 exf->update(ctx, zeroes,
72 POLY1305_BLOCK_LEN - resid % POLY1305_BLOCK_LEN);
75 lengths[0] = htole64(aad_len);
76 lengths[1] = htole64(src_len);
77 exf->update(ctx, lengths, sizeof(lengths));
80 explicit_bzero(ctx, exf->ctxsize);
81 explicit_bzero(lengths, sizeof(lengths));
85 chacha20_poly1305_decrypt(uint8_t *dst, const uint8_t *src,
86 const size_t src_len, const uint8_t *aad, const size_t aad_len,
87 const uint8_t *nonce, const size_t nonce_len, const uint8_t *key)
89 const struct enc_xform *exf;
94 char tag[POLY1305_HASH_LEN];
98 if (src_len < POLY1305_HASH_LEN)
100 resid = src_len - POLY1305_HASH_LEN;
102 exf = &enc_xform_chacha20_poly1305;
103 ctx = __builtin_alloca(exf->ctxsize);
104 exf->setkey(ctx, key, CHACHA20_POLY1305_KEY);
105 exf->reinit(ctx, nonce, nonce_len);
107 exf->update(ctx, aad, aad_len);
108 if (aad_len % POLY1305_BLOCK_LEN != 0)
109 exf->update(ctx, zeroes,
110 POLY1305_BLOCK_LEN - aad_len % POLY1305_BLOCK_LEN);
111 exf->update(ctx, src, resid);
112 if (resid % POLY1305_BLOCK_LEN != 0)
113 exf->update(ctx, zeroes,
114 POLY1305_BLOCK_LEN - resid % POLY1305_BLOCK_LEN);
116 u.lengths[0] = htole64(aad_len);
117 u.lengths[1] = htole64(resid);
118 exf->update(ctx, u.lengths, sizeof(u.lengths));
119 exf->final(u.tag, ctx);
120 result = (timingsafe_bcmp(u.tag, src + resid, POLY1305_HASH_LEN) == 0);
124 todo = rounddown2(resid, CHACHA20_NATIVE_BLOCK_LEN);
126 exf->decrypt_multi(ctx, src, dst, todo);
132 exf->decrypt_last(ctx, src, dst, resid);
135 explicit_bzero(ctx, exf->ctxsize);
136 explicit_bzero(&u, sizeof(u));
141 xchacha20_poly1305_encrypt(uint8_t *dst, const uint8_t *src,
142 const size_t src_len, const uint8_t *aad, const size_t aad_len,
143 const uint8_t *nonce, const uint8_t *key)
145 const struct enc_xform *exf;
150 exf = &enc_xform_xchacha20_poly1305;
151 ctx = __builtin_alloca(exf->ctxsize);
152 exf->setkey(ctx, key, XCHACHA20_POLY1305_KEY);
153 exf->reinit(ctx, nonce, XCHACHA20_POLY1305_IV_LEN);
155 exf->update(ctx, aad, aad_len);
156 if (aad_len % POLY1305_BLOCK_LEN != 0)
157 exf->update(ctx, zeroes,
158 POLY1305_BLOCK_LEN - aad_len % POLY1305_BLOCK_LEN);
161 todo = rounddown2(resid, CHACHA20_NATIVE_BLOCK_LEN);
163 exf->encrypt_multi(ctx, src, dst, todo);
164 exf->update(ctx, dst, todo);
170 exf->encrypt_last(ctx, src, dst, resid);
171 exf->update(ctx, dst, resid);
173 if (resid % POLY1305_BLOCK_LEN != 0)
174 exf->update(ctx, zeroes,
175 POLY1305_BLOCK_LEN - resid % POLY1305_BLOCK_LEN);
178 lengths[0] = htole64(aad_len);
179 lengths[1] = htole64(src_len);
180 exf->update(ctx, lengths, sizeof(lengths));
181 exf->final(dst, ctx);
183 explicit_bzero(ctx, exf->ctxsize);
184 explicit_bzero(lengths, sizeof(lengths));
188 xchacha20_poly1305_decrypt(uint8_t *dst, const uint8_t *src,
189 const size_t src_len, const uint8_t *aad, const size_t aad_len,
190 const uint8_t *nonce, const uint8_t *key)
192 const struct enc_xform *exf;
197 char tag[POLY1305_HASH_LEN];
201 if (src_len < POLY1305_HASH_LEN)
203 resid = src_len - POLY1305_HASH_LEN;
205 exf = &enc_xform_xchacha20_poly1305;
206 ctx = __builtin_alloca(exf->ctxsize);
207 exf->setkey(ctx, key, XCHACHA20_POLY1305_KEY);
208 exf->reinit(ctx, nonce, XCHACHA20_POLY1305_IV_LEN);
210 exf->update(ctx, aad, aad_len);
211 if (aad_len % POLY1305_BLOCK_LEN != 0)
212 exf->update(ctx, zeroes,
213 POLY1305_BLOCK_LEN - aad_len % POLY1305_BLOCK_LEN);
214 exf->update(ctx, src, resid);
215 if (resid % POLY1305_BLOCK_LEN != 0)
216 exf->update(ctx, zeroes,
217 POLY1305_BLOCK_LEN - resid % POLY1305_BLOCK_LEN);
219 u.lengths[0] = htole64(aad_len);
220 u.lengths[1] = htole64(resid);
221 exf->update(ctx, u.lengths, sizeof(u.lengths));
222 exf->final(u.tag, ctx);
223 result = (timingsafe_bcmp(u.tag, src + resid, POLY1305_HASH_LEN) == 0);
227 todo = rounddown2(resid, CHACHA20_NATIVE_BLOCK_LEN);
229 exf->decrypt_multi(ctx, src, dst, todo);
235 exf->decrypt_last(ctx, src, dst, resid);
238 explicit_bzero(ctx, exf->ctxsize);
239 explicit_bzero(&u, sizeof(u));