1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/digest.h>
58
59 #include <assert.h>
60 #include <string.h>
61
62 #include <openssl/err.h>
63 #include <openssl/obj.h>
64 #include <openssl/mem.h>
65
66 #include "internal.h"
67
68
EVP_MD_type(const EVP_MD * md)69 int EVP_MD_type(const EVP_MD *md) { return md->type; }
70
EVP_MD_flags(const EVP_MD * md)71 uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
72
EVP_MD_size(const EVP_MD * md)73 size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
74
EVP_MD_block_size(const EVP_MD * md)75 size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; }
76
77
EVP_MD_CTX_init(EVP_MD_CTX * ctx)78 void EVP_MD_CTX_init(EVP_MD_CTX *ctx) { memset(ctx, 0, sizeof(EVP_MD_CTX)); }
79
EVP_MD_CTX_create(void)80 EVP_MD_CTX *EVP_MD_CTX_create(void) {
81 EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX));
82
83 if (ctx) {
84 EVP_MD_CTX_init(ctx);
85 }
86
87 return ctx;
88 }
89
EVP_MD_CTX_cleanup(EVP_MD_CTX * ctx)90 int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
91 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data) {
92 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
93 OPENSSL_free(ctx->md_data);
94 }
95
96 assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
97 if (ctx->pctx_ops) {
98 ctx->pctx_ops->free(ctx->pctx);
99 }
100
101 EVP_MD_CTX_init(ctx);
102
103 return 1;
104 }
105
EVP_MD_CTX_destroy(EVP_MD_CTX * ctx)106 void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) {
107 if (!ctx) {
108 return;
109 }
110
111 EVP_MD_CTX_cleanup(ctx);
112 OPENSSL_free(ctx);
113 }
114
EVP_MD_CTX_copy_ex(EVP_MD_CTX * out,const EVP_MD_CTX * in)115 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
116 uint8_t *tmp_buf = NULL;
117
118 if (in == NULL || in->digest == NULL) {
119 OPENSSL_PUT_ERROR(DIGEST, EVP_MD_CTX_copy_ex,
120 DIGEST_R_INPUT_NOT_INITIALIZED);
121 return 0;
122 }
123
124 if (out->digest == in->digest) {
125 /* |md_data| will be the correct size in this case so it's removed from
126 * |out| at this point so that |EVP_MD_CTX_cleanup| doesn't free it and
127 * then it's reused. */
128 tmp_buf = out->md_data;
129 out->md_data = NULL;
130 }
131
132 EVP_MD_CTX_cleanup(out);
133 memcpy(out, in, sizeof(EVP_MD_CTX));
134
135 if (in->md_data && in->digest->ctx_size) {
136 if (tmp_buf) {
137 out->md_data = tmp_buf;
138 } else {
139 out->md_data = OPENSSL_malloc(in->digest->ctx_size);
140 if (!out->md_data) {
141 OPENSSL_PUT_ERROR(DIGEST, EVP_MD_CTX_copy_ex, ERR_R_MALLOC_FAILURE);
142 return 0;
143 }
144 }
145 memcpy(out->md_data, in->md_data, in->digest->ctx_size);
146 }
147
148 assert(in->pctx == NULL || in->pctx_ops != NULL);
149 if (in->pctx && in->pctx_ops) {
150 out->pctx = in->pctx_ops->dup(in->pctx);
151 if (!out->pctx) {
152 EVP_MD_CTX_cleanup(out);
153 return 0;
154 }
155 }
156
157 return 1;
158 }
159
EVP_MD_CTX_copy(EVP_MD_CTX * out,const EVP_MD_CTX * in)160 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
161 EVP_MD_CTX_init(out);
162 return EVP_MD_CTX_copy_ex(out, in);
163 }
164
EVP_DigestInit_ex(EVP_MD_CTX * ctx,const EVP_MD * type,ENGINE * engine)165 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) {
166 if (ctx->digest != type) {
167 if (ctx->digest && ctx->digest->ctx_size) {
168 OPENSSL_free(ctx->md_data);
169 }
170 ctx->digest = type;
171 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
172 ctx->update = type->update;
173 ctx->md_data = OPENSSL_malloc(type->ctx_size);
174 if (ctx->md_data == NULL) {
175 OPENSSL_PUT_ERROR(DIGEST, EVP_DigestInit_ex, ERR_R_MALLOC_FAILURE);
176 return 0;
177 }
178 }
179 }
180
181 assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
182 if (ctx->pctx_ops) {
183 if (!ctx->pctx_ops->begin_digest(ctx)) {
184 return 0;
185 }
186 }
187
188 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) {
189 return 1;
190 }
191
192 ctx->digest->init(ctx);
193 return 1;
194 }
195
EVP_DigestInit(EVP_MD_CTX * ctx,const EVP_MD * type)196 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) {
197 EVP_MD_CTX_init(ctx);
198 return EVP_DigestInit_ex(ctx, type, NULL);
199 }
200
EVP_DigestUpdate(EVP_MD_CTX * ctx,const void * data,size_t len)201 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) {
202 ctx->update(ctx, data, len);
203 return 1;
204 }
205
EVP_DigestFinal_ex(EVP_MD_CTX * ctx,uint8_t * md_out,unsigned int * size)206 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) {
207 assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
208 ctx->digest->final(ctx, md_out);
209 if (size != NULL) {
210 *size = ctx->digest->md_size;
211 }
212 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
213 return 1;
214 }
215
EVP_DigestFinal(EVP_MD_CTX * ctx,uint8_t * md,unsigned int * size)216 int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) {
217 EVP_DigestFinal_ex(ctx, md, size);
218 EVP_MD_CTX_cleanup(ctx);
219 return 1;
220 }
221
EVP_Digest(const void * data,size_t count,uint8_t * out_md,unsigned int * out_size,const EVP_MD * type,ENGINE * impl)222 int EVP_Digest(const void *data, size_t count, uint8_t *out_md,
223 unsigned int *out_size, const EVP_MD *type, ENGINE *impl) {
224 EVP_MD_CTX ctx;
225 int ret;
226
227 EVP_MD_CTX_init(&ctx);
228 ret = EVP_DigestInit_ex(&ctx, type, impl) &&
229 EVP_DigestUpdate(&ctx, data, count) &&
230 EVP_DigestFinal_ex(&ctx, out_md, out_size);
231 EVP_MD_CTX_cleanup(&ctx);
232
233 return ret;
234 }
235
236
EVP_MD_CTX_md(const EVP_MD_CTX * ctx)237 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) {
238 if (ctx == NULL) {
239 return NULL;
240 }
241 return ctx->digest;
242 }
243
EVP_MD_CTX_size(const EVP_MD_CTX * ctx)244 unsigned EVP_MD_CTX_size(const EVP_MD_CTX *ctx) {
245 return EVP_MD_size(EVP_MD_CTX_md(ctx));
246 }
247
EVP_MD_CTX_block_size(const EVP_MD_CTX * ctx)248 unsigned EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) {
249 return EVP_MD_block_size(EVP_MD_CTX_md(ctx));
250 }
251
EVP_MD_CTX_type(const EVP_MD_CTX * ctx)252 int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) {
253 return EVP_MD_type(EVP_MD_CTX_md(ctx));
254 }
255
EVP_MD_CTX_set_flags(EVP_MD_CTX * ctx,uint32_t flags)256 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, uint32_t flags) {
257 ctx->flags |= flags;
258 }
259
EVP_add_digest(const EVP_MD * digest)260 int EVP_add_digest(const EVP_MD *digest) {
261 return 1;
262 }
263