]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/digest.c
Add missing RAND_DRBG locking
[thirdparty/openssl.git] / crypto / evp / digest.c
CommitLineData
62867571
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
5ba372b1 3 *
62867571
RS
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
5ba372b1 8 */
d02b48c6
RE
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/objects.h>
13#include <openssl/evp.h>
3c27208f 14#include <openssl/engine.h>
ab0a14bb 15#include "internal/evp_int.h"
77a01145 16#include "evp_locl.h"
d02b48c6 17
74cabf3f 18/* This call frees resources associated with the context */
959ed531 19int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
0f113f3e 20{
74cabf3f
RL
21 if (ctx == NULL)
22 return 1;
23
24 /*
25 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
26 * sometimes only copies of the context are ever finalised.
27 */
28 if (ctx->digest && ctx->digest->cleanup
29 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
30 ctx->digest->cleanup(ctx);
31 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
32 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
33 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
34 }
35 EVP_PKEY_CTX_free(ctx->pctx);
36#ifndef OPENSSL_NO_ENGINE
7c96dbcd 37 ENGINE_finish(ctx->engine);
74cabf3f 38#endif
3ce2fdab 39 OPENSSL_cleanse(ctx, sizeof(*ctx));
74cabf3f
RL
40
41 return 1;
0f113f3e 42}
dbad1690 43
959ed531 44EVP_MD_CTX *EVP_MD_CTX_new(void)
0f113f3e 45{
74cabf3f
RL
46 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
47}
dbad1690 48
959ed531 49void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
74cabf3f 50{
959ed531 51 EVP_MD_CTX_reset(ctx);
74cabf3f 52 OPENSSL_free(ctx);
0f113f3e 53}
dbad1690 54
2dc769a1 55int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
0f113f3e 56{
959ed531 57 EVP_MD_CTX_reset(ctx);
0f113f3e
MC
58 return EVP_DigestInit_ex(ctx, type, NULL);
59}
0fea7ed4 60
11a57c7b 61int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
0f113f3e
MC
62{
63 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
0b13e9f0 64#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
65 /*
66 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
67 * this context may already have an ENGINE! Try to avoid releasing the
68 * previous handle, re-querying for an ENGINE, and having a
0d4fb843 69 * reinitialisation, when it may all be unnecessary.
0f113f3e 70 */
a93e0e78
MRA
71 if (ctx->engine && ctx->digest &&
72 (type == NULL || (type->type == ctx->digest->type)))
0f113f3e
MC
73 goto skip_to_init;
74 if (type) {
75 /*
76 * Ensure an ENGINE left lying around from last time is cleared (the
77 * previous check attempted to avoid this if the same ENGINE and
78 * EVP_MD could be used).
79 */
7c96dbcd
RS
80 ENGINE_finish(ctx->engine);
81 if (impl != NULL) {
0f113f3e
MC
82 if (!ENGINE_init(impl)) {
83 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
84 return 0;
85 }
7c96dbcd 86 } else {
0f113f3e
MC
87 /* Ask if an ENGINE is reserved for this job */
88 impl = ENGINE_get_digest_engine(type->type);
7c96dbcd
RS
89 }
90 if (impl != NULL) {
0f113f3e
MC
91 /* There's an ENGINE for this job ... (apparently) */
92 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
7c96dbcd
RS
93
94 if (d == NULL) {
0f113f3e
MC
95 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
96 ENGINE_finish(impl);
97 return 0;
98 }
99 /* We'll use the ENGINE's private digest definition */
100 type = d;
101 /*
102 * Store the ENGINE functional reference so we know 'type' came
103 * from an ENGINE and we need to release it when done.
104 */
105 ctx->engine = impl;
106 } else
107 ctx->engine = NULL;
a0108702
MC
108 } else {
109 if (!ctx->digest) {
110 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
111 return 0;
112 }
113 type = ctx->digest;
0f113f3e 114 }
90e8a310 115#endif
0f113f3e 116 if (ctx->digest != type) {
ffe9150b 117 if (ctx->digest && ctx->digest->ctx_size) {
a93e0e78 118 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
ffe9150b
MC
119 ctx->md_data = NULL;
120 }
0f113f3e
MC
121 ctx->digest = type;
122 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
123 ctx->update = type->update;
84c15091 124 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
0f113f3e
MC
125 if (ctx->md_data == NULL) {
126 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
127 return 0;
128 }
129 }
130 }
0b13e9f0 131#ifndef OPENSSL_NO_ENGINE
0f113f3e 132 skip_to_init:
0b13e9f0 133#endif
0f113f3e
MC
134 if (ctx->pctx) {
135 int r;
136 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
137 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
138 if (r <= 0 && (r != -2))
139 return 0;
140 }
141 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
142 return 1;
143 return ctx->digest->init(ctx);
144}
d02b48c6 145
f80921b6 146int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
0f113f3e
MC
147{
148 return ctx->update(ctx, data, count);
149}
d02b48c6 150
dbad1690 151/* The caller can assume that this removes any secret data from the context */
2dc769a1 152int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
0f113f3e
MC
153{
154 int ret;
155 ret = EVP_DigestFinal_ex(ctx, md, size);
959ed531 156 EVP_MD_CTX_reset(ctx);
0f113f3e
MC
157 return ret;
158}
20d2186c
DSH
159
160/* The caller can assume that this removes any secret data from the context */
161int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
0f113f3e
MC
162{
163 int ret;
54a656ef 164
0f113f3e
MC
165 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
166 ret = ctx->digest->final(ctx, md);
167 if (size != NULL)
168 *size = ctx->digest->md_size;
169 if (ctx->digest->cleanup) {
170 ctx->digest->cleanup(ctx);
171 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
172 }
3ce2fdab 173 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
0f113f3e
MC
174 return ret;
175}
351d8998 176
cd8d1456
AP
177int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
178{
179 int ret = 0;
180
181 if (ctx->digest->flags & EVP_MD_FLAG_XOF
182 && size <= INT_MAX
183 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
184 ret = ctx->digest->final(ctx, md);
185
186 if (ctx->digest->cleanup != NULL) {
187 ctx->digest->cleanup(ctx);
188 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
189 }
190 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
191 } else {
192 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
193 }
194
195 return ret;
196}
197
dbad1690 198int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e 199{
959ed531 200 EVP_MD_CTX_reset(out);
0f113f3e
MC
201 return EVP_MD_CTX_copy_ex(out, in);
202}
20d2186c
DSH
203
204int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e
MC
205{
206 unsigned char *tmp_buf;
207 if ((in == NULL) || (in->digest == NULL)) {
208 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
209 return 0;
210 }
0b13e9f0 211#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
212 /* Make sure it's safe to copy a digest context using an ENGINE */
213 if (in->engine && !ENGINE_init(in->engine)) {
214 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
215 return 0;
216 }
0b13e9f0 217#endif
26188931 218
0f113f3e
MC
219 if (out->digest == in->digest) {
220 tmp_buf = out->md_data;
221 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
222 } else
223 tmp_buf = NULL;
959ed531 224 EVP_MD_CTX_reset(out);
b4faea50 225 memcpy(out, in, sizeof(*out));
0f113f3e 226
6aa0ba4b
RL
227 /* Null these variables, since they are getting fixed up
228 * properly below. Anything else may cause a memleak and/or
229 * double free if any of the memory allocations below fail
230 */
231 out->md_data = NULL;
232 out->pctx = NULL;
233
0f113f3e
MC
234 if (in->md_data && out->digest->ctx_size) {
235 if (tmp_buf)
236 out->md_data = tmp_buf;
237 else {
238 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
90945fa3 239 if (out->md_data == NULL) {
0f113f3e
MC
240 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
241 return 0;
242 }
243 }
244 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
245 }
26188931 246
0f113f3e 247 out->update = in->update;
d4575825 248
0f113f3e
MC
249 if (in->pctx) {
250 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
251 if (!out->pctx) {
959ed531 252 EVP_MD_CTX_reset(out);
0f113f3e
MC
253 return 0;
254 }
255 }
18327cd0 256
0f113f3e
MC
257 if (out->digest->copy)
258 return out->digest->copy(out, in);
3a828611 259
0f113f3e
MC
260 return 1;
261}
88ce56f8 262
9e0aad9f 263int EVP_Digest(const void *data, size_t count,
0f113f3e
MC
264 unsigned char *md, unsigned int *size, const EVP_MD *type,
265 ENGINE *impl)
266{
bfb0641f 267 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
0f113f3e 268 int ret;
dbad1690 269
74cabf3f
RL
270 if (ctx == NULL)
271 return 0;
272 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
273 ret = EVP_DigestInit_ex(ctx, type, impl)
274 && EVP_DigestUpdate(ctx, data, count)
275 && EVP_DigestFinal_ex(ctx, md, size);
bfb0641f 276 EVP_MD_CTX_free(ctx);
dbad1690 277
0f113f3e
MC
278 return ret;
279}
dbad1690 280
396d5fd0
DSH
281int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
282{
283 if (ctx->digest && ctx->digest->md_ctrl) {
284 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
285 if (ret <= 0)
286 return 0;
287 return 1;
288 }
289 return 0;
290}