| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
|
| 6 |
#include <openssl/digest.h> |
| 7 |
#include <openssl/err.h> |
| 8 |
|
| 9 |
#include "../../internal.h" |
| 10 |
#include "../evp/internal.h" |
| 11 |
#include "internal.h" |
| 12 |
|
| 13 |
|
| 14 |
void EVP_MD_unstable_sha3_enable(bool enable) { // no-op |
| 15 |
} |
| 16 |
|
| 17 |
bool EVP_MD_unstable_sha3_is_enabled(void) { return true; } |
| 18 |
|
| 19 |
int EVP_MD_type(const EVP_MD *md) { return md->type; } |
| 20 |
|
| 21 |
int EVP_MD_nid(const EVP_MD *md) { return EVP_MD_type(md); } |
| 22 |
|
| 23 |
uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; } |
| 24 |
|
| 25 |
size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; } |
| 26 |
|
| 27 |
size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; } |
| 28 |
|
| 29 |
|
| 30 |
void EVP_MD_CTX_init(EVP_MD_CTX *ctx) { |
| 31 |
OPENSSL_memset(ctx, 0, sizeof(EVP_MD_CTX)); |
| 32 |
} |
| 33 |
|
| 34 |
EVP_MD_CTX *EVP_MD_CTX_new(void) { |
| 35 |
EVP_MD_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MD_CTX)); |
| 36 |
|
| 37 |
if (ctx) { |
| 38 |
// NO-OP: struct already zeroed |
| 39 |
//EVP_MD_CTX_init(ctx); |
| 40 |
} |
| 41 |
|
| 42 |
return ctx; |
| 43 |
} |
| 44 |
|
| 45 |
EVP_MD_CTX *EVP_MD_CTX_create(void) { return EVP_MD_CTX_new(); } |
| 46 |
|
| 47 |
int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) { |
| 48 |
if (ctx == NULL) { |
| 49 |
return 1; |
| 50 |
} |
| 51 |
|
| 52 |
OPENSSL_free(ctx->md_data); |
| 53 |
|
| 54 |
assert(ctx->pctx == NULL || ctx->pctx_ops != NULL); |
| 55 |
// |pctx| should be freed by the user of |EVP_MD_CTX| if |
| 56 |
// |EVP_MD_CTX_FLAG_KEEP_PKEY_CTX| is set. Everything other than the external |pctx| that |ctx->pctx| was pointing to is cleaned up when the flag is set. |
| 57 |
if (ctx->pctx_ops && !(ctx->flags & EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) { |
| 58 |
ctx->pctx_ops->free(ctx->pctx); |
| 59 |
} |
| 60 |
|
| 61 |
EVP_MD_CTX_init(ctx); |
| 62 |
|
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx) { |
| 67 |
if (ctx == NULL || ctx->md_data == NULL || ctx->digest == NULL) { |
| 68 |
return; |
| 69 |
} |
| 70 |
OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
| 71 |
EVP_MD_CTX_cleanup(ctx); |
| 72 |
} |
| 73 |
|
| 74 |
void EVP_MD_CTX_free(EVP_MD_CTX *ctx) { |
| 75 |
if (!ctx) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
EVP_MD_CTX_cleanup(ctx); |
| 80 |
OPENSSL_free(ctx); |
| 81 |
} |
| 82 |
|
| 83 |
void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); } |
| 84 |
|
| 85 |
// EVP_DigestFinalXOF is a single-call XOF output generation function. |
| 86 |
// The |ctx->digest| check prevents calling EVP_DigestFinalXOF consecutively. |
| 87 |
// To catch single-call XOF EVP_DigestFinalXOF calls after |EVP_DigestSqueeze|, |
| 88 |
// the return |SHAKE_Final| value is used (the check is internally performed via |
| 89 |
// the |KECCAK1600_CTX *ctx| state flag). |
| 90 |
int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len) { |
| 91 |
if (ctx->digest == NULL) { |
| 92 |
return 0; |
| 93 |
} |
| 94 |
if ((EVP_MD_flags(ctx->digest) & EVP_MD_FLAG_XOF) == 0) { |
| 95 |
OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
int ok = ctx->digest->finalXOF(ctx, out, len); |
| 99 |
EVP_MD_CTX_cleanse(ctx); |
| 100 |
return ok; |
| 101 |
} |
| 102 |
|
| 103 |
// EVP_DigestSqueeze is a streaming XOF output squeeze function |
| 104 |
// It can be called multiple times to generate an output of length |
| 105 |
// |len| bytes. |
| 106 |
int EVP_DigestSqueeze(EVP_MD_CTX *ctx, uint8_t *out, size_t len) { |
| 107 |
if (ctx->digest == NULL) { |
| 108 |
return 0; |
| 109 |
} |
| 110 |
if ((EVP_MD_flags(ctx->digest) & EVP_MD_FLAG_XOF) == 0) { |
| 111 |
OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
ctx->digest->squeezeXOF(ctx, out, len); |
| 115 |
return 1; |
| 116 |
} |
| 117 |
|
| 118 |
uint32_t EVP_MD_meth_get_flags(const EVP_MD *md) { return EVP_MD_flags(md); } |
| 119 |
|
| 120 |
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) {} |
| 121 |
|
| 122 |
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) { |
| 123 |
// |in->digest| may be NULL if this is a signing |EVP_MD_CTX| for, e.g., |
| 124 |
// Ed25519 which does not hash with |EVP_MD_CTX|. |
| 125 |
if (in == NULL || (in->pctx == NULL && in->digest == NULL)) { |
| 126 |
OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_INPUT_NOT_INITIALIZED); |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
EVP_PKEY_CTX *pctx = NULL; |
| 131 |
assert(in->pctx == NULL || in->pctx_ops != NULL); |
| 132 |
if (in->pctx) { |
| 133 |
pctx = in->pctx_ops->dup(in->pctx); |
| 134 |
if (!pctx) { |
| 135 |
return 0; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
uint8_t *tmp_buf = NULL; |
| 140 |
if (in->digest != NULL) { |
| 141 |
if (out->digest != in->digest) { |
| 142 |
assert(in->digest->ctx_size != 0); |
| 143 |
tmp_buf = OPENSSL_malloc(in->digest->ctx_size); |
| 144 |
if (tmp_buf == NULL) { |
| 145 |
if (pctx) { |
| 146 |
in->pctx_ops->free(pctx); |
| 147 |
} |
| 148 |
return 0; |
| 149 |
} |
| 150 |
} else { |
| 151 |
// |md_data| will be the correct size in this case. It's removed from |
| 152 |
// |out| so that |EVP_MD_CTX_cleanup| doesn't free it, and then it's |
| 153 |
// reused. |
| 154 |
tmp_buf = out->md_data; |
| 155 |
out->md_data = NULL; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
EVP_MD_CTX_cleanup(out); |
| 160 |
|
| 161 |
out->digest = in->digest; |
| 162 |
out->md_data = tmp_buf; |
| 163 |
if (in->digest != NULL && in->md_data != NULL) { |
| 164 |
OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size); |
| 165 |
} |
| 166 |
out->update = in->update; |
| 167 |
out->flags = in->flags; |
| 168 |
// copied |EVP_MD_CTX| should free its newly allocated |EVP_PKEY_CTX|. |
| 169 |
out->flags &= ~EVP_MD_CTX_FLAG_KEEP_PKEY_CTX; |
| 170 |
|
| 171 |
out->pctx = pctx; |
| 172 |
out->pctx_ops = in->pctx_ops; |
| 173 |
assert(out->pctx == NULL || out->pctx_ops != NULL); |
| 174 |
|
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in) { |
| 179 |
EVP_MD_CTX_cleanup(out); |
| 180 |
// While not guaranteed, |EVP_MD_CTX| is currently safe to move with |memcpy|. |
| 181 |
OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX)); |
| 182 |
EVP_MD_CTX_init(in); |
| 183 |
} |
| 184 |
|
| 185 |
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) { |
| 186 |
EVP_MD_CTX_init(out); |
| 187 |
return EVP_MD_CTX_copy_ex(out, in); |
| 188 |
} |
| 189 |
|
| 190 |
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) { |
| 191 |
EVP_MD_CTX_cleanup(ctx); |
| 192 |
EVP_MD_CTX_init(ctx); |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) { |
| 197 |
if (ctx->digest != type) { |
| 198 |
if (!used_for_hmac(ctx)) { |
| 199 |
// Drop any existing digest state first. On allocation failure below |
| 200 |
// |ctx| is left in an empty-but-valid state equivalent to a freshly |
| 201 |
// |EVP_MD_CTX_init|'d context. |
| 202 |
OPENSSL_free(ctx->md_data); |
| 203 |
ctx->md_data = NULL; |
| 204 |
ctx->update = NULL; |
| 205 |
ctx->digest = NULL; |
| 206 |
|
| 207 |
assert(type->ctx_size != 0); |
| 208 |
uint8_t *md_data = OPENSSL_malloc(type->ctx_size); |
| 209 |
if (md_data == NULL) { |
| 210 |
return 0; |
| 211 |
} |
| 212 |
ctx->md_data = md_data; |
| 213 |
ctx->update = type->update; |
| 214 |
} |
| 215 |
ctx->digest = type; |
| 216 |
} |
| 217 |
|
| 218 |
assert(ctx->pctx == NULL || ctx->pctx_ops != NULL); |
| 219 |
|
| 220 |
if (used_for_hmac(ctx)) { |
| 221 |
// These configurations are specific to |EVP_PKEY_HMAC|. |HMAC_PKEY_CTX| is |
| 222 |
// newly allocated by |EVP_DigestSignInit| at this point. The actual key |
| 223 |
// data is stored in |ctx->pkey| as |HMAC_KEY|. |
| 224 |
if (ctx->pctx == NULL || ctx->pctx->data == NULL || |
| 225 |
ctx->pctx->pkey == NULL || ctx->pctx->pkey->pkey.ptr == NULL) { |
| 226 |
return 0; |
| 227 |
} |
| 228 |
const HMAC_KEY *key = ctx->pctx->pkey->pkey.ptr; |
| 229 |
HMAC_PKEY_CTX *hmac_pctx = ctx->pctx->data; |
| 230 |
if (!HMAC_Init_ex(&hmac_pctx->ctx, key->key, key->key_len, hmac_pctx->md, |
| 231 |
ctx->pctx->engine)) { |
| 232 |
return 0; |
| 233 |
} |
| 234 |
return 1; |
| 235 |
} |
| 236 |
|
| 237 |
ctx->digest->init(ctx); |
| 238 |
return 1; |
| 239 |
} |
| 240 |
|
| 241 |
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) { |
| 242 |
EVP_MD_CTX_init(ctx); |
| 243 |
return EVP_DigestInit_ex(ctx, type, NULL); |
| 244 |
} |
| 245 |
|
| 246 |
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 247 |
if (ctx->update == NULL) { |
| 248 |
return 0; |
| 249 |
} |
| 250 |
return ctx->update(ctx, data, len); |
| 251 |
} |
| 252 |
|
| 253 |
int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) { |
| 254 |
if (ctx->digest == NULL) { |
| 255 |
return 0; |
| 256 |
} |
| 257 |
if (EVP_MD_flags(ctx->digest) & EVP_MD_FLAG_XOF) { |
| 258 |
OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); |
| 263 |
ctx->digest->final(ctx, md_out); |
| 264 |
if (size != NULL) { |
| 265 |
*size = ctx->digest->md_size; |
| 266 |
} |
| 267 |
OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
| 268 |
return 1; |
| 269 |
} |
| 270 |
|
| 271 |
int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) { |
| 272 |
int ok = EVP_DigestFinal_ex(ctx, md, size); |
| 273 |
EVP_MD_CTX_cleanup(ctx); |
| 274 |
return ok; |
| 275 |
} |
| 276 |
|
| 277 |
int EVP_Digest(const void *data, size_t count, uint8_t *out_md, |
| 278 |
unsigned int *out_size, const EVP_MD *type, ENGINE *impl) { |
| 279 |
EVP_MD_CTX ctx; |
| 280 |
int ret; |
| 281 |
|
| 282 |
if ((EVP_MD_flags(type) & EVP_MD_FLAG_XOF) && out_size == NULL) { |
| 283 |
OPENSSL_PUT_ERROR(DIGEST, ERR_R_PASSED_NULL_PARAMETER); |
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
EVP_MD_CTX_init(&ctx); |
| 288 |
ret = EVP_DigestInit_ex(&ctx, type, impl) && |
| 289 |
EVP_DigestUpdate(&ctx, data, count); |
| 290 |
if (ret == 0) { |
| 291 |
EVP_MD_CTX_cleanup(&ctx); |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
if (EVP_MD_flags(type) & EVP_MD_FLAG_XOF) { |
| 296 |
ret &= EVP_DigestFinalXOF(&ctx, out_md, *out_size); |
| 297 |
} else { |
| 298 |
ret &= EVP_DigestFinal(&ctx, out_md, out_size); |
| 299 |
} |
| 300 |
|
| 301 |
return ret; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) { |
| 306 |
if (ctx == NULL) { |
| 307 |
return NULL; |
| 308 |
} |
| 309 |
return ctx->digest; |
| 310 |
} |
| 311 |
|
| 312 |
size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx) { |
| 313 |
return EVP_MD_size(EVP_MD_CTX_md(ctx)); |
| 314 |
} |
| 315 |
|
| 316 |
size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) { |
| 317 |
return EVP_MD_block_size(EVP_MD_CTX_md(ctx)); |
| 318 |
} |
| 319 |
|
| 320 |
int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) { |
| 321 |
return EVP_MD_type(EVP_MD_CTX_md(ctx)); |
| 322 |
} |
| 323 |
|
| 324 |
int EVP_add_digest(const EVP_MD *digest) { return 1; } |