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