2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
15 #include <openssl/rand.h>
16 #include <openssl/engine.h>
17 #include "internal/evp_int.h"
20 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
24 if (c->cipher != NULL) {
25 if (c->cipher->cleanup && !c->cipher->cleanup(c))
27 /* Cleanse cipher context data */
28 if (c->cipher_data && c->cipher->ctx_size)
29 OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
31 OPENSSL_free(c->cipher_data);
32 #ifndef OPENSSL_NO_ENGINE
33 ENGINE_finish(c->engine);
35 memset(c, 0, sizeof(*c));
39 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
41 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
44 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
46 EVP_CIPHER_CTX_reset(ctx);
50 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
51 const unsigned char *key, const unsigned char *iv, int enc)
54 EVP_CIPHER_CTX_reset(ctx);
55 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
58 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
59 ENGINE *impl, const unsigned char *key,
60 const unsigned char *iv, int enc)
69 #ifndef OPENSSL_NO_ENGINE
71 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
72 * this context may already have an ENGINE! Try to avoid releasing the
73 * previous handle, re-querying for an ENGINE, and having a
74 * reinitialisation, when it may all be unnecessary.
76 if (ctx->engine && ctx->cipher
77 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
82 * Ensure a context left lying around from last time is cleared (the
83 * previous check attempted to avoid this if the same ENGINE and
84 * EVP_CIPHER could be used).
87 unsigned long flags = ctx->flags;
88 EVP_CIPHER_CTX_reset(ctx);
89 /* Restore encrypt and flags */
93 #ifndef OPENSSL_NO_ENGINE
95 if (!ENGINE_init(impl)) {
96 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
100 /* Ask if an ENGINE is reserved for this job */
101 impl = ENGINE_get_cipher_engine(cipher->nid);
103 /* There's an ENGINE for this job ... (apparently) */
104 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
107 * One positive side-effect of US's export control history,
108 * is that we should at least be able to avoid using US
109 * misspellings of "initialisation"?
111 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
114 /* We'll use the ENGINE's private cipher definition */
117 * Store the ENGINE functional reference so we know 'cipher' came
118 * from an ENGINE and we need to release it when done.
125 ctx->cipher = cipher;
126 if (ctx->cipher->ctx_size) {
127 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
128 if (ctx->cipher_data == NULL) {
130 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
134 ctx->cipher_data = NULL;
136 ctx->key_len = cipher->key_len;
137 /* Preserve wrap enable flag, zero everything else */
138 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
139 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
140 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
142 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
146 } else if (!ctx->cipher) {
147 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
150 #ifndef OPENSSL_NO_ENGINE
153 /* we assume block size is a power of 2 in *cryptUpdate */
154 OPENSSL_assert(ctx->cipher->block_size == 1
155 || ctx->cipher->block_size == 8
156 || ctx->cipher->block_size == 16);
158 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
159 && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
160 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
164 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
165 switch (EVP_CIPHER_CTX_mode(ctx)) {
167 case EVP_CIPH_STREAM_CIPHER:
168 case EVP_CIPH_ECB_MODE:
171 case EVP_CIPH_CFB_MODE:
172 case EVP_CIPH_OFB_MODE:
177 case EVP_CIPH_CBC_MODE:
179 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
180 (int)sizeof(ctx->iv));
182 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
183 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
186 case EVP_CIPH_CTR_MODE:
188 /* Don't reuse IV for CTR mode */
190 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
198 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
199 if (!ctx->cipher->init(ctx, key, iv, enc))
204 ctx->block_mask = ctx->cipher->block_size - 1;
208 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
209 const unsigned char *in, int inl)
212 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
214 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
217 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
220 return EVP_EncryptFinal_ex(ctx, out, outl);
222 return EVP_DecryptFinal_ex(ctx, out, outl);
225 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
228 return EVP_EncryptFinal(ctx, out, outl);
230 return EVP_DecryptFinal(ctx, out, outl);
233 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
234 const unsigned char *key, const unsigned char *iv)
236 return EVP_CipherInit(ctx, cipher, key, iv, 1);
239 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
240 ENGINE *impl, const unsigned char *key,
241 const unsigned char *iv)
243 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
246 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
247 const unsigned char *key, const unsigned char *iv)
249 return EVP_CipherInit(ctx, cipher, key, iv, 0);
252 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
253 ENGINE *impl, const unsigned char *key,
254 const unsigned char *iv)
256 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
260 * According to the letter of standard difference between pointers
261 * is specified to be valid only within same object. This makes
262 * it formally challenging to determine if input and output buffers
263 * are not partially overlapping with standard pointer arithmetic.
268 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
270 * Then we have VMS that distinguishes itself by adhering to
271 * sizeof(size_t)==4 even in 64-bit builds, which means that
272 * difference between two pointers might be truncated to 32 bits.
273 * In the context one can even wonder how comparison for
274 * equality is implemented. To be on the safe side we adhere to
275 * PTRDIFF_T even for comparison for equality.
277 # define PTRDIFF_T uint64_t
279 # define PTRDIFF_T size_t
282 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
284 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
286 * Check for partially overlapping buffers. [Binary logical
287 * operations are used instead of boolean to minimize number
288 * of conditional branches.]
290 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
291 (diff > (0 - (PTRDIFF_T)len)));
296 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
297 const unsigned char *in, int inl)
299 int i, j, bl, cmpl = inl;
301 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
302 cmpl = (cmpl + 7) / 8;
304 bl = ctx->cipher->block_size;
306 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
307 /* If block size > 1 then the cipher will have to do this check */
308 if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
309 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
313 i = ctx->cipher->do_cipher(ctx, out, in, inl);
325 if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
326 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
330 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
331 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
340 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
343 memcpy(&(ctx->buf[i]), in, inl);
349 memcpy(&(ctx->buf[i]), in, j);
352 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
362 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
368 memcpy(ctx->buf, &(in[inl]), i);
373 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
376 ret = EVP_EncryptFinal_ex(ctx, out, outl);
380 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
383 unsigned int i, b, bl;
385 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
386 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
394 b = ctx->cipher->block_size;
395 OPENSSL_assert(b <= sizeof(ctx->buf));
401 if (ctx->flags & EVP_CIPH_NO_PADDING) {
403 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
404 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
412 for (i = bl; i < b; i++)
414 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
422 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
423 const unsigned char *in, int inl)
425 int fix_len, cmpl = inl;
428 b = ctx->cipher->block_size;
430 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
431 cmpl = (cmpl + 7) / 8;
433 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
434 if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
435 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
439 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
453 if (ctx->flags & EVP_CIPH_NO_PADDING)
454 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
456 OPENSSL_assert(b <= sizeof(ctx->final));
458 if (ctx->final_used) {
459 /* see comment about PTRDIFF_T comparison above */
460 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
461 || is_partially_overlapping(out, in, b)) {
462 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
465 memcpy(out, ctx->final, b);
471 if (!EVP_EncryptUpdate(ctx, out, outl, in, inl))
475 * if we have 'decrypted' a multiple of block size, make sure we have a
476 * copy of this last block
478 if (b > 1 && !ctx->buf_len) {
481 memcpy(ctx->final, &out[*outl], b);
491 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
494 ret = EVP_DecryptFinal_ex(ctx, out, outl);
498 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
504 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
505 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
513 b = ctx->cipher->block_size;
514 if (ctx->flags & EVP_CIPH_NO_PADDING) {
516 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
517 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
524 if (ctx->buf_len || !ctx->final_used) {
525 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
528 OPENSSL_assert(b <= sizeof(ctx->final));
531 * The following assumes that the ciphertext has been authenticated.
532 * Otherwise it provides a padding oracle.
534 n = ctx->final[b - 1];
535 if (n == 0 || n > (int)b) {
536 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
539 for (i = 0; i < n; i++) {
540 if (ctx->final[--b] != n) {
541 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
545 n = ctx->cipher->block_size - n;
546 for (i = 0; i < n; i++)
547 out[i] = ctx->final[i];
554 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
556 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
557 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
558 if (c->key_len == keylen)
560 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
564 EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
568 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
571 ctx->flags &= ~EVP_CIPH_NO_PADDING;
573 ctx->flags |= EVP_CIPH_NO_PADDING;
577 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
581 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
585 if (!ctx->cipher->ctrl) {
586 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
590 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
592 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
593 EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
599 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
601 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
602 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
603 if (RAND_bytes(key, ctx->key_len) <= 0)
608 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
610 if ((in == NULL) || (in->cipher == NULL)) {
611 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
614 #ifndef OPENSSL_NO_ENGINE
615 /* Make sure it's safe to copy a cipher context using an ENGINE */
616 if (in->engine && !ENGINE_init(in->engine)) {
617 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
622 EVP_CIPHER_CTX_reset(out);
623 memcpy(out, in, sizeof(*out));
625 if (in->cipher_data && in->cipher->ctx_size) {
626 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
627 if (out->cipher_data == NULL) {
629 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
632 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
635 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
636 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
638 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);