blob: 68e81c49c4f19da6fe1db7eb13d2210549debd09 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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 Langleyd9e397b2015-01-22 14:27:53 -080063#include <openssl/mem.h>
64
65#include "internal.h"
Robert Sloan572a4e22017-04-17 10:52:19 -070066#include "../../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080067
68
69int EVP_MD_type(const EVP_MD *md) { return md->type; }
70
Adam Langleyd9e397b2015-01-22 14:27:53 -080071uint32_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
Robert Sloan69939df2017-01-09 10:53:07 -080078void EVP_MD_CTX_init(EVP_MD_CTX *ctx) {
79 OPENSSL_memset(ctx, 0, sizeof(EVP_MD_CTX));
80}
Adam Langleyd9e397b2015-01-22 14:27:53 -080081
Robert Sloan4562e9d2017-10-02 10:26:51 -070082EVP_MD_CTX *EVP_MD_CTX_new(void) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080083 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
Robert Sloan4562e9d2017-10-02 10:26:51 -070092EVP_MD_CTX *EVP_MD_CTX_create(void) { return EVP_MD_CTX_new(); }
93
Adam Langleyd9e397b2015-01-22 14:27:53 -080094int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
Robert Sloan2e9e66a2017-09-25 09:08:29 -070095 OPENSSL_free(ctx->md_data);
Adam Langleyd9e397b2015-01-22 14:27:53 -080096
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
Robert Sloan4562e9d2017-10-02 10:26:51 -0700107void EVP_MD_CTX_free(EVP_MD_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800108 if (!ctx) {
109 return;
110 }
111
112 EVP_MD_CTX_cleanup(ctx);
113 OPENSSL_free(ctx);
114}
115
Robert Sloan4562e9d2017-10-02 10:26:51 -0700116void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); }
117
Pete Bentley0c61efe2019-08-13 09:32:23 +0100118int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len) {
119 OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
120 return 0;
121}
122
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700124 // |in->digest| may be NULL if this is a signing |EVP_MD_CTX| for, e.g.,
125 // Ed25519 which does not hash with |EVP_MD_CTX|.
126 if (in == NULL || (in->pctx == NULL && in->digest == NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000127 OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_INPUT_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800128 return 0;
129 }
130
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700131 EVP_PKEY_CTX *pctx = NULL;
132 assert(in->pctx == NULL || in->pctx_ops != NULL);
133 if (in->pctx) {
134 pctx = in->pctx_ops->dup(in->pctx);
135 if (!pctx) {
136 OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
137 return 0;
138 }
139 }
140
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700141 uint8_t *tmp_buf = NULL;
142 if (in->digest != NULL) {
143 if (out->digest != in->digest) {
144 assert(in->digest->ctx_size != 0);
145 tmp_buf = OPENSSL_malloc(in->digest->ctx_size);
146 if (tmp_buf == NULL) {
147 if (pctx) {
148 in->pctx_ops->free(pctx);
149 }
150 OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
151 return 0;
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700152 }
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700153 } else {
154 // |md_data| will be the correct size in this case. It's removed from
155 // |out| so that |EVP_MD_CTX_cleanup| doesn't free it, and then it's
156 // reused.
157 tmp_buf = out->md_data;
158 out->md_data = NULL;
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700159 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800160 }
161
162 EVP_MD_CTX_cleanup(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800163
Kenny Rootb8494592015-09-25 02:29:14 +0000164 out->digest = in->digest;
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700165 out->md_data = tmp_buf;
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700166 if (in->digest != NULL) {
167 OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size);
168 }
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700169 out->pctx = pctx;
Kenny Rootb8494592015-09-25 02:29:14 +0000170 out->pctx_ops = in->pctx_ops;
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700171 assert(out->pctx == NULL || out->pctx_ops != NULL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172
173 return 1;
174}
175
176int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
177 EVP_MD_CTX_init(out);
178 return EVP_MD_CTX_copy_ex(out, in);
179}
180
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100181int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) {
Robert Sloan4562e9d2017-10-02 10:26:51 -0700182 EVP_MD_CTX_cleanup(ctx);
183 EVP_MD_CTX_init(ctx);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100184 return 1;
Robert Sloan4562e9d2017-10-02 10:26:51 -0700185}
186
Adam Langleyd9e397b2015-01-22 14:27:53 -0800187int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) {
188 if (ctx->digest != type) {
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700189 assert(type->ctx_size != 0);
190 uint8_t *md_data = OPENSSL_malloc(type->ctx_size);
191 if (md_data == NULL) {
192 OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
193 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800194 }
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700195
196 OPENSSL_free(ctx->md_data);
197 ctx->md_data = md_data;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198 ctx->digest = type;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800199 }
200
201 assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202
Adam Langleyf4e42722015-06-04 17:45:09 -0700203 ctx->digest->init(ctx);
204 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800205}
206
207int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) {
208 EVP_MD_CTX_init(ctx);
209 return EVP_DigestInit_ex(ctx, type, NULL);
210}
211
212int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000213 ctx->digest->update(ctx, data, len);
Adam Langleyf4e42722015-06-04 17:45:09 -0700214 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800215}
216
217int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800218 assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
Adam Langleyf4e42722015-06-04 17:45:09 -0700219 ctx->digest->final(ctx, md_out);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 if (size != NULL) {
221 *size = ctx->digest->md_size;
222 }
223 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
Adam Langleyf4e42722015-06-04 17:45:09 -0700224 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800225}
226
227int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) {
Kenny Rootb8494592015-09-25 02:29:14 +0000228 (void)EVP_DigestFinal_ex(ctx, md, size);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 EVP_MD_CTX_cleanup(ctx);
Adam Langleyf4e42722015-06-04 17:45:09 -0700230 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800231}
232
233int EVP_Digest(const void *data, size_t count, uint8_t *out_md,
234 unsigned int *out_size, const EVP_MD *type, ENGINE *impl) {
235 EVP_MD_CTX ctx;
236 int ret;
237
238 EVP_MD_CTX_init(&ctx);
239 ret = EVP_DigestInit_ex(&ctx, type, impl) &&
240 EVP_DigestUpdate(&ctx, data, count) &&
241 EVP_DigestFinal_ex(&ctx, out_md, out_size);
242 EVP_MD_CTX_cleanup(&ctx);
243
244 return ret;
245}
246
247
248const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) {
249 if (ctx == NULL) {
250 return NULL;
251 }
252 return ctx->digest;
253}
254
David Benjamin4969cc92016-04-22 15:02:23 -0400255size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256 return EVP_MD_size(EVP_MD_CTX_md(ctx));
257}
258
David Benjamin4969cc92016-04-22 15:02:23 -0400259size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800260 return EVP_MD_block_size(EVP_MD_CTX_md(ctx));
261}
262
263int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) {
264 return EVP_MD_type(EVP_MD_CTX_md(ctx));
265}
266
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267int EVP_add_digest(const EVP_MD *digest) {
268 return 1;
269}