2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* chacha20_poly1305 cipher implementation */
12 #include "cipher_chacha20_poly1305.h"
14 static int chacha_poly1305_tls_init(PROV_CIPHER_CTX *bctx,
15 unsigned char *aad, size_t alen)
18 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
20 if (alen != EVP_AEAD_TLS1_AAD_LEN)
23 memcpy(ctx->tls_aad, aad, EVP_AEAD_TLS1_AAD_LEN);
24 len = aad[EVP_AEAD_TLS1_AAD_LEN - 2] << 8 | aad[EVP_AEAD_TLS1_AAD_LEN - 1];
27 if (len < POLY1305_BLOCK_SIZE)
29 len -= POLY1305_BLOCK_SIZE; /* discount attached tag */
30 aad[EVP_AEAD_TLS1_AAD_LEN - 2] = (unsigned char)(len >> 8);
31 aad[EVP_AEAD_TLS1_AAD_LEN - 1] = (unsigned char)len;
33 ctx->tls_payload_length = len;
35 /* merge record sequence number as per RFC7905 */
36 ctx->chacha.counter[1] = ctx->nonce[0];
37 ctx->chacha.counter[2] = ctx->nonce[1] ^ CHACHA_U8TOU32(aad);
38 ctx->chacha.counter[3] = ctx->nonce[2] ^ CHACHA_U8TOU32(aad+4);
41 return POLY1305_BLOCK_SIZE; /* tag length */
44 static int chacha_poly1305_tls_iv_set_fixed(PROV_CIPHER_CTX *bctx,
45 unsigned char *fixed, size_t flen)
47 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
49 if (flen != CHACHA20_POLY1305_IVLEN)
51 ctx->nonce[0] = ctx->chacha.counter[1] = CHACHA_U8TOU32(fixed);
52 ctx->nonce[1] = ctx->chacha.counter[2] = CHACHA_U8TOU32(fixed + 4);
53 ctx->nonce[2] = ctx->chacha.counter[3] = CHACHA_U8TOU32(fixed + 8);
58 static int chacha20_poly1305_initkey(PROV_CIPHER_CTX *bctx,
59 const unsigned char *key, size_t keylen)
61 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
67 ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
70 return chacha20_einit(&ctx->chacha, key, keylen, NULL, 0);
72 return chacha20_dinit(&ctx->chacha, key, keylen, NULL, 0);
75 static int chacha20_poly1305_initiv(PROV_CIPHER_CTX *bctx)
77 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
78 unsigned char tempiv[CHACHA_CTR_SIZE] = { 0 };
82 if (ctx->nonce_len <= CHACHA_CTR_SIZE) {
83 memcpy(tempiv + CHACHA_CTR_SIZE - ctx->nonce_len, bctx->oiv,
87 ret = chacha20_einit(&ctx->chacha, NULL, 0, tempiv, sizeof(tempiv));
89 ret = chacha20_dinit(&ctx->chacha, NULL, 0, tempiv, sizeof(tempiv));
90 ctx->nonce[0] = ctx->chacha.counter[1];
91 ctx->nonce[1] = ctx->chacha.counter[2];
92 ctx->nonce[2] = ctx->chacha.counter[3];
98 #if !defined(OPENSSL_SMALL_FOOTPRINT)
100 # if defined(POLY1305_ASM) && (defined(__x86_64) || defined(__x86_64__) \
101 || defined(_M_AMD64) || defined(_M_X64))
102 # define XOR128_HELPERS
103 void *xor128_encrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
104 void *xor128_decrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
105 static const unsigned char zero[4 * CHACHA_BLK_SIZE] = { 0 };
107 static const unsigned char zero[2 * CHACHA_BLK_SIZE] = { 0 };
110 static int chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX *bctx,
113 const unsigned char *in, size_t len)
115 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
116 POLY1305 *poly = &ctx->poly1305;
117 size_t tail, tohash_len, buf_len, plen = ctx->tls_payload_length;
118 unsigned char *buf, *tohash, *ctr, storage[sizeof(zero) + 32];
125 if (len != plen + POLY1305_BLOCK_SIZE)
128 buf = storage + ((0 - (size_t)storage) & 15); /* align */
129 ctr = buf + CHACHA_BLK_SIZE;
130 tohash = buf + CHACHA_BLK_SIZE - POLY1305_BLOCK_SIZE;
132 # ifdef XOR128_HELPERS
133 if (plen <= 3 * CHACHA_BLK_SIZE) {
134 ctx->chacha.counter[0] = 0;
135 buf_len = (plen + 2 * CHACHA_BLK_SIZE - 1) & (0 - CHACHA_BLK_SIZE);
136 ChaCha20_ctr32(buf, zero, buf_len, ctx->chacha.key.d, ctx->chacha.counter);
137 Poly1305_Init(poly, buf);
138 ctx->chacha.partial_len = 0;
139 memcpy(tohash, ctx->tls_aad, POLY1305_BLOCK_SIZE);
140 tohash_len = POLY1305_BLOCK_SIZE;
141 ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
142 ctx->len.text = plen;
146 ctr = xor128_encrypt_n_pad(out, in, ctr, plen);
148 ctr = xor128_decrypt_n_pad(out, in, ctr, plen);
152 tohash_len = (size_t)(ctr - tohash);
156 if (plen <= CHACHA_BLK_SIZE) {
159 ctx->chacha.counter[0] = 0;
160 ChaCha20_ctr32(buf, zero, (buf_len = 2 * CHACHA_BLK_SIZE),
161 ctx->chacha.key.d, ctx->chacha.counter);
162 Poly1305_Init(poly, buf);
163 ctx->chacha.partial_len = 0;
164 memcpy(tohash, ctx->tls_aad, POLY1305_BLOCK_SIZE);
165 tohash_len = POLY1305_BLOCK_SIZE;
166 ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
167 ctx->len.text = plen;
170 for (i = 0; i < plen; i++)
171 out[i] = ctr[i] ^= in[i];
173 for (i = 0; i < plen; i++) {
174 unsigned char c = in[i];
184 tail = (0 - i) & (POLY1305_BLOCK_SIZE - 1);
185 memset(ctr + i, 0, tail);
187 tohash_len += i + tail;
191 ctx->chacha.counter[0] = 0;
192 ChaCha20_ctr32(buf, zero, (buf_len = CHACHA_BLK_SIZE),
193 ctx->chacha.key.d, ctx->chacha.counter);
194 Poly1305_Init(poly, buf);
195 ctx->chacha.counter[0] = 1;
196 ctx->chacha.partial_len = 0;
197 Poly1305_Update(poly, ctx->tls_aad, POLY1305_BLOCK_SIZE);
200 ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
201 ctx->len.text = plen;
204 ChaCha20_ctr32(out, in, plen, ctx->chacha.key.d, ctx->chacha.counter);
205 Poly1305_Update(poly, out, plen);
207 Poly1305_Update(poly, in, plen);
208 ChaCha20_ctr32(out, in, plen, ctx->chacha.key.d, ctx->chacha.counter);
213 tail = (0 - plen) & (POLY1305_BLOCK_SIZE - 1);
214 Poly1305_Update(poly, zero, tail);
217 if (is_endian.little) {
218 memcpy(ctr, (unsigned char *)&ctx->len, POLY1305_BLOCK_SIZE);
220 ctr[0] = (unsigned char)(ctx->len.aad);
221 ctr[1] = (unsigned char)(ctx->len.aad>>8);
222 ctr[2] = (unsigned char)(ctx->len.aad>>16);
223 ctr[3] = (unsigned char)(ctx->len.aad>>24);
224 ctr[4] = (unsigned char)(ctx->len.aad>>32);
225 ctr[5] = (unsigned char)(ctx->len.aad>>40);
226 ctr[6] = (unsigned char)(ctx->len.aad>>48);
227 ctr[7] = (unsigned char)(ctx->len.aad>>56);
229 ctr[8] = (unsigned char)(ctx->len.text);
230 ctr[9] = (unsigned char)(ctx->len.text>>8);
231 ctr[10] = (unsigned char)(ctx->len.text>>16);
232 ctr[11] = (unsigned char)(ctx->len.text>>24);
233 ctr[12] = (unsigned char)(ctx->len.text>>32);
234 ctr[13] = (unsigned char)(ctx->len.text>>40);
235 ctr[14] = (unsigned char)(ctx->len.text>>48);
236 ctr[15] = (unsigned char)(ctx->len.text>>56);
238 tohash_len += POLY1305_BLOCK_SIZE;
240 Poly1305_Update(poly, tohash, tohash_len);
241 OPENSSL_cleanse(buf, buf_len);
242 Poly1305_Final(poly, bctx->enc ? ctx->tag : tohash);
244 ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
247 memcpy(out, ctx->tag, POLY1305_BLOCK_SIZE);
249 if (CRYPTO_memcmp(tohash, in, POLY1305_BLOCK_SIZE)) {
250 if (len > POLY1305_BLOCK_SIZE)
251 memset(out - (len - POLY1305_BLOCK_SIZE), 0,
252 len - POLY1305_BLOCK_SIZE);
261 static const unsigned char zero[CHACHA_BLK_SIZE] = { 0 };
262 #endif /* OPENSSL_SMALL_FOOTPRINT */
264 static int chacha20_poly1305_aead_cipher(PROV_CIPHER_CTX *bctx,
265 unsigned char *out, size_t *outl,
266 const unsigned char *in, size_t inl)
268 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
269 POLY1305 *poly = &ctx->poly1305;
270 size_t rem, plen = ctx->tls_payload_length;
279 if (!ctx->mac_inited) {
280 #if !defined(OPENSSL_SMALL_FOOTPRINT)
281 if (plen != NO_TLS_PAYLOAD_LENGTH && out != NULL) {
282 return chacha20_poly1305_tls_cipher(bctx, out, outl, in, inl);
285 ctx->chacha.counter[0] = 0;
286 ChaCha20_ctr32(ctx->chacha.buf, zero, CHACHA_BLK_SIZE,
287 ctx->chacha.key.d, ctx->chacha.counter);
288 Poly1305_Init(poly, ctx->chacha.buf);
289 ctx->chacha.counter[0] = 1;
290 ctx->chacha.partial_len = 0;
291 ctx->len.aad = ctx->len.text = 0;
293 if (plen != NO_TLS_PAYLOAD_LENGTH) {
294 Poly1305_Update(poly, ctx->tls_aad, EVP_AEAD_TLS1_AAD_LEN);
295 ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
300 if (in != NULL) { /* aad or text */
301 if (out == NULL) { /* aad */
302 Poly1305_Update(poly, in, inl);
306 } else { /* plain- or ciphertext */
307 if (ctx->aad) { /* wrap up aad */
308 if ((rem = (size_t)ctx->len.aad % POLY1305_BLOCK_SIZE))
309 Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
313 ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
314 if (plen == NO_TLS_PAYLOAD_LENGTH)
316 else if (inl != plen + POLY1305_BLOCK_SIZE)
319 if (bctx->enc) { /* plaintext */
320 ctx->chacha.base.hw->cipher(&ctx->chacha.base, out, in, plen);
321 Poly1305_Update(poly, out, plen);
324 ctx->len.text += plen;
325 } else { /* ciphertext */
326 Poly1305_Update(poly, in, plen);
327 ctx->chacha.base.hw->cipher(&ctx->chacha.base, out, in, plen);
330 ctx->len.text += plen;
334 /* explicit final, or tls mode */
335 if (in == NULL || inl != plen) {
337 unsigned char temp[POLY1305_BLOCK_SIZE];
339 if (ctx->aad) { /* wrap up aad */
340 if ((rem = (size_t)ctx->len.aad % POLY1305_BLOCK_SIZE))
341 Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
345 if ((rem = (size_t)ctx->len.text % POLY1305_BLOCK_SIZE))
346 Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
348 if (is_endian.little) {
349 Poly1305_Update(poly, (unsigned char *)&ctx->len,
350 POLY1305_BLOCK_SIZE);
352 temp[0] = (unsigned char)(ctx->len.aad);
353 temp[1] = (unsigned char)(ctx->len.aad>>8);
354 temp[2] = (unsigned char)(ctx->len.aad>>16);
355 temp[3] = (unsigned char)(ctx->len.aad>>24);
356 temp[4] = (unsigned char)(ctx->len.aad>>32);
357 temp[5] = (unsigned char)(ctx->len.aad>>40);
358 temp[6] = (unsigned char)(ctx->len.aad>>48);
359 temp[7] = (unsigned char)(ctx->len.aad>>56);
360 temp[8] = (unsigned char)(ctx->len.text);
361 temp[9] = (unsigned char)(ctx->len.text>>8);
362 temp[10] = (unsigned char)(ctx->len.text>>16);
363 temp[11] = (unsigned char)(ctx->len.text>>24);
364 temp[12] = (unsigned char)(ctx->len.text>>32);
365 temp[13] = (unsigned char)(ctx->len.text>>40);
366 temp[14] = (unsigned char)(ctx->len.text>>48);
367 temp[15] = (unsigned char)(ctx->len.text>>56);
368 Poly1305_Update(poly, temp, POLY1305_BLOCK_SIZE);
370 Poly1305_Final(poly, bctx->enc ? ctx->tag : temp);
373 if (in != NULL && inl != plen) {
375 memcpy(out, ctx->tag, POLY1305_BLOCK_SIZE);
377 if (CRYPTO_memcmp(temp, in, POLY1305_BLOCK_SIZE)) {
378 memset(out - plen, 0, plen);
383 else if (!bctx->enc) {
384 if (CRYPTO_memcmp(temp, ctx->tag, ctx->tag_len))
396 static const PROV_CIPHER_HW_CHACHA20_POLY1305 chacha20poly1305_hw =
398 { chacha20_poly1305_initkey, NULL },
399 chacha20_poly1305_aead_cipher,
400 chacha20_poly1305_initiv,
401 chacha_poly1305_tls_init,
402 chacha_poly1305_tls_iv_set_fixed
405 const PROV_CIPHER_HW *PROV_CIPHER_HW_chacha20_poly1305(size_t keybits)
407 return (PROV_CIPHER_HW *)&chacha20poly1305_hw;