Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/evp/e_chacha20_poly1305.c
Line
Count
Source
1
/*
2
 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include "internal/endian.h"
13
14
#ifndef OPENSSL_NO_CHACHA
15
16
#include <openssl/evp.h>
17
#include <openssl/objects.h>
18
#include "crypto/evp.h"
19
#include "evp_local.h"
20
#include "crypto/chacha.h"
21
22
typedef struct {
23
    union {
24
        OSSL_UNION_ALIGN; /* this ensures even sizeof(EVP_CHACHA_KEY)%8==0 */
25
        unsigned int d[CHACHA_KEY_SIZE / 4];
26
    } key;
27
    unsigned int counter[CHACHA_CTR_SIZE / 4];
28
    unsigned char buf[CHACHA_BLK_SIZE];
29
    unsigned int partial_len;
30
} EVP_CHACHA_KEY;
31
32
0
#define data(ctx) ((EVP_CHACHA_KEY *)(ctx)->cipher_data)
33
34
0
#define CHACHA20_POLY1305_MAX_IVLEN 12
35
36
static int chacha_init_key(EVP_CIPHER_CTX *ctx,
37
    const unsigned char user_key[CHACHA_KEY_SIZE],
38
    const unsigned char iv[CHACHA_CTR_SIZE], int enc)
39
0
{
40
0
    EVP_CHACHA_KEY *key = data(ctx);
41
0
    unsigned int i;
42
43
0
    if (user_key)
44
0
        for (i = 0; i < CHACHA_KEY_SIZE; i += 4) {
45
0
            key->key.d[i / 4] = CHACHA_U8TOU32(user_key + i);
46
0
        }
47
48
0
    if (iv)
49
0
        for (i = 0; i < CHACHA_CTR_SIZE; i += 4) {
50
0
            key->counter[i / 4] = CHACHA_U8TOU32(iv + i);
51
0
        }
52
53
0
    key->partial_len = 0;
54
55
0
    return 1;
56
0
}
57
58
static int chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
59
    const unsigned char *inp, size_t len)
60
0
{
61
0
    EVP_CHACHA_KEY *key = data(ctx);
62
0
    unsigned int n, rem, ctr32;
63
64
0
    if ((n = key->partial_len)) {
65
0
        while (len && n < CHACHA_BLK_SIZE) {
66
0
            *out++ = *inp++ ^ key->buf[n++];
67
0
            len--;
68
0
        }
69
0
        key->partial_len = n;
70
71
0
        if (len == 0)
72
0
            return 1;
73
74
0
        if (n == CHACHA_BLK_SIZE) {
75
0
            key->partial_len = 0;
76
0
            key->counter[0]++;
77
0
            if (key->counter[0] == 0)
78
0
                key->counter[1]++;
79
0
        }
80
0
    }
81
82
0
    rem = (unsigned int)(len % CHACHA_BLK_SIZE);
83
0
    len -= rem;
84
0
    ctr32 = key->counter[0];
85
0
    while (len >= CHACHA_BLK_SIZE) {
86
0
        size_t blocks = len / CHACHA_BLK_SIZE;
87
        /*
88
         * 1<<28 is just a not-so-small yet not-so-large number...
89
         * Below condition is practically never met, but it has to
90
         * be checked for code correctness.
91
         */
92
0
        if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))
93
0
            blocks = (1U << 28);
94
95
        /*
96
         * As ChaCha20_ctr32 operates on 32-bit counter, caller
97
         * has to handle overflow. 'if' below detects the
98
         * overflow, which is then handled by limiting the
99
         * amount of blocks to the exact overflow point...
100
         */
101
0
        ctr32 += (unsigned int)blocks;
102
0
        if (ctr32 < blocks) {
103
0
            blocks -= ctr32;
104
0
            ctr32 = 0;
105
0
        }
106
0
        blocks *= CHACHA_BLK_SIZE;
107
0
        ChaCha20_ctr32(out, inp, blocks, key->key.d, key->counter);
108
0
        len -= blocks;
109
0
        inp += blocks;
110
0
        out += blocks;
111
112
0
        key->counter[0] = ctr32;
113
0
        if (ctr32 == 0)
114
0
            key->counter[1]++;
115
0
    }
116
117
0
    if (rem) {
118
0
        memset(key->buf, 0, sizeof(key->buf));
119
0
        ChaCha20_ctr32(key->buf, key->buf, CHACHA_BLK_SIZE,
120
0
            key->key.d, key->counter);
121
0
        for (n = 0; n < rem; n++)
122
0
            out[n] = inp[n] ^ key->buf[n];
123
0
        key->partial_len = rem;
124
0
    }
125
126
0
    return 1;
127
0
}
128
129
static const EVP_CIPHER chacha20 = {
130
    NID_chacha20,
131
    1, /* block_size */
132
    CHACHA_KEY_SIZE, /* key_len */
133
    CHACHA_CTR_SIZE, /* iv_len, 128-bit counter in the context */
134
    EVP_CIPH_CUSTOM_IV | EVP_CIPH_ALWAYS_CALL_INIT,
135
    EVP_ORIG_GLOBAL,
136
    chacha_init_key,
137
    chacha_cipher,
138
    NULL,
139
    sizeof(EVP_CHACHA_KEY),
140
    NULL,
141
    NULL,
142
    NULL,
143
    NULL
144
};
145
146
const EVP_CIPHER *EVP_chacha20(void)
147
165
{
148
165
    return &chacha20;
149
165
}
150
151
#ifndef OPENSSL_NO_POLY1305
152
#include "crypto/poly1305.h"
153
154
typedef struct {
155
    EVP_CHACHA_KEY key;
156
    unsigned int nonce[12 / 4];
157
    unsigned char tag[POLY1305_BLOCK_SIZE];
158
    unsigned char tls_aad[POLY1305_BLOCK_SIZE];
159
    struct {
160
        uint64_t aad, text;
161
    } len;
162
    int aad, mac_inited, tag_len, nonce_len;
163
    size_t tls_payload_length;
164
} EVP_CHACHA_AEAD_CTX;
165
166
0
#define NO_TLS_PAYLOAD_LENGTH ((size_t)-1)
167
0
#define aead_data(ctx) ((EVP_CHACHA_AEAD_CTX *)(ctx)->cipher_data)
168
0
#define POLY1305_ctx(actx) ((POLY1305 *)(actx + 1))
169
170
static int chacha20_poly1305_init_key(EVP_CIPHER_CTX *ctx,
171
    const unsigned char *inkey,
172
    const unsigned char *iv, int enc)
173
0
{
174
0
    EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
175
176
0
    if (!inkey && !iv)
177
0
        return 1;
178
179
0
    actx->len.aad = 0;
180
0
    actx->len.text = 0;
181
0
    actx->aad = 0;
182
0
    actx->mac_inited = 0;
183
0
    actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
184
185
0
    if (iv != NULL) {
186
0
        unsigned char temp[CHACHA_CTR_SIZE] = { 0 };
187
188
        /* pad on the left */
189
0
        if (actx->nonce_len <= CHACHA_CTR_SIZE)
190
0
            memcpy(temp + CHACHA_CTR_SIZE - actx->nonce_len, iv,
191
0
                actx->nonce_len);
192
193
0
        chacha_init_key(ctx, inkey, temp, enc);
194
195
0
        actx->nonce[0] = actx->key.counter[1];
196
0
        actx->nonce[1] = actx->key.counter[2];
197
0
        actx->nonce[2] = actx->key.counter[3];
198
0
    } else {
199
0
        chacha_init_key(ctx, inkey, NULL, enc);
200
0
    }
201
202
0
    return 1;
203
0
}
204
205
#if !defined(OPENSSL_SMALL_FOOTPRINT)
206
207
#if defined(POLY1305_ASM) && (defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64))
208
#define XOR128_HELPERS
209
void *xor128_encrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
210
void *xor128_decrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
211
static const unsigned char zero[4 * CHACHA_BLK_SIZE] = { 0 };
212
#else
213
static const unsigned char zero[2 * CHACHA_BLK_SIZE] = { 0 };
214
#endif
215
216
static int chacha20_poly1305_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
217
    const unsigned char *in, size_t len)
218
0
{
219
0
    EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
220
0
    size_t tail, tohash_len, buf_len, plen = actx->tls_payload_length;
221
0
    unsigned char *buf, *tohash, *ctr, storage[sizeof(zero) + 32];
222
223
0
    if (len != plen + POLY1305_BLOCK_SIZE)
224
0
        return -1;
225
226
0
    buf = storage + ((0 - (size_t)storage) & 15); /* align */
227
0
    ctr = buf + CHACHA_BLK_SIZE;
228
0
    tohash = buf + CHACHA_BLK_SIZE - POLY1305_BLOCK_SIZE;
229
230
0
#ifdef XOR128_HELPERS
231
0
    if (plen <= 3 * CHACHA_BLK_SIZE) {
232
0
        actx->key.counter[0] = 0;
233
0
        buf_len = (plen + 2 * CHACHA_BLK_SIZE - 1) & (0 - CHACHA_BLK_SIZE);
234
0
        ChaCha20_ctr32(buf, zero, buf_len, actx->key.key.d,
235
0
            actx->key.counter);
236
0
        Poly1305_Init(POLY1305_ctx(actx), buf);
237
0
        actx->key.partial_len = 0;
238
0
        memcpy(tohash, actx->tls_aad, POLY1305_BLOCK_SIZE);
239
0
        tohash_len = POLY1305_BLOCK_SIZE;
240
0
        actx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
241
0
        actx->len.text = plen;
242
243
0
        if (plen) {
244
0
            if (ctx->encrypt)
245
0
                ctr = xor128_encrypt_n_pad(out, in, ctr, plen);
246
0
            else
247
0
                ctr = xor128_decrypt_n_pad(out, in, ctr, plen);
248
249
0
            in += plen;
250
0
            out += plen;
251
0
            tohash_len = (size_t)(ctr - tohash);
252
0
        }
253
0
    }
254
#else
255
    if (plen <= CHACHA_BLK_SIZE) {
256
        size_t i;
257
258
        actx->key.counter[0] = 0;
259
        ChaCha20_ctr32(buf, zero, (buf_len = 2 * CHACHA_BLK_SIZE),
260
            actx->key.key.d, actx->key.counter);
261
        Poly1305_Init(POLY1305_ctx(actx), buf);
262
        actx->key.partial_len = 0;
263
        memcpy(tohash, actx->tls_aad, POLY1305_BLOCK_SIZE);
264
        tohash_len = POLY1305_BLOCK_SIZE;
265
        actx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
266
        actx->len.text = plen;
267
268
        if (ctx->encrypt) {
269
            for (i = 0; i < plen; i++) {
270
                out[i] = ctr[i] ^= in[i];
271
            }
272
        } else {
273
            for (i = 0; i < plen; i++) {
274
                unsigned char c = in[i];
275
                out[i] = ctr[i] ^ c;
276
                ctr[i] = c;
277
            }
278
        }
279
280
        in += i;
281
        out += i;
282
283
        tail = (0 - i) & (POLY1305_BLOCK_SIZE - 1);
284
        memset(ctr + i, 0, tail);
285
        ctr += i + tail;
286
        tohash_len += i + tail;
287
    }
288
#endif
289
0
    else {
290
0
        actx->key.counter[0] = 0;
291
0
        ChaCha20_ctr32(buf, zero, (buf_len = CHACHA_BLK_SIZE),
292
0
            actx->key.key.d, actx->key.counter);
293
0
        Poly1305_Init(POLY1305_ctx(actx), buf);
294
0
        actx->key.counter[0] = 1;
295
0
        actx->key.partial_len = 0;
296
0
        Poly1305_Update(POLY1305_ctx(actx), actx->tls_aad, POLY1305_BLOCK_SIZE);
297
0
        tohash = ctr;
298
0
        tohash_len = 0;
299
0
        actx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
300
0
        actx->len.text = plen;
301
302
0
        if (ctx->encrypt) {
303
0
            ChaCha20_ctr32(out, in, plen, actx->key.key.d, actx->key.counter);
304
0
            Poly1305_Update(POLY1305_ctx(actx), out, plen);
305
0
        } else {
306
0
            Poly1305_Update(POLY1305_ctx(actx), in, plen);
307
0
            ChaCha20_ctr32(out, in, plen, actx->key.key.d, actx->key.counter);
308
0
        }
309
310
0
        in += plen;
311
0
        out += plen;
312
0
        tail = (0 - plen) & (POLY1305_BLOCK_SIZE - 1);
313
0
        Poly1305_Update(POLY1305_ctx(actx), zero, tail);
314
0
    }
315
316
0
    {
317
0
        DECLARE_IS_ENDIAN;
318
319
0
        if (IS_LITTLE_ENDIAN) {
320
0
            memcpy(ctr, (unsigned char *)&actx->len, POLY1305_BLOCK_SIZE);
321
0
        } else {
322
0
            ctr[0] = (unsigned char)(actx->len.aad);
323
0
            ctr[1] = (unsigned char)(actx->len.aad >> 8);
324
0
            ctr[2] = (unsigned char)(actx->len.aad >> 16);
325
0
            ctr[3] = (unsigned char)(actx->len.aad >> 24);
326
0
            ctr[4] = (unsigned char)(actx->len.aad >> 32);
327
0
            ctr[5] = (unsigned char)(actx->len.aad >> 40);
328
0
            ctr[6] = (unsigned char)(actx->len.aad >> 48);
329
0
            ctr[7] = (unsigned char)(actx->len.aad >> 56);
330
331
0
            ctr[8] = (unsigned char)(actx->len.text);
332
0
            ctr[9] = (unsigned char)(actx->len.text >> 8);
333
0
            ctr[10] = (unsigned char)(actx->len.text >> 16);
334
0
            ctr[11] = (unsigned char)(actx->len.text >> 24);
335
0
            ctr[12] = (unsigned char)(actx->len.text >> 32);
336
0
            ctr[13] = (unsigned char)(actx->len.text >> 40);
337
0
            ctr[14] = (unsigned char)(actx->len.text >> 48);
338
0
            ctr[15] = (unsigned char)(actx->len.text >> 56);
339
0
        }
340
0
        tohash_len += POLY1305_BLOCK_SIZE;
341
0
    }
342
343
0
    Poly1305_Update(POLY1305_ctx(actx), tohash, tohash_len);
344
0
    OPENSSL_cleanse(buf, buf_len);
345
0
    Poly1305_Final(POLY1305_ctx(actx), ctx->encrypt ? actx->tag : tohash);
346
347
0
    actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
348
349
0
    if (ctx->encrypt) {
350
0
        memcpy(out, actx->tag, POLY1305_BLOCK_SIZE);
351
0
    } else {
352
0
        if (CRYPTO_memcmp(tohash, in, POLY1305_BLOCK_SIZE)) {
353
0
            memset(out - (len - POLY1305_BLOCK_SIZE), 0,
354
0
                len - POLY1305_BLOCK_SIZE);
355
0
            return -1;
356
0
        }
357
0
    }
358
359
0
    return len;
360
0
}
361
#else
362
static const unsigned char zero[CHACHA_BLK_SIZE] = { 0 };
363
#endif
364
365
static int chacha20_poly1305_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
366
    const unsigned char *in, size_t len)
367
0
{
368
0
    EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
369
0
    size_t rem, plen = actx->tls_payload_length;
370
371
0
    if (!actx->mac_inited) {
372
0
#if !defined(OPENSSL_SMALL_FOOTPRINT)
373
0
        if (plen != NO_TLS_PAYLOAD_LENGTH && out != NULL)
374
0
            return chacha20_poly1305_tls_cipher(ctx, out, in, len);
375
0
#endif
376
0
        actx->key.counter[0] = 0;
377
0
        ChaCha20_ctr32(actx->key.buf, zero, CHACHA_BLK_SIZE,
378
0
            actx->key.key.d, actx->key.counter);
379
0
        Poly1305_Init(POLY1305_ctx(actx), actx->key.buf);
380
0
        actx->key.counter[0] = 1;
381
0
        actx->key.partial_len = 0;
382
0
        actx->len.aad = actx->len.text = 0;
383
0
        actx->mac_inited = 1;
384
0
        if (plen != NO_TLS_PAYLOAD_LENGTH) {
385
0
            Poly1305_Update(POLY1305_ctx(actx), actx->tls_aad,
386
0
                EVP_AEAD_TLS1_AAD_LEN);
387
0
            actx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
388
0
            actx->aad = 1;
389
0
        }
390
0
    }
391
392
0
    if (in) { /* aad or text */
393
0
        if (out == NULL) { /* aad */
394
0
            Poly1305_Update(POLY1305_ctx(actx), in, len);
395
0
            actx->len.aad += len;
396
0
            actx->aad = 1;
397
0
            return len;
398
0
        } else { /* plain- or ciphertext */
399
0
            if (actx->aad) { /* wrap up aad */
400
0
                if ((rem = (size_t)actx->len.aad % POLY1305_BLOCK_SIZE))
401
0
                    Poly1305_Update(POLY1305_ctx(actx), zero,
402
0
                        POLY1305_BLOCK_SIZE - rem);
403
0
                actx->aad = 0;
404
0
            }
405
406
0
            actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
407
0
            if (plen == NO_TLS_PAYLOAD_LENGTH)
408
0
                plen = len;
409
0
            else if (len != plen + POLY1305_BLOCK_SIZE)
410
0
                return -1;
411
412
0
            if (ctx->encrypt) { /* plaintext */
413
0
                chacha_cipher(ctx, out, in, plen);
414
0
                Poly1305_Update(POLY1305_ctx(actx), out, plen);
415
0
                in += plen;
416
0
                out += plen;
417
0
                actx->len.text += plen;
418
0
            } else { /* ciphertext */
419
0
                Poly1305_Update(POLY1305_ctx(actx), in, plen);
420
0
                chacha_cipher(ctx, out, in, plen);
421
0
                in += plen;
422
0
                out += plen;
423
0
                actx->len.text += plen;
424
0
            }
425
0
        }
426
0
    }
427
0
    if (in == NULL /* explicit final */
428
0
        || plen != len) { /* or tls mode */
429
0
        DECLARE_IS_ENDIAN;
430
0
        unsigned char temp[POLY1305_BLOCK_SIZE];
431
432
0
        if (actx->aad) { /* wrap up aad */
433
0
            if ((rem = (size_t)actx->len.aad % POLY1305_BLOCK_SIZE))
434
0
                Poly1305_Update(POLY1305_ctx(actx), zero,
435
0
                    POLY1305_BLOCK_SIZE - rem);
436
0
            actx->aad = 0;
437
0
        }
438
439
0
        if ((rem = (size_t)actx->len.text % POLY1305_BLOCK_SIZE))
440
0
            Poly1305_Update(POLY1305_ctx(actx), zero,
441
0
                POLY1305_BLOCK_SIZE - rem);
442
443
0
        if (IS_LITTLE_ENDIAN) {
444
0
            Poly1305_Update(POLY1305_ctx(actx),
445
0
                (unsigned char *)&actx->len, POLY1305_BLOCK_SIZE);
446
0
        } else {
447
0
            temp[0] = (unsigned char)(actx->len.aad);
448
0
            temp[1] = (unsigned char)(actx->len.aad >> 8);
449
0
            temp[2] = (unsigned char)(actx->len.aad >> 16);
450
0
            temp[3] = (unsigned char)(actx->len.aad >> 24);
451
0
            temp[4] = (unsigned char)(actx->len.aad >> 32);
452
0
            temp[5] = (unsigned char)(actx->len.aad >> 40);
453
0
            temp[6] = (unsigned char)(actx->len.aad >> 48);
454
0
            temp[7] = (unsigned char)(actx->len.aad >> 56);
455
456
0
            temp[8] = (unsigned char)(actx->len.text);
457
0
            temp[9] = (unsigned char)(actx->len.text >> 8);
458
0
            temp[10] = (unsigned char)(actx->len.text >> 16);
459
0
            temp[11] = (unsigned char)(actx->len.text >> 24);
460
0
            temp[12] = (unsigned char)(actx->len.text >> 32);
461
0
            temp[13] = (unsigned char)(actx->len.text >> 40);
462
0
            temp[14] = (unsigned char)(actx->len.text >> 48);
463
0
            temp[15] = (unsigned char)(actx->len.text >> 56);
464
465
0
            Poly1305_Update(POLY1305_ctx(actx), temp, POLY1305_BLOCK_SIZE);
466
0
        }
467
0
        Poly1305_Final(POLY1305_ctx(actx), ctx->encrypt ? actx->tag : temp);
468
0
        actx->mac_inited = 0;
469
470
0
        if (in != NULL && len != plen) { /* tls mode */
471
0
            if (ctx->encrypt) {
472
0
                memcpy(out, actx->tag, POLY1305_BLOCK_SIZE);
473
0
            } else {
474
0
                if (CRYPTO_memcmp(temp, in, POLY1305_BLOCK_SIZE)) {
475
0
                    memset(out - plen, 0, plen);
476
0
                    return -1;
477
0
                }
478
0
            }
479
0
        } else if (!ctx->encrypt) {
480
0
            if (CRYPTO_memcmp(temp, actx->tag, actx->tag_len))
481
0
                return -1;
482
0
        }
483
0
    }
484
0
    return len;
485
0
}
486
487
static int chacha20_poly1305_cleanup(EVP_CIPHER_CTX *ctx)
488
0
{
489
0
    EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
490
0
    if (actx)
491
0
        OPENSSL_cleanse(ctx->cipher_data, sizeof(*actx) + Poly1305_ctx_size());
492
0
    return 1;
493
0
}
494
495
static int chacha20_poly1305_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
496
    void *ptr)
497
0
{
498
0
    EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
499
500
0
    switch (type) {
501
0
    case EVP_CTRL_INIT:
502
0
        if (actx == NULL)
503
0
            actx = ctx->cipher_data
504
0
                = OPENSSL_zalloc(sizeof(*actx) + Poly1305_ctx_size());
505
0
        if (actx == NULL) {
506
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
507
0
            return 0;
508
0
        }
509
0
        actx->len.aad = 0;
510
0
        actx->len.text = 0;
511
0
        actx->aad = 0;
512
0
        actx->mac_inited = 0;
513
0
        actx->tag_len = 0;
514
0
        actx->nonce_len = 12;
515
0
        actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
516
0
        memset(actx->tls_aad, 0, POLY1305_BLOCK_SIZE);
517
0
        return 1;
518
519
0
    case EVP_CTRL_COPY:
520
0
        if (actx) {
521
0
            EVP_CIPHER_CTX *dst = (EVP_CIPHER_CTX *)ptr;
522
523
0
            dst->cipher_data = OPENSSL_memdup(actx, sizeof(*actx) + Poly1305_ctx_size());
524
0
            if (dst->cipher_data == NULL) {
525
0
                ERR_raise(ERR_LIB_EVP, EVP_R_COPY_ERROR);
526
0
                return 0;
527
0
            }
528
0
        }
529
0
        return 1;
530
531
0
    case EVP_CTRL_GET_IVLEN:
532
0
        *(int *)ptr = actx->nonce_len;
533
0
        return 1;
534
535
0
    case EVP_CTRL_AEAD_SET_IVLEN:
536
0
        if (arg <= 0 || arg > CHACHA20_POLY1305_MAX_IVLEN)
537
0
            return 0;
538
0
        actx->nonce_len = arg;
539
0
        return 1;
540
541
0
    case EVP_CTRL_AEAD_SET_IV_FIXED:
542
0
        if (arg != 12)
543
0
            return 0;
544
0
        actx->nonce[0] = actx->key.counter[1]
545
0
            = CHACHA_U8TOU32((unsigned char *)ptr);
546
0
        actx->nonce[1] = actx->key.counter[2]
547
0
            = CHACHA_U8TOU32((unsigned char *)ptr + 4);
548
0
        actx->nonce[2] = actx->key.counter[3]
549
0
            = CHACHA_U8TOU32((unsigned char *)ptr + 8);
550
0
        return 1;
551
552
0
    case EVP_CTRL_AEAD_SET_TAG:
553
0
        if (arg <= 0 || arg > POLY1305_BLOCK_SIZE)
554
0
            return 0;
555
0
        if (ptr != NULL) {
556
0
            memcpy(actx->tag, ptr, arg);
557
0
            actx->tag_len = arg;
558
0
        }
559
0
        return 1;
560
561
0
    case EVP_CTRL_AEAD_GET_TAG:
562
0
        if (arg <= 0 || arg > POLY1305_BLOCK_SIZE || !ctx->encrypt)
563
0
            return 0;
564
0
        memcpy(ptr, actx->tag, arg);
565
0
        return 1;
566
567
0
    case EVP_CTRL_AEAD_TLS1_AAD:
568
0
        if (arg != EVP_AEAD_TLS1_AAD_LEN)
569
0
            return 0;
570
0
        {
571
0
            unsigned int len;
572
0
            unsigned char *aad = ptr;
573
574
0
            memcpy(actx->tls_aad, ptr, EVP_AEAD_TLS1_AAD_LEN);
575
0
            len = aad[EVP_AEAD_TLS1_AAD_LEN - 2] << 8 | aad[EVP_AEAD_TLS1_AAD_LEN - 1];
576
0
            aad = actx->tls_aad;
577
0
            if (!ctx->encrypt) {
578
0
                if (len < POLY1305_BLOCK_SIZE)
579
0
                    return 0;
580
0
                len -= POLY1305_BLOCK_SIZE; /* discount attached tag */
581
0
                aad[EVP_AEAD_TLS1_AAD_LEN - 2] = (unsigned char)(len >> 8);
582
0
                aad[EVP_AEAD_TLS1_AAD_LEN - 1] = (unsigned char)len;
583
0
            }
584
0
            actx->tls_payload_length = len;
585
586
            /*
587
             * merge record sequence number as per RFC7905
588
             */
589
0
            actx->key.counter[1] = actx->nonce[0];
590
0
            actx->key.counter[2] = actx->nonce[1] ^ CHACHA_U8TOU32(aad);
591
0
            actx->key.counter[3] = actx->nonce[2] ^ CHACHA_U8TOU32(aad + 4);
592
0
            actx->mac_inited = 0;
593
594
0
            return POLY1305_BLOCK_SIZE; /* tag length */
595
0
        }
596
597
0
    case EVP_CTRL_AEAD_SET_MAC_KEY:
598
        /* no-op */
599
0
        return 1;
600
601
0
    default:
602
0
        return -1;
603
0
    }
604
0
}
605
606
static EVP_CIPHER chacha20_poly1305 = {
607
    NID_chacha20_poly1305,
608
    1, /* block_size */
609
    CHACHA_KEY_SIZE, /* key_len */
610
    12, /* iv_len, 96-bit nonce in the context */
611
    EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_CUSTOM_IV | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_CUSTOM_IV_LENGTH,
612
    EVP_ORIG_GLOBAL,
613
    chacha20_poly1305_init_key,
614
    chacha20_poly1305_cipher,
615
    chacha20_poly1305_cleanup,
616
    0, /* 0 moves context-specific structure allocation to ctrl */
617
    NULL, /* set_asn1_parameters */
618
    NULL, /* get_asn1_parameters */
619
    chacha20_poly1305_ctrl,
620
    NULL /* app_data */
621
};
622
623
const EVP_CIPHER *EVP_chacha20_poly1305(void)
624
165
{
625
165
    return (&chacha20_poly1305);
626
165
}
627
#endif
628
#endif