/src/boringssl/crypto/fipsmodule/cipher/cipher.cc.inc
Line | Count | Source |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/cipher.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <limits.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include <openssl/err.h> |
22 | | #include <openssl/mem.h> |
23 | | #include <openssl/nid.h> |
24 | | #include <openssl/span.h> |
25 | | |
26 | | #include "../../internal.h" |
27 | | #include "../../mem_internal.h" |
28 | | #include "../service_indicator/internal.h" |
29 | | #include "internal.h" |
30 | | |
31 | | |
32 | | using namespace bssl; |
33 | | |
34 | 28.8k | void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) { |
35 | 28.8k | OPENSSL_memset(ctx, 0, sizeof(EVP_CIPHER_CTX)); |
36 | 28.8k | } |
37 | | |
38 | 0 | EVP_CIPHER_CTX *EVP_CIPHER_CTX_new() { |
39 | 0 | EVP_CIPHER_CTX *ctx = New<EVP_CIPHER_CTX>(); |
40 | 0 | if (ctx) { |
41 | 0 | EVP_CIPHER_CTX_init(ctx); |
42 | 0 | } |
43 | 0 | return ctx; |
44 | 0 | } |
45 | | |
46 | 28.8k | int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) { |
47 | 28.8k | if (c->cipher != nullptr && c->cipher->cleanup) { |
48 | 0 | c->cipher->cleanup(c); |
49 | 0 | } |
50 | 28.8k | OPENSSL_free(c->cipher_data); |
51 | | |
52 | 28.8k | OPENSSL_memset(c, 0, sizeof(EVP_CIPHER_CTX)); |
53 | 28.8k | return 1; |
54 | 28.8k | } |
55 | | |
56 | 0 | void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) { |
57 | 0 | if (ctx) { |
58 | 0 | EVP_CIPHER_CTX_cleanup(ctx); |
59 | 0 | Delete(ctx); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) { |
64 | 0 | if (in == nullptr || in->cipher == nullptr) { |
65 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INPUT_NOT_INITIALIZED); |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |
69 | 0 | EVP_CIPHER_CTX_cleanup(out); |
70 | 0 | OPENSSL_memcpy(out, in, sizeof(EVP_CIPHER_CTX)); |
71 | |
|
72 | 0 | if (in->cipher_data && in->cipher->ctx_size) { |
73 | 0 | out->cipher_data = OPENSSL_memdup(in->cipher_data, in->cipher->ctx_size); |
74 | 0 | if (!out->cipher_data) { |
75 | 0 | out->cipher = nullptr; |
76 | 0 | return 0; |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | 0 | if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) { |
81 | 0 | if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) { |
82 | 0 | out->cipher = nullptr; |
83 | 0 | return 0; |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | return 1; |
88 | 0 | } |
89 | | |
90 | 0 | int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) { |
91 | 0 | EVP_CIPHER_CTX_cleanup(ctx); |
92 | 0 | EVP_CIPHER_CTX_init(ctx); |
93 | 0 | return 1; |
94 | 0 | } |
95 | | |
96 | | int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
97 | | ENGINE *engine, const uint8_t *key, const uint8_t *iv, |
98 | 30.3k | int enc) { |
99 | 30.3k | if (enc == -1) { |
100 | 0 | enc = ctx->encrypt; |
101 | 30.3k | } else { |
102 | 30.3k | if (enc) { |
103 | 16.4k | enc = 1; |
104 | 16.4k | } |
105 | 30.3k | ctx->encrypt = enc; |
106 | 30.3k | } |
107 | | |
108 | 30.3k | if (cipher) { |
109 | | // Ensure a context left from last time is cleared (the previous check |
110 | | // attempted to avoid this if the same ENGINE and EVP_CIPHER could be |
111 | | // used). |
112 | 28.7k | if (ctx->cipher) { |
113 | 0 | EVP_CIPHER_CTX_cleanup(ctx); |
114 | | // Restore encrypt and flags |
115 | 0 | ctx->encrypt = enc; |
116 | 0 | } |
117 | | |
118 | 28.7k | ctx->cipher = cipher; |
119 | 28.7k | if (ctx->cipher->ctx_size) { |
120 | 28.7k | ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size); |
121 | 28.7k | if (!ctx->cipher_data) { |
122 | 0 | ctx->cipher = nullptr; |
123 | 0 | return 0; |
124 | 0 | } |
125 | 28.7k | } else { |
126 | 0 | ctx->cipher_data = nullptr; |
127 | 0 | } |
128 | | |
129 | 28.7k | ctx->key_len = cipher->key_len; |
130 | 28.7k | ctx->flags = 0; |
131 | | |
132 | 28.7k | if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { |
133 | 7 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, nullptr)) { |
134 | 0 | ctx->cipher = nullptr; |
135 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INITIALIZATION_ERROR); |
136 | 0 | return 0; |
137 | 0 | } |
138 | 7 | } |
139 | 28.7k | } else if (!ctx->cipher) { |
140 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET); |
141 | 0 | return 0; |
142 | 0 | } |
143 | | |
144 | | // we assume block size is a power of 2 in *cryptUpdate |
145 | 30.3k | assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 || |
146 | 30.3k | ctx->cipher->block_size == 16); |
147 | | |
148 | 30.3k | if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) { |
149 | 30.3k | switch (EVP_CIPHER_CTX_mode(ctx)) { |
150 | 0 | case EVP_CIPH_STREAM_CIPHER: |
151 | 0 | case EVP_CIPH_ECB_MODE: |
152 | 0 | break; |
153 | | |
154 | 0 | case EVP_CIPH_CFB_MODE: |
155 | 0 | ctx->num = 0; |
156 | 0 | [[fallthrough]]; |
157 | |
|
158 | 30.3k | case EVP_CIPH_CBC_MODE: |
159 | 30.3k | assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv)); |
160 | 30.3k | if (iv) { |
161 | 6.54k | OPENSSL_memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
162 | 6.54k | } |
163 | 30.3k | OPENSSL_memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); |
164 | 30.3k | break; |
165 | | |
166 | 0 | case EVP_CIPH_CTR_MODE: |
167 | 0 | case EVP_CIPH_OFB_MODE: |
168 | 0 | ctx->num = 0; |
169 | | // Don't reuse IV for CTR mode |
170 | 0 | if (iv) { |
171 | 0 | OPENSSL_memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
172 | 0 | } |
173 | 0 | break; |
174 | | |
175 | 0 | default: |
176 | 0 | return 0; |
177 | 30.3k | } |
178 | 30.3k | } |
179 | | |
180 | 30.3k | if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { |
181 | 28.7k | if (!ctx->cipher->init(ctx, key, iv, enc)) { |
182 | 0 | return 0; |
183 | 0 | } |
184 | 28.7k | } |
185 | | |
186 | 30.3k | ctx->buf_len = 0; |
187 | 30.3k | ctx->final_used = 0; |
188 | 30.3k | return 1; |
189 | 30.3k | } |
190 | | |
191 | | int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
192 | 2.36k | ENGINE *impl, const uint8_t *key, const uint8_t *iv) { |
193 | 2.36k | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); |
194 | 2.36k | } |
195 | | |
196 | | int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
197 | 794 | ENGINE *impl, const uint8_t *key, const uint8_t *iv) { |
198 | 794 | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); |
199 | 794 | } |
200 | | |
201 | | // block_remainder returns the number of bytes to remove from `len` to get a |
202 | | // multiple of `ctx`'s block size. |
203 | 9.29k | static size_t block_remainder(const EVP_CIPHER_CTX *ctx, size_t len) { |
204 | | // `block_size` must be a power of two. |
205 | 9.29k | assert(ctx->cipher->block_size != 0); |
206 | 9.29k | assert((ctx->cipher->block_size & (ctx->cipher->block_size - 1)) == 0); |
207 | 9.29k | return len & (ctx->cipher->block_size - 1); |
208 | 9.29k | } |
209 | | |
210 | | static size_t evp_cipher_ctx_max_encrypt_update(const EVP_CIPHER_CTX *ctx, |
211 | 9.28k | size_t in_len) { |
212 | 9.28k | if (in_len == 0) { |
213 | 0 | return 0; |
214 | 0 | } |
215 | | |
216 | 9.28k | size_t block_size = ctx->cipher->block_size; |
217 | | // `block_size` must be a power of 2. |
218 | 9.28k | assert(block_size != 0 && (block_size & (block_size - 1)) == 0); |
219 | 9.28k | size_t buf_len = ctx->buf_len; |
220 | | |
221 | | // Any buffered input is combined with `in_len`, then we round down to a |
222 | | // multiple of the block size. |
223 | 9.28k | return (in_len + buf_len) & ~(block_size - 1); |
224 | 9.28k | } |
225 | | |
226 | | size_t EVP_CIPHER_CTX_max_next_update(const EVP_CIPHER_CTX *ctx, |
227 | 16 | size_t in_len) { |
228 | 16 | if (in_len == 0) { |
229 | 0 | return 0; |
230 | 0 | } |
231 | | |
232 | 16 | size_t ret = evp_cipher_ctx_max_encrypt_update(ctx, in_len); |
233 | 16 | size_t block_size = ctx->cipher->block_size; |
234 | 16 | size_t buf_len = ctx->buf_len; |
235 | 16 | if (!ctx->encrypt && block_size > 1 && !(ctx->flags & EVP_CIPH_NO_PADDING)) { |
236 | 16 | if (ctx->final_used) { |
237 | | // There was a buffered decrypted block. Now that it is known to not have |
238 | | // padding, DecryptUpdate will output it. |
239 | 0 | ret += block_size; |
240 | 0 | } |
241 | 16 | if (block_remainder(ctx, in_len + buf_len) == 0) { |
242 | | // This call ends on a block boundary. The last block will be buffered in |
243 | | // `ctx->final` until it is known to have padding. |
244 | 16 | assert(ret >= block_size); |
245 | 16 | ret -= block_size; |
246 | 16 | } |
247 | 16 | } |
248 | 16 | return ret; |
249 | 16 | } |
250 | | |
251 | | int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
252 | 0 | const uint8_t *in, int in_len) { |
253 | 0 | *out_len = 0; |
254 | 0 | if (in_len < 0) { |
255 | 0 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW); |
256 | 0 | return 0; |
257 | 0 | } |
258 | 0 | size_t in_len_sz = static_cast<size_t>(in_len); |
259 | 0 | size_t out_len_sz; |
260 | 0 | if ((ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) && out == nullptr) { |
261 | 0 | if (!EVP_CipherUpdateAAD(ctx, in, in_len_sz)) { |
262 | 0 | return 0; |
263 | 0 | } |
264 | 0 | out_len_sz = in_len_sz; // Even though no output was written! |
265 | 0 | } else { |
266 | | // in_len_sz is < INT_MAX which is no more than half of SIZE_MAX. |
267 | 0 | size_t max_out_len = |
268 | 0 | std::min(in_len_sz + ctx->cipher->block_size - 1, size_t{INT_MAX}); |
269 | 0 | if (!EVP_EncryptUpdate_ex(ctx, out, &out_len_sz, max_out_len, in, |
270 | 0 | in_len_sz)) { |
271 | 0 | return 0; |
272 | 0 | } |
273 | 0 | } |
274 | 0 | *out_len = static_cast<int>(out_len_sz); |
275 | 0 | return 1; |
276 | 0 | } |
277 | | |
278 | | int EVP_EncryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, |
279 | 9.26k | size_t max_out_len, const uint8_t *in, size_t in_len) { |
280 | 9.26k | *out_len = 0; |
281 | | |
282 | | // `evp_cipher_ctx_max_encrypt_update` is currently exact. Check bounds before |
283 | | // performing any operations so that, if the output is too small, retrying the |
284 | | // operation with a larger buffer is well-defined. This only matters when it |
285 | | // takes two steps to process `in`. |
286 | 9.26k | size_t expected = evp_cipher_ctx_max_encrypt_update(ctx, in_len); |
287 | 9.26k | if (max_out_len < expected) { |
288 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
289 | 0 | return 0; |
290 | 0 | } |
291 | | |
292 | | // Ciphers that use blocks may write up to `block_size` extra bytes. Ensure |
293 | | // the output does not overflow `*out_len`. |
294 | 9.26k | Span<const uint8_t> in_span(in, in_len); |
295 | 9.26k | size_t block_size = ctx->cipher->block_size; |
296 | | |
297 | 9.26k | if (in_span.empty()) { |
298 | 0 | return 1; |
299 | 0 | } |
300 | | |
301 | 9.26k | size_t buf_len = ctx->buf_len; |
302 | 9.26k | assert(block_size <= sizeof(ctx->buf)); |
303 | 9.26k | Span<uint8_t> out_span(out, max_out_len); |
304 | 9.26k | if (buf_len != 0) { |
305 | 1.82k | if (block_size - buf_len > in_span.size()) { |
306 | 0 | CopyToPrefix(in_span, Span(ctx->buf).subspan(buf_len)); |
307 | 0 | ctx->buf_len += in_span.size(); |
308 | 0 | return 1; |
309 | 1.82k | } else { |
310 | 1.82k | size_t j = block_size - buf_len; |
311 | 1.82k | CopyToPrefix(in_span.first(j), Span(ctx->buf).subspan(buf_len)); |
312 | 1.82k | if (out_span.size() < block_size) { |
313 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
314 | 0 | return 0; |
315 | 0 | } |
316 | 1.82k | if (!ctx->cipher->cipher_update(ctx, out_span.data(), ctx->buf, |
317 | 1.82k | block_size)) { |
318 | 0 | return 0; |
319 | 0 | } |
320 | 1.82k | in_span = in_span.subspan(j); |
321 | 1.82k | out_span = out_span.subspan(block_size); |
322 | 1.82k | } |
323 | 1.82k | } |
324 | | |
325 | 9.26k | size_t whole_blocks = in_span.size() - block_remainder(ctx, in_span.size()); |
326 | 9.26k | if (whole_blocks > 0) { |
327 | 6.74k | if (out_span.size() < whole_blocks) { |
328 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
329 | 0 | return 0; |
330 | 0 | } |
331 | 6.74k | if (!ctx->cipher->cipher_update(ctx, out_span.data(), in_span.data(), |
332 | 6.74k | whole_blocks)) { |
333 | 0 | return 0; |
334 | 0 | } |
335 | 6.74k | in_span = in_span.subspan(whole_blocks); |
336 | 6.74k | out_span = out_span.subspan(whole_blocks); |
337 | 6.74k | } |
338 | | |
339 | 9.26k | assert(in_span.size() < block_size); |
340 | 9.26k | CopyToPrefix(in_span, ctx->buf); |
341 | 9.26k | ctx->buf_len = in_span.size(); |
342 | | |
343 | 9.26k | *out_len = max_out_len - out_span.size(); |
344 | 9.26k | assert(*out_len == expected); // See `EVP_CIPHER_CTX_max_next_update` check. |
345 | 9.26k | return 1; |
346 | 9.26k | } |
347 | | |
348 | 0 | int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
349 | 0 | size_t out_len_sz; |
350 | 0 | int ret = |
351 | 0 | EVP_EncryptFinal_ex2(ctx, out, &out_len_sz, ctx->cipher->block_size); |
352 | 0 | static_assert(EVP_MAX_BLOCK_LENGTH <= INT_MAX); |
353 | 0 | *out_len = static_cast<int>(out_len_sz); |
354 | 0 | return ret; |
355 | 0 | } |
356 | | |
357 | | int EVP_EncryptFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, |
358 | 1.50k | size_t max_out_len) { |
359 | 1.50k | *out_len = 0; |
360 | | |
361 | 1.50k | size_t block_size = ctx->cipher->block_size; |
362 | 1.50k | assert(block_size <= sizeof(ctx->buf)); |
363 | 1.50k | if (block_size == 1) { |
364 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
365 | 0 | return ctx->cipher->cipher_final(ctx); |
366 | 0 | } |
367 | 0 | EVP_Cipher_verify_service_indicator(ctx); |
368 | 0 | return 1; |
369 | 0 | } |
370 | | |
371 | 1.50k | size_t buf_len = ctx->buf_len; |
372 | 1.50k | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
373 | 1.38k | if (buf_len) { |
374 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
375 | 0 | return 0; |
376 | 0 | } |
377 | 1.38k | EVP_Cipher_verify_service_indicator(ctx); |
378 | 1.38k | return 1; |
379 | 1.38k | } |
380 | | |
381 | 114 | Span<uint8_t> out_span(out, max_out_len); |
382 | 114 | if (out_span.size() < block_size) { |
383 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
384 | 0 | return 0; |
385 | 0 | } |
386 | | |
387 | 114 | size_t padding = block_size - buf_len; |
388 | 882 | for (size_t i = buf_len; i < block_size; i++) { |
389 | 768 | ctx->buf[i] = padding; |
390 | 768 | } |
391 | 114 | if (!ctx->cipher->cipher_update(ctx, out_span.data(), ctx->buf, block_size)) { |
392 | 0 | return 0; |
393 | 0 | } |
394 | 114 | out_span = out_span.subspan(block_size); |
395 | | |
396 | 114 | *out_len = max_out_len - out_span.size(); |
397 | 114 | EVP_Cipher_verify_service_indicator(ctx); |
398 | 114 | return 1; |
399 | 114 | } |
400 | | |
401 | | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
402 | 0 | const uint8_t *in, int in_len) { |
403 | 0 | *out_len = 0; |
404 | 0 | if (in_len < 0) { |
405 | 0 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW); |
406 | 0 | return 0; |
407 | 0 | } |
408 | 0 | size_t in_len_sz = static_cast<size_t>(in_len); |
409 | 0 | size_t out_len_sz; |
410 | 0 | if ((ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) && out == nullptr) { |
411 | 0 | if (!EVP_CipherUpdateAAD(ctx, in, in_len_sz)) { |
412 | 0 | return 0; |
413 | 0 | } |
414 | 0 | out_len_sz = in_len_sz; |
415 | 0 | } else { |
416 | | // in_len_sz is < INT_MAX which is no more than half of SIZE_MAX. |
417 | 0 | size_t max_out_len = |
418 | 0 | std::min(in_len_sz + ctx->cipher->block_size - 1, size_t{INT_MAX}); |
419 | 0 | if (!EVP_DecryptUpdate_ex(ctx, out, &out_len_sz, max_out_len, in, |
420 | 0 | in_len_sz)) { |
421 | 0 | return 0; |
422 | 0 | } |
423 | 0 | } |
424 | 0 | *out_len = static_cast<int>(out_len_sz); |
425 | 0 | return 1; |
426 | 0 | } |
427 | | |
428 | | int EVP_DecryptUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, |
429 | 4.50k | size_t max_out_len, const uint8_t *in, size_t in_len) { |
430 | 4.50k | *out_len = 0; |
431 | | |
432 | | // Ciphers that use blocks may write up to `block_size` extra bytes. Ensure |
433 | | // the output does not overflow `*out_len`. |
434 | 4.50k | Span<const uint8_t> in_span(in, in_len); |
435 | 4.50k | size_t block_size = ctx->cipher->block_size; |
436 | 4.50k | if (in_span.empty()) { |
437 | 0 | return 1; |
438 | 0 | } |
439 | | |
440 | 4.50k | Span<uint8_t> out_span(out, max_out_len); |
441 | 4.50k | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
442 | | // Use the shared block handling logic from encryption. |
443 | 4.49k | return EVP_EncryptUpdate_ex(ctx, out_span.data(), out_len, out_span.size(), |
444 | 4.49k | in_span.data(), in_span.size()); |
445 | 4.49k | } |
446 | | |
447 | | // `EVP_CIPHER_CTX_max_next_update` is currently exact. Check bounds before |
448 | | // performing any operations so that, if the output is too small, retrying the |
449 | | // operation with a larger buffer is well-defined. This only matters when it |
450 | | // takes two steps to process `in`. |
451 | 16 | size_t expected = EVP_CIPHER_CTX_max_next_update(ctx, in_len); |
452 | 16 | if (max_out_len < expected) { |
453 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
454 | 0 | return 0; |
455 | 0 | } |
456 | | |
457 | 16 | assert(block_size <= sizeof(ctx->final)); |
458 | 16 | if (ctx->final_used) { |
459 | 0 | if (out_span.size() < block_size) { |
460 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
461 | 0 | return 0; |
462 | 0 | } |
463 | 0 | CopyToPrefix(Span(ctx->final).first(block_size), out_span); |
464 | 0 | ctx->final_used = 0; |
465 | 0 | out_span = out_span.subspan(block_size); |
466 | 0 | } |
467 | | |
468 | | // Use the shared block handling logic from encryption. |
469 | 16 | if (block_size > 1 && |
470 | 16 | block_remainder(ctx, ctx->buf_len + in_span.size()) == 0) { |
471 | | // Decryption would end on a block boundary. In this case, although we |
472 | | // can decrypt up to the block boundary, we cannot output the final |
473 | | // plaintext block yet. It may be the final block, with padding to |
474 | | // remove. |
475 | | // |
476 | | // Instead, output all but the final block's decryption, then decrypt the |
477 | | // final block into ctx->final, to be processed later. |
478 | | |
479 | | // NOTE: Not _really_ necessary, but let's try aligning the second |
480 | | // EVP_EncryptUpdate_ex call to a block boundary to mess with the buffer |
481 | | // less. |
482 | 16 | size_t head = in_span.size() > block_size ? in_span.size() - block_size : 0; |
483 | 16 | size_t head_out_len; |
484 | 16 | if (!EVP_EncryptUpdate_ex(ctx, out_span.data(), &head_out_len, |
485 | 16 | out_span.size(), in_span.data(), head)) { |
486 | 0 | return 0; |
487 | 0 | } |
488 | 16 | in_span = in_span.subspan(head); |
489 | 16 | out_span = out_span.subspan(head_out_len); |
490 | 16 | size_t final_size; |
491 | 16 | if (!EVP_EncryptUpdate_ex(ctx, ctx->final, &final_size, sizeof(ctx->final), |
492 | 16 | in_span.data(), in_span.size())) { |
493 | 0 | return 0; |
494 | 0 | } |
495 | 16 | ctx->final_used = 1; |
496 | 16 | assert(final_size == block_size); |
497 | 16 | assert(ctx->buf_len == 0); |
498 | 16 | } else { |
499 | | // Buffer will be non-empty. |
500 | 0 | size_t written_out_len; |
501 | 0 | if (!EVP_EncryptUpdate_ex(ctx, out_span.data(), &written_out_len, |
502 | 0 | out_span.size(), in_span.data(), |
503 | 0 | in_span.size())) { |
504 | 0 | return 0; |
505 | 0 | } |
506 | 0 | assert(block_size == 1 || ctx->buf_len != 0); |
507 | 0 | out_span = out_span.subspan(written_out_len); |
508 | 0 | } |
509 | | |
510 | 16 | *out_len = max_out_len - out_span.size(); |
511 | 16 | assert(*out_len == expected); // See `EVP_CIPHER_CTX_max_next_update` check. |
512 | 16 | return 1; |
513 | 16 | } |
514 | | |
515 | 0 | int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
516 | 0 | size_t out_len_sz; |
517 | 0 | int ret = |
518 | 0 | EVP_DecryptFinal_ex2(ctx, out, &out_len_sz, ctx->cipher->block_size); |
519 | 0 | static_assert(EVP_MAX_BLOCK_LENGTH <= INT_MAX); |
520 | 0 | *out_len = static_cast<int>(out_len_sz); |
521 | 0 | return ret; |
522 | 0 | } |
523 | | |
524 | | int EVP_DecryptFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, |
525 | 16 | size_t max_out_len) { |
526 | 16 | *out_len = 0; |
527 | | |
528 | 16 | size_t block_size = ctx->cipher->block_size; |
529 | 16 | assert(block_size <= sizeof(ctx->buf)); |
530 | 16 | if (block_size == 1) { |
531 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
532 | 0 | return ctx->cipher->cipher_final(ctx); |
533 | 0 | } |
534 | 0 | EVP_Cipher_verify_service_indicator(ctx); |
535 | 0 | return 1; |
536 | 0 | } |
537 | | |
538 | 16 | size_t buf_len = ctx->buf_len; |
539 | 16 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
540 | 0 | if (buf_len) { |
541 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
542 | 0 | return 0; |
543 | 0 | } |
544 | 0 | EVP_Cipher_verify_service_indicator(ctx); |
545 | 0 | return 1; |
546 | 0 | } |
547 | | |
548 | 16 | if (buf_len || !ctx->final_used) { |
549 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_WRONG_FINAL_BLOCK_LENGTH); |
550 | 0 | return 0; |
551 | 0 | } |
552 | 16 | assert(block_size <= sizeof(ctx->final)); |
553 | | |
554 | | // The following assumes that the ciphertext has been authenticated. Otherwise |
555 | | // it provides a padding oracle. |
556 | 16 | size_t padding = ctx->final[block_size - 1]; |
557 | 16 | if (padding == 0 || padding > block_size) { |
558 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
559 | 0 | return 0; |
560 | 0 | } |
561 | | |
562 | 125 | for (size_t i = block_size - padding; i < block_size; i++) { |
563 | 109 | if (ctx->final[i] != padding) { |
564 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
565 | 0 | return 0; |
566 | 0 | } |
567 | 109 | } |
568 | | |
569 | 16 | Span<uint8_t> out_span(out, max_out_len); |
570 | 16 | size_t payload = ctx->cipher->block_size - padding; |
571 | 16 | if (out_span.size() < payload) { |
572 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
573 | 0 | return 0; |
574 | 0 | } |
575 | 16 | CopyToPrefix(Span(ctx->final).first(payload), out_span); |
576 | 16 | out_span = out_span.subspan(payload); |
577 | | |
578 | 16 | *out_len = max_out_len - out_span.size(); |
579 | 16 | EVP_Cipher_verify_service_indicator(ctx); |
580 | 16 | return 1; |
581 | 16 | } |
582 | | |
583 | | int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
584 | 0 | size_t in_len) { |
585 | 0 | const int kError = |
586 | 0 | (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) ? -1 : 0; |
587 | |
|
588 | 0 | if ((ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) && |
589 | 0 | in_len > size_t{INT_MAX}) { |
590 | | // Can't represent the return value? That'd be bad. |
591 | 0 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW); |
592 | 0 | return kError; |
593 | 0 | } |
594 | | |
595 | 0 | size_t out_len; |
596 | 0 | if ((ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) && in == nullptr) { |
597 | 0 | if (!ctx->cipher->cipher_final(ctx)) { |
598 | 0 | return kError; |
599 | 0 | } |
600 | 0 | out_len = 0; |
601 | 0 | } else if ((ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) && |
602 | 0 | out == nullptr) { |
603 | 0 | if (!ctx->cipher->update_aad(ctx, in, in_len)) { |
604 | 0 | return kError; |
605 | 0 | } |
606 | 0 | out_len = in_len; // Yes, even though no output was written! |
607 | 0 | } else { |
608 | 0 | if (!ctx->cipher->cipher_update(ctx, out, in, in_len)) { |
609 | 0 | return kError; |
610 | 0 | } |
611 | 0 | out_len = in_len; |
612 | 0 | } |
613 | | |
614 | | // `EVP_CIPH_FLAG_CUSTOM_CIPHER` never sets the FIPS indicator via |
615 | | // `EVP_Cipher` because it's complicated whether the operation has completed |
616 | | // or not. E.g. AES-GCM with a non-NULL `in` argument hasn't completed an |
617 | | // operation. Callers should use the `EVP_AEAD` API or, at least, |
618 | | // `EVP_CipherUpdate` etc. |
619 | | // |
620 | | // This call can't be pushed into `EVP_Cipher_verify_service_indicator` |
621 | | // because whether `ret` indicates success or not depends on whether |
622 | | // `EVP_CIPH_FLAG_CUSTOM_CIPHER` is set. (This unreasonable, but matches |
623 | | // OpenSSL.) |
624 | 0 | if (!(ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)) { |
625 | 0 | EVP_Cipher_verify_service_indicator(ctx); |
626 | 0 | } |
627 | | |
628 | | // Custom ciphers return byte count; regular ciphers return boolean. |
629 | 0 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
630 | 0 | return static_cast<int>(out_len); |
631 | 0 | } |
632 | 0 | return 1; |
633 | 0 | } |
634 | | |
635 | | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
636 | 0 | const uint8_t *in, int in_len) { |
637 | 0 | if (ctx->encrypt) { |
638 | 0 | return EVP_EncryptUpdate(ctx, out, out_len, in, in_len); |
639 | 0 | } else { |
640 | 0 | return EVP_DecryptUpdate(ctx, out, out_len, in, in_len); |
641 | 0 | } |
642 | 0 | } |
643 | | |
644 | | int EVP_CipherUpdate_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, |
645 | 0 | size_t max_out_len, const uint8_t *in, size_t in_len) { |
646 | 0 | if (ctx->encrypt) { |
647 | 0 | return EVP_EncryptUpdate_ex(ctx, out, out_len, max_out_len, in, in_len); |
648 | 0 | } else { |
649 | 0 | return EVP_DecryptUpdate_ex(ctx, out, out_len, max_out_len, in, in_len); |
650 | 0 | } |
651 | 0 | } |
652 | | |
653 | 0 | int EVP_CipherUpdateAAD(EVP_CIPHER_CTX *ctx, const uint8_t *in, size_t in_len) { |
654 | 0 | if (!(ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)) { |
655 | 0 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
656 | 0 | return 0; |
657 | 0 | } |
658 | 0 | return ctx->cipher->update_aad(ctx, in, in_len); |
659 | 0 | } |
660 | | |
661 | 0 | int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
662 | 0 | if (ctx->encrypt) { |
663 | 0 | return EVP_EncryptFinal_ex(ctx, out, out_len); |
664 | 0 | } else { |
665 | 0 | return EVP_DecryptFinal_ex(ctx, out, out_len); |
666 | 0 | } |
667 | 0 | } |
668 | | |
669 | | int EVP_CipherFinal_ex2(EVP_CIPHER_CTX *ctx, uint8_t *out, size_t *out_len, |
670 | 0 | size_t max_out_len) { |
671 | 0 | if (ctx->encrypt) { |
672 | 0 | return EVP_EncryptFinal_ex2(ctx, out, out_len, max_out_len); |
673 | 0 | } else { |
674 | 0 | return EVP_DecryptFinal_ex2(ctx, out, out_len, max_out_len); |
675 | 0 | } |
676 | 0 | } |
677 | | |
678 | 0 | size_t EVP_CIPHER_CTX_max_final(const EVP_CIPHER_CTX *ctx) { |
679 | 0 | size_t block_size = ctx->cipher->block_size; |
680 | 0 | if (block_size == 1 || (ctx->flags & EVP_CIPH_NO_PADDING)) { |
681 | 0 | return 0; |
682 | 0 | } |
683 | 0 | if (ctx->encrypt) { |
684 | 0 | return block_size; |
685 | 0 | } else { |
686 | | // At least one byte will be removed when processing padding. |
687 | 0 | return block_size - 1; |
688 | 0 | } |
689 | 0 | } |
690 | | |
691 | 0 | const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) { |
692 | 0 | return ctx->cipher; |
693 | 0 | } |
694 | | |
695 | 0 | int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) { return ctx->cipher->nid; } |
696 | | |
697 | 0 | int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx) { |
698 | 0 | return ctx->encrypt; |
699 | 0 | } |
700 | | |
701 | 14.2k | unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) { |
702 | 14.2k | return ctx->cipher->block_size; |
703 | 14.2k | } |
704 | | |
705 | 14 | unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) { |
706 | 14 | return ctx->key_len; |
707 | 14 | } |
708 | | |
709 | 68.8k | unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) { |
710 | 68.8k | if (EVP_CIPHER_mode(ctx->cipher) == EVP_CIPH_GCM_MODE) { |
711 | 0 | int length; |
712 | 0 | int res = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN, 0, |
713 | 0 | &length); |
714 | | // EVP_CIPHER_CTX_ctrl returning an error should be impossible under this |
715 | | // circumstance. If it somehow did, fallback to the static cipher iv_len. |
716 | 0 | if (res == 1) { |
717 | 0 | return length; |
718 | 0 | } |
719 | 0 | } |
720 | 68.8k | return ctx->cipher->iv_len; |
721 | 68.8k | } |
722 | | |
723 | 0 | void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) { |
724 | 0 | return ctx->app_data; |
725 | 0 | } |
726 | | |
727 | 0 | void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) { |
728 | 0 | ctx->app_data = data; |
729 | 0 | } |
730 | | |
731 | 30.3k | uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) { |
732 | 30.3k | return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK; |
733 | 30.3k | } |
734 | | |
735 | 44.5k | uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) { |
736 | 44.5k | return ctx->cipher->flags & EVP_CIPH_MODE_MASK; |
737 | 44.5k | } |
738 | | |
739 | 7 | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) { |
740 | 7 | int ret; |
741 | 7 | if (!ctx->cipher) { |
742 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET); |
743 | 0 | return 0; |
744 | 0 | } |
745 | | |
746 | 7 | if (!ctx->cipher->ctrl) { |
747 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_NOT_IMPLEMENTED); |
748 | 0 | return 0; |
749 | 0 | } |
750 | | |
751 | 7 | ret = ctx->cipher->ctrl(ctx, command, arg, ptr); |
752 | 7 | if (ret == -1) { |
753 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED); |
754 | 0 | return 0; |
755 | 0 | } |
756 | | |
757 | 7 | return ret; |
758 | 7 | } |
759 | | |
760 | 27.2k | int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) { |
761 | 27.2k | if (pad) { |
762 | 0 | ctx->flags &= ~EVP_CIPH_NO_PADDING; |
763 | 27.2k | } else { |
764 | 27.2k | ctx->flags |= EVP_CIPH_NO_PADDING; |
765 | 27.2k | } |
766 | 27.2k | return 1; |
767 | 27.2k | } |
768 | | |
769 | 0 | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, unsigned key_len) { |
770 | 0 | if (c->key_len == key_len) { |
771 | 0 | return 1; |
772 | 0 | } |
773 | | |
774 | 0 | if (key_len == 0 || !(c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) { |
775 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_KEY_LENGTH); |
776 | 0 | return 0; |
777 | 0 | } |
778 | | |
779 | 0 | c->key_len = key_len; |
780 | 0 | return 1; |
781 | 0 | } |
782 | | |
783 | 0 | int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { return cipher->nid; } |
784 | | |
785 | 0 | unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher) { |
786 | 0 | return cipher->block_size; |
787 | 0 | } |
788 | | |
789 | 27.2k | unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher) { |
790 | 27.2k | return cipher->key_len; |
791 | 27.2k | } |
792 | | |
793 | 3.52k | unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) { |
794 | 3.52k | return cipher->iv_len; |
795 | 3.52k | } |
796 | | |
797 | 0 | uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher) { |
798 | 0 | return cipher->flags & ~EVP_CIPH_MODE_MASK; |
799 | 0 | } |
800 | | |
801 | 68.8k | uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) { |
802 | 68.8k | return cipher->flags & EVP_CIPH_MODE_MASK; |
803 | 68.8k | } |
804 | | |
805 | | int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
806 | 0 | const uint8_t *key, const uint8_t *iv, int enc) { |
807 | 0 | if (cipher) { |
808 | 0 | EVP_CIPHER_CTX_init(ctx); |
809 | 0 | } |
810 | 0 | return EVP_CipherInit_ex(ctx, cipher, nullptr, key, iv, enc); |
811 | 0 | } |
812 | | |
813 | | int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
814 | 0 | const uint8_t *key, const uint8_t *iv) { |
815 | 0 | return EVP_CipherInit(ctx, cipher, key, iv, 1); |
816 | 0 | } |
817 | | |
818 | | int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
819 | 0 | const uint8_t *key, const uint8_t *iv) { |
820 | 0 | return EVP_CipherInit(ctx, cipher, key, iv, 0); |
821 | 0 | } |
822 | | |
823 | 0 | int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
824 | 0 | return EVP_CipherFinal_ex(ctx, out, out_len); |
825 | 0 | } |
826 | | |
827 | 0 | int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
828 | 0 | return EVP_EncryptFinal_ex(ctx, out, out_len); |
829 | 0 | } |
830 | | |
831 | 0 | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
832 | 0 | return EVP_DecryptFinal_ex(ctx, out, out_len); |
833 | 0 | } |
834 | | |
835 | 0 | int EVP_add_cipher_alias(const char *a, const char *b) { return 1; } |
836 | | |
837 | 0 | void EVP_CIPHER_CTX_set_flags(const EVP_CIPHER_CTX *ctx, uint32_t flags) {} |