xref: /freebsd-src/crypto/openssl/providers/implementations/ciphers/cipher_chacha20_poly1305_hw.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery /* chacha20_poly1305 cipher implementation */
11*b077aed3SPierre Pronchery 
12*b077aed3SPierre Pronchery #include "internal/endian.h"
13*b077aed3SPierre Pronchery #include "cipher_chacha20_poly1305.h"
14*b077aed3SPierre Pronchery 
chacha_poly1305_tls_init(PROV_CIPHER_CTX * bctx,unsigned char * aad,size_t alen)15*b077aed3SPierre Pronchery static int chacha_poly1305_tls_init(PROV_CIPHER_CTX *bctx,
16*b077aed3SPierre Pronchery                                     unsigned char *aad, size_t alen)
17*b077aed3SPierre Pronchery {
18*b077aed3SPierre Pronchery     unsigned int len;
19*b077aed3SPierre Pronchery     PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
20*b077aed3SPierre Pronchery 
21*b077aed3SPierre Pronchery     if (alen != EVP_AEAD_TLS1_AAD_LEN)
22*b077aed3SPierre Pronchery         return 0;
23*b077aed3SPierre Pronchery 
24*b077aed3SPierre Pronchery     memcpy(ctx->tls_aad, aad, EVP_AEAD_TLS1_AAD_LEN);
25*b077aed3SPierre Pronchery     len = aad[EVP_AEAD_TLS1_AAD_LEN - 2] << 8 | aad[EVP_AEAD_TLS1_AAD_LEN - 1];
26*b077aed3SPierre Pronchery     aad = ctx->tls_aad;
27*b077aed3SPierre Pronchery     if (!bctx->enc) {
28*b077aed3SPierre Pronchery         if (len < POLY1305_BLOCK_SIZE)
29*b077aed3SPierre Pronchery             return 0;
30*b077aed3SPierre Pronchery         len -= POLY1305_BLOCK_SIZE; /* discount attached tag */
31*b077aed3SPierre Pronchery         aad[EVP_AEAD_TLS1_AAD_LEN - 2] = (unsigned char)(len >> 8);
32*b077aed3SPierre Pronchery         aad[EVP_AEAD_TLS1_AAD_LEN - 1] = (unsigned char)len;
33*b077aed3SPierre Pronchery     }
34*b077aed3SPierre Pronchery     ctx->tls_payload_length = len;
35*b077aed3SPierre Pronchery 
36*b077aed3SPierre Pronchery     /* merge record sequence number as per RFC7905 */
37*b077aed3SPierre Pronchery     ctx->chacha.counter[1] = ctx->nonce[0];
38*b077aed3SPierre Pronchery     ctx->chacha.counter[2] = ctx->nonce[1] ^ CHACHA_U8TOU32(aad);
39*b077aed3SPierre Pronchery     ctx->chacha.counter[3] = ctx->nonce[2] ^ CHACHA_U8TOU32(aad+4);
40*b077aed3SPierre Pronchery     ctx->mac_inited = 0;
41*b077aed3SPierre Pronchery 
42*b077aed3SPierre Pronchery     return POLY1305_BLOCK_SIZE;         /* tag length */
43*b077aed3SPierre Pronchery }
44*b077aed3SPierre Pronchery 
chacha_poly1305_tls_iv_set_fixed(PROV_CIPHER_CTX * bctx,unsigned char * fixed,size_t flen)45*b077aed3SPierre Pronchery static int chacha_poly1305_tls_iv_set_fixed(PROV_CIPHER_CTX *bctx,
46*b077aed3SPierre Pronchery                                             unsigned char *fixed, size_t flen)
47*b077aed3SPierre Pronchery {
48*b077aed3SPierre Pronchery     PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
49*b077aed3SPierre Pronchery 
50*b077aed3SPierre Pronchery     if (flen != CHACHA20_POLY1305_IVLEN)
51*b077aed3SPierre Pronchery         return 0;
52*b077aed3SPierre Pronchery     ctx->nonce[0] = ctx->chacha.counter[1] = CHACHA_U8TOU32(fixed);
53*b077aed3SPierre Pronchery     ctx->nonce[1] = ctx->chacha.counter[2] = CHACHA_U8TOU32(fixed + 4);
54*b077aed3SPierre Pronchery     ctx->nonce[2] = ctx->chacha.counter[3] = CHACHA_U8TOU32(fixed + 8);
55*b077aed3SPierre Pronchery     return 1;
56*b077aed3SPierre Pronchery }
57*b077aed3SPierre Pronchery 
chacha20_poly1305_initkey(PROV_CIPHER_CTX * bctx,const unsigned char * key,size_t keylen)58*b077aed3SPierre Pronchery static int chacha20_poly1305_initkey(PROV_CIPHER_CTX *bctx,
59*b077aed3SPierre Pronchery                                      const unsigned char *key, size_t keylen)
60*b077aed3SPierre Pronchery {
61*b077aed3SPierre Pronchery     PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
62*b077aed3SPierre Pronchery 
63*b077aed3SPierre Pronchery     ctx->len.aad = 0;
64*b077aed3SPierre Pronchery     ctx->len.text = 0;
65*b077aed3SPierre Pronchery     ctx->aad = 0;
66*b077aed3SPierre Pronchery     ctx->mac_inited = 0;
67*b077aed3SPierre Pronchery     ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
68*b077aed3SPierre Pronchery 
69*b077aed3SPierre Pronchery     if (bctx->enc)
70*b077aed3SPierre Pronchery         return ossl_chacha20_einit(&ctx->chacha, key, keylen, NULL, 0, NULL);
71*b077aed3SPierre Pronchery     else
72*b077aed3SPierre Pronchery         return ossl_chacha20_dinit(&ctx->chacha, key, keylen, NULL, 0, NULL);
73*b077aed3SPierre Pronchery }
74*b077aed3SPierre Pronchery 
chacha20_poly1305_initiv(PROV_CIPHER_CTX * bctx)75*b077aed3SPierre Pronchery static int chacha20_poly1305_initiv(PROV_CIPHER_CTX *bctx)
76*b077aed3SPierre Pronchery {
77*b077aed3SPierre Pronchery     PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
78*b077aed3SPierre Pronchery     unsigned char tempiv[CHACHA_CTR_SIZE] = { 0 };
79*b077aed3SPierre Pronchery     int ret = 1;
80*b077aed3SPierre Pronchery     size_t noncelen = CHACHA20_POLY1305_IVLEN;
81*b077aed3SPierre Pronchery 
82*b077aed3SPierre Pronchery     ctx->len.aad = 0;
83*b077aed3SPierre Pronchery     ctx->len.text = 0;
84*b077aed3SPierre Pronchery     ctx->aad = 0;
85*b077aed3SPierre Pronchery     ctx->mac_inited = 0;
86*b077aed3SPierre Pronchery     ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
87*b077aed3SPierre Pronchery 
88*b077aed3SPierre Pronchery     /* pad on the left */
89*b077aed3SPierre Pronchery     memcpy(tempiv + CHACHA_CTR_SIZE - noncelen, bctx->oiv,
90*b077aed3SPierre Pronchery            noncelen);
91*b077aed3SPierre Pronchery 
92*b077aed3SPierre Pronchery     if (bctx->enc)
93*b077aed3SPierre Pronchery         ret = ossl_chacha20_einit(&ctx->chacha, NULL, 0,
94*b077aed3SPierre Pronchery                                   tempiv, sizeof(tempiv), NULL);
95*b077aed3SPierre Pronchery     else
96*b077aed3SPierre Pronchery         ret = ossl_chacha20_dinit(&ctx->chacha, NULL, 0,
97*b077aed3SPierre Pronchery                                   tempiv, sizeof(tempiv), NULL);
98*b077aed3SPierre Pronchery     ctx->nonce[0] = ctx->chacha.counter[1];
99*b077aed3SPierre Pronchery     ctx->nonce[1] = ctx->chacha.counter[2];
100*b077aed3SPierre Pronchery     ctx->nonce[2] = ctx->chacha.counter[3];
101*b077aed3SPierre Pronchery     bctx->iv_set = 1;
102*b077aed3SPierre Pronchery     return ret;
103*b077aed3SPierre Pronchery }
104*b077aed3SPierre Pronchery 
105*b077aed3SPierre Pronchery #if !defined(OPENSSL_SMALL_FOOTPRINT)
106*b077aed3SPierre Pronchery 
107*b077aed3SPierre Pronchery # if defined(POLY1305_ASM) && (defined(__x86_64) || defined(__x86_64__) \
108*b077aed3SPierre Pronchery      || defined(_M_AMD64) || defined(_M_X64))
109*b077aed3SPierre Pronchery #  define XOR128_HELPERS
110*b077aed3SPierre Pronchery void *xor128_encrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
111*b077aed3SPierre Pronchery void *xor128_decrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
112*b077aed3SPierre Pronchery static const unsigned char zero[4 * CHACHA_BLK_SIZE] = { 0 };
113*b077aed3SPierre Pronchery # else
114*b077aed3SPierre Pronchery static const unsigned char zero[2 * CHACHA_BLK_SIZE] = { 0 };
115*b077aed3SPierre Pronchery # endif
116*b077aed3SPierre Pronchery 
chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX * bctx,unsigned char * out,size_t * out_padlen,const unsigned char * in,size_t len)117*b077aed3SPierre Pronchery static int chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX *bctx,
118*b077aed3SPierre Pronchery                                         unsigned char *out,
119*b077aed3SPierre Pronchery                                         size_t *out_padlen,
120*b077aed3SPierre Pronchery                                         const unsigned char *in, size_t len)
121*b077aed3SPierre Pronchery {
122*b077aed3SPierre Pronchery     PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
123*b077aed3SPierre Pronchery     POLY1305 *poly = &ctx->poly1305;
124*b077aed3SPierre Pronchery     size_t tail, tohash_len, buf_len, plen = ctx->tls_payload_length;
125*b077aed3SPierre Pronchery     unsigned char *buf, *tohash, *ctr, storage[sizeof(zero) + 32];
126*b077aed3SPierre Pronchery 
127*b077aed3SPierre Pronchery     DECLARE_IS_ENDIAN;
128*b077aed3SPierre Pronchery 
129*b077aed3SPierre Pronchery     buf = storage + ((0 - (size_t)storage) & 15);   /* align */
130*b077aed3SPierre Pronchery     ctr = buf + CHACHA_BLK_SIZE;
131*b077aed3SPierre Pronchery     tohash = buf + CHACHA_BLK_SIZE - POLY1305_BLOCK_SIZE;
132*b077aed3SPierre Pronchery 
133*b077aed3SPierre Pronchery # ifdef XOR128_HELPERS
134*b077aed3SPierre Pronchery     if (plen <= 3 * CHACHA_BLK_SIZE) {
135*b077aed3SPierre Pronchery         ctx->chacha.counter[0] = 0;
136*b077aed3SPierre Pronchery         buf_len = (plen + 2 * CHACHA_BLK_SIZE - 1) & (0 - CHACHA_BLK_SIZE);
137*b077aed3SPierre Pronchery         ChaCha20_ctr32(buf, zero, buf_len, ctx->chacha.key.d, ctx->chacha.counter);
138*b077aed3SPierre Pronchery         Poly1305_Init(poly, buf);
139*b077aed3SPierre Pronchery         ctx->chacha.partial_len = 0;
140*b077aed3SPierre Pronchery         memcpy(tohash, ctx->tls_aad, POLY1305_BLOCK_SIZE);
141*b077aed3SPierre Pronchery         tohash_len = POLY1305_BLOCK_SIZE;
142*b077aed3SPierre Pronchery         ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
143*b077aed3SPierre Pronchery         ctx->len.text = plen;
144*b077aed3SPierre Pronchery 
145*b077aed3SPierre Pronchery         if (plen) {
146*b077aed3SPierre Pronchery             if (bctx->enc)
147*b077aed3SPierre Pronchery                 ctr = xor128_encrypt_n_pad(out, in, ctr, plen);
148*b077aed3SPierre Pronchery             else
149*b077aed3SPierre Pronchery                 ctr = xor128_decrypt_n_pad(out, in, ctr, plen);
150*b077aed3SPierre Pronchery 
151*b077aed3SPierre Pronchery             in += plen;
152*b077aed3SPierre Pronchery             out += plen;
153*b077aed3SPierre Pronchery             tohash_len = (size_t)(ctr - tohash);
154*b077aed3SPierre Pronchery         }
155*b077aed3SPierre Pronchery     }
156*b077aed3SPierre Pronchery # else
157*b077aed3SPierre Pronchery     if (plen <= CHACHA_BLK_SIZE) {
158*b077aed3SPierre Pronchery         size_t i;
159*b077aed3SPierre Pronchery 
160*b077aed3SPierre Pronchery         ctx->chacha.counter[0] = 0;
161*b077aed3SPierre Pronchery         ChaCha20_ctr32(buf, zero, (buf_len = 2 * CHACHA_BLK_SIZE),
162*b077aed3SPierre Pronchery                        ctx->chacha.key.d, ctx->chacha.counter);
163*b077aed3SPierre Pronchery         Poly1305_Init(poly, buf);
164*b077aed3SPierre Pronchery         ctx->chacha.partial_len = 0;
165*b077aed3SPierre Pronchery         memcpy(tohash, ctx->tls_aad, POLY1305_BLOCK_SIZE);
166*b077aed3SPierre Pronchery         tohash_len = POLY1305_BLOCK_SIZE;
167*b077aed3SPierre Pronchery         ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
168*b077aed3SPierre Pronchery         ctx->len.text = plen;
169*b077aed3SPierre Pronchery 
170*b077aed3SPierre Pronchery         if (bctx->enc) {
171*b077aed3SPierre Pronchery             for (i = 0; i < plen; i++)
172*b077aed3SPierre Pronchery                 out[i] = ctr[i] ^= in[i];
173*b077aed3SPierre Pronchery         } else {
174*b077aed3SPierre Pronchery             for (i = 0; i < plen; i++) {
175*b077aed3SPierre Pronchery                 unsigned char c = in[i];
176*b077aed3SPierre Pronchery 
177*b077aed3SPierre Pronchery                 out[i] = ctr[i] ^ c;
178*b077aed3SPierre Pronchery                 ctr[i] = c;
179*b077aed3SPierre Pronchery             }
180*b077aed3SPierre Pronchery         }
181*b077aed3SPierre Pronchery 
182*b077aed3SPierre Pronchery         in += i;
183*b077aed3SPierre Pronchery         out += i;
184*b077aed3SPierre Pronchery 
185*b077aed3SPierre Pronchery         tail = (0 - i) & (POLY1305_BLOCK_SIZE - 1);
186*b077aed3SPierre Pronchery         memset(ctr + i, 0, tail);
187*b077aed3SPierre Pronchery         ctr += i + tail;
188*b077aed3SPierre Pronchery         tohash_len += i + tail;
189*b077aed3SPierre Pronchery     }
190*b077aed3SPierre Pronchery # endif
191*b077aed3SPierre Pronchery     else {
192*b077aed3SPierre Pronchery         ctx->chacha.counter[0] = 0;
193*b077aed3SPierre Pronchery         ChaCha20_ctr32(buf, zero, (buf_len = CHACHA_BLK_SIZE),
194*b077aed3SPierre Pronchery                        ctx->chacha.key.d, ctx->chacha.counter);
195*b077aed3SPierre Pronchery         Poly1305_Init(poly, buf);
196*b077aed3SPierre Pronchery         ctx->chacha.counter[0] = 1;
197*b077aed3SPierre Pronchery         ctx->chacha.partial_len = 0;
198*b077aed3SPierre Pronchery         Poly1305_Update(poly, ctx->tls_aad, POLY1305_BLOCK_SIZE);
199*b077aed3SPierre Pronchery         tohash = ctr;
200*b077aed3SPierre Pronchery         tohash_len = 0;
201*b077aed3SPierre Pronchery         ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
202*b077aed3SPierre Pronchery         ctx->len.text = plen;
203*b077aed3SPierre Pronchery 
204*b077aed3SPierre Pronchery         if (bctx->enc) {
205*b077aed3SPierre Pronchery             ChaCha20_ctr32(out, in, plen, ctx->chacha.key.d, ctx->chacha.counter);
206*b077aed3SPierre Pronchery             Poly1305_Update(poly, out, plen);
207*b077aed3SPierre Pronchery         } else {
208*b077aed3SPierre Pronchery             Poly1305_Update(poly, in, plen);
209*b077aed3SPierre Pronchery             ChaCha20_ctr32(out, in, plen, ctx->chacha.key.d, ctx->chacha.counter);
210*b077aed3SPierre Pronchery         }
211*b077aed3SPierre Pronchery 
212*b077aed3SPierre Pronchery         in += plen;
213*b077aed3SPierre Pronchery         out += plen;
214*b077aed3SPierre Pronchery         tail = (0 - plen) & (POLY1305_BLOCK_SIZE - 1);
215*b077aed3SPierre Pronchery         Poly1305_Update(poly, zero, tail);
216*b077aed3SPierre Pronchery     }
217*b077aed3SPierre Pronchery 
218*b077aed3SPierre Pronchery     if (IS_LITTLE_ENDIAN) {
219*b077aed3SPierre Pronchery         memcpy(ctr, (unsigned char *)&ctx->len, POLY1305_BLOCK_SIZE);
220*b077aed3SPierre Pronchery     } else {
221*b077aed3SPierre Pronchery         ctr[0]  = (unsigned char)(ctx->len.aad);
222*b077aed3SPierre Pronchery         ctr[1]  = (unsigned char)(ctx->len.aad>>8);
223*b077aed3SPierre Pronchery         ctr[2]  = (unsigned char)(ctx->len.aad>>16);
224*b077aed3SPierre Pronchery         ctr[3]  = (unsigned char)(ctx->len.aad>>24);
225*b077aed3SPierre Pronchery         ctr[4]  = (unsigned char)(ctx->len.aad>>32);
226*b077aed3SPierre Pronchery         ctr[5]  = (unsigned char)(ctx->len.aad>>40);
227*b077aed3SPierre Pronchery         ctr[6]  = (unsigned char)(ctx->len.aad>>48);
228*b077aed3SPierre Pronchery         ctr[7]  = (unsigned char)(ctx->len.aad>>56);
229*b077aed3SPierre Pronchery 
230*b077aed3SPierre Pronchery         ctr[8]  = (unsigned char)(ctx->len.text);
231*b077aed3SPierre Pronchery         ctr[9]  = (unsigned char)(ctx->len.text>>8);
232*b077aed3SPierre Pronchery         ctr[10] = (unsigned char)(ctx->len.text>>16);
233*b077aed3SPierre Pronchery         ctr[11] = (unsigned char)(ctx->len.text>>24);
234*b077aed3SPierre Pronchery         ctr[12] = (unsigned char)(ctx->len.text>>32);
235*b077aed3SPierre Pronchery         ctr[13] = (unsigned char)(ctx->len.text>>40);
236*b077aed3SPierre Pronchery         ctr[14] = (unsigned char)(ctx->len.text>>48);
237*b077aed3SPierre Pronchery         ctr[15] = (unsigned char)(ctx->len.text>>56);
238*b077aed3SPierre Pronchery     }
239*b077aed3SPierre Pronchery     tohash_len += POLY1305_BLOCK_SIZE;
240*b077aed3SPierre Pronchery 
241*b077aed3SPierre Pronchery     Poly1305_Update(poly, tohash, tohash_len);
242*b077aed3SPierre Pronchery     OPENSSL_cleanse(buf, buf_len);
243*b077aed3SPierre Pronchery     Poly1305_Final(poly, bctx->enc ? ctx->tag : tohash);
244*b077aed3SPierre Pronchery 
245*b077aed3SPierre Pronchery     ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
246*b077aed3SPierre Pronchery 
247*b077aed3SPierre Pronchery     if (bctx->enc) {
248*b077aed3SPierre Pronchery         memcpy(out, ctx->tag, POLY1305_BLOCK_SIZE);
249*b077aed3SPierre Pronchery     } else {
250*b077aed3SPierre Pronchery         if (CRYPTO_memcmp(tohash, in, POLY1305_BLOCK_SIZE)) {
251*b077aed3SPierre Pronchery             if (len > POLY1305_BLOCK_SIZE)
252*b077aed3SPierre Pronchery                 memset(out - (len - POLY1305_BLOCK_SIZE), 0,
253*b077aed3SPierre Pronchery                        len - POLY1305_BLOCK_SIZE);
254*b077aed3SPierre Pronchery             return 0;
255*b077aed3SPierre Pronchery         }
256*b077aed3SPierre Pronchery         /* Strip the tag */
257*b077aed3SPierre Pronchery         len -= POLY1305_BLOCK_SIZE;
258*b077aed3SPierre Pronchery     }
259*b077aed3SPierre Pronchery 
260*b077aed3SPierre Pronchery     *out_padlen = len;
261*b077aed3SPierre Pronchery     return 1;
262*b077aed3SPierre Pronchery }
263*b077aed3SPierre Pronchery #else
264*b077aed3SPierre Pronchery static const unsigned char zero[CHACHA_BLK_SIZE] = { 0 };
265*b077aed3SPierre Pronchery #endif /* OPENSSL_SMALL_FOOTPRINT */
266*b077aed3SPierre Pronchery 
chacha20_poly1305_aead_cipher(PROV_CIPHER_CTX * bctx,unsigned char * out,size_t * outl,const unsigned char * in,size_t inl)267*b077aed3SPierre Pronchery static int chacha20_poly1305_aead_cipher(PROV_CIPHER_CTX *bctx,
268*b077aed3SPierre Pronchery                                          unsigned char *out, size_t *outl,
269*b077aed3SPierre Pronchery                                          const unsigned char *in, size_t inl)
270*b077aed3SPierre Pronchery {
271*b077aed3SPierre Pronchery     PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
272*b077aed3SPierre Pronchery     POLY1305 *poly = &ctx->poly1305;
273*b077aed3SPierre Pronchery     size_t rem, plen = ctx->tls_payload_length;
274*b077aed3SPierre Pronchery     size_t olen = 0;
275*b077aed3SPierre Pronchery     int rv = 0;
276*b077aed3SPierre Pronchery 
277*b077aed3SPierre Pronchery     DECLARE_IS_ENDIAN;
278*b077aed3SPierre Pronchery 
279*b077aed3SPierre Pronchery     if (!ctx->mac_inited) {
280*b077aed3SPierre Pronchery         if (plen != NO_TLS_PAYLOAD_LENGTH && out != NULL) {
281*b077aed3SPierre Pronchery             if (inl != plen + POLY1305_BLOCK_SIZE)
282*b077aed3SPierre Pronchery                 return 0;
283*b077aed3SPierre Pronchery #if !defined(OPENSSL_SMALL_FOOTPRINT)
284*b077aed3SPierre Pronchery             return chacha20_poly1305_tls_cipher(bctx, out, outl, in, inl);
285*b077aed3SPierre Pronchery #endif
286*b077aed3SPierre Pronchery         }
287*b077aed3SPierre Pronchery 
288*b077aed3SPierre Pronchery         ctx->chacha.counter[0] = 0;
289*b077aed3SPierre Pronchery         ChaCha20_ctr32(ctx->chacha.buf, zero, CHACHA_BLK_SIZE,
290*b077aed3SPierre Pronchery                        ctx->chacha.key.d, ctx->chacha.counter);
291*b077aed3SPierre Pronchery         Poly1305_Init(poly, ctx->chacha.buf);
292*b077aed3SPierre Pronchery         ctx->chacha.counter[0] = 1;
293*b077aed3SPierre Pronchery         ctx->chacha.partial_len = 0;
294*b077aed3SPierre Pronchery         ctx->len.aad = ctx->len.text = 0;
295*b077aed3SPierre Pronchery         ctx->mac_inited = 1;
296*b077aed3SPierre Pronchery         if (plen != NO_TLS_PAYLOAD_LENGTH) {
297*b077aed3SPierre Pronchery             Poly1305_Update(poly, ctx->tls_aad, EVP_AEAD_TLS1_AAD_LEN);
298*b077aed3SPierre Pronchery             ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
299*b077aed3SPierre Pronchery             ctx->aad = 1;
300*b077aed3SPierre Pronchery         }
301*b077aed3SPierre Pronchery     }
302*b077aed3SPierre Pronchery 
303*b077aed3SPierre Pronchery     if (in != NULL) { /* aad or text */
304*b077aed3SPierre Pronchery         if (out == NULL) { /* aad */
305*b077aed3SPierre Pronchery             Poly1305_Update(poly, in, inl);
306*b077aed3SPierre Pronchery             ctx->len.aad += inl;
307*b077aed3SPierre Pronchery             ctx->aad = 1;
308*b077aed3SPierre Pronchery             goto finish;
309*b077aed3SPierre Pronchery         } else { /* plain- or ciphertext */
310*b077aed3SPierre Pronchery             if (ctx->aad) { /* wrap up aad */
311*b077aed3SPierre Pronchery                 if ((rem = (size_t)ctx->len.aad % POLY1305_BLOCK_SIZE))
312*b077aed3SPierre Pronchery                     Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
313*b077aed3SPierre Pronchery                 ctx->aad = 0;
314*b077aed3SPierre Pronchery             }
315*b077aed3SPierre Pronchery 
316*b077aed3SPierre Pronchery             ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
317*b077aed3SPierre Pronchery             if (plen == NO_TLS_PAYLOAD_LENGTH)
318*b077aed3SPierre Pronchery                 plen = inl;
319*b077aed3SPierre Pronchery             else if (inl != plen + POLY1305_BLOCK_SIZE)
320*b077aed3SPierre Pronchery                 goto err;
321*b077aed3SPierre Pronchery 
322*b077aed3SPierre Pronchery             if (bctx->enc) { /* plaintext */
323*b077aed3SPierre Pronchery                 ctx->chacha.base.hw->cipher(&ctx->chacha.base, out, in, plen);
324*b077aed3SPierre Pronchery                 Poly1305_Update(poly, out, plen);
325*b077aed3SPierre Pronchery                 in += plen;
326*b077aed3SPierre Pronchery                 out += plen;
327*b077aed3SPierre Pronchery                 ctx->len.text += plen;
328*b077aed3SPierre Pronchery             } else { /* ciphertext */
329*b077aed3SPierre Pronchery                 Poly1305_Update(poly, in, plen);
330*b077aed3SPierre Pronchery                 ctx->chacha.base.hw->cipher(&ctx->chacha.base, out, in, plen);
331*b077aed3SPierre Pronchery                 in += plen;
332*b077aed3SPierre Pronchery                 out += plen;
333*b077aed3SPierre Pronchery                 ctx->len.text += plen;
334*b077aed3SPierre Pronchery             }
335*b077aed3SPierre Pronchery         }
336*b077aed3SPierre Pronchery     }
337*b077aed3SPierre Pronchery     /* explicit final, or tls mode */
338*b077aed3SPierre Pronchery     if (in == NULL || inl != plen) {
339*b077aed3SPierre Pronchery 
340*b077aed3SPierre Pronchery         unsigned char temp[POLY1305_BLOCK_SIZE];
341*b077aed3SPierre Pronchery 
342*b077aed3SPierre Pronchery         if (ctx->aad) {                        /* wrap up aad */
343*b077aed3SPierre Pronchery             if ((rem = (size_t)ctx->len.aad % POLY1305_BLOCK_SIZE))
344*b077aed3SPierre Pronchery                 Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
345*b077aed3SPierre Pronchery             ctx->aad = 0;
346*b077aed3SPierre Pronchery         }
347*b077aed3SPierre Pronchery 
348*b077aed3SPierre Pronchery         if ((rem = (size_t)ctx->len.text % POLY1305_BLOCK_SIZE))
349*b077aed3SPierre Pronchery             Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
350*b077aed3SPierre Pronchery 
351*b077aed3SPierre Pronchery         if (IS_LITTLE_ENDIAN) {
352*b077aed3SPierre Pronchery             Poly1305_Update(poly, (unsigned char *)&ctx->len,
353*b077aed3SPierre Pronchery                             POLY1305_BLOCK_SIZE);
354*b077aed3SPierre Pronchery         } else {
355*b077aed3SPierre Pronchery             temp[0]  = (unsigned char)(ctx->len.aad);
356*b077aed3SPierre Pronchery             temp[1]  = (unsigned char)(ctx->len.aad>>8);
357*b077aed3SPierre Pronchery             temp[2]  = (unsigned char)(ctx->len.aad>>16);
358*b077aed3SPierre Pronchery             temp[3]  = (unsigned char)(ctx->len.aad>>24);
359*b077aed3SPierre Pronchery             temp[4]  = (unsigned char)(ctx->len.aad>>32);
360*b077aed3SPierre Pronchery             temp[5]  = (unsigned char)(ctx->len.aad>>40);
361*b077aed3SPierre Pronchery             temp[6]  = (unsigned char)(ctx->len.aad>>48);
362*b077aed3SPierre Pronchery             temp[7]  = (unsigned char)(ctx->len.aad>>56);
363*b077aed3SPierre Pronchery             temp[8]  = (unsigned char)(ctx->len.text);
364*b077aed3SPierre Pronchery             temp[9]  = (unsigned char)(ctx->len.text>>8);
365*b077aed3SPierre Pronchery             temp[10] = (unsigned char)(ctx->len.text>>16);
366*b077aed3SPierre Pronchery             temp[11] = (unsigned char)(ctx->len.text>>24);
367*b077aed3SPierre Pronchery             temp[12] = (unsigned char)(ctx->len.text>>32);
368*b077aed3SPierre Pronchery             temp[13] = (unsigned char)(ctx->len.text>>40);
369*b077aed3SPierre Pronchery             temp[14] = (unsigned char)(ctx->len.text>>48);
370*b077aed3SPierre Pronchery             temp[15] = (unsigned char)(ctx->len.text>>56);
371*b077aed3SPierre Pronchery             Poly1305_Update(poly, temp, POLY1305_BLOCK_SIZE);
372*b077aed3SPierre Pronchery         }
373*b077aed3SPierre Pronchery         Poly1305_Final(poly, bctx->enc ? ctx->tag : temp);
374*b077aed3SPierre Pronchery         ctx->mac_inited = 0;
375*b077aed3SPierre Pronchery 
376*b077aed3SPierre Pronchery         if (in != NULL && inl != plen) {
377*b077aed3SPierre Pronchery             if (bctx->enc) {
378*b077aed3SPierre Pronchery                 memcpy(out, ctx->tag, POLY1305_BLOCK_SIZE);
379*b077aed3SPierre Pronchery             } else {
380*b077aed3SPierre Pronchery                 if (CRYPTO_memcmp(temp, in, POLY1305_BLOCK_SIZE)) {
381*b077aed3SPierre Pronchery                     memset(out - plen, 0, plen);
382*b077aed3SPierre Pronchery                     goto err;
383*b077aed3SPierre Pronchery                 }
384*b077aed3SPierre Pronchery                 /* Strip the tag */
385*b077aed3SPierre Pronchery                 inl -= POLY1305_BLOCK_SIZE;
386*b077aed3SPierre Pronchery             }
387*b077aed3SPierre Pronchery         }
388*b077aed3SPierre Pronchery         else if (!bctx->enc) {
389*b077aed3SPierre Pronchery             if (CRYPTO_memcmp(temp, ctx->tag, ctx->tag_len))
390*b077aed3SPierre Pronchery                 goto err;
391*b077aed3SPierre Pronchery         }
392*b077aed3SPierre Pronchery     }
393*b077aed3SPierre Pronchery finish:
394*b077aed3SPierre Pronchery     olen = inl;
395*b077aed3SPierre Pronchery     rv = 1;
396*b077aed3SPierre Pronchery err:
397*b077aed3SPierre Pronchery     *outl = olen;
398*b077aed3SPierre Pronchery     return rv;
399*b077aed3SPierre Pronchery }
400*b077aed3SPierre Pronchery 
401*b077aed3SPierre Pronchery static const PROV_CIPHER_HW_CHACHA20_POLY1305 chacha20poly1305_hw =
402*b077aed3SPierre Pronchery {
403*b077aed3SPierre Pronchery     { chacha20_poly1305_initkey, NULL },
404*b077aed3SPierre Pronchery     chacha20_poly1305_aead_cipher,
405*b077aed3SPierre Pronchery     chacha20_poly1305_initiv,
406*b077aed3SPierre Pronchery     chacha_poly1305_tls_init,
407*b077aed3SPierre Pronchery     chacha_poly1305_tls_iv_set_fixed
408*b077aed3SPierre Pronchery };
409*b077aed3SPierre Pronchery 
ossl_prov_cipher_hw_chacha20_poly1305(size_t keybits)410*b077aed3SPierre Pronchery const PROV_CIPHER_HW *ossl_prov_cipher_hw_chacha20_poly1305(size_t keybits)
411*b077aed3SPierre Pronchery {
412*b077aed3SPierre Pronchery     return (PROV_CIPHER_HW *)&chacha20poly1305_hw;
413*b077aed3SPierre Pronchery }
414