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/obj.h>
69 const EVP_CIPHER *EVP_get_cipherbynid(int nid) {
71 case NID_des_ede3_cbc:
72 return EVP_des_ede3_cbc();
76 return EVP_aes_128_cbc();
78 return EVP_aes_256_cbc();
84 void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) {
85 memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
88 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) {
89 EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
91 EVP_CIPHER_CTX_init(ctx);
96 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
97 const unsigned char *key, const unsigned char *iv, int enc) {
99 EVP_CIPHER_CTX_init(ctx);
101 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
104 int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) {
105 if (c->cipher != NULL && c->cipher->cleanup && !c->cipher->cleanup(c)) {
109 if (c->cipher_data) {
110 OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
111 OPENSSL_free(c->cipher_data);
114 memset(c, 0, sizeof(EVP_CIPHER_CTX));
118 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) {
120 EVP_CIPHER_CTX_cleanup(ctx);
125 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) {
126 if (in == NULL || in->cipher == NULL) {
127 OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_copy, CIPHER_R_INPUT_NOT_INITIALIZED);
131 EVP_CIPHER_CTX_cleanup(out);
132 memcpy(out, in, sizeof(EVP_CIPHER_CTX));
134 if (in->cipher_data && in->cipher->ctx_size) {
135 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
136 if (!out->cipher_data) {
137 OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_copy, ERR_R_MALLOC_FAILURE);
140 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
143 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) {
144 return in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out);
150 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
151 ENGINE *engine, const uint8_t *key, const uint8_t *iv,
163 /* Ensure a context left from last time is cleared (the previous check
164 * attempted to avoid this if the same ENGINE and EVP_CIPHER could be
167 EVP_CIPHER_CTX_cleanup(ctx);
168 /* Restore encrypt and flags */
172 ctx->cipher = cipher;
173 if (ctx->cipher->ctx_size) {
174 ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
175 if (!ctx->cipher_data) {
176 OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, ERR_R_MALLOC_FAILURE);
180 ctx->cipher_data = NULL;
183 ctx->key_len = cipher->key_len;
186 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
187 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
188 OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, CIPHER_R_INITIALIZATION_ERROR);
192 } else if (!ctx->cipher) {
193 OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, CIPHER_R_NO_CIPHER_SET);
197 /* we assume block size is a power of 2 in *cryptUpdate */
198 assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 ||
199 ctx->cipher->block_size == 16);
201 if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
202 switch (EVP_CIPHER_CTX_mode(ctx)) {
203 case EVP_CIPH_STREAM_CIPHER:
204 case EVP_CIPH_ECB_MODE:
207 case EVP_CIPH_CFB_MODE:
208 case EVP_CIPH_OFB_MODE:
212 case EVP_CIPH_CBC_MODE:
213 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
215 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
217 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
220 case EVP_CIPH_CTR_MODE:
222 /* Don't reuse IV for CTR mode */
224 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
233 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
234 if (!ctx->cipher->init(ctx, key, iv, enc)) {
241 ctx->block_mask = ctx->cipher->block_size - 1;
245 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
246 ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
247 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
250 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
251 ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
252 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
255 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
256 const uint8_t *in, int in_len) {
259 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
260 i = ctx->cipher->cipher(ctx, out, in, in_len);
274 if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) {
275 if (ctx->cipher->cipher(ctx, out, in, in_len)) {
285 bl = ctx->cipher->block_size;
286 assert(bl <= (int)sizeof(ctx->buf));
288 if (i + in_len < bl) {
289 memcpy(&ctx->buf[i], in, in_len);
290 ctx->buf_len += in_len;
295 memcpy(&ctx->buf[i], in, j);
296 if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) {
308 i = in_len & ctx->block_mask;
311 if (!ctx->cipher->cipher(ctx, out, in, in_len)) {
318 memcpy(ctx->buf, &in[in_len], i);
324 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
326 unsigned int i, b, bl;
328 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
329 ret = ctx->cipher->cipher(ctx, out, NULL, 0);
338 b = ctx->cipher->block_size;
339 assert(b <= sizeof(ctx->buf));
346 if (ctx->flags & EVP_CIPH_NO_PADDING) {
348 OPENSSL_PUT_ERROR(CIPHER, EVP_EncryptFinal_ex,
349 CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
357 for (i = bl; i < b; i++) {
360 ret = ctx->cipher->cipher(ctx, out, ctx->buf, b);
369 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
370 const uint8_t *in, int in_len) {
374 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
375 int r = ctx->cipher->cipher(ctx, out, in, in_len);
390 if (ctx->flags & EVP_CIPH_NO_PADDING) {
391 return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
394 b = ctx->cipher->block_size;
395 assert(b <= sizeof(ctx->final));
397 if (ctx->final_used) {
398 memcpy(out, ctx->final, b);
405 if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) {
409 /* if we have 'decrypted' a multiple of block size, make sure
410 * we have a copy of this last block */
411 if (b > 1 && !ctx->buf_len) {
414 memcpy(ctx->final, &out[*out_len], b);
426 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) {
431 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
432 i = ctx->cipher->cipher(ctx, out, NULL, 0);
441 b = ctx->cipher->block_size;
442 if (ctx->flags & EVP_CIPH_NO_PADDING) {
444 OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex,
445 CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
453 if (ctx->buf_len || !ctx->final_used) {
454 OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex,
455 CIPHER_R_WRONG_FINAL_BLOCK_LENGTH);
458 assert(b <= sizeof(ctx->final));
460 /* The following assumes that the ciphertext has been authenticated.
461 * Otherwise it provides a padding oracle. */
462 n = ctx->final[b - 1];
463 if (n == 0 || n > (int)b) {
464 OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex, CIPHER_R_BAD_DECRYPT);
468 for (i = 0; i < n; i++) {
469 if (ctx->final[--b] != n) {
470 OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex, CIPHER_R_BAD_DECRYPT);
475 n = ctx->cipher->block_size - n;
476 for (i = 0; i < n; i++) {
477 out[i] = ctx->final[i];
487 int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
489 return ctx->cipher->cipher(ctx, out, in, in_len);
492 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
493 const uint8_t *in, int in_len) {
495 return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
497 return EVP_DecryptUpdate(ctx, out, out_len, in, in_len);
501 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
503 return EVP_EncryptFinal_ex(ctx, out, out_len);
505 return EVP_DecryptFinal_ex(ctx, out, out_len);
509 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) {
513 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) {
514 return ctx->cipher->nid;
517 unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) {
518 return ctx->cipher->block_size;
521 unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) {
525 unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) {
526 return ctx->cipher->iv_len;
529 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) {
530 return ctx->app_data;
533 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) {
534 ctx->app_data = data;
537 uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) {
538 return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK;
541 uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) {
542 return ctx->cipher->flags & EVP_CIPH_MODE_MASK;
545 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) {
548 OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl, CIPHER_R_NO_CIPHER_SET);
552 if (!ctx->cipher->ctrl) {
553 OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl, CIPHER_R_CTRL_NOT_IMPLEMENTED);
557 ret = ctx->cipher->ctrl(ctx, command, arg, ptr);
559 OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl,
560 CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED);
567 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) {
569 ctx->flags &= ~EVP_CIPH_NO_PADDING;
571 ctx->flags |= EVP_CIPH_NO_PADDING;
576 int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { return cipher->nid; }
578 const char *EVP_CIPHER_name(const EVP_CIPHER *cipher) {
579 return OBJ_nid2sn(cipher->nid);
582 unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher) {
583 return cipher->block_size;
586 unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher) {
587 return cipher->key_len;
590 unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) {
591 return cipher->iv_len;
594 uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher) {
595 return cipher->flags & ~EVP_CIPH_MODE_MASK;
598 uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) {
599 return cipher->flags & EVP_CIPH_MODE_MASK;