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 = {
49 BIO_TYPE_CIPHER, "cipher",
60 const BIO_METHOD *BIO_f_cipher(void)
62 return (&methods_enc);
65 static int enc_new(BIO *bi)
69 ctx = OPENSSL_zalloc(sizeof(*ctx));
73 ctx->cipher = EVP_CIPHER_CTX_new();
74 if (ctx->cipher == NULL) {
80 ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
81 BIO_set_data(bi, ctx);
87 static int enc_free(BIO *a)
98 EVP_CIPHER_CTX_free(b->cipher);
99 OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
100 BIO_set_data(a, NULL);
106 static int enc_read(BIO *b, char *out, int outl)
108 int ret = 0, i, blocksize;
114 ctx = BIO_get_data(b);
117 if ((ctx == NULL) || (next == NULL))
120 /* First check if there are bytes decoded/encoded */
121 if (ctx->buf_len > 0) {
122 i = ctx->buf_len - ctx->buf_off;
125 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
130 if (ctx->buf_len == ctx->buf_off) {
136 blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
141 * At this point, we have room of outl bytes and an empty buffer, so we
142 * should read in some more.
149 if (ctx->read_start == ctx->read_end) { /* time to read more data */
150 ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
151 ctx->read_end += BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
153 i = ctx->read_end - ctx->read_start;
156 /* Should be continue next time we are called? */
157 if (!BIO_should_retry(next)) {
159 i = EVP_CipherFinal_ex(ctx->cipher,
160 ctx->buf, &(ctx->buf_len));
164 ret = (ret == 0) ? i : ret;
168 if (outl > ENC_MIN_CHUNK) {
170 * Depending on flags block cipher decrypt can write
171 * one extra block and then back off, i.e. output buffer
172 * has to accommodate extra block...
174 int j = outl - blocksize, buf_len;
176 if (!EVP_CipherUpdate(ctx->cipher,
177 (unsigned char *)out, &buf_len,
178 ctx->read_start, i > j ? j : i)) {
179 BIO_clear_retry_flags(b);
187 ctx->read_start = ctx->read_end;
190 ctx->read_start += j;
192 if (i > ENC_MIN_CHUNK)
194 if (!EVP_CipherUpdate(ctx->cipher,
195 ctx->buf, &ctx->buf_len,
196 ctx->read_start, i)) {
197 BIO_clear_retry_flags(b);
201 ctx->read_start += i;
204 * Note: it is possible for EVP_CipherUpdate to decrypt zero
205 * bytes because this is or looks like the final block: if this
206 * happens we should retry and either read more data or decrypt
209 if (ctx->buf_len == 0)
213 if (ctx->buf_len <= outl)
219 memcpy(out, ctx->buf, i);
226 BIO_clear_retry_flags(b);
227 BIO_copy_next_retry(b);
228 return ((ret == 0) ? ctx->cont : ret);
231 static int enc_write(BIO *b, const char *in, int inl)
237 ctx = BIO_get_data(b);
239 if ((ctx == NULL) || (next == NULL))
244 BIO_clear_retry_flags(b);
245 n = ctx->buf_len - ctx->buf_off;
247 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
249 BIO_copy_next_retry(b);
255 /* at this point all pending data has been written */
257 if ((in == NULL) || (inl <= 0))
262 n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
263 if (!EVP_CipherUpdate(ctx->cipher,
264 ctx->buf, &ctx->buf_len,
265 (const unsigned char *)in, n)) {
266 BIO_clear_retry_flags(b);
276 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
278 BIO_copy_next_retry(b);
279 return (ret == inl) ? i : ret - inl;
287 BIO_copy_next_retry(b);
291 static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
294 BIO_ENC_CTX *ctx, *dctx;
297 EVP_CIPHER_CTX **c_ctx;
300 ctx = BIO_get_data(b);
309 if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
310 EVP_CIPHER_CTX_encrypting(ctx->cipher)))
312 ret = BIO_ctrl(next, cmd, num, ptr);
314 case BIO_CTRL_EOF: /* More to read */
318 ret = BIO_ctrl(next, cmd, num, ptr);
320 case BIO_CTRL_WPENDING:
321 ret = ctx->buf_len - ctx->buf_off;
323 ret = BIO_ctrl(next, cmd, num, ptr);
325 case BIO_CTRL_PENDING: /* More to read in buffer */
326 ret = ctx->buf_len - ctx->buf_off;
328 ret = BIO_ctrl(next, cmd, num, ptr);
331 /* do a final write */
333 while (ctx->buf_len != ctx->buf_off) {
334 i = enc_write(b, NULL, 0);
339 if (!ctx->finished) {
342 ret = EVP_CipherFinal_ex(ctx->cipher,
343 (unsigned char *)ctx->buf,
349 /* push out the bytes */
353 /* Finally flush the underlying BIO */
354 ret = BIO_ctrl(next, cmd, num, ptr);
356 case BIO_C_GET_CIPHER_STATUS:
359 case BIO_C_DO_STATE_MACHINE:
360 BIO_clear_retry_flags(b);
361 ret = BIO_ctrl(next, cmd, num, ptr);
362 BIO_copy_next_retry(b);
364 case BIO_C_GET_CIPHER_CTX:
365 c_ctx = (EVP_CIPHER_CTX **)ptr;
366 *c_ctx = ctx->cipher;
371 dctx = BIO_get_data(dbio);
372 dctx->cipher = EVP_CIPHER_CTX_new();
373 if (dctx->cipher == NULL)
375 ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
377 BIO_set_init(dbio, 1);
380 ret = BIO_ctrl(next, cmd, num, ptr);
386 static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
389 BIO *next = BIO_next(b);
395 ret = BIO_callback_ctrl(next, cmd, fp);
402 void BIO_set_cipher_ctx(b,c)
406 if (b == NULL) return;
408 if ((b->callback != NULL) &&
409 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
413 ctx=(BIO_ENC_CTX *)b->ptr;
414 memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
416 if (b->callback != NULL)
417 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
421 int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
422 const unsigned char *i, int e)
425 long (*callback) (struct bio_st *, int, const char *, int, long, long);
427 ctx = BIO_get_data(b);
431 callback = BIO_get_callback(b);
433 if ((callback != NULL) &&
434 (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
440 if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
443 if (callback != NULL)
444 return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);