Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 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. |
| 7 | * |
| 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). |
| 14 | * |
| 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. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 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)" |
| 39 | * |
| 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 |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 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.] */ |
| 56 | |
| 57 | #include <openssl/cipher.h> |
| 58 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 59 | #include <assert.h> |
Adam Langley | 704453f | 2014-09-23 16:19:16 -0700 | [diff] [blame] | 60 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | |
| 62 | #include <openssl/err.h> |
| 63 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 64 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 65 | |
| 66 | #include "internal.h" |
Adam Langley | 2e2a226 | 2017-05-03 13:23:37 -0700 | [diff] [blame] | 67 | #include "../../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | |
| 69 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 70 | void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 71 | OPENSSL_memset(ctx, 0, sizeof(EVP_CIPHER_CTX)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) { |
| 75 | EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX)); |
| 76 | if (ctx) { |
| 77 | EVP_CIPHER_CTX_init(ctx); |
| 78 | } |
| 79 | return ctx; |
| 80 | } |
| 81 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 82 | int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) { |
Martin Kreichgauer | 6dc892f | 2017-08-30 10:49:05 -0700 | [diff] [blame^] | 83 | if (c->cipher != NULL && c->cipher->cleanup) { |
| 84 | c->cipher->cleanup(c); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | } |
David Benjamin | 3fa65f0 | 2015-05-15 19:11:57 -0400 | [diff] [blame] | 86 | OPENSSL_free(c->cipher_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 88 | OPENSSL_memset(c, 0, sizeof(EVP_CIPHER_CTX)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) { |
| 93 | if (ctx) { |
| 94 | EVP_CIPHER_CTX_cleanup(ctx); |
| 95 | OPENSSL_free(ctx); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) { |
| 100 | if (in == NULL || in->cipher == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 101 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INPUT_NOT_INITIALIZED); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | EVP_CIPHER_CTX_cleanup(out); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 106 | OPENSSL_memcpy(out, in, sizeof(EVP_CIPHER_CTX)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 107 | |
| 108 | if (in->cipher_data && in->cipher->ctx_size) { |
| 109 | out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size); |
| 110 | if (!out->cipher_data) { |
David Benjamin | ec1d963 | 2017-02-09 17:45:33 -0500 | [diff] [blame] | 111 | out->cipher = NULL; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 112 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 115 | OPENSSL_memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Adam Langley | 7578f3f | 2014-07-24 17:42:11 -0700 | [diff] [blame] | 118 | if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) { |
David Benjamin | ec1d963 | 2017-02-09 17:45:33 -0500 | [diff] [blame] | 119 | if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) { |
| 120 | out->cipher = NULL; |
| 121 | return 0; |
| 122 | } |
Adam Langley | 7578f3f | 2014-07-24 17:42:11 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 129 | ENGINE *engine, const uint8_t *key, const uint8_t *iv, |
| 130 | int enc) { |
| 131 | if (enc == -1) { |
| 132 | enc = ctx->encrypt; |
| 133 | } else { |
| 134 | if (enc) { |
| 135 | enc = 1; |
| 136 | } |
| 137 | ctx->encrypt = enc; |
| 138 | } |
| 139 | |
| 140 | if (cipher) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 141 | // Ensure a context left from last time is cleared (the previous check |
| 142 | // attempted to avoid this if the same ENGINE and EVP_CIPHER could be |
| 143 | // used). |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | if (ctx->cipher) { |
| 145 | EVP_CIPHER_CTX_cleanup(ctx); |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 146 | // Restore encrypt and flags |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | ctx->encrypt = enc; |
| 148 | } |
| 149 | |
| 150 | ctx->cipher = cipher; |
| 151 | if (ctx->cipher->ctx_size) { |
| 152 | ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size); |
| 153 | if (!ctx->cipher_data) { |
David Benjamin | 3fa65f0 | 2015-05-15 19:11:57 -0400 | [diff] [blame] | 154 | ctx->cipher = NULL; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 155 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | } else { |
| 159 | ctx->cipher_data = NULL; |
| 160 | } |
| 161 | |
| 162 | ctx->key_len = cipher->key_len; |
| 163 | ctx->flags = 0; |
| 164 | |
| 165 | if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { |
| 166 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) { |
David Benjamin | 3fa65f0 | 2015-05-15 19:11:57 -0400 | [diff] [blame] | 167 | ctx->cipher = NULL; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 168 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INITIALIZATION_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | } |
| 172 | } else if (!ctx->cipher) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 173 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 177 | // we assume block size is a power of 2 in *cryptUpdate |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 178 | assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 || |
| 179 | ctx->cipher->block_size == 16); |
| 180 | |
| 181 | if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) { |
| 182 | switch (EVP_CIPHER_CTX_mode(ctx)) { |
| 183 | case EVP_CIPH_STREAM_CIPHER: |
| 184 | case EVP_CIPH_ECB_MODE: |
| 185 | break; |
| 186 | |
| 187 | case EVP_CIPH_CFB_MODE: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | ctx->num = 0; |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 189 | // fall-through |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
| 191 | case EVP_CIPH_CBC_MODE: |
| 192 | assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv)); |
| 193 | if (iv) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 194 | OPENSSL_memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 195 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 196 | OPENSSL_memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | break; |
| 198 | |
| 199 | case EVP_CIPH_CTR_MODE: |
Adam Langley | 087930f | 2015-02-24 12:44:40 -0800 | [diff] [blame] | 200 | case EVP_CIPH_OFB_MODE: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | ctx->num = 0; |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 202 | // Don't reuse IV for CTR mode |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | if (iv) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 204 | OPENSSL_memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | } |
| 206 | break; |
| 207 | |
| 208 | default: |
| 209 | return 0; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { |
| 214 | if (!ctx->cipher->init(ctx, key, iv, enc)) { |
| 215 | return 0; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | ctx->buf_len = 0; |
| 220 | ctx->final_used = 0; |
| 221 | ctx->block_mask = ctx->cipher->block_size - 1; |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 226 | ENGINE *impl, const uint8_t *key, const uint8_t *iv) { |
| 227 | return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); |
| 228 | } |
| 229 | |
| 230 | int EVP_DecryptInit_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, 0); |
| 233 | } |
| 234 | |
| 235 | int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
| 236 | const uint8_t *in, int in_len) { |
| 237 | int i, j, bl; |
| 238 | |
| 239 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
| 240 | i = ctx->cipher->cipher(ctx, out, in, in_len); |
| 241 | if (i < 0) { |
| 242 | return 0; |
| 243 | } else { |
| 244 | *out_len = i; |
| 245 | } |
| 246 | return 1; |
| 247 | } |
| 248 | |
| 249 | if (in_len <= 0) { |
| 250 | *out_len = 0; |
| 251 | return in_len == 0; |
| 252 | } |
| 253 | |
| 254 | if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) { |
| 255 | if (ctx->cipher->cipher(ctx, out, in, in_len)) { |
| 256 | *out_len = in_len; |
| 257 | return 1; |
| 258 | } else { |
| 259 | *out_len = 0; |
| 260 | return 0; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | i = ctx->buf_len; |
| 265 | bl = ctx->cipher->block_size; |
| 266 | assert(bl <= (int)sizeof(ctx->buf)); |
| 267 | if (i != 0) { |
David Benjamin | 204dea8 | 2016-05-03 07:42:19 -0400 | [diff] [blame] | 268 | if (bl - i > in_len) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 269 | OPENSSL_memcpy(&ctx->buf[i], in, in_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | ctx->buf_len += in_len; |
| 271 | *out_len = 0; |
| 272 | return 1; |
| 273 | } else { |
| 274 | j = bl - i; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 275 | OPENSSL_memcpy(&ctx->buf[i], in, j); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) { |
| 277 | return 0; |
| 278 | } |
| 279 | in_len -= j; |
| 280 | in += j; |
| 281 | out += bl; |
| 282 | *out_len = bl; |
| 283 | } |
| 284 | } else { |
| 285 | *out_len = 0; |
| 286 | } |
| 287 | |
| 288 | i = in_len & ctx->block_mask; |
| 289 | in_len -= i; |
| 290 | if (in_len > 0) { |
| 291 | if (!ctx->cipher->cipher(ctx, out, in, in_len)) { |
| 292 | return 0; |
| 293 | } |
| 294 | *out_len += in_len; |
| 295 | } |
| 296 | |
| 297 | if (i != 0) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 298 | OPENSSL_memcpy(ctx->buf, &in[in_len], i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | } |
| 300 | ctx->buf_len = i; |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
| 305 | int n, ret; |
| 306 | unsigned int i, b, bl; |
| 307 | |
| 308 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
| 309 | ret = ctx->cipher->cipher(ctx, out, NULL, 0); |
| 310 | if (ret < 0) { |
| 311 | return 0; |
| 312 | } else { |
| 313 | *out_len = ret; |
| 314 | } |
| 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | b = ctx->cipher->block_size; |
| 319 | assert(b <= sizeof(ctx->buf)); |
| 320 | if (b == 1) { |
| 321 | *out_len = 0; |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | bl = ctx->buf_len; |
| 326 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
| 327 | if (bl) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 328 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | *out_len = 0; |
| 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | n = b - bl; |
| 336 | for (i = bl; i < b; i++) { |
| 337 | ctx->buf[i] = n; |
| 338 | } |
| 339 | ret = ctx->cipher->cipher(ctx, out, ctx->buf, b); |
| 340 | |
| 341 | if (ret) { |
| 342 | *out_len = b; |
| 343 | } |
| 344 | |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
| 349 | const uint8_t *in, int in_len) { |
| 350 | int fix_len; |
| 351 | unsigned int b; |
| 352 | |
| 353 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
| 354 | int r = ctx->cipher->cipher(ctx, out, in, in_len); |
| 355 | if (r < 0) { |
| 356 | *out_len = 0; |
| 357 | return 0; |
| 358 | } else { |
| 359 | *out_len = r; |
| 360 | } |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | if (in_len <= 0) { |
| 365 | *out_len = 0; |
| 366 | return in_len == 0; |
| 367 | } |
| 368 | |
| 369 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
| 370 | return EVP_EncryptUpdate(ctx, out, out_len, in, in_len); |
| 371 | } |
| 372 | |
| 373 | b = ctx->cipher->block_size; |
| 374 | assert(b <= sizeof(ctx->final)); |
| 375 | |
| 376 | if (ctx->final_used) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 377 | OPENSSL_memcpy(out, ctx->final, b); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | out += b; |
| 379 | fix_len = 1; |
| 380 | } else { |
| 381 | fix_len = 0; |
| 382 | } |
| 383 | |
| 384 | if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) { |
| 385 | return 0; |
| 386 | } |
| 387 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 388 | // if we have 'decrypted' a multiple of block size, make sure |
| 389 | // we have a copy of this last block |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | if (b > 1 && !ctx->buf_len) { |
| 391 | *out_len -= b; |
| 392 | ctx->final_used = 1; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 393 | OPENSSL_memcpy(ctx->final, &out[*out_len], b); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 394 | } else { |
| 395 | ctx->final_used = 0; |
| 396 | } |
| 397 | |
| 398 | if (fix_len) { |
| 399 | *out_len += b; |
| 400 | } |
| 401 | |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) { |
| 406 | int i, n; |
| 407 | unsigned int b; |
| 408 | *out_len = 0; |
| 409 | |
| 410 | if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { |
| 411 | i = ctx->cipher->cipher(ctx, out, NULL, 0); |
| 412 | if (i < 0) { |
| 413 | return 0; |
| 414 | } else { |
| 415 | *out_len = i; |
| 416 | } |
| 417 | return 1; |
| 418 | } |
| 419 | |
| 420 | b = ctx->cipher->block_size; |
| 421 | if (ctx->flags & EVP_CIPH_NO_PADDING) { |
| 422 | if (ctx->buf_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 423 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | *out_len = 0; |
| 427 | return 1; |
| 428 | } |
| 429 | |
| 430 | if (b > 1) { |
| 431 | if (ctx->buf_len || !ctx->final_used) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 432 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_WRONG_FINAL_BLOCK_LENGTH); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | assert(b <= sizeof(ctx->final)); |
| 436 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 437 | // The following assumes that the ciphertext has been authenticated. |
| 438 | // Otherwise it provides a padding oracle. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 439 | n = ctx->final[b - 1]; |
| 440 | if (n == 0 || n > (int)b) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 441 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | for (i = 0; i < n; i++) { |
| 446 | if (ctx->final[--b] != n) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 447 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | n = ctx->cipher->block_size - n; |
| 453 | for (i = 0; i < n; i++) { |
| 454 | out[i] = ctx->final[i]; |
| 455 | } |
| 456 | *out_len = n; |
| 457 | } else { |
| 458 | *out_len = 0; |
| 459 | } |
| 460 | |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 465 | size_t in_len) { |
| 466 | return ctx->cipher->cipher(ctx, out, in, in_len); |
| 467 | } |
| 468 | |
| 469 | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len, |
| 470 | const uint8_t *in, int in_len) { |
| 471 | if (ctx->encrypt) { |
| 472 | return EVP_EncryptUpdate(ctx, out, out_len, in, in_len); |
| 473 | } else { |
| 474 | return EVP_DecryptUpdate(ctx, out, out_len, in, in_len); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) { |
| 479 | if (ctx->encrypt) { |
| 480 | return EVP_EncryptFinal_ex(ctx, out, out_len); |
| 481 | } else { |
| 482 | return EVP_DecryptFinal_ex(ctx, out, out_len); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) { |
| 487 | return ctx->cipher; |
| 488 | } |
| 489 | |
| 490 | int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) { |
| 491 | return ctx->cipher->nid; |
| 492 | } |
| 493 | |
| 494 | unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) { |
| 495 | return ctx->cipher->block_size; |
| 496 | } |
| 497 | |
| 498 | unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) { |
| 499 | return ctx->key_len; |
| 500 | } |
| 501 | |
| 502 | unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) { |
| 503 | return ctx->cipher->iv_len; |
| 504 | } |
| 505 | |
| 506 | void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) { |
| 507 | return ctx->app_data; |
| 508 | } |
| 509 | |
| 510 | void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) { |
| 511 | ctx->app_data = data; |
| 512 | } |
| 513 | |
| 514 | uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) { |
| 515 | return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK; |
| 516 | } |
| 517 | |
| 518 | uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) { |
| 519 | return ctx->cipher->flags & EVP_CIPH_MODE_MASK; |
| 520 | } |
| 521 | |
| 522 | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) { |
| 523 | int ret; |
| 524 | if (!ctx->cipher) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 525 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | if (!ctx->cipher->ctrl) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 530 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_NOT_IMPLEMENTED); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | ret = ctx->cipher->ctrl(ctx, command, arg, ptr); |
| 535 | if (ret == -1) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 536 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) { |
| 544 | if (pad) { |
| 545 | ctx->flags &= ~EVP_CIPH_NO_PADDING; |
| 546 | } else { |
| 547 | ctx->flags |= EVP_CIPH_NO_PADDING; |
| 548 | } |
| 549 | return 1; |
| 550 | } |
| 551 | |
Adam Langley | 539112f | 2014-08-22 11:06:41 -0700 | [diff] [blame] | 552 | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, unsigned key_len) { |
| 553 | if (c->key_len == key_len) { |
| 554 | return 1; |
| 555 | } |
| 556 | |
| 557 | if (key_len == 0 || !(c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 558 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_KEY_LENGTH); |
Adam Langley | 539112f | 2014-08-22 11:06:41 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | c->key_len = key_len; |
| 563 | return 1; |
| 564 | } |
| 565 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 566 | int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { return cipher->nid; } |
| 567 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher) { |
| 569 | return cipher->block_size; |
| 570 | } |
| 571 | |
| 572 | unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher) { |
| 573 | return cipher->key_len; |
| 574 | } |
| 575 | |
| 576 | unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) { |
| 577 | return cipher->iv_len; |
| 578 | } |
| 579 | |
| 580 | uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher) { |
| 581 | return cipher->flags & ~EVP_CIPH_MODE_MASK; |
| 582 | } |
| 583 | |
| 584 | uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) { |
| 585 | return cipher->flags & EVP_CIPH_MODE_MASK; |
| 586 | } |
Adam Langley | 704453f | 2014-09-23 16:19:16 -0700 | [diff] [blame] | 587 | |
| 588 | int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 589 | const uint8_t *key, const uint8_t *iv, int enc) { |
| 590 | if (cipher) { |
| 591 | EVP_CIPHER_CTX_init(ctx); |
| 592 | } |
| 593 | return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc); |
| 594 | } |
| 595 | |
| 596 | int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 597 | const uint8_t *key, const uint8_t *iv) { |
| 598 | return EVP_CipherInit(ctx, cipher, key, iv, 1); |
| 599 | } |
| 600 | |
| 601 | int EVP_DecryptInit(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, 0); |
| 604 | } |
| 605 | |
| 606 | int EVP_add_cipher_alias(const char *a, const char *b) { |
| 607 | return 1; |
| 608 | } |