Implement Aria GCM/CCM Modes and TLS cipher suites
[openssl.git] / crypto / evp / bio_md.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/evp_int.h"
16 #include "evp_locl.h"
17 #include "internal/bio.h"
18
19 /*
20  * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
21  */
22
23 static int md_write(BIO *h, char const *buf, int num);
24 static int md_read(BIO *h, char *buf, int size);
25 static int md_gets(BIO *h, char *str, int size);
26 static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
27 static int md_new(BIO *h);
28 static int md_free(BIO *data);
29 static long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
30
31 static const BIO_METHOD methods_md = {
32     BIO_TYPE_MD, "message digest",
33     /* TODO: Convert to new style write function */
34     bwrite_conv,
35     md_write,
36     /* TODO: Convert to new style read function */
37     bread_conv,
38     md_read,
39     NULL,                       /* md_puts, */
40     md_gets,
41     md_ctrl,
42     md_new,
43     md_free,
44     md_callback_ctrl,
45 };
46
47 const BIO_METHOD *BIO_f_md(void)
48 {
49     return (&methods_md);
50 }
51
52 static int md_new(BIO *bi)
53 {
54     EVP_MD_CTX *ctx;
55
56     ctx = EVP_MD_CTX_new();
57     if (ctx == NULL)
58         return (0);
59
60     BIO_set_init(bi, 1);
61     BIO_set_data(bi, ctx);
62
63     return 1;
64 }
65
66 static int md_free(BIO *a)
67 {
68     if (a == NULL)
69         return (0);
70     EVP_MD_CTX_free(BIO_get_data(a));
71     BIO_set_data(a, NULL);
72     BIO_set_init(a, 0);
73
74     return 1;
75 }
76
77 static int md_read(BIO *b, char *out, int outl)
78 {
79     int ret = 0;
80     EVP_MD_CTX *ctx;
81     BIO *next;
82
83     if (out == NULL)
84         return (0);
85
86     ctx = BIO_get_data(b);
87     next = BIO_next(b);
88
89     if ((ctx == NULL) || (next == NULL))
90         return (0);
91
92     ret = BIO_read(next, out, outl);
93     if (BIO_get_init(b)) {
94         if (ret > 0) {
95             if (EVP_DigestUpdate(ctx, (unsigned char *)out,
96                                  (unsigned int)ret) <= 0)
97                 return (-1);
98         }
99     }
100     BIO_clear_retry_flags(b);
101     BIO_copy_next_retry(b);
102     return (ret);
103 }
104
105 static int md_write(BIO *b, const char *in, int inl)
106 {
107     int ret = 0;
108     EVP_MD_CTX *ctx;
109     BIO *next;
110
111     if ((in == NULL) || (inl <= 0))
112         return 0;
113
114     ctx = BIO_get_data(b);
115     next = BIO_next(b);
116     if ((ctx != NULL) && (next != NULL))
117         ret = BIO_write(next, in, inl);
118
119     if (BIO_get_init(b)) {
120         if (ret > 0) {
121             if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
122                                   (unsigned int)ret)) {
123                 BIO_clear_retry_flags(b);
124                 return 0;
125             }
126         }
127     }
128     if (next != NULL) {
129         BIO_clear_retry_flags(b);
130         BIO_copy_next_retry(b);
131     }
132     return ret;
133 }
134
135 static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
136 {
137     EVP_MD_CTX *ctx, *dctx, **pctx;
138     const EVP_MD **ppmd;
139     EVP_MD *md;
140     long ret = 1;
141     BIO *dbio, *next;
142
143
144     ctx = BIO_get_data(b);
145     next = BIO_next(b);
146
147     switch (cmd) {
148     case BIO_CTRL_RESET:
149         if (BIO_get_init(b))
150             ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
151         else
152             ret = 0;
153         if (ret > 0)
154             ret = BIO_ctrl(next, cmd, num, ptr);
155         break;
156     case BIO_C_GET_MD:
157         if (BIO_get_init(b)) {
158             ppmd = ptr;
159             *ppmd = ctx->digest;
160         } else
161             ret = 0;
162         break;
163     case BIO_C_GET_MD_CTX:
164         pctx = ptr;
165         *pctx = ctx;
166         BIO_set_init(b, 1);
167         break;
168     case BIO_C_SET_MD_CTX:
169         if (BIO_get_init(b))
170             BIO_set_data(b, ptr);
171         else
172             ret = 0;
173         break;
174     case BIO_C_DO_STATE_MACHINE:
175         BIO_clear_retry_flags(b);
176         ret = BIO_ctrl(next, cmd, num, ptr);
177         BIO_copy_next_retry(b);
178         break;
179
180     case BIO_C_SET_MD:
181         md = ptr;
182         ret = EVP_DigestInit_ex(ctx, md, NULL);
183         if (ret > 0)
184             BIO_set_init(b, 1);
185         break;
186     case BIO_CTRL_DUP:
187         dbio = ptr;
188         dctx = BIO_get_data(dbio);
189         if (!EVP_MD_CTX_copy_ex(dctx, ctx))
190             return 0;
191         BIO_set_init(b, 1);
192         break;
193     default:
194         ret = BIO_ctrl(next, cmd, num, ptr);
195         break;
196     }
197     return (ret);
198 }
199
200 static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
201 {
202     long ret = 1;
203     BIO *next;
204
205     next = BIO_next(b);
206
207     if (next == NULL)
208         return 0;
209
210     switch (cmd) {
211     default:
212         ret = BIO_callback_ctrl(next, cmd, fp);
213         break;
214     }
215     return (ret);
216 }
217
218 static int md_gets(BIO *bp, char *buf, int size)
219 {
220     EVP_MD_CTX *ctx;
221     unsigned int ret;
222
223     ctx = BIO_get_data(bp);
224
225     if (size < ctx->digest->md_size)
226         return 0;
227
228     if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
229         return -1;
230
231     return ((int)ret);
232 }