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 BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2)
32 typedef struct enc_struct {
35 int cont; /* <= 0 when finished */
37 int ok; /* bad decrypt */
38 EVP_CIPHER_CTX *cipher;
40 * buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate can return
41 * up to a block more data than is presented to it
43 unsigned char buf[ENC_BLOCK_SIZE + BUF_OFFSET + 2];
46 static const BIO_METHOD methods_enc = {
47 BIO_TYPE_CIPHER, "cipher",
58 const BIO_METHOD *BIO_f_cipher(void)
60 return (&methods_enc);
63 static int enc_new(BIO *bi)
67 ctx = OPENSSL_zalloc(sizeof(*ctx));
71 ctx->cipher = EVP_CIPHER_CTX_new();
72 if (ctx->cipher == NULL) {
78 BIO_set_data(bi, ctx);
84 static int enc_free(BIO *a)
95 EVP_CIPHER_CTX_free(b->cipher);
96 OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
97 BIO_set_data(a, NULL);
103 static int enc_read(BIO *b, char *out, int outl)
111 ctx = BIO_get_data(b);
114 if ((ctx == NULL) || (next == NULL))
117 /* First check if there are bytes decoded/encoded */
118 if (ctx->buf_len > 0) {
119 i = ctx->buf_len - ctx->buf_off;
122 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
127 if (ctx->buf_len == ctx->buf_off) {
134 * At this point, we have room of outl bytes and an empty buffer, so we
135 * should read in some more.
144 buf_len = outl + EVP_MAX_BLOCK_LENGTH - 1;
145 buf_len -= buf_len % EVP_MAX_BLOCK_LENGTH;
146 if (buf_len > ENC_BLOCK_SIZE) {
147 buf_len = ENC_BLOCK_SIZE;
151 * read in at IV offset, read the EVP_Cipher documentation about why
153 i = BIO_read(next, &(ctx->buf[BUF_OFFSET]), buf_len);
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;
167 } else if (outl >= EVP_MAX_BLOCK_LENGTH) {
168 if (!EVP_CipherUpdate(ctx->cipher,
169 (unsigned char *)out, &buf_len,
170 &(ctx->buf[BUF_OFFSET]), i)) {
171 BIO_clear_retry_flags(b);
180 if (!EVP_CipherUpdate(ctx->cipher,
181 ctx->buf, &ctx->buf_len,
182 &(ctx->buf[BUF_OFFSET]), i)) {
183 BIO_clear_retry_flags(b);
189 * Note: it is possible for EVP_CipherUpdate to decrypt zero
190 * bytes because this is or looks like the final block: if this
191 * happens we should retry and either read more data or decrypt
194 if (ctx->buf_len == 0)
198 if (ctx->buf_len <= outl)
204 memcpy(out, ctx->buf, i);
211 BIO_clear_retry_flags(b);
212 BIO_copy_next_retry(b);
213 return ((ret == 0) ? ctx->cont : ret);
216 static int enc_write(BIO *b, const char *in, int inl)
222 ctx = BIO_get_data(b);
224 if ((ctx == NULL) || (next == NULL))
229 BIO_clear_retry_flags(b);
230 n = ctx->buf_len - ctx->buf_off;
232 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
234 BIO_copy_next_retry(b);
240 /* at this point all pending data has been written */
242 if ((in == NULL) || (inl <= 0))
247 n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
248 if (!EVP_CipherUpdate(ctx->cipher,
249 ctx->buf, &ctx->buf_len,
250 (const unsigned char *)in, n)) {
251 BIO_clear_retry_flags(b);
261 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
263 BIO_copy_next_retry(b);
264 return (ret == inl) ? i : ret - inl;
272 BIO_copy_next_retry(b);
276 static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
279 BIO_ENC_CTX *ctx, *dctx;
282 EVP_CIPHER_CTX **c_ctx;
285 ctx = BIO_get_data(b);
294 if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
295 EVP_CIPHER_CTX_encrypting(ctx->cipher)))
297 ret = BIO_ctrl(next, cmd, num, ptr);
299 case BIO_CTRL_EOF: /* More to read */
303 ret = BIO_ctrl(next, cmd, num, ptr);
305 case BIO_CTRL_WPENDING:
306 ret = ctx->buf_len - ctx->buf_off;
308 ret = BIO_ctrl(next, cmd, num, ptr);
310 case BIO_CTRL_PENDING: /* More to read in buffer */
311 ret = ctx->buf_len - ctx->buf_off;
313 ret = BIO_ctrl(next, cmd, num, ptr);
316 /* do a final write */
318 while (ctx->buf_len != ctx->buf_off) {
319 i = enc_write(b, NULL, 0);
324 if (!ctx->finished) {
327 ret = EVP_CipherFinal_ex(ctx->cipher,
328 (unsigned char *)ctx->buf,
334 /* push out the bytes */
338 /* Finally flush the underlying BIO */
339 ret = BIO_ctrl(next, cmd, num, ptr);
341 case BIO_C_GET_CIPHER_STATUS:
344 case BIO_C_DO_STATE_MACHINE:
345 BIO_clear_retry_flags(b);
346 ret = BIO_ctrl(next, cmd, num, ptr);
347 BIO_copy_next_retry(b);
349 case BIO_C_GET_CIPHER_CTX:
350 c_ctx = (EVP_CIPHER_CTX **)ptr;
351 *c_ctx = ctx->cipher;
356 dctx = BIO_get_data(dbio);
357 dctx->cipher = EVP_CIPHER_CTX_new();
358 if (dctx->cipher == NULL)
360 ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
362 BIO_set_init(dbio, 1);
365 ret = BIO_ctrl(next, cmd, num, ptr);
371 static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
374 BIO *next = BIO_next(b);
380 ret = BIO_callback_ctrl(next, cmd, fp);
387 void BIO_set_cipher_ctx(b,c)
391 if (b == NULL) return;
393 if ((b->callback != NULL) &&
394 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
398 ctx=(BIO_ENC_CTX *)b->ptr;
399 memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
401 if (b->callback != NULL)
402 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
406 int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
407 const unsigned char *i, int e)
410 long (*callback) (struct bio_st *, int, const char *, int, long, long);
412 ctx = BIO_get_data(b);
416 callback = BIO_get_callback(b);
418 if ((callback != NULL) &&
419 (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
425 if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
428 if (callback != NULL)
429 return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);