]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/digest.c
Copyright consolidation 04/10
[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
16f8d4eb 39 memset(ctx, 0, 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
MC
70 */
71 if (ctx->engine && ctx->digest && (!type ||
72 (type
73 && (type->type ==
74 ctx->digest->type))))
75 goto skip_to_init;
76 if (type) {
77 /*
78 * Ensure an ENGINE left lying around from last time is cleared (the
79 * previous check attempted to avoid this if the same ENGINE and
80 * EVP_MD could be used).
81 */
7c96dbcd
RS
82 ENGINE_finish(ctx->engine);
83 if (impl != NULL) {
0f113f3e
MC
84 if (!ENGINE_init(impl)) {
85 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
86 return 0;
87 }
7c96dbcd 88 } else {
0f113f3e
MC
89 /* Ask if an ENGINE is reserved for this job */
90 impl = ENGINE_get_digest_engine(type->type);
7c96dbcd
RS
91 }
92 if (impl != NULL) {
0f113f3e
MC
93 /* There's an ENGINE for this job ... (apparently) */
94 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
7c96dbcd
RS
95
96 if (d == NULL) {
0f113f3e
MC
97 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
98 ENGINE_finish(impl);
99 return 0;
100 }
101 /* We'll use the ENGINE's private digest definition */
102 type = d;
103 /*
104 * Store the ENGINE functional reference so we know 'type' came
105 * from an ENGINE and we need to release it when done.
106 */
107 ctx->engine = impl;
108 } else
109 ctx->engine = NULL;
a0108702
MC
110 } else {
111 if (!ctx->digest) {
112 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
113 return 0;
114 }
115 type = ctx->digest;
0f113f3e 116 }
90e8a310 117#endif
0f113f3e 118 if (ctx->digest != type) {
ffe9150b 119 if (ctx->digest && ctx->digest->ctx_size) {
0f113f3e 120 OPENSSL_free(ctx->md_data);
ffe9150b
MC
121 ctx->md_data = NULL;
122 }
0f113f3e
MC
123 ctx->digest = type;
124 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
125 ctx->update = type->update;
84c15091 126 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
0f113f3e
MC
127 if (ctx->md_data == NULL) {
128 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
129 return 0;
130 }
131 }
132 }
0b13e9f0 133#ifndef OPENSSL_NO_ENGINE
0f113f3e 134 skip_to_init:
0b13e9f0 135#endif
0f113f3e
MC
136 if (ctx->pctx) {
137 int r;
138 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
139 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
140 if (r <= 0 && (r != -2))
141 return 0;
142 }
143 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
144 return 1;
145 return ctx->digest->init(ctx);
146}
d02b48c6 147
f80921b6 148int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
0f113f3e
MC
149{
150 return ctx->update(ctx, data, count);
151}
d02b48c6 152
dbad1690 153/* The caller can assume that this removes any secret data from the context */
2dc769a1 154int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
0f113f3e
MC
155{
156 int ret;
157 ret = EVP_DigestFinal_ex(ctx, md, size);
959ed531 158 EVP_MD_CTX_reset(ctx);
0f113f3e
MC
159 return ret;
160}
20d2186c
DSH
161
162/* The caller can assume that this removes any secret data from the context */
163int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
0f113f3e
MC
164{
165 int ret;
54a656ef 166
0f113f3e
MC
167 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
168 ret = ctx->digest->final(ctx, md);
169 if (size != NULL)
170 *size = ctx->digest->md_size;
171 if (ctx->digest->cleanup) {
172 ctx->digest->cleanup(ctx);
173 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
174 }
175 memset(ctx->md_data, 0, ctx->digest->ctx_size);
176 return ret;
177}
351d8998 178
dbad1690 179int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e 180{
959ed531 181 EVP_MD_CTX_reset(out);
0f113f3e
MC
182 return EVP_MD_CTX_copy_ex(out, in);
183}
20d2186c
DSH
184
185int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e
MC
186{
187 unsigned char *tmp_buf;
188 if ((in == NULL) || (in->digest == NULL)) {
189 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
190 return 0;
191 }
0b13e9f0 192#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
193 /* Make sure it's safe to copy a digest context using an ENGINE */
194 if (in->engine && !ENGINE_init(in->engine)) {
195 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
196 return 0;
197 }
0b13e9f0 198#endif
26188931 199
0f113f3e
MC
200 if (out->digest == in->digest) {
201 tmp_buf = out->md_data;
202 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
203 } else
204 tmp_buf = NULL;
959ed531 205 EVP_MD_CTX_reset(out);
b4faea50 206 memcpy(out, in, sizeof(*out));
0f113f3e 207
6aa0ba4b
RL
208 /* Null these variables, since they are getting fixed up
209 * properly below. Anything else may cause a memleak and/or
210 * double free if any of the memory allocations below fail
211 */
212 out->md_data = NULL;
213 out->pctx = NULL;
214
0f113f3e
MC
215 if (in->md_data && out->digest->ctx_size) {
216 if (tmp_buf)
217 out->md_data = tmp_buf;
218 else {
219 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
90945fa3 220 if (out->md_data == NULL) {
0f113f3e
MC
221 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
222 return 0;
223 }
224 }
225 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
226 }
26188931 227
0f113f3e 228 out->update = in->update;
d4575825 229
0f113f3e
MC
230 if (in->pctx) {
231 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
232 if (!out->pctx) {
959ed531 233 EVP_MD_CTX_reset(out);
0f113f3e
MC
234 return 0;
235 }
236 }
18327cd0 237
0f113f3e
MC
238 if (out->digest->copy)
239 return out->digest->copy(out, in);
3a828611 240
0f113f3e
MC
241 return 1;
242}
88ce56f8 243
9e0aad9f 244int EVP_Digest(const void *data, size_t count,
0f113f3e
MC
245 unsigned char *md, unsigned int *size, const EVP_MD *type,
246 ENGINE *impl)
247{
bfb0641f 248 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
0f113f3e 249 int ret;
dbad1690 250
74cabf3f
RL
251 if (ctx == NULL)
252 return 0;
253 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
254 ret = EVP_DigestInit_ex(ctx, type, impl)
255 && EVP_DigestUpdate(ctx, data, count)
256 && EVP_DigestFinal_ex(ctx, md, size);
bfb0641f 257 EVP_MD_CTX_free(ctx);
dbad1690 258
0f113f3e
MC
259 return ret;
260}
dbad1690 261
396d5fd0
DSH
262int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
263{
264 if (ctx->digest && ctx->digest->md_ctrl) {
265 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
266 if (ret <= 0)
267 return 0;
268 return 1;
269 }
270 return 0;
271}