| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/cipher.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
#include <openssl/nid.h> |
| 13 |
#include <openssl/obj.h> |
| 14 |
|
| 15 |
#include "internal.h" |
| 16 |
#include "../../internal.h" |
| 17 |
|
| 18 |
|
| 19 |
void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) { |
| 20 |
OPENSSL_memset(ctx, 0, sizeof(EVP_CIPHER_CTX)); |
| 21 |
ctx->poisoned = 1; |
| 22 |
} |
| 23 |
|
| 24 |
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) { |
| 25 |
EVP_CIPHER_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX)); |
| 26 |
if (ctx) { |
| 27 |
ctx->poisoned = 1; |
| 28 |
// NO-OP: struct already zeroed |
| 29 |
// EVP_CIPHER_CTX_init(ctx); |
| 30 |
} |
| 31 |
return ctx; |
| 32 |
} |
| 33 |
|
| 34 |
int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) { |
| 35 |
GUARD_PTR(c); |
| 36 |
if (c->cipher != NULL && c->cipher->cleanup) { |
| 37 |
c->cipher->cleanup(c); |
| 38 |
} |
| 39 |
OPENSSL_free(c->cipher_data); |
| 40 |
|
| 41 |
OPENSSL_memset(c, 0, sizeof(EVP_CIPHER_CTX)); |
| 42 |
c->poisoned = 1; |
| 43 |
return 1; |
| 44 |
} |
| 45 |
|
| 46 |
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) { |
| 47 |
if (ctx) { |
| 48 |
EVP_CIPHER_CTX_cleanup(ctx); |
| 49 |
OPENSSL_free(ctx); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) { |
| 54 |
SET_DIT_AUTO_RESET; |
| 55 |
if (in == NULL || in->cipher == NULL) { |
| 56 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INPUT_NOT_INITIALIZED); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
if (in->poisoned) { |
| 61 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 62 |
return 0; |
| 63 |
} |
| 64 |
GUARD_PTR(out); |
| 65 |
|
| 66 |
EVP_CIPHER_CTX_cleanup(out); |
| 67 |
OPENSSL_memcpy(out, in, sizeof(EVP_CIPHER_CTX)); |
| 68 |
|
| 69 |
if (in->cipher_data && in->cipher->ctx_size) { |
| 70 |
out->cipher_data = OPENSSL_memdup(in->cipher_data, in->cipher->ctx_size); |
| 71 |
if (!out->cipher_data) { |
| 72 |
out->cipher = NULL; |
| 73 |
return 0; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) { |
| 78 |
if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) { |
| 79 |
out->cipher = NULL; |
| 80 |
return 0; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return 1; |
| 85 |
} |
| 86 |
|
| 87 |
int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) { |
| 88 |
EVP_CIPHER_CTX_cleanup(ctx); |
| 89 |
EVP_CIPHER_CTX_init(ctx); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 94 |
ENGINE *engine, const uint8_t *key, const uint8_t *iv, |
| 95 |
int enc) { |
| 96 |
SET_DIT_AUTO_RESET; |
| 97 |
GUARD_PTR(ctx); |
| 98 |
if (enc == -1) { |
| 99 |
enc = ctx->encrypt; |
| 100 |
} else { |
| 101 |
if (enc) { |
| 102 |
enc = 1; |
| 103 |
} |
| 104 |
ctx->encrypt = enc; |
| 105 |
} |
| 106 |
|
| 107 |
if (cipher) { |
| 108 |
// Ensure a context left from last time is cleared (the previous check |
| 109 |
// attempted to avoid this if the same ENGINE and EVP_CIPHER could be |
| 110 |
// used). |
| 111 |
if (ctx->cipher) { |
| 112 |
EVP_CIPHER_CTX_cleanup(ctx); |
| 113 |
// Restore encrypt and flags |
| 114 |
ctx->encrypt = enc; |
| 115 |
} |
| 116 |
|
| 117 |
ctx->cipher = cipher; |
| 118 |
if (ctx->cipher->ctx_size) { |
| 119 |
ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size); |
| 120 |
if (!ctx->cipher_data) { |
| 121 |
ctx->cipher = NULL; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
} else { |
| 125 |
ctx->cipher_data = NULL; |
| 126 |
} |
| 127 |
|
| 128 |
ctx->key_len = cipher->key_len; |
| 129 |
ctx->flags = 0; |
| 130 |
|
| 131 |
if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { |
| 132 |
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) { |
| 133 |
ctx->cipher = NULL; |
| 134 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INITIALIZATION_ERROR); |
| 135 |
return 0; |
| 136 |
} |
| 137 |
} |
| 138 |
} else if (!ctx->cipher) { |
| 139 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
// we assume block size is a power of 2 in *cryptUpdate |
| 144 |
assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 || |
| 145 |
ctx->cipher->block_size == 16); |
| 146 |
|
| 147 |
if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) { |
| 148 |
switch (EVP_CIPHER_CTX_mode(ctx)) { |
| 149 |
case EVP_CIPH_STREAM_CIPHER: |
| 150 |
case EVP_CIPH_ECB_MODE: |
| 151 |
break; |
| 152 |
|
| 153 |
case EVP_CIPH_CFB_MODE: |
| 154 |
ctx->num = 0; |
| 155 |
OPENSSL_FALLTHROUGH; |
| 156 |
|
| 157 |
case EVP_CIPH_CBC_MODE: |
| 158 |
assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv)); |
| 159 |
if (iv) { |
| 160 |
OPENSSL_memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
| 161 |
} |
| 162 |
OPENSSL_memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); |
| 163 |
break; |
| 164 |
|
| 165 |
case EVP_CIPH_CTR_MODE: |
| 166 |
case EVP_CIPH_OFB_MODE: |
| 167 |
ctx->num = 0; |
| 168 |
// Don't reuse IV for CTR mode |
| 169 |
if (iv) { |
| 170 |
OPENSSL_memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
| 171 |
} |
| 172 |
break; |
| 173 |
|
| 174 |
default: |
| 175 |
return 0; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { |
| 180 |
if (!ctx->cipher->init(ctx, key, iv, enc)) { |
| 181 |
return 0; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
ctx->buf_len = 0; |
| 186 |
ctx->final_used = 0; |
| 187 |
// Clear the poisoned flag to permit re-use of a CTX that previously had a |
| 188 |
// failed operation. |
| 189 |
ctx->poisoned = 0; |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 194 |
ENGINE *impl, const uint8_t *key, const uint8_t *iv) { |
| 195 |
return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); |
| 196 |
} |
| 197 |
|
| 198 |
int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 199 |
ENGINE *impl, const uint8_t *key, const uint8_t *iv) { |
| 200 |
return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); |
| 201 |
} |
| 202 |
|
| 203 |
// block_remainder returns the number of bytes to remove from |len| to get a |
| 204 |
// multiple of |ctx|'s block size. |
| 205 |
static int block_remainder(const EVP_CIPHER_CTX *ctx, int len) { |
| 206 |
// |block_size| must be a power of two. |
| 207 |
assert(ctx->cipher->block_size != 0); |
| 208 |
assert((ctx->cipher->block_size & (ctx->cipher->block_size - 1)) == 0); |
| 209 |
return len & (ctx->cipher->block_size - 1); |
| 210 |
} |
| 211 |
|
| 212 |
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
| 213 |
const uint8_t *in, int in_len) { |
| 214 |
SET_DIT_AUTO_RESET; |
| 215 |
GUARD_PTR(ctx); |
| 216 |
if (ctx->poisoned) { |
| 217 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
// If the first call to |cipher| succeeds and the second fails, |ctx| may be |
| 221 |
// left in an indeterminate state. We set a poison flag on failure to ensure |
| 222 |
// callers do not continue to use the object in that case. |
| 223 |
ctx->poisoned = 1; |
| 224 |
|
| 225 |
// Ciphers that use blocks may write up to |bl| extra bytes. Ensure the output |
| 226 |
// does not overflow |*out_len|. |
| 227 |
GUARD_PTR(ctx->cipher); |
| 228 |
int bl = ctx->cipher->block_size; |
| 229 |
if (bl > 1 && in_len > INT_MAX - bl) { |
| 230 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW); |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
| 235 |
int ret = ctx->cipher->cipher(ctx, out, in, in_len); |
| 236 |
if (ret < 0) { |
| 237 |
return 0; |
| 238 |
} else { |
| 239 |
*out_len = ret; |
| 240 |
} |
| 241 |
ctx->poisoned = 0; |
| 242 |
return 1; |
| 243 |
} |
| 244 |
|
| 245 |
if (in_len <= 0) { |
| 246 |
*out_len = 0; |
| 247 |
if (in_len == 0) { |
| 248 |
ctx->poisoned = 0; |
| 249 |
return 1; |
| 250 |
} |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
if (ctx->buf_len == 0 && block_remainder(ctx, in_len) == 0) { |
| 255 |
if (ctx->cipher->cipher(ctx, out, in, in_len)) { |
| 256 |
*out_len = in_len; |
| 257 |
ctx->poisoned = 0; |
| 258 |
return 1; |
| 259 |
} else { |
| 260 |
*out_len = 0; |
| 261 |
return 0; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
int i = ctx->buf_len; |
| 266 |
assert(bl <= (int)sizeof(ctx->buf)); |
| 267 |
if (i != 0) { |
| 268 |
if (bl - i > in_len) { |
| 269 |
OPENSSL_memcpy(&ctx->buf[i], in, in_len); |
| 270 |
ctx->buf_len += in_len; |
| 271 |
*out_len = 0; |
| 272 |
ctx->poisoned = 0; |
| 273 |
return 1; |
| 274 |
} else { |
| 275 |
int j = bl - i; |
| 276 |
OPENSSL_memcpy(&ctx->buf[i], in, j); |
| 277 |
if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) { |
| 278 |
return 0; |
| 279 |
} |
| 280 |
in_len -= j; |
| 281 |
in += j; |
| 282 |
out += bl; |
| 283 |
*out_len = bl; |
| 284 |
} |
| 285 |
} else { |
| 286 |
*out_len = 0; |
| 287 |
} |
| 288 |
|
| 289 |
i = block_remainder(ctx, in_len); |
| 290 |
in_len -= i; |
| 291 |
if (in_len > 0) { |
| 292 |
if (!ctx->cipher->cipher(ctx, out, in, in_len)) { |
| 293 |
return 0; |
| 294 |
} |
| 295 |
*out_len += in_len; |
| 296 |
} |
| 297 |
|
| 298 |
if (i != 0) { |
| 299 |
OPENSSL_memcpy(ctx->buf, &in[in_len], i); |
| 300 |
} |
| 301 |
ctx->buf_len = i; |
| 302 |
ctx->poisoned = 0; |
| 303 |
return 1; |
| 304 |
} |
| 305 |
|
| 306 |
int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
| 307 |
SET_DIT_AUTO_RESET; |
| 308 |
int n; |
| 309 |
unsigned int i, b, bl; |
| 310 |
GUARD_PTR(ctx); |
| 311 |
|
| 312 |
if (ctx->poisoned) { |
| 313 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 314 |
return 0; |
| 315 |
} |
| 316 |
GUARD_PTR(ctx->cipher); |
| 317 |
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
| 318 |
// When EVP_CIPH_FLAG_CUSTOM_CIPHER is set, the return value of |cipher| is |
| 319 |
// the number of bytes written, or -1 on error. Otherwise the return value |
| 320 |
// is one on success and zero on error. |
| 321 |
const int num_bytes = ctx->cipher->cipher(ctx, out, NULL, 0); |
| 322 |
if (num_bytes < 0) { |
| 323 |
return 0; |
| 324 |
} |
| 325 |
*out_len = num_bytes; |
| 326 |
goto out; |
| 327 |
} |
| 328 |
|
| 329 |
b = ctx->cipher->block_size; |
| 330 |
assert(b <= sizeof(ctx->buf)); |
| 331 |
if (b == 1) { |
| 332 |
*out_len = 0; |
| 333 |
goto out; |
| 334 |
} |
| 335 |
|
| 336 |
bl = ctx->buf_len; |
| 337 |
if (ctx->flags & EVP_CIPH_NO_PADDING) { |
| 338 |
if (bl) { |
| 339 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
| 340 |
return 0; |
| 341 |
} |
| 342 |
*out_len = 0; |
| 343 |
goto out; |
| 344 |
} |
| 345 |
|
| 346 |
n = b - bl; |
| 347 |
for (i = bl; i < b; i++) { |
| 348 |
ctx->buf[i] = n; |
| 349 |
} |
| 350 |
if (!ctx->cipher->cipher(ctx, out, ctx->buf, b)) { |
| 351 |
return 0; |
| 352 |
} |
| 353 |
*out_len = b; |
| 354 |
|
| 355 |
out: |
| 356 |
EVP_Cipher_verify_service_indicator(ctx); |
| 357 |
return 1; |
| 358 |
} |
| 359 |
|
| 360 |
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
| 361 |
const uint8_t *in, int in_len) { |
| 362 |
SET_DIT_AUTO_RESET; |
| 363 |
GUARD_PTR(ctx); |
| 364 |
if (ctx->poisoned) { |
| 365 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
// Ciphers that use blocks may write up to |bl| extra bytes. Ensure the output |
| 371 |
// does not overflow |*out_len|. |
| 372 |
GUARD_PTR(ctx->cipher); |
| 373 |
unsigned int b = ctx->cipher->block_size; |
| 374 |
if (b > 1 && in_len > INT_MAX - (int)b) { |
| 375 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW); |
| 376 |
return 0; |
| 377 |
} |
| 378 |
|
| 379 |
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
| 380 |
int r = ctx->cipher->cipher(ctx, out, in, in_len); |
| 381 |
if (r < 0) { |
| 382 |
*out_len = 0; |
| 383 |
return 0; |
| 384 |
} else { |
| 385 |
*out_len = r; |
| 386 |
} |
| 387 |
return 1; |
| 388 |
} |
| 389 |
|
| 390 |
if (in_len <= 0) { |
| 391 |
*out_len = 0; |
| 392 |
return in_len == 0; |
| 393 |
} |
| 394 |
|
| 395 |
if (ctx->flags & EVP_CIPH_NO_PADDING) { |
| 396 |
return EVP_EncryptUpdate(ctx, out, out_len, in, in_len); |
| 397 |
} |
| 398 |
|
| 399 |
assert(b <= sizeof(ctx->final)); |
| 400 |
int fix_len = 0; |
| 401 |
if (ctx->final_used) { |
| 402 |
OPENSSL_memcpy(out, ctx->final, b); |
| 403 |
out += b; |
| 404 |
fix_len = 1; |
| 405 |
} |
| 406 |
|
| 407 |
if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) { |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
// if we have 'decrypted' a multiple of block size, make sure |
| 412 |
// we have a copy of this last block |
| 413 |
if (b > 1 && !ctx->buf_len) { |
| 414 |
*out_len -= b; |
| 415 |
ctx->final_used = 1; |
| 416 |
OPENSSL_memcpy(ctx->final, &out[*out_len], b); |
| 417 |
} else { |
| 418 |
ctx->final_used = 0; |
| 419 |
} |
| 420 |
|
| 421 |
if (fix_len) { |
| 422 |
*out_len += b; |
| 423 |
} |
| 424 |
|
| 425 |
return 1; |
| 426 |
} |
| 427 |
|
| 428 |
int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) { |
| 429 |
SET_DIT_AUTO_RESET; |
| 430 |
int i, n; |
| 431 |
unsigned int b; |
| 432 |
*out_len = 0; |
| 433 |
GUARD_PTR(ctx); |
| 434 |
|
| 435 |
// |ctx->cipher->cipher| calls the static aes encryption function way under |
| 436 |
// the hood instead of |EVP_Cipher|, so the service indicator does not need |
| 437 |
// locking here. |
| 438 |
|
| 439 |
if (ctx->poisoned) { |
| 440 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 441 |
return 0; |
| 442 |
} |
| 443 |
GUARD_PTR(ctx->cipher); |
| 444 |
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
| 445 |
i = ctx->cipher->cipher(ctx, out, NULL, 0); |
| 446 |
if (i < 0) { |
| 447 |
return 0; |
| 448 |
} else { |
| 449 |
*out_len = i; |
| 450 |
} |
| 451 |
goto out; |
| 452 |
} |
| 453 |
|
| 454 |
b = ctx->cipher->block_size; |
| 455 |
if (ctx->flags & EVP_CIPH_NO_PADDING) { |
| 456 |
if (ctx->buf_len) { |
| 457 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
| 458 |
return 0; |
| 459 |
} |
| 460 |
*out_len = 0; |
| 461 |
goto out; |
| 462 |
} |
| 463 |
|
| 464 |
if (b > 1) { |
| 465 |
if (ctx->buf_len || !ctx->final_used) { |
| 466 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_WRONG_FINAL_BLOCK_LENGTH); |
| 467 |
return 0; |
| 468 |
} |
| 469 |
assert(b <= sizeof(ctx->final)); |
| 470 |
|
| 471 |
// The following assumes that the ciphertext has been authenticated. |
| 472 |
// Otherwise it provides a padding oracle. |
| 473 |
n = ctx->final[b - 1]; |
| 474 |
if (n == 0 || n > (int)b) { |
| 475 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
for (i = 0; i < n; i++) { |
| 480 |
if (ctx->final[--b] != n) { |
| 481 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 482 |
return 0; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
n = ctx->cipher->block_size - n; |
| 487 |
for (i = 0; i < n; i++) { |
| 488 |
out[i] = ctx->final[i]; |
| 489 |
} |
| 490 |
*out_len = n; |
| 491 |
} else { |
| 492 |
*out_len = 0; |
| 493 |
} |
| 494 |
|
| 495 |
out: |
| 496 |
EVP_Cipher_verify_service_indicator(ctx); |
| 497 |
return 1; |
| 498 |
} |
| 499 |
|
| 500 |
int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 501 |
size_t in_len) { |
| 502 |
SET_DIT_AUTO_RESET; |
| 503 |
GUARD_PTR(ctx); |
| 504 |
GUARD_PTR(ctx->cipher); |
| 505 |
const int ret = ctx->cipher->cipher(ctx, out, in, in_len); |
| 506 |
|
| 507 |
// |EVP_CIPH_FLAG_CUSTOM_CIPHER| never sets the FIPS indicator via |
| 508 |
// |EVP_Cipher| because it's complicated whether the operation has completed |
| 509 |
// or not. E.g. AES-GCM with a non-NULL |in| argument hasn't completed an |
| 510 |
// operation. Callers should use the |EVP_AEAD| API or, at least, |
| 511 |
// |EVP_CipherUpdate| etc. AES-KeyWrap users should use the |AES_wrap_key| |
| 512 |
// API instead. |
| 513 |
// |
| 514 |
// This call can't be pushed into |EVP_Cipher_verify_service_indicator| |
| 515 |
// because whether |ret| indicates success or not depends on whether |
| 516 |
// |EVP_CIPH_FLAG_CUSTOM_CIPHER| is set. (This is unreasonable, but matches |
| 517 |
// OpenSSL.) |
| 518 |
if (!(ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) && ret) { |
| 519 |
EVP_Cipher_verify_service_indicator(ctx); |
| 520 |
} |
| 521 |
|
| 522 |
return ret; |
| 523 |
} |
| 524 |
|
| 525 |
int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
| 526 |
const uint8_t *in, int in_len) { |
| 527 |
GUARD_PTR(ctx); |
| 528 |
if (ctx->encrypt) { |
| 529 |
return EVP_EncryptUpdate(ctx, out, out_len, in, in_len); |
| 530 |
} else { |
| 531 |
return EVP_DecryptUpdate(ctx, out, out_len, in, in_len); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
| 536 |
GUARD_PTR(ctx); |
| 537 |
if (ctx->encrypt) { |
| 538 |
return EVP_EncryptFinal_ex(ctx, out, out_len); |
| 539 |
} else { |
| 540 |
return EVP_DecryptFinal_ex(ctx, out, out_len); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) { |
| 545 |
return ctx->cipher; |
| 546 |
} |
| 547 |
|
| 548 |
int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) { |
| 549 |
return ctx->cipher->nid; |
| 550 |
} |
| 551 |
|
| 552 |
int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx) { |
| 553 |
return ctx->encrypt; |
| 554 |
} |
| 555 |
|
| 556 |
unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) { |
| 557 |
return ctx->cipher->block_size; |
| 558 |
} |
| 559 |
|
| 560 |
unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) { |
| 561 |
return ctx->key_len; |
| 562 |
} |
| 563 |
|
| 564 |
unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) { |
| 565 |
if (EVP_CIPHER_mode(ctx->cipher) == EVP_CIPH_GCM_MODE || |
| 566 |
EVP_CIPHER_mode(ctx->cipher) == EVP_CIPH_CCM_MODE) { |
| 567 |
int length; |
| 568 |
int res = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN, 0, |
| 569 |
&length); |
| 570 |
// EVP_CIPHER_CTX_ctrl returning an error should be impossible under this |
| 571 |
// circumstance. If it somehow did, fallback to the static cipher iv_len. |
| 572 |
if (res == 1) { |
| 573 |
return length; |
| 574 |
} |
| 575 |
} |
| 576 |
return ctx->cipher->iv_len; |
| 577 |
} |
| 578 |
|
| 579 |
void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) { |
| 580 |
return ctx->app_data; |
| 581 |
} |
| 582 |
|
| 583 |
void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) { |
| 584 |
ctx->app_data = data; |
| 585 |
} |
| 586 |
|
| 587 |
uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) { |
| 588 |
return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK; |
| 589 |
} |
| 590 |
|
| 591 |
uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) { |
| 592 |
return ctx->cipher->flags & EVP_CIPH_MODE_MASK; |
| 593 |
} |
| 594 |
|
| 595 |
int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) { |
| 596 |
int ret; |
| 597 |
if (!ctx->cipher) { |
| 598 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET); |
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
if (!ctx->cipher->ctrl) { |
| 603 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_NOT_IMPLEMENTED); |
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
ret = ctx->cipher->ctrl(ctx, command, arg, ptr); |
| 608 |
if (ret == -1) { |
| 609 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED); |
| 610 |
return 0; |
| 611 |
} |
| 612 |
|
| 613 |
return ret; |
| 614 |
} |
| 615 |
|
| 616 |
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) { |
| 617 |
if (pad) { |
| 618 |
ctx->flags &= ~EVP_CIPH_NO_PADDING; |
| 619 |
} else { |
| 620 |
ctx->flags |= EVP_CIPH_NO_PADDING; |
| 621 |
} |
| 622 |
return 1; |
| 623 |
} |
| 624 |
|
| 625 |
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, unsigned key_len) { |
| 626 |
if (c->key_len == key_len) { |
| 627 |
return 1; |
| 628 |
} |
| 629 |
|
| 630 |
if (key_len == 0 || !(c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) { |
| 631 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_KEY_LENGTH); |
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
c->key_len = key_len; |
| 636 |
return 1; |
| 637 |
} |
| 638 |
|
| 639 |
int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { |
| 640 |
if (cipher != NULL) { |
| 641 |
return cipher->nid; |
| 642 |
} |
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher) { |
| 647 |
if (cipher != NULL) { |
| 648 |
return cipher->block_size; |
| 649 |
} |
| 650 |
return 0; |
| 651 |
} |
| 652 |
|
| 653 |
unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher) { |
| 654 |
if (cipher != NULL) { |
| 655 |
return cipher->key_len; |
| 656 |
} |
| 657 |
return 0; |
| 658 |
} |
| 659 |
|
| 660 |
unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) { |
| 661 |
if (cipher != NULL) { |
| 662 |
return cipher->iv_len; |
| 663 |
} |
| 664 |
return 0; |
| 665 |
} |
| 666 |
|
| 667 |
uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher) { |
| 668 |
return cipher->flags & ~EVP_CIPH_MODE_MASK; |
| 669 |
} |
| 670 |
|
| 671 |
uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) { |
| 672 |
return cipher->flags & EVP_CIPH_MODE_MASK; |
| 673 |
} |
| 674 |
|
| 675 |
const char *EVP_CIPHER_name(const EVP_CIPHER *cipher) { |
| 676 |
if (cipher != NULL) { |
| 677 |
return OBJ_nid2sn(cipher->nid); |
| 678 |
} |
| 679 |
return NULL; |
| 680 |
} |
| 681 |
|
| 682 |
int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 683 |
const uint8_t *key, const uint8_t *iv, int enc) { |
| 684 |
if (cipher) { |
| 685 |
EVP_CIPHER_CTX_init(ctx); |
| 686 |
} |
| 687 |
return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc); |
| 688 |
} |
| 689 |
|
| 690 |
int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 691 |
const uint8_t *key, const uint8_t *iv) { |
| 692 |
return EVP_CipherInit(ctx, cipher, key, iv, 1); |
| 693 |
} |
| 694 |
|
| 695 |
int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 696 |
const uint8_t *key, const uint8_t *iv) { |
| 697 |
return EVP_CipherInit(ctx, cipher, key, iv, 0); |
| 698 |
} |
| 699 |
|
| 700 |
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
| 701 |
return EVP_CipherFinal_ex(ctx, out, out_len); |
| 702 |
} |
| 703 |
|
| 704 |
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
| 705 |
return EVP_EncryptFinal_ex(ctx, out, out_len); |
| 706 |
} |
| 707 |
|
| 708 |
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
| 709 |
return EVP_DecryptFinal_ex(ctx, out, out_len); |
| 710 |
} |
| 711 |
|
| 712 |
int EVP_add_cipher_alias(const char *a, const char *b) { |
| 713 |
return 1; |
| 714 |
} |
| 715 |
|
| 716 |
void EVP_CIPHER_CTX_set_flags(const EVP_CIPHER_CTX *ctx, uint32_t flags) {} |