blob: 886c910bc07fea0795811fc2dd4a614d01b653bc [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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>
Adam Langley95c29f32014-06-20 12:00:00 -070063#include <openssl/mem.h>
64
65#include "internal.h"
Adam Langleyfd499932017-04-04 14:21:43 -070066#include "../../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070067
68
69int EVP_MD_type(const EVP_MD *md) { return md->type; }
70
Adam Langley95c29f32014-06-20 12:00:00 -070071uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
72
73size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
74
75size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; }
76
77
David Benjamin17cf2cb2016-12-13 01:07:13 -050078void EVP_MD_CTX_init(EVP_MD_CTX *ctx) {
79 OPENSSL_memset(ctx, 0, sizeof(EVP_MD_CTX));
80}
Adam Langley95c29f32014-06-20 12:00:00 -070081
82EVP_MD_CTX *EVP_MD_CTX_create(void) {
83 EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX));
84
85 if (ctx) {
86 EVP_MD_CTX_init(ctx);
87 }
88
89 return ctx;
90}
91
92int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
93 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data) {
Adam Langley95c29f32014-06-20 12:00:00 -070094 OPENSSL_free(ctx->md_data);
95 }
96
97 assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
98 if (ctx->pctx_ops) {
99 ctx->pctx_ops->free(ctx->pctx);
100 }
101
102 EVP_MD_CTX_init(ctx);
103
104 return 1;
105}
106
107void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) {
108 if (!ctx) {
109 return;
110 }
111
112 EVP_MD_CTX_cleanup(ctx);
113 OPENSSL_free(ctx);
114}
115
116int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
117 uint8_t *tmp_buf = NULL;
118
119 if (in == NULL || in->digest == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400120 OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_INPUT_NOT_INITIALIZED);
Adam Langley95c29f32014-06-20 12:00:00 -0700121 return 0;
122 }
123
124 if (out->digest == in->digest) {
David Benjamin808f8322017-08-18 14:06:02 -0400125 // |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.
Adam Langley95c29f32014-06-20 12:00:00 -0700128 tmp_buf = out->md_data;
129 out->md_data = NULL;
130 }
131
132 EVP_MD_CTX_cleanup(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700133
David Benjamin306ece32015-09-17 13:46:22 -0400134 out->digest = in->digest;
Adam Langley95c29f32014-06-20 12:00:00 -0700135 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) {
David Benjamin3570d732015-06-29 00:28:17 -0400141 OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700142 return 0;
143 }
144 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500145 OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700146 }
147
148 assert(in->pctx == NULL || in->pctx_ops != NULL);
David Benjamin306ece32015-09-17 13:46:22 -0400149 out->pctx_ops = in->pctx_ops;
Adam Langley95c29f32014-06-20 12:00:00 -0700150 if (in->pctx && in->pctx_ops) {
151 out->pctx = in->pctx_ops->dup(in->pctx);
152 if (!out->pctx) {
153 EVP_MD_CTX_cleanup(out);
154 return 0;
155 }
156 }
157
158 return 1;
159}
160
161int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
162 EVP_MD_CTX_init(out);
163 return EVP_MD_CTX_copy_ex(out, in);
164}
165
166int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) {
167 if (ctx->digest != type) {
David Benjamin65ee9b72015-06-15 15:51:19 -0400168 if (ctx->digest && ctx->digest->ctx_size > 0) {
Adam Langley95c29f32014-06-20 12:00:00 -0700169 OPENSSL_free(ctx->md_data);
Steven Valdezc087c332016-03-22 11:14:22 -0400170 ctx->md_data = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700171 }
172 ctx->digest = type;
David Benjamin65ee9b72015-06-15 15:51:19 -0400173 if (type->ctx_size > 0) {
Adam Langley95c29f32014-06-20 12:00:00 -0700174 ctx->md_data = OPENSSL_malloc(type->ctx_size);
175 if (ctx->md_data == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400176 OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700177 return 0;
178 }
179 }
180 }
181
182 assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjamin2c367922015-05-27 01:41:01 -0400184 ctx->digest->init(ctx);
185 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700186}
187
188int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) {
189 EVP_MD_CTX_init(ctx);
190 return EVP_DigestInit_ex(ctx, type, NULL);
191}
192
193int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) {
David Benjamin65ee9b72015-06-15 15:51:19 -0400194 ctx->digest->update(ctx, data, len);
David Benjamin2c367922015-05-27 01:41:01 -0400195 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700196}
197
198int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) {
Adam Langley95c29f32014-06-20 12:00:00 -0700199 assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
David Benjamin2c367922015-05-27 01:41:01 -0400200 ctx->digest->final(ctx, md_out);
Adam Langley95c29f32014-06-20 12:00:00 -0700201 if (size != NULL) {
202 *size = ctx->digest->md_size;
203 }
204 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
David Benjamin2c367922015-05-27 01:41:01 -0400205 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700206}
207
208int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) {
Brian Smitha89e2082015-08-06 10:41:58 -0400209 (void)EVP_DigestFinal_ex(ctx, md, size);
Adam Langley95c29f32014-06-20 12:00:00 -0700210 EVP_MD_CTX_cleanup(ctx);
David Benjamin2c367922015-05-27 01:41:01 -0400211 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700212}
213
214int EVP_Digest(const void *data, size_t count, uint8_t *out_md,
215 unsigned int *out_size, const EVP_MD *type, ENGINE *impl) {
216 EVP_MD_CTX ctx;
217 int ret;
218
219 EVP_MD_CTX_init(&ctx);
220 ret = EVP_DigestInit_ex(&ctx, type, impl) &&
221 EVP_DigestUpdate(&ctx, data, count) &&
222 EVP_DigestFinal_ex(&ctx, out_md, out_size);
223 EVP_MD_CTX_cleanup(&ctx);
224
225 return ret;
226}
227
228
229const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) {
230 if (ctx == NULL) {
231 return NULL;
232 }
233 return ctx->digest;
234}
235
David Benjamin1a0a8b62016-04-16 15:23:54 -0400236size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700237 return EVP_MD_size(EVP_MD_CTX_md(ctx));
238}
239
David Benjamin1a0a8b62016-04-16 15:23:54 -0400240size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700241 return EVP_MD_block_size(EVP_MD_CTX_md(ctx));
242}
243
244int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) {
245 return EVP_MD_type(EVP_MD_CTX_md(ctx));
246}
247
Adam Langleybed8ce72014-09-05 17:04:51 -0700248int EVP_add_digest(const EVP_MD *digest) {
249 return 1;
250}