Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/providers/implementations/ciphers/cipher_chacha20_hw.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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
/* chacha20 cipher implementation */
11
12
#include "cipher_chacha20.h"
13
14
static int chacha20_initkey(PROV_CIPHER_CTX *bctx, const uint8_t *key,
15
    size_t keylen)
16
11.5k
{
17
11.5k
    PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)bctx;
18
11.5k
    unsigned int i;
19
20
11.5k
    if (key != NULL) {
21
103k
        for (i = 0; i < CHACHA_KEY_SIZE; i += 4)
22
92.1k
            ctx->key.d[i / 4] = CHACHA_U8TOU32(key + i);
23
11.5k
    }
24
11.5k
    ctx->partial_len = 0;
25
11.5k
    return 1;
26
11.5k
}
27
28
static int chacha20_initiv(PROV_CIPHER_CTX *bctx)
29
264k
{
30
264k
    PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)bctx;
31
264k
    unsigned int i;
32
33
264k
    if (bctx->iv_set) {
34
1.32M
        for (i = 0; i < CHACHA_CTR_SIZE; i += 4)
35
1.05M
            ctx->counter[i / 4] = CHACHA_U8TOU32(bctx->oiv + i);
36
264k
    }
37
264k
    ctx->partial_len = 0;
38
264k
    return 1;
39
264k
}
40
41
static int chacha20_cipher(PROV_CIPHER_CTX *bctx, unsigned char *out,
42
    const unsigned char *in, size_t inl)
43
132k
{
44
132k
    PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)bctx;
45
132k
    unsigned int n, rem, ctr32;
46
47
132k
    n = ctx->partial_len;
48
132k
    if (n > 0) {
49
2.87k
        while (inl > 0 && n < CHACHA_BLK_SIZE) {
50
2.52k
            *out++ = *in++ ^ ctx->buf[n++];
51
2.52k
            inl--;
52
2.52k
        }
53
348
        ctx->partial_len = n;
54
55
348
        if (inl == 0)
56
346
            return 1;
57
58
2
        if (n == CHACHA_BLK_SIZE) {
59
2
            ctx->partial_len = 0;
60
2
            ctx->counter[0]++;
61
2
            if (ctx->counter[0] == 0)
62
0
                ctx->counter[1]++;
63
2
        }
64
2
    }
65
66
131k
    rem = (unsigned int)(inl % CHACHA_BLK_SIZE);
67
131k
    inl -= rem;
68
131k
    ctr32 = ctx->counter[0];
69
137k
    while (inl >= CHACHA_BLK_SIZE) {
70
5.57k
        size_t blocks = inl / CHACHA_BLK_SIZE;
71
72
        /*
73
         * 1<<28 is just a not-so-small yet not-so-large number...
74
         * Below condition is practically never met, but it has to
75
         * be checked for code correctness.
76
         */
77
5.57k
        if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))
78
0
            blocks = (1U << 28);
79
80
        /*
81
         * As ChaCha20_ctr32 operates on 32-bit counter, caller
82
         * has to handle overflow. 'if' below detects the
83
         * overflow, which is then handled by limiting the
84
         * amount of blocks to the exact overflow point...
85
         */
86
5.57k
        ctr32 += (unsigned int)blocks;
87
5.57k
        if (ctr32 < blocks) {
88
0
            blocks -= ctr32;
89
0
            ctr32 = 0;
90
0
        }
91
5.57k
        blocks *= CHACHA_BLK_SIZE;
92
5.57k
        ChaCha20_ctr32(out, in, blocks, ctx->key.d, ctx->counter);
93
5.57k
        inl -= blocks;
94
5.57k
        in += blocks;
95
5.57k
        out += blocks;
96
97
5.57k
        ctx->counter[0] = ctr32;
98
5.57k
        if (ctr32 == 0)
99
0
            ctx->counter[1]++;
100
5.57k
    }
101
102
131k
    if (rem > 0) {
103
129k
        memset(ctx->buf, 0, sizeof(ctx->buf));
104
129k
        ChaCha20_ctr32(ctx->buf, ctx->buf, CHACHA_BLK_SIZE,
105
129k
            ctx->key.d, ctx->counter);
106
720k
        for (n = 0; n < rem; n++)
107
590k
            out[n] = in[n] ^ ctx->buf[n];
108
129k
        ctx->partial_len = rem;
109
129k
    }
110
111
131k
    return 1;
112
132k
}
113
114
static const PROV_CIPHER_HW_CHACHA20 chacha20_hw = {
115
    { chacha20_initkey, chacha20_cipher },
116
    chacha20_initiv
117
};
118
119
const PROV_CIPHER_HW *ossl_prov_cipher_hw_chacha20(size_t keybits)
120
11.5k
{
121
11.5k
    return (PROV_CIPHER_HW *)&chacha20_hw;
122
11.5k
}