2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/evp.h>
15 #include "internal/bio.h"
17 static int enc_write(BIO *h, const char *buf, int num);
18 static int enc_read(BIO *h, char *buf, int size);
20 * static int enc_puts(BIO *h, const char *str);
23 * static int enc_gets(BIO *h, char *str, int size);
25 static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
26 static int enc_new(BIO *h);
27 static int enc_free(BIO *data);
28 static long enc_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fps);
29 #define ENC_BLOCK_SIZE (1024*4)
30 #define ENC_MIN_CHUNK (256)
31 #define BUF_OFFSET (ENC_MIN_CHUNK + EVP_MAX_BLOCK_LENGTH)
33 typedef struct enc_struct {
36 int cont; /* <= 0 when finished */
38 int ok; /* bad decrypt */
39 EVP_CIPHER_CTX *cipher;
40 unsigned char *read_start, *read_end;
42 * buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate can return
43 * up to a block more data than is presented to it
45 unsigned char buf[BUF_OFFSET + ENC_BLOCK_SIZE];
48 static const BIO_METHOD methods_enc = {
61 const BIO_METHOD *BIO_f_cipher(void)
63 return (&methods_enc);
66 static int enc_new(BIO *bi)
70 ctx = OPENSSL_zalloc(sizeof(*ctx));
74 ctx->cipher = EVP_CIPHER_CTX_new();
75 if (ctx->cipher == NULL) {
81 ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
82 BIO_set_data(bi, ctx);
88 static int enc_free(BIO *a)
99 EVP_CIPHER_CTX_free(b->cipher);
100 OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
101 BIO_set_data(a, NULL);
107 static int enc_read(BIO *b, char *out, int outl)
109 int ret = 0, i, blocksize;
115 ctx = BIO_get_data(b);
118 if ((ctx == NULL) || (next == NULL))
121 /* First check if there are bytes decoded/encoded */
122 if (ctx->buf_len > 0) {
123 i = ctx->buf_len - ctx->buf_off;
126 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
131 if (ctx->buf_len == ctx->buf_off) {
137 blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
142 * At this point, we have room of outl bytes and an empty buffer, so we
143 * should read in some more.
150 if (ctx->read_start == ctx->read_end) { /* time to read more data */
151 ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
152 i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
156 i = ctx->read_end - ctx->read_start;
160 /* Should be continue next time we are called? */
161 if (!BIO_should_retry(next)) {
163 i = EVP_CipherFinal_ex(ctx->cipher,
164 ctx->buf, &(ctx->buf_len));
168 ret = (ret == 0) ? i : ret;
172 if (outl > ENC_MIN_CHUNK) {
174 * Depending on flags block cipher decrypt can write
175 * one extra block and then back off, i.e. output buffer
176 * has to accommodate extra block...
178 int j = outl - blocksize, buf_len;
180 if (!EVP_CipherUpdate(ctx->cipher,
181 (unsigned char *)out, &buf_len,
182 ctx->read_start, i > j ? j : i)) {
183 BIO_clear_retry_flags(b);
191 ctx->read_start = ctx->read_end;
194 ctx->read_start += j;
196 if (i > ENC_MIN_CHUNK)
198 if (!EVP_CipherUpdate(ctx->cipher,
199 ctx->buf, &ctx->buf_len,
200 ctx->read_start, i)) {
201 BIO_clear_retry_flags(b);
205 ctx->read_start += i;
208 * Note: it is possible for EVP_CipherUpdate to decrypt zero
209 * bytes because this is or looks like the final block: if this
210 * happens we should retry and either read more data or decrypt
213 if (ctx->buf_len == 0)
217 if (ctx->buf_len <= outl)
223 memcpy(out, ctx->buf, i);
230 BIO_clear_retry_flags(b);
231 BIO_copy_next_retry(b);
232 return ((ret == 0) ? ctx->cont : ret);
235 static int enc_write(BIO *b, const char *in, int inl)
241 ctx = BIO_get_data(b);
243 if ((ctx == NULL) || (next == NULL))
248 BIO_clear_retry_flags(b);
249 n = ctx->buf_len - ctx->buf_off;
251 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
253 BIO_copy_next_retry(b);
259 /* at this point all pending data has been written */
261 if ((in == NULL) || (inl <= 0))
266 n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
267 if (!EVP_CipherUpdate(ctx->cipher,
268 ctx->buf, &ctx->buf_len,
269 (const unsigned char *)in, n)) {
270 BIO_clear_retry_flags(b);
280 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
282 BIO_copy_next_retry(b);
283 return (ret == inl) ? i : ret - inl;
291 BIO_copy_next_retry(b);
295 static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
298 BIO_ENC_CTX *ctx, *dctx;
301 EVP_CIPHER_CTX **c_ctx;
304 ctx = BIO_get_data(b);
313 if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
314 EVP_CIPHER_CTX_encrypting(ctx->cipher)))
316 ret = BIO_ctrl(next, cmd, num, ptr);
318 case BIO_CTRL_EOF: /* More to read */
322 ret = BIO_ctrl(next, cmd, num, ptr);
324 case BIO_CTRL_WPENDING:
325 ret = ctx->buf_len - ctx->buf_off;
327 ret = BIO_ctrl(next, cmd, num, ptr);
329 case BIO_CTRL_PENDING: /* More to read in buffer */
330 ret = ctx->buf_len - ctx->buf_off;
332 ret = BIO_ctrl(next, cmd, num, ptr);
335 /* do a final write */
337 while (ctx->buf_len != ctx->buf_off) {
338 i = enc_write(b, NULL, 0);
343 if (!ctx->finished) {
346 ret = EVP_CipherFinal_ex(ctx->cipher,
347 (unsigned char *)ctx->buf,
353 /* push out the bytes */
357 /* Finally flush the underlying BIO */
358 ret = BIO_ctrl(next, cmd, num, ptr);
360 case BIO_C_GET_CIPHER_STATUS:
363 case BIO_C_DO_STATE_MACHINE:
364 BIO_clear_retry_flags(b);
365 ret = BIO_ctrl(next, cmd, num, ptr);
366 BIO_copy_next_retry(b);
368 case BIO_C_GET_CIPHER_CTX:
369 c_ctx = (EVP_CIPHER_CTX **)ptr;
370 *c_ctx = ctx->cipher;
375 dctx = BIO_get_data(dbio);
376 dctx->cipher = EVP_CIPHER_CTX_new();
377 if (dctx->cipher == NULL)
379 ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
381 BIO_set_init(dbio, 1);
384 ret = BIO_ctrl(next, cmd, num, ptr);
390 static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
393 BIO *next = BIO_next(b);
399 ret = BIO_callback_ctrl(next, cmd, fp);
406 void BIO_set_cipher_ctx(b,c)
410 if (b == NULL) return;
412 if ((b->callback != NULL) &&
413 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
417 ctx=(BIO_ENC_CTX *)b->ptr;
418 memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
420 if (b->callback != NULL)
421 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
425 int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
426 const unsigned char *i, int e)
429 long (*callback) (struct bio_st *, int, const char *, int, long, long);
431 ctx = BIO_get_data(b);
435 callback = BIO_get_callback(b);
437 if ((callback != NULL) &&
438 (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
444 if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
447 if (callback != NULL)
448 return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);