rsa/rsa_oaep.c: remove memcpy calls from RSA_padding_check_PKCS1_OAEP.
[oweals/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,
50     "cipher",
51     enc_write,
52     enc_read,
53     NULL,                       /* enc_puts, */
54     NULL,                       /* enc_gets, */
55     enc_ctrl,
56     enc_new,
57     enc_free,
58     enc_callback_ctrl,
59 };
60
61 const BIO_METHOD *BIO_f_cipher(void)
62 {
63     return (&methods_enc);
64 }
65
66 static int enc_new(BIO *bi)
67 {
68     BIO_ENC_CTX *ctx;
69
70     ctx = OPENSSL_zalloc(sizeof(*ctx));
71     if (ctx == NULL)
72         return 0;
73
74     ctx->cipher = EVP_CIPHER_CTX_new();
75     if (ctx->cipher == NULL) {
76         OPENSSL_free(ctx);
77         return 0;
78     }
79     ctx->cont = 1;
80     ctx->ok = 1;
81     ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
82     BIO_set_data(bi, ctx);
83     BIO_set_init(bi, 1);
84
85     return 1;
86 }
87
88 static int enc_free(BIO *a)
89 {
90     BIO_ENC_CTX *b;
91
92     if (a == NULL)
93         return 0;
94
95     b = BIO_get_data(a);
96     if (b == NULL)
97         return 0;
98
99     EVP_CIPHER_CTX_free(b->cipher);
100     OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
101     BIO_set_data(a, NULL);
102     BIO_set_init(a, 0);
103
104     return 1;
105 }
106
107 static int enc_read(BIO *b, char *out, int outl)
108 {
109     int ret = 0, i, blocksize;
110     BIO_ENC_CTX *ctx;
111     BIO *next;
112
113     if (out == NULL)
114         return (0);
115     ctx = BIO_get_data(b);
116
117     next = BIO_next(b);
118     if ((ctx == NULL) || (next == NULL))
119         return 0;
120
121     /* First check if there are bytes decoded/encoded */
122     if (ctx->buf_len > 0) {
123         i = ctx->buf_len - ctx->buf_off;
124         if (i > outl)
125             i = outl;
126         memcpy(out, &(ctx->buf[ctx->buf_off]), i);
127         ret = i;
128         out += i;
129         outl -= i;
130         ctx->buf_off += i;
131         if (ctx->buf_len == ctx->buf_off) {
132             ctx->buf_len = 0;
133             ctx->buf_off = 0;
134         }
135     }
136
137     blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
138     if (blocksize == 1)
139         blocksize = 0;
140
141     /*
142      * At this point, we have room of outl bytes and an empty buffer, so we
143      * should read in some more.
144      */
145
146     while (outl > 0) {
147         if (ctx->cont <= 0)
148             break;
149
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);
153             if (i > 0)
154                 ctx->read_end += i;
155         } else {
156             i = ctx->read_end - ctx->read_start;
157         }
158
159         if (i <= 0) {
160             /* Should be continue next time we are called? */
161             if (!BIO_should_retry(next)) {
162                 ctx->cont = i;
163                 i = EVP_CipherFinal_ex(ctx->cipher,
164                                        ctx->buf, &(ctx->buf_len));
165                 ctx->ok = i;
166                 ctx->buf_off = 0;
167             } else {
168                 ret = (ret == 0) ? i : ret;
169                 break;
170             }
171         } else {
172             if (outl > ENC_MIN_CHUNK) {
173                 /*
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...
177                  */
178                 int j = outl - blocksize, buf_len;
179
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);
184                     return 0;
185                 }
186                 ret += buf_len;
187                 out += buf_len;
188                 outl -= buf_len;
189
190                 if ((i -= j) <= 0) {
191                     ctx->read_start = ctx->read_end;
192                     continue;
193                 }
194                 ctx->read_start += j;
195             }
196             if (i > ENC_MIN_CHUNK)
197                 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);
202                 ctx->ok = 0;
203                 return 0;
204             }
205             ctx->read_start += i;
206             ctx->cont = 1;
207             /*
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
211              * the final block
212              */
213             if (ctx->buf_len == 0)
214                 continue;
215         }
216
217         if (ctx->buf_len <= outl)
218             i = ctx->buf_len;
219         else
220             i = outl;
221         if (i <= 0)
222             break;
223         memcpy(out, ctx->buf, i);
224         ret += i;
225         ctx->buf_off = i;
226         outl -= i;
227         out += i;
228     }
229
230     BIO_clear_retry_flags(b);
231     BIO_copy_next_retry(b);
232     return ((ret == 0) ? ctx->cont : ret);
233 }
234
235 static int enc_write(BIO *b, const char *in, int inl)
236 {
237     int ret = 0, n, i;
238     BIO_ENC_CTX *ctx;
239     BIO *next;
240
241     ctx = BIO_get_data(b);
242     next = BIO_next(b);
243     if ((ctx == NULL) || (next == NULL))
244         return 0;
245
246     ret = inl;
247
248     BIO_clear_retry_flags(b);
249     n = ctx->buf_len - ctx->buf_off;
250     while (n > 0) {
251         i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
252         if (i <= 0) {
253             BIO_copy_next_retry(b);
254             return (i);
255         }
256         ctx->buf_off += i;
257         n -= i;
258     }
259     /* at this point all pending data has been written */
260
261     if ((in == NULL) || (inl <= 0))
262         return (0);
263
264     ctx->buf_off = 0;
265     while (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);
271             ctx->ok = 0;
272             return 0;
273         }
274         inl -= n;
275         in += n;
276
277         ctx->buf_off = 0;
278         n = ctx->buf_len;
279         while (n > 0) {
280             i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
281             if (i <= 0) {
282                 BIO_copy_next_retry(b);
283                 return (ret == inl) ? i : ret - inl;
284             }
285             n -= i;
286             ctx->buf_off += i;
287         }
288         ctx->buf_len = 0;
289         ctx->buf_off = 0;
290     }
291     BIO_copy_next_retry(b);
292     return (ret);
293 }
294
295 static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
296 {
297     BIO *dbio;
298     BIO_ENC_CTX *ctx, *dctx;
299     long ret = 1;
300     int i;
301     EVP_CIPHER_CTX **c_ctx;
302     BIO *next;
303
304     ctx = BIO_get_data(b);
305     next = BIO_next(b);
306     if (ctx == NULL)
307         return 0;
308
309     switch (cmd) {
310     case BIO_CTRL_RESET:
311         ctx->ok = 1;
312         ctx->finished = 0;
313         if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
314                                EVP_CIPHER_CTX_encrypting(ctx->cipher)))
315             return 0;
316         ret = BIO_ctrl(next, cmd, num, ptr);
317         break;
318     case BIO_CTRL_EOF:         /* More to read */
319         if (ctx->cont <= 0)
320             ret = 1;
321         else
322             ret = BIO_ctrl(next, cmd, num, ptr);
323         break;
324     case BIO_CTRL_WPENDING:
325         ret = ctx->buf_len - ctx->buf_off;
326         if (ret <= 0)
327             ret = BIO_ctrl(next, cmd, num, ptr);
328         break;
329     case BIO_CTRL_PENDING:     /* More to read in buffer */
330         ret = ctx->buf_len - ctx->buf_off;
331         if (ret <= 0)
332             ret = BIO_ctrl(next, cmd, num, ptr);
333         break;
334     case BIO_CTRL_FLUSH:
335         /* do a final write */
336  again:
337         while (ctx->buf_len != ctx->buf_off) {
338             i = enc_write(b, NULL, 0);
339             if (i < 0)
340                 return i;
341         }
342
343         if (!ctx->finished) {
344             ctx->finished = 1;
345             ctx->buf_off = 0;
346             ret = EVP_CipherFinal_ex(ctx->cipher,
347                                      (unsigned char *)ctx->buf,
348                                      &(ctx->buf_len));
349             ctx->ok = (int)ret;
350             if (ret <= 0)
351                 break;
352
353             /* push out the bytes */
354             goto again;
355         }
356
357         /* Finally flush the underlying BIO */
358         ret = BIO_ctrl(next, cmd, num, ptr);
359         break;
360     case BIO_C_GET_CIPHER_STATUS:
361         ret = (long)ctx->ok;
362         break;
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);
367         break;
368     case BIO_C_GET_CIPHER_CTX:
369         c_ctx = (EVP_CIPHER_CTX **)ptr;
370         *c_ctx = ctx->cipher;
371         BIO_set_init(b, 1);
372         break;
373     case BIO_CTRL_DUP:
374         dbio = (BIO *)ptr;
375         dctx = BIO_get_data(dbio);
376         dctx->cipher = EVP_CIPHER_CTX_new();
377         if (dctx->cipher == NULL)
378             return 0;
379         ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
380         if (ret)
381             BIO_set_init(dbio, 1);
382         break;
383     default:
384         ret = BIO_ctrl(next, cmd, num, ptr);
385         break;
386     }
387     return (ret);
388 }
389
390 static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
391 {
392     long ret = 1;
393     BIO *next = BIO_next(b);
394
395     if (next == NULL)
396         return (0);
397     switch (cmd) {
398     default:
399         ret = BIO_callback_ctrl(next, cmd, fp);
400         break;
401     }
402     return (ret);
403 }
404
405 /*-
406 void BIO_set_cipher_ctx(b,c)
407 BIO *b;
408 EVP_CIPHER_ctx *c;
409         {
410         if (b == NULL) return;
411
412         if ((b->callback != NULL) &&
413                 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
414                 return;
415
416         b->init=1;
417         ctx=(BIO_ENC_CTX *)b->ptr;
418         memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
419
420         if (b->callback != NULL)
421                 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
422         }
423 */
424
425 int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
426                    const unsigned char *i, int e)
427 {
428     BIO_ENC_CTX *ctx;
429     long (*callback) (struct bio_st *, int, const char *, int, long, long);
430
431     ctx = BIO_get_data(b);
432     if (ctx == NULL)
433         return 0;
434
435     callback = BIO_get_callback(b);
436
437     if ((callback != NULL) &&
438             (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
439                       0L) <= 0))
440         return 0;
441
442     BIO_set_init(b, 1);
443
444     if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
445         return 0;
446
447     if (callback != NULL)
448         return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
449     return 1;
450 }