summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp/evp_enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/evp/evp_enc.c')
-rw-r--r--src/lib/libcrypto/evp/evp_enc.c127
1 files changed, 78 insertions, 49 deletions
diff --git a/src/lib/libcrypto/evp/evp_enc.c b/src/lib/libcrypto/evp/evp_enc.c
index a1904993bf..f549eeb437 100644
--- a/src/lib/libcrypto/evp/evp_enc.c
+++ b/src/lib/libcrypto/evp/evp_enc.c
@@ -60,13 +60,12 @@
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include <openssl/evp.h> 61#include <openssl/evp.h>
62#include <openssl/err.h> 62#include <openssl/err.h>
63#include <openssl/rand.h>
64#ifndef OPENSSL_NO_ENGINE 63#ifndef OPENSSL_NO_ENGINE
65#include <openssl/engine.h> 64#include <openssl/engine.h>
66#endif 65#endif
67#include "evp_locl.h" 66#include "evp_locl.h"
68 67
69const char EVP_version[]="EVP" OPENSSL_VERSION_PTEXT; 68const char *EVP_version="EVP" OPENSSL_VERSION_PTEXT;
70 69
71void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) 70void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
72 { 71 {
@@ -74,13 +73,6 @@ void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
74 /* ctx->cipher=NULL; */ 73 /* ctx->cipher=NULL; */
75 } 74 }
76 75
77EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
78 {
79 EVP_CIPHER_CTX *ctx=OPENSSL_malloc(sizeof *ctx);
80 if (ctx)
81 EVP_CIPHER_CTX_init(ctx);
82 return ctx;
83 }
84 76
85int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, 77int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
86 const unsigned char *key, const unsigned char *iv, int enc) 78 const unsigned char *key, const unsigned char *iv, int enc)
@@ -90,6 +82,48 @@ int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
90 return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc); 82 return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc);
91 } 83 }
92 84
85#ifdef OPENSSL_FIPS
86
87/* The purpose of these is to trap programs that attempt to use non FIPS
88 * algorithms in FIPS mode and ignore the errors.
89 */
90
91int bad_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
92 const unsigned char *iv, int enc)
93 { FIPS_ERROR_IGNORED("Cipher init"); return 0;}
94
95int bad_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
96 const unsigned char *in, unsigned int inl)
97 { FIPS_ERROR_IGNORED("Cipher update"); return 0;}
98
99/* NB: no cleanup because it is allowed after failed init */
100
101int bad_set_asn1(EVP_CIPHER_CTX *ctx, ASN1_TYPE *typ)
102 { FIPS_ERROR_IGNORED("Cipher set_asn1"); return 0;}
103int bad_get_asn1(EVP_CIPHER_CTX *ctx, ASN1_TYPE *typ)
104 { FIPS_ERROR_IGNORED("Cipher get_asn1"); return 0;}
105int bad_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
106 { FIPS_ERROR_IGNORED("Cipher ctrl"); return 0;}
107
108static const EVP_CIPHER bad_cipher =
109 {
110 0,
111 0,
112 0,
113 0,
114 0,
115 bad_init,
116 bad_do_cipher,
117 NULL,
118 0,
119 bad_set_asn1,
120 bad_get_asn1,
121 bad_ctrl,
122 NULL
123 };
124
125#endif
126
93int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, 127int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
94 const unsigned char *key, const unsigned char *iv, int enc) 128 const unsigned char *key, const unsigned char *iv, int enc)
95 { 129 {
@@ -124,7 +158,7 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *imp
124 { 158 {
125 if (!ENGINE_init(impl)) 159 if (!ENGINE_init(impl))
126 { 160 {
127 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); 161 EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_INITIALIZATION_ERROR);
128 return 0; 162 return 0;
129 } 163 }
130 } 164 }
@@ -141,7 +175,7 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *imp
141 * control history, is that we should at least 175 * control history, is that we should at least
142 * be able to avoid using US mispellings of 176 * be able to avoid using US mispellings of
143 * "initialisation"? */ 177 * "initialisation"? */
144 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); 178 EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_INITIALIZATION_ERROR);
145 return 0; 179 return 0;
146 } 180 }
147 /* We'll use the ENGINE's private cipher definition */ 181 /* We'll use the ENGINE's private cipher definition */
@@ -154,14 +188,13 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *imp
154 else 188 else
155 ctx->engine = NULL; 189 ctx->engine = NULL;
156#endif 190#endif
157
158 ctx->cipher=cipher; 191 ctx->cipher=cipher;
159 if (ctx->cipher->ctx_size) 192 if (ctx->cipher->ctx_size)
160 { 193 {
161 ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size); 194 ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size);
162 if (!ctx->cipher_data) 195 if (!ctx->cipher_data)
163 { 196 {
164 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE); 197 EVPerr(EVP_F_EVP_CIPHERINIT, ERR_R_MALLOC_FAILURE);
165 return 0; 198 return 0;
166 } 199 }
167 } 200 }
@@ -175,14 +208,14 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *imp
175 { 208 {
176 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) 209 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL))
177 { 210 {
178 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR); 211 EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_INITIALIZATION_ERROR);
179 return 0; 212 return 0;
180 } 213 }
181 } 214 }
182 } 215 }
183 else if(!ctx->cipher) 216 else if(!ctx->cipher)
184 { 217 {
185 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET); 218 EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_NO_CIPHER_SET);
186 return 0; 219 return 0;
187 } 220 }
188#ifndef OPENSSL_NO_ENGINE 221#ifndef OPENSSL_NO_ENGINE
@@ -207,8 +240,7 @@ skip_to_init:
207 240
208 case EVP_CIPH_CBC_MODE: 241 case EVP_CIPH_CBC_MODE:
209 242
210 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <= 243 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof ctx->iv);
211 (int)sizeof(ctx->iv));
212 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); 244 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
213 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); 245 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
214 break; 246 break;
@@ -219,6 +251,24 @@ skip_to_init:
219 } 251 }
220 } 252 }
221 253
254#ifdef OPENSSL_FIPS
255 /* After 'key' is set no further parameters changes are permissible.
256 * So only check for non FIPS enabling at this point.
257 */
258 if (key && FIPS_mode())
259 {
260 if (!(ctx->cipher->flags & EVP_CIPH_FLAG_FIPS)
261 & !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW))
262 {
263 EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_DISABLED_FOR_FIPS);
264 ERR_add_error_data(2, "cipher=",
265 EVP_CIPHER_name(ctx->cipher));
266 ctx->cipher = &bad_cipher;
267 return 0;
268 }
269 }
270#endif
271
222 if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { 272 if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
223 if(!ctx->cipher->init(ctx,key,iv,enc)) return 0; 273 if(!ctx->cipher->init(ctx,key,iv,enc)) return 0;
224 } 274 }
@@ -295,7 +345,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
295 } 345 }
296 i=ctx->buf_len; 346 i=ctx->buf_len;
297 bl=ctx->cipher->block_size; 347 bl=ctx->cipher->block_size;
298 OPENSSL_assert(bl <= (int)sizeof(ctx->buf)); 348 OPENSSL_assert(bl <= sizeof ctx->buf);
299 if (i != 0) 349 if (i != 0)
300 { 350 {
301 if (i+inl < bl) 351 if (i+inl < bl)
@@ -341,8 +391,7 @@ int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
341 391
342int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) 392int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
343 { 393 {
344 int n,ret; 394 int i,n,b,bl,ret;
345 unsigned int i, b, bl;
346 395
347 b=ctx->cipher->block_size; 396 b=ctx->cipher->block_size;
348 OPENSSL_assert(b <= sizeof ctx->buf); 397 OPENSSL_assert(b <= sizeof ctx->buf);
@@ -356,7 +405,7 @@ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
356 { 405 {
357 if(bl) 406 if(bl)
358 { 407 {
359 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); 408 EVPerr(EVP_F_EVP_ENCRYPTFINAL,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
360 return 0; 409 return 0;
361 } 410 }
362 *outl = 0; 411 *outl = 0;
@@ -378,8 +427,7 @@ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
378int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, 427int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
379 const unsigned char *in, int inl) 428 const unsigned char *in, int inl)
380 { 429 {
381 int fix_len; 430 int b, fix_len;
382 unsigned int b;
383 431
384 if (inl == 0) 432 if (inl == 0)
385 { 433 {
@@ -432,8 +480,8 @@ int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
432 480
433int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) 481int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
434 { 482 {
435 int i,n; 483 int i,b;
436 unsigned int b; 484 int n;
437 485
438 *outl=0; 486 *outl=0;
439 b=ctx->cipher->block_size; 487 b=ctx->cipher->block_size;
@@ -441,7 +489,7 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
441 { 489 {
442 if(ctx->buf_len) 490 if(ctx->buf_len)
443 { 491 {
444 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); 492 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
445 return 0; 493 return 0;
446 } 494 }
447 *outl = 0; 495 *outl = 0;
@@ -451,21 +499,21 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
451 { 499 {
452 if (ctx->buf_len || !ctx->final_used) 500 if (ctx->buf_len || !ctx->final_used)
453 { 501 {
454 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_WRONG_FINAL_BLOCK_LENGTH); 502 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_WRONG_FINAL_BLOCK_LENGTH);
455 return(0); 503 return(0);
456 } 504 }
457 OPENSSL_assert(b <= sizeof ctx->final); 505 OPENSSL_assert(b <= sizeof ctx->final);
458 n=ctx->final[b-1]; 506 n=ctx->final[b-1];
459 if (n == 0 || n > (int)b) 507 if (n > b)
460 { 508 {
461 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_BAD_DECRYPT); 509 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
462 return(0); 510 return(0);
463 } 511 }
464 for (i=0; i<n; i++) 512 for (i=0; i<n; i++)
465 { 513 {
466 if (ctx->final[--b] != n) 514 if (ctx->final[--b] != n)
467 { 515 {
468 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,EVP_R_BAD_DECRYPT); 516 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
469 return(0); 517 return(0);
470 } 518 }
471 } 519 }
@@ -479,15 +527,6 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
479 return(1); 527 return(1);
480 } 528 }
481 529
482void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
483 {
484 if (ctx)
485 {
486 EVP_CIPHER_CTX_cleanup(ctx);
487 OPENSSL_free(ctx);
488 }
489 }
490
491int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) 530int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
492 { 531 {
493 if (c->cipher != NULL) 532 if (c->cipher != NULL)
@@ -551,13 +590,3 @@ int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
551 } 590 }
552 return ret; 591 return ret;
553} 592}
554
555int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
556 {
557 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
558 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
559 if (RAND_bytes(key, ctx->key_len) <= 0)
560 return 0;
561 return 1;
562 }
563