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