2 * Copyright 1995-2018 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/rand_drbg.h>
17 #include <openssl/engine.h>
18 #include "crypto/evp.h"
19 #include "evp_local.h"
21 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
25 if (c->cipher != NULL) {
26 if (c->cipher->cleanup && !c->cipher->cleanup(c))
28 /* Cleanse cipher context data */
29 if (c->cipher_data && c->cipher->ctx_size)
30 OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
32 OPENSSL_free(c->cipher_data);
33 #ifndef OPENSSL_NO_ENGINE
34 ENGINE_finish(c->engine);
36 memset(c, 0, sizeof(*c));
40 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
42 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
45 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
47 EVP_CIPHER_CTX_reset(ctx);
51 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
52 const unsigned char *key, const unsigned char *iv, int enc)
55 EVP_CIPHER_CTX_reset(ctx);
56 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
59 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
60 ENGINE *impl, const unsigned char *key,
61 const unsigned char *iv, int enc)
70 #ifndef OPENSSL_NO_ENGINE
72 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
73 * this context may already have an ENGINE! Try to avoid releasing the
74 * previous handle, re-querying for an ENGINE, and having a
75 * reinitialisation, when it may all be unnecessary.
77 if (ctx->engine && ctx->cipher
78 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
83 * Ensure a context left lying around from last time is cleared (the
84 * previous check attempted to avoid this if the same ENGINE and
85 * EVP_CIPHER could be used).
88 unsigned long flags = ctx->flags;
89 EVP_CIPHER_CTX_reset(ctx);
90 /* Restore encrypt and flags */
94 #ifndef OPENSSL_NO_ENGINE
96 if (!ENGINE_init(impl)) {
97 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
101 /* Ask if an ENGINE is reserved for this job */
102 impl = ENGINE_get_cipher_engine(cipher->nid);
104 /* There's an ENGINE for this job ... (apparently) */
105 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
108 * One positive side-effect of US's export control history,
109 * is that we should at least be able to avoid using US
110 * misspellings of "initialisation"?
112 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
115 /* We'll use the ENGINE's private cipher definition */
118 * Store the ENGINE functional reference so we know 'cipher' came
119 * from an ENGINE and we need to release it when done.
126 ctx->cipher = cipher;
127 if (ctx->cipher->ctx_size) {
128 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
129 if (ctx->cipher_data == NULL) {
131 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
135 ctx->cipher_data = NULL;
137 ctx->key_len = cipher->key_len;
138 /* Preserve wrap enable flag, zero everything else */
139 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
140 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
141 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
143 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
147 } else if (!ctx->cipher) {
148 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
151 #ifndef OPENSSL_NO_ENGINE
154 /* we assume block size is a power of 2 in *cryptUpdate */
155 OPENSSL_assert(ctx->cipher->block_size == 1
156 || ctx->cipher->block_size == 8
157 || ctx->cipher->block_size == 16);
159 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
160 && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
161 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
165 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
166 switch (EVP_CIPHER_CTX_mode(ctx)) {
168 case EVP_CIPH_STREAM_CIPHER:
169 case EVP_CIPH_ECB_MODE:
172 case EVP_CIPH_CFB_MODE:
173 case EVP_CIPH_OFB_MODE:
178 case EVP_CIPH_CBC_MODE:
180 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
181 (int)sizeof(ctx->iv));
183 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
184 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
187 case EVP_CIPH_CTR_MODE:
189 /* Don't reuse IV for CTR mode */
191 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
199 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
200 if (!ctx->cipher->init(ctx, key, iv, enc))
205 ctx->block_mask = ctx->cipher->block_size - 1;
209 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
210 const unsigned char *in, int inl)
213 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
215 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
218 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
221 return EVP_EncryptFinal_ex(ctx, out, outl);
223 return EVP_DecryptFinal_ex(ctx, out, outl);
226 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
229 return EVP_EncryptFinal(ctx, out, outl);
231 return EVP_DecryptFinal(ctx, out, outl);
234 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
235 const unsigned char *key, const unsigned char *iv)
237 return EVP_CipherInit(ctx, cipher, key, iv, 1);
240 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
241 ENGINE *impl, const unsigned char *key,
242 const unsigned char *iv)
244 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
247 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
248 const unsigned char *key, const unsigned char *iv)
250 return EVP_CipherInit(ctx, cipher, key, iv, 0);
253 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
254 ENGINE *impl, const unsigned char *key,
255 const unsigned char *iv)
257 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
261 * According to the letter of standard difference between pointers
262 * is specified to be valid only within same object. This makes
263 * it formally challenging to determine if input and output buffers
264 * are not partially overlapping with standard pointer arithmetic.
269 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
271 * Then we have VMS that distinguishes itself by adhering to
272 * sizeof(size_t)==4 even in 64-bit builds, which means that
273 * difference between two pointers might be truncated to 32 bits.
274 * In the context one can even wonder how comparison for
275 * equality is implemented. To be on the safe side we adhere to
276 * PTRDIFF_T even for comparison for equality.
278 # define PTRDIFF_T uint64_t
280 # define PTRDIFF_T size_t
283 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
285 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
287 * Check for partially overlapping buffers. [Binary logical
288 * operations are used instead of boolean to minimize number
289 * of conditional branches.]
291 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
292 (diff > (0 - (PTRDIFF_T)len)));
297 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
298 unsigned char *out, int *outl,
299 const unsigned char *in, int inl)
301 int i, j, bl, cmpl = inl;
303 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
304 cmpl = (cmpl + 7) / 8;
306 bl = ctx->cipher->block_size;
309 * CCM mode needs to know about the case where inl == 0 && in == NULL - it
310 * means the plaintext/ciphertext length is 0
314 && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)) {
319 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
320 /* If block size > 1 then the cipher will have to do this check */
321 if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
322 EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
326 i = ctx->cipher->do_cipher(ctx, out, in, inl);
334 if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
335 EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
339 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
340 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
349 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
352 memcpy(&(ctx->buf[i]), in, inl);
358 memcpy(&(ctx->buf[i]), in, j);
361 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
371 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
377 memcpy(ctx->buf, &(in[inl]), i);
383 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
384 const unsigned char *in, int inl)
386 /* Prevent accidental use of decryption context when encrypting */
388 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
392 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
395 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
398 ret = EVP_EncryptFinal_ex(ctx, out, outl);
402 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
405 unsigned int i, b, bl;
407 /* Prevent accidental use of decryption context when encrypting */
409 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
413 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
414 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
422 b = ctx->cipher->block_size;
423 OPENSSL_assert(b <= sizeof(ctx->buf));
429 if (ctx->flags & EVP_CIPH_NO_PADDING) {
431 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
432 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
440 for (i = bl; i < b; i++)
442 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
450 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
451 const unsigned char *in, int inl)
453 int fix_len, cmpl = inl;
456 /* Prevent accidental use of encryption context when decrypting */
458 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
462 b = ctx->cipher->block_size;
464 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
465 cmpl = (cmpl + 7) / 8;
468 * CCM mode needs to know about the case where inl == 0 - it means the
469 * plaintext/ciphertext length is 0
473 && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)) {
478 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
479 if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
480 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
484 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
493 if (ctx->flags & EVP_CIPH_NO_PADDING)
494 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
496 OPENSSL_assert(b <= sizeof(ctx->final));
498 if (ctx->final_used) {
499 /* see comment about PTRDIFF_T comparison above */
500 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
501 || is_partially_overlapping(out, in, b)) {
502 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
505 memcpy(out, ctx->final, b);
511 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
515 * if we have 'decrypted' a multiple of block size, make sure we have a
516 * copy of this last block
518 if (b > 1 && !ctx->buf_len) {
521 memcpy(ctx->final, &out[*outl], b);
531 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
534 ret = EVP_DecryptFinal_ex(ctx, out, outl);
538 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
543 /* Prevent accidental use of encryption context when decrypting */
545 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
551 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
552 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
560 b = ctx->cipher->block_size;
561 if (ctx->flags & EVP_CIPH_NO_PADDING) {
563 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
564 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
571 if (ctx->buf_len || !ctx->final_used) {
572 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
575 OPENSSL_assert(b <= sizeof(ctx->final));
578 * The following assumes that the ciphertext has been authenticated.
579 * Otherwise it provides a padding oracle.
581 n = ctx->final[b - 1];
582 if (n == 0 || n > (int)b) {
583 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
586 for (i = 0; i < n; i++) {
587 if (ctx->final[--b] != n) {
588 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
592 n = ctx->cipher->block_size - n;
593 for (i = 0; i < n; i++)
594 out[i] = ctx->final[i];
601 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
603 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
604 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
605 if (c->key_len == keylen)
607 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
611 EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
615 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
618 ctx->flags &= ~EVP_CIPH_NO_PADDING;
620 ctx->flags |= EVP_CIPH_NO_PADDING;
624 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
629 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
633 if (!ctx->cipher->ctrl) {
634 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
638 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
640 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
641 EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
647 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
649 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
650 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
651 if (RAND_priv_bytes(key, ctx->key_len) <= 0)
656 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
658 if ((in == NULL) || (in->cipher == NULL)) {
659 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
662 #ifndef OPENSSL_NO_ENGINE
663 /* Make sure it's safe to copy a cipher context using an ENGINE */
664 if (in->engine && !ENGINE_init(in->engine)) {
665 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
670 EVP_CIPHER_CTX_reset(out);
671 memcpy(out, in, sizeof(*out));
673 if (in->cipher_data && in->cipher->ctx_size) {
674 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
675 if (out->cipher_data == NULL) {
677 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
680 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
683 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
684 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
686 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);