diff options
Diffstat (limited to 'src/lib/libcrypto/evp/evp_enc.c')
-rw-r--r-- | src/lib/libcrypto/evp/evp_enc.c | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/src/lib/libcrypto/evp/evp_enc.c b/src/lib/libcrypto/evp/evp_enc.c index 0dfb7a5dc3..d0a5eb2d5f 100644 --- a/src/lib/libcrypto/evp/evp_enc.c +++ b/src/lib/libcrypto/evp/evp_enc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: evp_enc.c,v 1.35 2016/09/09 00:03:22 bcook Exp $ */ | 1 | /* $OpenBSD: evp_enc.c,v 1.36 2017/01/29 17:49:23 beck Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -130,8 +130,7 @@ EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, | |||
130 | #ifndef OPENSSL_NO_ENGINE | 130 | #ifndef OPENSSL_NO_ENGINE |
131 | if (impl) { | 131 | if (impl) { |
132 | if (!ENGINE_init(impl)) { | 132 | if (!ENGINE_init(impl)) { |
133 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, | 133 | EVPerror(EVP_R_INITIALIZATION_ERROR); |
134 | EVP_R_INITIALIZATION_ERROR); | ||
135 | return 0; | 134 | return 0; |
136 | } | 135 | } |
137 | } else | 136 | } else |
@@ -142,8 +141,7 @@ EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, | |||
142 | const EVP_CIPHER *c = | 141 | const EVP_CIPHER *c = |
143 | ENGINE_get_cipher(impl, cipher->nid); | 142 | ENGINE_get_cipher(impl, cipher->nid); |
144 | if (!c) { | 143 | if (!c) { |
145 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, | 144 | EVPerror(EVP_R_INITIALIZATION_ERROR); |
146 | EVP_R_INITIALIZATION_ERROR); | ||
147 | return 0; | 145 | return 0; |
148 | } | 146 | } |
149 | /* We'll use the ENGINE's private cipher definition */ | 147 | /* We'll use the ENGINE's private cipher definition */ |
@@ -160,8 +158,7 @@ EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, | |||
160 | if (ctx->cipher->ctx_size) { | 158 | if (ctx->cipher->ctx_size) { |
161 | ctx->cipher_data = malloc(ctx->cipher->ctx_size); | 159 | ctx->cipher_data = malloc(ctx->cipher->ctx_size); |
162 | if (!ctx->cipher_data) { | 160 | if (!ctx->cipher_data) { |
163 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, | 161 | EVPerror(ERR_R_MALLOC_FAILURE); |
164 | ERR_R_MALLOC_FAILURE); | ||
165 | return 0; | 162 | return 0; |
166 | } | 163 | } |
167 | } else { | 164 | } else { |
@@ -171,13 +168,12 @@ EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, | |||
171 | ctx->flags = 0; | 168 | ctx->flags = 0; |
172 | if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { | 169 | if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { |
173 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) { | 170 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) { |
174 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, | 171 | EVPerror(EVP_R_INITIALIZATION_ERROR); |
175 | EVP_R_INITIALIZATION_ERROR); | ||
176 | return 0; | 172 | return 0; |
177 | } | 173 | } |
178 | } | 174 | } |
179 | } else if (!ctx->cipher) { | 175 | } else if (!ctx->cipher) { |
180 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET); | 176 | EVPerror(EVP_R_NO_CIPHER_SET); |
181 | return 0; | 177 | return 0; |
182 | } | 178 | } |
183 | #ifndef OPENSSL_NO_ENGINE | 179 | #ifndef OPENSSL_NO_ENGINE |
@@ -187,7 +183,7 @@ skip_to_init: | |||
187 | if (ctx->cipher->block_size != 1 && | 183 | if (ctx->cipher->block_size != 1 && |
188 | ctx->cipher->block_size != 8 && | 184 | ctx->cipher->block_size != 8 && |
189 | ctx->cipher->block_size != 16) { | 185 | ctx->cipher->block_size != 16) { |
190 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_BAD_BLOCK_LENGTH); | 186 | EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
191 | return 0; | 187 | return 0; |
192 | } | 188 | } |
193 | 189 | ||
@@ -208,8 +204,7 @@ skip_to_init: | |||
208 | 204 | ||
209 | if ((size_t)EVP_CIPHER_CTX_iv_length(ctx) > | 205 | if ((size_t)EVP_CIPHER_CTX_iv_length(ctx) > |
210 | sizeof(ctx->iv)) { | 206 | sizeof(ctx->iv)) { |
211 | EVPerr(EVP_F_EVP_CIPHERINIT_EX, | 207 | EVPerror(EVP_R_IV_TOO_LARGE); |
212 | EVP_R_IV_TOO_LARGE); | ||
213 | return 0; | 208 | return 0; |
214 | } | 209 | } |
215 | if (iv) | 210 | if (iv) |
@@ -336,7 +331,7 @@ EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | |||
336 | i = ctx->buf_len; | 331 | i = ctx->buf_len; |
337 | bl = ctx->cipher->block_size; | 332 | bl = ctx->cipher->block_size; |
338 | if ((size_t)bl > sizeof(ctx->buf)) { | 333 | if ((size_t)bl > sizeof(ctx->buf)) { |
339 | EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_BAD_BLOCK_LENGTH); | 334 | EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
340 | *outl = 0; | 335 | *outl = 0; |
341 | return 0; | 336 | return 0; |
342 | } | 337 | } |
@@ -401,7 +396,7 @@ EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |||
401 | 396 | ||
402 | b = ctx->cipher->block_size; | 397 | b = ctx->cipher->block_size; |
403 | if (b > sizeof ctx->buf) { | 398 | if (b > sizeof ctx->buf) { |
404 | EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_BAD_BLOCK_LENGTH); | 399 | EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
405 | return 0; | 400 | return 0; |
406 | } | 401 | } |
407 | if (b == 1) { | 402 | if (b == 1) { |
@@ -411,8 +406,7 @@ EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |||
411 | bl = ctx->buf_len; | 406 | bl = ctx->buf_len; |
412 | if (ctx->flags & EVP_CIPH_NO_PADDING) { | 407 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
413 | if (bl) { | 408 | if (bl) { |
414 | EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, | 409 | EVPerror(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
415 | EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); | ||
416 | return 0; | 410 | return 0; |
417 | } | 411 | } |
418 | *outl = 0; | 412 | *outl = 0; |
@@ -458,7 +452,7 @@ EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | |||
458 | 452 | ||
459 | b = ctx->cipher->block_size; | 453 | b = ctx->cipher->block_size; |
460 | if (b > sizeof ctx->final) { | 454 | if (b > sizeof ctx->final) { |
461 | EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_BAD_BLOCK_LENGTH); | 455 | EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
462 | return 0; | 456 | return 0; |
463 | } | 457 | } |
464 | 458 | ||
@@ -519,8 +513,7 @@ EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |||
519 | b = ctx->cipher->block_size; | 513 | b = ctx->cipher->block_size; |
520 | if (ctx->flags & EVP_CIPH_NO_PADDING) { | 514 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
521 | if (ctx->buf_len) { | 515 | if (ctx->buf_len) { |
522 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, | 516 | EVPerror(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
523 | EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); | ||
524 | return 0; | 517 | return 0; |
525 | } | 518 | } |
526 | *outl = 0; | 519 | *outl = 0; |
@@ -528,24 +521,21 @@ EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |||
528 | } | 521 | } |
529 | if (b > 1) { | 522 | if (b > 1) { |
530 | if (ctx->buf_len || !ctx->final_used) { | 523 | if (ctx->buf_len || !ctx->final_used) { |
531 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, | 524 | EVPerror(EVP_R_WRONG_FINAL_BLOCK_LENGTH); |
532 | EVP_R_WRONG_FINAL_BLOCK_LENGTH); | ||
533 | return (0); | 525 | return (0); |
534 | } | 526 | } |
535 | if (b > sizeof ctx->final) { | 527 | if (b > sizeof ctx->final) { |
536 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, | 528 | EVPerror(EVP_R_BAD_BLOCK_LENGTH); |
537 | EVP_R_BAD_BLOCK_LENGTH); | ||
538 | return 0; | 529 | return 0; |
539 | } | 530 | } |
540 | n = ctx->final[b - 1]; | 531 | n = ctx->final[b - 1]; |
541 | if (n == 0 || n > (int)b) { | 532 | if (n == 0 || n > (int)b) { |
542 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT); | 533 | EVPerror(EVP_R_BAD_DECRYPT); |
543 | return (0); | 534 | return (0); |
544 | } | 535 | } |
545 | for (i = 0; i < n; i++) { | 536 | for (i = 0; i < n; i++) { |
546 | if (ctx->final[--b] != n) { | 537 | if (ctx->final[--b] != n) { |
547 | EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, | 538 | EVPerror(EVP_R_BAD_DECRYPT); |
548 | EVP_R_BAD_DECRYPT); | ||
549 | return (0); | 539 | return (0); |
550 | } | 540 | } |
551 | } | 541 | } |
@@ -600,7 +590,7 @@ EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) | |||
600 | c->key_len = keylen; | 590 | c->key_len = keylen; |
601 | return 1; | 591 | return 1; |
602 | } | 592 | } |
603 | EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH); | 593 | EVPerror(EVP_R_INVALID_KEY_LENGTH); |
604 | return 0; | 594 | return 0; |
605 | } | 595 | } |
606 | 596 | ||
@@ -620,19 +610,18 @@ EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) | |||
620 | int ret; | 610 | int ret; |
621 | 611 | ||
622 | if (!ctx->cipher) { | 612 | if (!ctx->cipher) { |
623 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET); | 613 | EVPerror(EVP_R_NO_CIPHER_SET); |
624 | return 0; | 614 | return 0; |
625 | } | 615 | } |
626 | 616 | ||
627 | if (!ctx->cipher->ctrl) { | 617 | if (!ctx->cipher->ctrl) { |
628 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED); | 618 | EVPerror(EVP_R_CTRL_NOT_IMPLEMENTED); |
629 | return 0; | 619 | return 0; |
630 | } | 620 | } |
631 | 621 | ||
632 | ret = ctx->cipher->ctrl(ctx, type, arg, ptr); | 622 | ret = ctx->cipher->ctrl(ctx, type, arg, ptr); |
633 | if (ret == -1) { | 623 | if (ret == -1) { |
634 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, | 624 | EVPerror(EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); |
635 | EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); | ||
636 | return 0; | 625 | return 0; |
637 | } | 626 | } |
638 | return ret; | 627 | return ret; |
@@ -651,13 +640,13 @@ int | |||
651 | EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) | 640 | EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) |
652 | { | 641 | { |
653 | if ((in == NULL) || (in->cipher == NULL)) { | 642 | if ((in == NULL) || (in->cipher == NULL)) { |
654 | EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED); | 643 | EVPerror(EVP_R_INPUT_NOT_INITIALIZED); |
655 | return 0; | 644 | return 0; |
656 | } | 645 | } |
657 | #ifndef OPENSSL_NO_ENGINE | 646 | #ifndef OPENSSL_NO_ENGINE |
658 | /* Make sure it's safe to copy a cipher context using an ENGINE */ | 647 | /* Make sure it's safe to copy a cipher context using an ENGINE */ |
659 | if (in->engine && !ENGINE_init(in->engine)) { | 648 | if (in->engine && !ENGINE_init(in->engine)) { |
660 | EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB); | 649 | EVPerror(ERR_R_ENGINE_LIB); |
661 | return 0; | 650 | return 0; |
662 | } | 651 | } |
663 | #endif | 652 | #endif |
@@ -668,7 +657,7 @@ EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) | |||
668 | if (in->cipher_data && in->cipher->ctx_size) { | 657 | if (in->cipher_data && in->cipher->ctx_size) { |
669 | out->cipher_data = malloc(in->cipher->ctx_size); | 658 | out->cipher_data = malloc(in->cipher->ctx_size); |
670 | if (!out->cipher_data) { | 659 | if (!out->cipher_data) { |
671 | EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE); | 660 | EVPerror(ERR_R_MALLOC_FAILURE); |
672 | return 0; | 661 | return 0; |
673 | } | 662 | } |
674 | memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size); | 663 | memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size); |