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