digest.c
Go to the documentation of this file.
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/mem.h>
64 
65 #include "internal.h"
66 #include "../../internal.h"
67 
68 
69 int EVP_MD_type(const EVP_MD *md) { return md->type; }
70 
71 int EVP_MD_nid(const EVP_MD *md) { return EVP_MD_type(md); }
72 
73 uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
74 
75 size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
76 
77 size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; }
78 
79 
81  OPENSSL_memset(ctx, 0, sizeof(EVP_MD_CTX));
82 }
83 
86 
87  if (ctx) {
89  }
90 
91  return ctx;
92 }
93 
95 
97  OPENSSL_free(ctx->md_data);
98 
99  assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
100  if (ctx->pctx_ops) {
101  ctx->pctx_ops->free(ctx->pctx);
102  }
103 
105 
106  return 1;
107 }
108 
110  if (!ctx) {
111  return;
112  }
113 
115  OPENSSL_free(ctx);
116 }
117 
119 
122  return 0;
123 }
124 
126 
128 
130  // |in->digest| may be NULL if this is a signing |EVP_MD_CTX| for, e.g.,
131  // Ed25519 which does not hash with |EVP_MD_CTX|.
132  if (in == NULL || (in->pctx == NULL && in->digest == NULL)) {
134  return 0;
135  }
136 
137  EVP_PKEY_CTX *pctx = NULL;
138  assert(in->pctx == NULL || in->pctx_ops != NULL);
139  if (in->pctx) {
140  pctx = in->pctx_ops->dup(in->pctx);
141  if (!pctx) {
143  return 0;
144  }
145  }
146 
147  uint8_t *tmp_buf = NULL;
148  if (in->digest != NULL) {
149  if (out->digest != in->digest) {
150  assert(in->digest->ctx_size != 0);
151  tmp_buf = OPENSSL_malloc(in->digest->ctx_size);
152  if (tmp_buf == NULL) {
153  if (pctx) {
154  in->pctx_ops->free(pctx);
155  }
157  return 0;
158  }
159  } else {
160  // |md_data| will be the correct size in this case. It's removed from
161  // |out| so that |EVP_MD_CTX_cleanup| doesn't free it, and then it's
162  // reused.
163  tmp_buf = out->md_data;
164  out->md_data = NULL;
165  }
166  }
167 
169 
170  out->digest = in->digest;
171  out->md_data = tmp_buf;
172  if (in->digest != NULL) {
173  OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size);
174  }
175  out->pctx = pctx;
176  out->pctx_ops = in->pctx_ops;
177  assert(out->pctx == NULL || out->pctx_ops != NULL);
178 
179  return 1;
180 }
181 
184  // While not guaranteed, |EVP_MD_CTX| is currently safe to move with |memcpy|.
185  OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX));
187 }
188 
191  return EVP_MD_CTX_copy_ex(out, in);
192 }
193 
197  return 1;
198 }
199 
201  if (ctx->digest != type) {
202  assert(type->ctx_size != 0);
203  uint8_t *md_data = OPENSSL_malloc(type->ctx_size);
204  if (md_data == NULL) {
206  return 0;
207  }
208 
209  OPENSSL_free(ctx->md_data);
210  ctx->md_data = md_data;
211  ctx->digest = type;
212  }
213 
214  assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
215 
216  ctx->digest->init(ctx);
217  return 1;
218 }
219 
222  return EVP_DigestInit_ex(ctx, type, NULL);
223 }
224 
225 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) {
226  ctx->digest->update(ctx, data, len);
227  return 1;
228 }
229 
230 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) {
231  assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
232  ctx->digest->final(ctx, md_out);
233  if (size != NULL) {
234  *size = ctx->digest->md_size;
235  }
236  OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
237  return 1;
238 }
239 
240 int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) {
241  (void)EVP_DigestFinal_ex(ctx, md, size);
243  return 1;
244 }
245 
246 int EVP_Digest(const void *data, size_t count, uint8_t *out_md,
247  unsigned int *out_size, const EVP_MD *type, ENGINE *impl) {
248  EVP_MD_CTX ctx;
249  int ret;
250 
252  ret = EVP_DigestInit_ex(&ctx, type, impl) &&
254  EVP_DigestFinal_ex(&ctx, out_md, out_size);
256 
257  return ret;
258 }
259 
260 
262  if (ctx == NULL) {
263  return NULL;
264  }
265  return ctx->digest;
266 }
267 
269  return EVP_MD_size(EVP_MD_CTX_md(ctx));
270 }
271 
274 }
275 
277  return EVP_MD_type(EVP_MD_CTX_md(ctx));
278 }
279 
280 int EVP_add_digest(const EVP_MD *digest) {
281  return 1;
282 }
EVP_MD_size
size_t EVP_MD_size(const EVP_MD *md)
Definition: digest.c:75
EVP_add_digest
int EVP_add_digest(const EVP_MD *digest)
Definition: digest.c:280
EVP_DigestFinalXOF
int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len)
Definition: digest.c:120
EVP_DigestInit_ex
int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine)
Definition: digest.c:200
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
EVP_MD_CTX_size
size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx)
Definition: digest.c:268
EVP_MD_CTX_destroy
void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
Definition: digest.c:118
ctx
Definition: benchmark-async.c:30
OPENSSL_cleanse
#define OPENSSL_cleanse
Definition: boringssl_prefix_symbols.h:1864
EVP_MD_CTX_md
const EVP_MD * EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
Definition: digest.c:261
env_md_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/internal.h:67
DIGEST_R_INPUT_NOT_INITIALIZED
#define DIGEST_R_INPUT_NOT_INITIALIZED
Definition: digest.h:344
EVP_MD_type
int EVP_MD_type(const EVP_MD *md)
Definition: digest.c:69
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
EVP_MD_CTX_cleanup
int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
Definition: digest.c:96
EVP_MD_CTX_new
EVP_MD_CTX * EVP_MD_CTX_new(void)
Definition: digest.c:84
EVP_MD_nid
int EVP_MD_nid(const EVP_MD *md)
Definition: digest.c:71
EVP_Digest
int EVP_Digest(const void *data, size_t count, uint8_t *out_md, unsigned int *out_size, const EVP_MD *type, ENGINE *impl)
Definition: digest.c:246
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
EVP_MD_CTX_copy
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
Definition: digest.c:189
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
env_md_ctx_st
Definition: digest.h:306
EVP_DigestInit
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
Definition: digest.c:220
EVP_DigestFinal_ex
int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size)
Definition: digest.c:230
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
evp_pkey_ctx_st
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:182
EVP_DigestUpdate
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len)
Definition: digest.c:225
EVP_MD_CTX_free
void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
Definition: digest.c:109
ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
#define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
Definition: err.h:372
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
err.h
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
EVP_MD_CTX_set_flags
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
Definition: digest.c:127
EVP_MD_CTX_block_size
size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx)
Definition: digest.c:272
absl::out_size
char int out_size
Definition: abseil-cpp/absl/synchronization/mutex.h:1048
benchmark.md
md
Definition: benchmark.py:86
EVP_MD_CTX_type
int EVP_MD_CTX_type(const EVP_MD_CTX *ctx)
Definition: digest.c:276
digest.h
EVP_MD_block_size
size_t EVP_MD_block_size(const EVP_MD *md)
Definition: digest.c:77
EVP_DigestFinal
int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size)
Definition: digest.c:240
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
EVP_MD_CTX_move
void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in)
Definition: digest.c:182
EVP_MD_meth_get_flags
uint32_t EVP_MD_meth_get_flags(const EVP_MD *md)
Definition: digest.c:125
EVP_MAX_MD_SIZE
#define EVP_MAX_MD_SIZE
Definition: digest.h:156
EVP_MD_flags
uint32_t EVP_MD_flags(const EVP_MD *md)
Definition: digest.c:73
EVP_MD_CTX_init
void EVP_MD_CTX_init(EVP_MD_CTX *ctx)
Definition: digest.c:80
engine_st
Definition: engine.c:29
mem.h
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
internal.h
EVP_MD_CTX_copy_ex
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
Definition: digest.c:129
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
EVP_MD_CTX_reset
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
Definition: digest.c:194
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371
EVP_MD_CTX_create
EVP_MD_CTX * EVP_MD_CTX_create(void)
Definition: digest.c:94


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:12