crypto/evp: harden AEAD ciphers.
[openssl.git] / crypto / evp / bio_enc.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/evp.h>
15 #include "internal/bio.h"
16
17 static int enc_write(BIO *h, const char *buf, int num);
18 static int enc_read(BIO *h, char *buf, int size);
19 /*
20  * static int enc_puts(BIO *h, const char *str);
21  */
22 /*
23  * static int enc_gets(BIO *h, char *str, int size);
24  */
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)
32
33 typedef struct enc_struct {
34     int buf_len;
35     int buf_off;
36     int cont;                   /* <= 0 when finished */
37     int finished;
38     int ok;                     /* bad decrypt */
39     EVP_CIPHER_CTX *cipher;
40     unsigned char *read_start, *read_end;
41     /*
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
44      */
45     unsigned char buf[BUF_OFFSET + ENC_BLOCK_SIZE];
46 } BIO_ENC_CTX;
47
48 static const BIO_METHOD methods_enc = {
49     BIO_TYPE_CIPHER, "cipher",
50     /* TODO: Convert to new style write function */
51     bwrite_conv,
52     enc_write,
53     /* TODO: Convert to new style read function */
54     bread_conv,
55     enc_read,
56     NULL,                       /* enc_puts, */
57     NULL,                       /* enc_gets, */
58     enc_ctrl,
59     enc_new,
60     enc_free,
61     enc_callback_ctrl,
62 };
63
64 const BIO_METHOD *BIO_f_cipher(void)
65 {
66     return (&methods_enc);
67 }
68
69 static int enc_new(BIO *bi)
70 {
71     BIO_ENC_CTX *ctx;
72
73     ctx = OPENSSL_zalloc(sizeof(*ctx));
74     if (ctx == NULL)
75         return 0;
76
77     ctx->cipher = EVP_CIPHER_CTX_new();
78     if (ctx->cipher == NULL) {
79         OPENSSL_free(ctx);
80         return 0;
81     }
82     ctx->cont = 1;
83     ctx->ok = 1;
84     ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
85     BIO_set_data(bi, ctx);
86     BIO_set_init(bi, 1);
87
88     return 1;
89 }
90
91 static int enc_free(BIO *a)
92 {
93     BIO_ENC_CTX *b;
94
95     if (a == NULL)
96         return 0;
97
98     b = BIO_get_data(a);
99     if (b == NULL)
100         return 0;
101
102     EVP_CIPHER_CTX_free(b->cipher);
103     OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
104     BIO_set_data(a, NULL);
105     BIO_set_init(a, 0);
106
107     return 1;
108 }
109
110 static int enc_read(BIO *b, char *out, int outl)
111 {
112     int ret = 0, i, blocksize;
113     BIO_ENC_CTX *ctx;
114     BIO *next;
115
116     if (out == NULL)
117         return (0);
118     ctx = BIO_get_data(b);
119
120     next = BIO_next(b);
121     if ((ctx == NULL) || (next == NULL))
122         return 0;
123
124     /* First check if there are bytes decoded/encoded */
125     if (ctx->buf_len > 0) {
126         i = ctx->buf_len - ctx->buf_off;
127         if (i > outl)
128             i = outl;
129         memcpy(out, &(ctx->buf[ctx->buf_off]), i);
130         ret = i;
131         out += i;
132         outl -= i;
133         ctx->buf_off += i;
134         if (ctx->buf_len == ctx->buf_off) {
135             ctx->buf_len = 0;
136             ctx->buf_off = 0;
137         }
138     }
139
140     blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
141     if (blocksize == 1)
142         blocksize = 0;
143
144     /*
145      * At this point, we have room of outl bytes and an empty buffer, so we
146      * should read in some more.
147      */
148
149     while (outl > 0) {
150         if (ctx->cont <= 0)
151             break;
152
153         if (ctx->read_start == ctx->read_end) { /* time to read more data */
154             ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
155             i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
156             if (i > 0)
157                 ctx->read_end += i;
158         } else {
159             i = ctx->read_end - ctx->read_start;
160         }
161
162         if (i <= 0) {
163             /* Should be continue next time we are called? */
164             if (!BIO_should_retry(next)) {
165                 ctx->cont = i;
166                 i = EVP_CipherFinal_ex(ctx->cipher,
167                                        ctx->buf, &(ctx->buf_len));
168                 ctx->ok = i;
169                 ctx->buf_off = 0;
170             } else {
171                 ret = (ret == 0) ? i : ret;
172                 break;
173             }
174         } else {
175             if (outl > ENC_MIN_CHUNK) {
176                 /*
177                  * Depending on flags block cipher decrypt can write
178                  * one extra block and then back off, i.e. output buffer
179                  * has to accommodate extra block...
180                  */
181                 int j = outl - blocksize, buf_len;
182
183                 if (!EVP_CipherUpdate(ctx->cipher,
184                                       (unsigned char *)out, &buf_len,
185                                       ctx->read_start, i > j ? j : i)) {
186                     BIO_clear_retry_flags(b);
187                     return 0;
188                 }
189                 ret += buf_len;
190                 out += buf_len;
191                 outl -= buf_len;
192
193                 if ((i -= j) <= 0) {
194                     ctx->read_start = ctx->read_end;
195                     continue;
196                 }
197                 ctx->read_start += j;
198             }
199             if (i > ENC_MIN_CHUNK)
200                 i = ENC_MIN_CHUNK;
201             if (!EVP_CipherUpdate(ctx->cipher,
202                                   ctx->buf, &ctx->buf_len,
203                                   ctx->read_start, i)) {
204                 BIO_clear_retry_flags(b);
205                 ctx->ok = 0;
206                 return 0;
207             }
208             ctx->read_start += i;
209             ctx->cont = 1;
210             /*
211              * Note: it is possible for EVP_CipherUpdate to decrypt zero
212              * bytes because this is or looks like the final block: if this
213              * happens we should retry and either read more data or decrypt
214              * the final block
215              */
216             if (ctx->buf_len == 0)
217                 continue;
218         }
219
220         if (ctx->buf_len <= outl)
221             i = ctx->buf_len;
222         else
223             i = outl;
224         if (i <= 0)
225             break;
226         memcpy(out, ctx->buf, i);
227         ret += i;
228         ctx->buf_off = i;
229         outl -= i;
230         out += i;
231     }
232
233     BIO_clear_retry_flags(b);
234     BIO_copy_next_retry(b);
235     return ((ret == 0) ? ctx->cont : ret);
236 }
237
238 static int enc_write(BIO *b, const char *in, int inl)
239 {
240     int ret = 0, n, i;
241     BIO_ENC_CTX *ctx;
242     BIO *next;
243
244     ctx = BIO_get_data(b);
245     next = BIO_next(b);
246     if ((ctx == NULL) || (next == NULL))
247         return 0;
248
249     ret = inl;
250
251     BIO_clear_retry_flags(b);
252     n = ctx->buf_len - ctx->buf_off;
253     while (n > 0) {
254         i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
255         if (i <= 0) {
256             BIO_copy_next_retry(b);
257             return (i);
258         }
259         ctx->buf_off += i;
260         n -= i;
261     }
262     /* at this point all pending data has been written */
263
264     if ((in == NULL) || (inl <= 0))
265         return (0);
266
267     ctx->buf_off = 0;
268     while (inl > 0) {
269         n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
270         if (!EVP_CipherUpdate(ctx->cipher,
271                               ctx->buf, &ctx->buf_len,
272                               (const unsigned char *)in, n)) {
273             BIO_clear_retry_flags(b);
274             ctx->ok = 0;
275             return 0;
276         }
277         inl -= n;
278         in += n;
279
280         ctx->buf_off = 0;
281         n = ctx->buf_len;
282         while (n > 0) {
283             i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
284             if (i <= 0) {
285                 BIO_copy_next_retry(b);
286                 return (ret == inl) ? i : ret - inl;
287             }
288             n -= i;
289             ctx->buf_off += i;
290         }
291         ctx->buf_len = 0;
292         ctx->buf_off = 0;
293     }
294     BIO_copy_next_retry(b);
295     return (ret);
296 }
297
298 static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
299 {
300     BIO *dbio;
301     BIO_ENC_CTX *ctx, *dctx;
302     long ret = 1;
303     int i;
304     EVP_CIPHER_CTX **c_ctx;
305     BIO *next;
306
307     ctx = BIO_get_data(b);
308     next = BIO_next(b);
309     if (ctx == NULL)
310         return 0;
311
312     switch (cmd) {
313     case BIO_CTRL_RESET:
314         ctx->ok = 1;
315         ctx->finished = 0;
316         if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
317                                EVP_CIPHER_CTX_encrypting(ctx->cipher)))
318             return 0;
319         ret = BIO_ctrl(next, cmd, num, ptr);
320         break;
321     case BIO_CTRL_EOF:         /* More to read */
322         if (ctx->cont <= 0)
323             ret = 1;
324         else
325             ret = BIO_ctrl(next, cmd, num, ptr);
326         break;
327     case BIO_CTRL_WPENDING:
328         ret = ctx->buf_len - ctx->buf_off;
329         if (ret <= 0)
330             ret = BIO_ctrl(next, cmd, num, ptr);
331         break;
332     case BIO_CTRL_PENDING:     /* More to read in buffer */
333         ret = ctx->buf_len - ctx->buf_off;
334         if (ret <= 0)
335             ret = BIO_ctrl(next, cmd, num, ptr);
336         break;
337     case BIO_CTRL_FLUSH:
338         /* do a final write */
339  again:
340         while (ctx->buf_len != ctx->buf_off) {
341             i = enc_write(b, NULL, 0);
342             if (i < 0)
343                 return i;
344         }
345
346         if (!ctx->finished) {
347             ctx->finished = 1;
348             ctx->buf_off = 0;
349             ret = EVP_CipherFinal_ex(ctx->cipher,
350                                      (unsigned char *)ctx->buf,
351                                      &(ctx->buf_len));
352             ctx->ok = (int)ret;
353             if (ret <= 0)
354                 break;
355
356             /* push out the bytes */
357             goto again;
358         }
359
360         /* Finally flush the underlying BIO */
361         ret = BIO_ctrl(next, cmd, num, ptr);
362         break;
363     case BIO_C_GET_CIPHER_STATUS:
364         ret = (long)ctx->ok;
365         break;
366     case BIO_C_DO_STATE_MACHINE:
367         BIO_clear_retry_flags(b);
368         ret = BIO_ctrl(next, cmd, num, ptr);
369         BIO_copy_next_retry(b);
370         break;
371     case BIO_C_GET_CIPHER_CTX:
372         c_ctx = (EVP_CIPHER_CTX **)ptr;
373         *c_ctx = ctx->cipher;
374         BIO_set_init(b, 1);
375         break;
376     case BIO_CTRL_DUP:
377         dbio = (BIO *)ptr;
378         dctx = BIO_get_data(dbio);
379         dctx->cipher = EVP_CIPHER_CTX_new();
380         if (dctx->cipher == NULL)
381             return 0;
382         ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
383         if (ret)
384             BIO_set_init(dbio, 1);
385         break;
386     default:
387         ret = BIO_ctrl(next, cmd, num, ptr);
388         break;
389     }
390     return (ret);
391 }
392
393 static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
394 {
395     long ret = 1;
396     BIO *next = BIO_next(b);
397
398     if (next == NULL)
399         return (0);
400     switch (cmd) {
401     default:
402         ret = BIO_callback_ctrl(next, cmd, fp);
403         break;
404     }
405     return (ret);
406 }
407
408 /*-
409 void BIO_set_cipher_ctx(b,c)
410 BIO *b;
411 EVP_CIPHER_ctx *c;
412         {
413         if (b == NULL) return;
414
415         if ((b->callback != NULL) &&
416                 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
417                 return;
418
419         b->init=1;
420         ctx=(BIO_ENC_CTX *)b->ptr;
421         memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
422
423         if (b->callback != NULL)
424                 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
425         }
426 */
427
428 int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
429                    const unsigned char *i, int e)
430 {
431     BIO_ENC_CTX *ctx;
432     long (*callback) (struct bio_st *, int, const char *, int, long, long);
433
434     ctx = BIO_get_data(b);
435     if (ctx == NULL)
436         return 0;
437
438     callback = BIO_get_callback(b);
439
440     if ((callback != NULL) &&
441             (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
442                       0L) <= 0))
443         return 0;
444
445     BIO_set_init(b, 1);
446
447     if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
448         return 0;
449
450     if (callback != NULL)
451         return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
452     return 1;
453 }