1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
57 #include <openssl/cipher.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/nid.h>
67 #include "../../internal.h"
70 void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) {
71 OPENSSL_memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
74 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) {
75 EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
77 EVP_CIPHER_CTX_init(ctx);
82 int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) {
83 if (c->cipher != NULL && c->cipher->cleanup) {
84 c->cipher->cleanup(c);
86 OPENSSL_free(c->cipher_data);
88 OPENSSL_memset(c, 0, sizeof(EVP_CIPHER_CTX));
92 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) {
94 EVP_CIPHER_CTX_cleanup(ctx);
99 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) {
100 if (in == NULL || in->cipher == NULL) {
101 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INPUT_NOT_INITIALIZED);
105 EVP_CIPHER_CTX_cleanup(out);
106 OPENSSL_memcpy(out, in, sizeof(EVP_CIPHER_CTX));
108 if (in->cipher_data && in->cipher->ctx_size) {
109 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
110 if (!out->cipher_data) {
112 OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
115 OPENSSL_memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
118 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) {
119 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
128 void EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) {
129 EVP_CIPHER_CTX_cleanup(ctx);
130 EVP_CIPHER_CTX_init(ctx);
133 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
134 ENGINE *engine, const uint8_t *key, const uint8_t *iv,
146 // Ensure a context left from last time is cleared (the previous check
147 // attempted to avoid this if the same ENGINE and EVP_CIPHER could be
150 EVP_CIPHER_CTX_cleanup(ctx);
151 // Restore encrypt and flags
155 ctx->cipher = cipher;
156 if (ctx->cipher->ctx_size) {
157 ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
158 if (!ctx->cipher_data) {
160 OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
164 ctx->cipher_data = NULL;
167 ctx->key_len = cipher->key_len;
170 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
171 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
173 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INITIALIZATION_ERROR);
177 } else if (!ctx->cipher) {
178 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET);
182 // we assume block size is a power of 2 in *cryptUpdate
183 assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 ||
184 ctx->cipher->block_size == 16);
186 if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
187 switch (EVP_CIPHER_CTX_mode(ctx)) {
188 case EVP_CIPH_STREAM_CIPHER:
189 case EVP_CIPH_ECB_MODE:
192 case EVP_CIPH_CFB_MODE:
196 case EVP_CIPH_CBC_MODE:
197 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
199 OPENSSL_memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
201 OPENSSL_memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
204 case EVP_CIPH_CTR_MODE:
205 case EVP_CIPH_OFB_MODE:
207 // Don't reuse IV for CTR mode
209 OPENSSL_memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
218 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
219 if (!ctx->cipher->init(ctx, key, iv, enc)) {
226 ctx->block_mask = ctx->cipher->block_size - 1;
230 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
231 ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
232 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
235 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
236 ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
237 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
240 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
241 const uint8_t *in, int in_len) {
244 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
245 i = ctx->cipher->cipher(ctx, out, in, in_len);
259 if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) {
260 if (ctx->cipher->cipher(ctx, out, in, in_len)) {
270 bl = ctx->cipher->block_size;
271 assert(bl <= (int)sizeof(ctx->buf));
273 if (bl - i > in_len) {
274 OPENSSL_memcpy(&ctx->buf[i], in, in_len);
275 ctx->buf_len += in_len;
280 OPENSSL_memcpy(&ctx->buf[i], in, j);
281 if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) {
293 i = in_len & ctx->block_mask;
296 if (!ctx->cipher->cipher(ctx, out, in, in_len)) {
303 OPENSSL_memcpy(ctx->buf, &in[in_len], i);
309 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
311 unsigned int i, b, bl;
313 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
314 ret = ctx->cipher->cipher(ctx, out, NULL, 0);
323 b = ctx->cipher->block_size;
324 assert(b <= sizeof(ctx->buf));
331 if (ctx->flags & EVP_CIPH_NO_PADDING) {
333 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
341 for (i = bl; i < b; i++) {
344 ret = ctx->cipher->cipher(ctx, out, ctx->buf, b);
353 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
354 const uint8_t *in, int in_len) {
358 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
359 int r = ctx->cipher->cipher(ctx, out, in, in_len);
374 if (ctx->flags & EVP_CIPH_NO_PADDING) {
375 return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
378 b = ctx->cipher->block_size;
379 assert(b <= sizeof(ctx->final));
381 if (ctx->final_used) {
382 OPENSSL_memcpy(out, ctx->final, b);
389 if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) {
393 // if we have 'decrypted' a multiple of block size, make sure
394 // we have a copy of this last block
395 if (b > 1 && !ctx->buf_len) {
398 OPENSSL_memcpy(ctx->final, &out[*out_len], b);
410 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) {
415 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
416 i = ctx->cipher->cipher(ctx, out, NULL, 0);
425 b = ctx->cipher->block_size;
426 if (ctx->flags & EVP_CIPH_NO_PADDING) {
428 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
436 if (ctx->buf_len || !ctx->final_used) {
437 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_WRONG_FINAL_BLOCK_LENGTH);
440 assert(b <= sizeof(ctx->final));
442 // The following assumes that the ciphertext has been authenticated.
443 // Otherwise it provides a padding oracle.
444 n = ctx->final[b - 1];
445 if (n == 0 || n > (int)b) {
446 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
450 for (i = 0; i < n; i++) {
451 if (ctx->final[--b] != n) {
452 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
457 n = ctx->cipher->block_size - n;
458 for (i = 0; i < n; i++) {
459 out[i] = ctx->final[i];
469 int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
471 return ctx->cipher->cipher(ctx, out, in, in_len);
474 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
475 const uint8_t *in, int in_len) {
477 return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
479 return EVP_DecryptUpdate(ctx, out, out_len, in, in_len);
483 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
485 return EVP_EncryptFinal_ex(ctx, out, out_len);
487 return EVP_DecryptFinal_ex(ctx, out, out_len);
491 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) {
495 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) {
496 return ctx->cipher->nid;
499 unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) {
500 return ctx->cipher->block_size;
503 unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) {
507 unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) {
508 return ctx->cipher->iv_len;
511 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) {
512 return ctx->app_data;
515 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) {
516 ctx->app_data = data;
519 uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) {
520 return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK;
523 uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) {
524 return ctx->cipher->flags & EVP_CIPH_MODE_MASK;
527 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) {
530 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET);
534 if (!ctx->cipher->ctrl) {
535 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_NOT_IMPLEMENTED);
539 ret = ctx->cipher->ctrl(ctx, command, arg, ptr);
541 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED);
548 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) {
550 ctx->flags &= ~EVP_CIPH_NO_PADDING;
552 ctx->flags |= EVP_CIPH_NO_PADDING;
557 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, unsigned key_len) {
558 if (c->key_len == key_len) {
562 if (key_len == 0 || !(c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
563 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_KEY_LENGTH);
567 c->key_len = key_len;
571 int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { return cipher->nid; }
573 unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher) {
574 return cipher->block_size;
577 unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher) {
578 return cipher->key_len;
581 unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) {
582 return cipher->iv_len;
585 uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher) {
586 return cipher->flags & ~EVP_CIPH_MODE_MASK;
589 uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) {
590 return cipher->flags & EVP_CIPH_MODE_MASK;
593 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
594 const uint8_t *key, const uint8_t *iv, int enc) {
596 EVP_CIPHER_CTX_init(ctx);
598 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
601 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
602 const uint8_t *key, const uint8_t *iv) {
603 return EVP_CipherInit(ctx, cipher, key, iv, 1);
606 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
607 const uint8_t *key, const uint8_t *iv) {
608 return EVP_CipherInit(ctx, cipher, key, iv, 0);
611 int EVP_add_cipher_alias(const char *a, const char *b) {
615 void EVP_CIPHER_CTX_set_flags(const EVP_CIPHER_CTX *ctx, uint32_t flags) {}