blob: 00d8390a1be07735e761b8a2a66e8eeb94169248 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Langley95c29f32014-06-20 12:00:00 -070059#include <assert.h>
Adam Langley704453f2014-09-23 16:19:16 -070060#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070061
62#include <openssl/err.h>
63#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -040064#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -070065
66#include "internal.h"
Adam Langley2e2a2262017-05-03 13:23:37 -070067#include "../../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070068
69
Adam Langley95c29f32014-06-20 12:00:00 -070070void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) {
David Benjamin17cf2cb2016-12-13 01:07:13 -050071 OPENSSL_memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -070072}
73
74EVP_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 Langley95c29f32014-06-20 12:00:00 -070082int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) {
Martin Kreichgauer6dc892f2017-08-30 10:49:05 -070083 if (c->cipher != NULL && c->cipher->cleanup) {
84 c->cipher->cleanup(c);
Adam Langley95c29f32014-06-20 12:00:00 -070085 }
David Benjamin3fa65f02015-05-15 19:11:57 -040086 OPENSSL_free(c->cipher_data);
Adam Langley95c29f32014-06-20 12:00:00 -070087
David Benjamin17cf2cb2016-12-13 01:07:13 -050088 OPENSSL_memset(c, 0, sizeof(EVP_CIPHER_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -070089 return 1;
90}
91
92void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) {
93 if (ctx) {
94 EVP_CIPHER_CTX_cleanup(ctx);
95 OPENSSL_free(ctx);
96 }
97}
98
99int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) {
100 if (in == NULL || in->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400101 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INPUT_NOT_INITIALIZED);
Adam Langley95c29f32014-06-20 12:00:00 -0700102 return 0;
103 }
104
105 EVP_CIPHER_CTX_cleanup(out);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500106 OPENSSL_memcpy(out, in, sizeof(EVP_CIPHER_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -0700107
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 Benjaminec1d9632017-02-09 17:45:33 -0500111 out->cipher = NULL;
David Benjamin3570d732015-06-29 00:28:17 -0400112 OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700113 return 0;
114 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500115 OPENSSL_memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700116 }
117
Adam Langley7578f3f2014-07-24 17:42:11 -0700118 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) {
David Benjaminec1d9632017-02-09 17:45:33 -0500119 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
120 out->cipher = NULL;
121 return 0;
122 }
Adam Langley7578f3f2014-07-24 17:42:11 -0700123 }
124
Adam Langley95c29f32014-06-20 12:00:00 -0700125 return 1;
126}
127
128int 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 Benjamin808f8322017-08-18 14:06:02 -0400141 // 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 Langley95c29f32014-06-20 12:00:00 -0700144 if (ctx->cipher) {
145 EVP_CIPHER_CTX_cleanup(ctx);
David Benjamin808f8322017-08-18 14:06:02 -0400146 // Restore encrypt and flags
Adam Langley95c29f32014-06-20 12:00:00 -0700147 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 Benjamin3fa65f02015-05-15 19:11:57 -0400154 ctx->cipher = NULL;
David Benjamin3570d732015-06-29 00:28:17 -0400155 OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700156 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 Benjamin3fa65f02015-05-15 19:11:57 -0400167 ctx->cipher = NULL;
David Benjamin3570d732015-06-29 00:28:17 -0400168 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INITIALIZATION_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700169 return 0;
170 }
171 }
172 } else if (!ctx->cipher) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET);
Adam Langley95c29f32014-06-20 12:00:00 -0700174 return 0;
175 }
176
David Benjamin808f8322017-08-18 14:06:02 -0400177 // we assume block size is a power of 2 in *cryptUpdate
Adam Langley95c29f32014-06-20 12:00:00 -0700178 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 Langley95c29f32014-06-20 12:00:00 -0700188 ctx->num = 0;
David Benjamin808f8322017-08-18 14:06:02 -0400189 // fall-through
Adam Langley95c29f32014-06-20 12:00:00 -0700190
191 case EVP_CIPH_CBC_MODE:
192 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
193 if (iv) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500194 OPENSSL_memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
Adam Langley95c29f32014-06-20 12:00:00 -0700195 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500196 OPENSSL_memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
Adam Langley95c29f32014-06-20 12:00:00 -0700197 break;
198
199 case EVP_CIPH_CTR_MODE:
Adam Langley087930f2015-02-24 12:44:40 -0800200 case EVP_CIPH_OFB_MODE:
Adam Langley95c29f32014-06-20 12:00:00 -0700201 ctx->num = 0;
David Benjamin808f8322017-08-18 14:06:02 -0400202 // Don't reuse IV for CTR mode
Adam Langley95c29f32014-06-20 12:00:00 -0700203 if (iv) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500204 OPENSSL_memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
Adam Langley95c29f32014-06-20 12:00:00 -0700205 }
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
225int 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
230int 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
235int 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 Benjamin204dea82016-05-03 07:42:19 -0400268 if (bl - i > in_len) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500269 OPENSSL_memcpy(&ctx->buf[i], in, in_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700270 ctx->buf_len += in_len;
271 *out_len = 0;
272 return 1;
273 } else {
274 j = bl - i;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500275 OPENSSL_memcpy(&ctx->buf[i], in, j);
Adam Langley95c29f32014-06-20 12:00:00 -0700276 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 Benjamin17cf2cb2016-12-13 01:07:13 -0500298 OPENSSL_memcpy(ctx->buf, &in[in_len], i);
Adam Langley95c29f32014-06-20 12:00:00 -0700299 }
300 ctx->buf_len = i;
301 return 1;
302}
303
304int 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 Benjamin3570d732015-06-29 00:28:17 -0400328 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700329 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
348int 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 Benjamin17cf2cb2016-12-13 01:07:13 -0500377 OPENSSL_memcpy(out, ctx->final, b);
Adam Langley95c29f32014-06-20 12:00:00 -0700378 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 Benjamin808f8322017-08-18 14:06:02 -0400388 // if we have 'decrypted' a multiple of block size, make sure
389 // we have a copy of this last block
Adam Langley95c29f32014-06-20 12:00:00 -0700390 if (b > 1 && !ctx->buf_len) {
391 *out_len -= b;
392 ctx->final_used = 1;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500393 OPENSSL_memcpy(ctx->final, &out[*out_len], b);
Adam Langley95c29f32014-06-20 12:00:00 -0700394 } else {
395 ctx->final_used = 0;
396 }
397
398 if (fix_len) {
399 *out_len += b;
400 }
401
402 return 1;
403}
404
405int 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 Benjamin3570d732015-06-29 00:28:17 -0400423 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700424 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 Benjamin3570d732015-06-29 00:28:17 -0400432 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_WRONG_FINAL_BLOCK_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700433 return 0;
434 }
435 assert(b <= sizeof(ctx->final));
436
David Benjamin808f8322017-08-18 14:06:02 -0400437 // The following assumes that the ciphertext has been authenticated.
438 // Otherwise it provides a padding oracle.
Adam Langley95c29f32014-06-20 12:00:00 -0700439 n = ctx->final[b - 1];
440 if (n == 0 || n > (int)b) {
David Benjamin3570d732015-06-29 00:28:17 -0400441 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
Adam Langley95c29f32014-06-20 12:00:00 -0700442 return 0;
443 }
444
445 for (i = 0; i < n; i++) {
446 if (ctx->final[--b] != n) {
David Benjamin3570d732015-06-29 00:28:17 -0400447 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
Adam Langley95c29f32014-06-20 12:00:00 -0700448 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
464int 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
469int 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
478int 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
486const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) {
487 return ctx->cipher;
488}
489
490int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) {
491 return ctx->cipher->nid;
492}
493
494unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) {
495 return ctx->cipher->block_size;
496}
497
498unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) {
499 return ctx->key_len;
500}
501
502unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) {
503 return ctx->cipher->iv_len;
504}
505
506void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) {
507 return ctx->app_data;
508}
509
510void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) {
511 ctx->app_data = data;
512}
513
514uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) {
515 return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK;
516}
517
518uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) {
519 return ctx->cipher->flags & EVP_CIPH_MODE_MASK;
520}
521
522int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) {
523 int ret;
524 if (!ctx->cipher) {
David Benjamin3570d732015-06-29 00:28:17 -0400525 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET);
Adam Langley95c29f32014-06-20 12:00:00 -0700526 return 0;
527 }
528
529 if (!ctx->cipher->ctrl) {
David Benjamin3570d732015-06-29 00:28:17 -0400530 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_NOT_IMPLEMENTED);
Adam Langley95c29f32014-06-20 12:00:00 -0700531 return 0;
532 }
533
534 ret = ctx->cipher->ctrl(ctx, command, arg, ptr);
535 if (ret == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400536 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED);
Adam Langley95c29f32014-06-20 12:00:00 -0700537 return 0;
538 }
539
540 return ret;
541}
542
543int 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 Langley539112f2014-08-22 11:06:41 -0700552int 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 Benjamin3570d732015-06-29 00:28:17 -0400558 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_KEY_LENGTH);
Adam Langley539112f2014-08-22 11:06:41 -0700559 return 0;
560 }
561
562 c->key_len = key_len;
563 return 1;
564}
565
Adam Langley95c29f32014-06-20 12:00:00 -0700566int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { return cipher->nid; }
567
Adam Langley95c29f32014-06-20 12:00:00 -0700568unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher) {
569 return cipher->block_size;
570}
571
572unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher) {
573 return cipher->key_len;
574}
575
576unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) {
577 return cipher->iv_len;
578}
579
580uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher) {
581 return cipher->flags & ~EVP_CIPH_MODE_MASK;
582}
583
584uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) {
585 return cipher->flags & EVP_CIPH_MODE_MASK;
586}
Adam Langley704453f2014-09-23 16:19:16 -0700587
588int 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
596int 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
601int 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
606int EVP_add_cipher_alias(const char *a, const char *b) {
607 return 1;
608}