| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 2006. |
| 2 |
// Copyright (c) 2006,2007 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/evp.h> |
| 6 |
|
| 7 |
#include <openssl/err.h> |
| 8 |
|
| 9 |
#include "../pqdsa/internal.h" |
| 10 |
#include "../delocate.h" |
| 11 |
#include "../digest/internal.h" |
| 12 |
#include "internal.h" |
| 13 |
|
| 14 |
enum evp_sign_verify_t { |
| 15 |
evp_sign, |
| 16 |
evp_verify, |
| 17 |
}; |
| 18 |
|
| 19 |
DEFINE_LOCAL_DATA(struct evp_md_pctx_ops, EVP_MD_pctx_ops) { |
| 20 |
out->free = EVP_PKEY_CTX_free; |
| 21 |
out->dup = EVP_PKEY_CTX_dup; |
| 22 |
} |
| 23 |
|
| 24 |
static int uses_prehash(EVP_MD_CTX *ctx, enum evp_sign_verify_t op) { |
| 25 |
// Pre-hash modes of ML-DSA that uses an external mu calculation differs from |
| 26 |
// other signing algorithms, so we specifically check for NIDs of type NID_MLDSAXX. |
| 27 |
if (ctx->pctx->pkey->type == EVP_PKEY_PQDSA && |
| 28 |
ctx->pctx->pkey->pkey.pqdsa_key != NULL) { |
| 29 |
int nid = ctx->pctx->pkey->pkey.pqdsa_key->pqdsa->nid; |
| 30 |
|
| 31 |
if (nid == NID_MLDSA44 || nid == NID_MLDSA65 || nid == NID_MLDSA87) { |
| 32 |
return 0; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return (op == evp_sign) ? (ctx->pctx->pmeth->sign != NULL) |
| 37 |
: (ctx->pctx->pmeth->verify != NULL); |
| 38 |
} |
| 39 |
|
| 40 |
static int hmac_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 41 |
HMAC_PKEY_CTX *hctx = ctx->pctx->data; |
| 42 |
// HMAC_Update returns 1 on success and 0 on failure. |
| 43 |
return HMAC_Update(&hctx->ctx, data, count); |
| 44 |
} |
| 45 |
|
| 46 |
static int HMAC_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *out_sig, |
| 47 |
size_t *out_sig_len) { |
| 48 |
unsigned int mdlen; |
| 49 |
if (*out_sig_len < EVP_MD_CTX_size(ctx)) { |
| 50 |
OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
HMAC_PKEY_CTX *hctx = ctx->pctx->data; |
| 55 |
if (!HMAC_Final(&hctx->ctx, out_sig, &mdlen)) { |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
*out_sig_len = (size_t)mdlen; |
| 60 |
return 1; |
| 61 |
} |
| 62 |
|
| 63 |
static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
| 64 |
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey, |
| 65 |
enum evp_sign_verify_t op) { |
| 66 |
if (ctx->pctx == NULL) { |
| 67 |
ctx->pctx = EVP_PKEY_CTX_new(pkey, e); |
| 68 |
} |
| 69 |
if (ctx->pctx == NULL) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
ctx->pctx_ops = EVP_MD_pctx_ops(); |
| 73 |
|
| 74 |
if (op == evp_verify) { |
| 75 |
if (!EVP_PKEY_verify_init(ctx->pctx)) { |
| 76 |
return 0; |
| 77 |
} |
| 78 |
} else { |
| 79 |
if (pkey->type == EVP_PKEY_HMAC) { |
| 80 |
// |ctx->update| gets repurposed as a hook to call |HMAC_Update|. |
| 81 |
// |ctx->update| is normally copied from |ctx->digest->update|, but |
| 82 |
// |EVP_PKEY_HMAC| has its own definition. We suppress the automatic |
| 83 |
// setting of |mctx->update| and the rest of its initialization here. |
| 84 |
ctx->pctx->operation = EVP_PKEY_OP_SIGN; |
| 85 |
ctx->flags |= EVP_MD_CTX_HMAC; |
| 86 |
ctx->update = hmac_update; |
| 87 |
} else { |
| 88 |
if (!EVP_PKEY_sign_init(ctx->pctx)) { |
| 89 |
return 0; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (type != NULL && !EVP_PKEY_CTX_set_signature_md(ctx->pctx, type)) { |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
if (uses_prehash(ctx, op) || used_for_hmac(ctx)) { |
| 99 |
if (type == NULL) { |
| 100 |
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_DEFAULT_DIGEST); |
| 101 |
return 0; |
| 102 |
} |
| 103 |
if (!EVP_DigestInit_ex(ctx, type, e)) { |
| 104 |
return 0; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (pctx) { |
| 109 |
*pctx = ctx->pctx; |
| 110 |
} |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, |
| 115 |
ENGINE *e, EVP_PKEY *pkey) { |
| 116 |
SET_DIT_AUTO_RESET; |
| 117 |
return do_sigver_init(ctx, pctx, type, e, pkey, evp_sign); |
| 118 |
} |
| 119 |
|
| 120 |
int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
| 121 |
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) { |
| 122 |
SET_DIT_AUTO_RESET; |
| 123 |
return do_sigver_init(ctx, pctx, type, e, pkey, evp_verify); |
| 124 |
} |
| 125 |
|
| 126 |
int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 127 |
SET_DIT_AUTO_RESET; |
| 128 |
if (!uses_prehash(ctx, evp_sign) && !used_for_hmac(ctx)) { |
| 129 |
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
return EVP_DigestUpdate(ctx, data, len); |
| 134 |
} |
| 135 |
|
| 136 |
int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 137 |
SET_DIT_AUTO_RESET; |
| 138 |
if (!uses_prehash(ctx, evp_verify) || used_for_hmac(ctx)) { |
| 139 |
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
return EVP_DigestUpdate(ctx, data, len); |
| 144 |
} |
| 145 |
|
| 146 |
int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig, |
| 147 |
size_t *out_sig_len) { |
| 148 |
SET_DIT_AUTO_RESET; |
| 149 |
if (!uses_prehash(ctx, evp_sign) && !used_for_hmac(ctx)) { |
| 150 |
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
if (out_sig) { |
| 155 |
EVP_MD_CTX tmp_ctx; |
| 156 |
int ret = 0; |
| 157 |
uint8_t md[EVP_MAX_MD_SIZE]; |
| 158 |
unsigned int mdlen; |
| 159 |
// We have to avoid the underlying SHA services updating the indicator |
| 160 |
// state, so we lock the state here. |
| 161 |
FIPS_service_indicator_lock_state(); |
| 162 |
EVP_MD_CTX_init(&tmp_ctx); |
| 163 |
if (EVP_MD_CTX_copy_ex(&tmp_ctx, ctx)) { |
| 164 |
if (used_for_hmac(ctx)) { |
| 165 |
ret = HMAC_DigestFinal_ex(&tmp_ctx, out_sig, out_sig_len); |
| 166 |
} else { |
| 167 |
ret = EVP_DigestFinal_ex(&tmp_ctx, md, &mdlen) && |
| 168 |
EVP_PKEY_sign(ctx->pctx, out_sig, out_sig_len, md, mdlen); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
EVP_MD_CTX_cleanup(&tmp_ctx); |
| 173 |
FIPS_service_indicator_unlock_state(); |
| 174 |
if (ret > 0) { |
| 175 |
EVP_DigestSign_verify_service_indicator(ctx); |
| 176 |
} |
| 177 |
return ret; |
| 178 |
} else { |
| 179 |
// This only determines the size of the signature. This case of |
| 180 |
// |EVP_DigestSignFinal| should not return an approval check because no |
| 181 |
// crypto is being done. |
| 182 |
if (used_for_hmac(ctx)) { |
| 183 |
// This is only defined in |EVP_PKEY_HMAC|. |
| 184 |
*out_sig_len = EVP_MD_CTX_size(ctx); |
| 185 |
return 1; |
| 186 |
} else { |
| 187 |
size_t s = EVP_MD_size(ctx->digest); |
| 188 |
return EVP_PKEY_sign(ctx->pctx, out_sig, out_sig_len, NULL, s); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len) { |
| 194 |
SET_DIT_AUTO_RESET; |
| 195 |
if (!uses_prehash(ctx, evp_verify) || used_for_hmac(ctx)) { |
| 196 |
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 197 |
return 0; |
| 198 |
} |
| 199 |
// We have to avoid the underlying SHA services updating the indicator |
| 200 |
// state, so we lock the state here. |
| 201 |
FIPS_service_indicator_lock_state(); |
| 202 |
|
| 203 |
EVP_MD_CTX tmp_ctx; |
| 204 |
int ret; |
| 205 |
uint8_t md[EVP_MAX_MD_SIZE]; |
| 206 |
unsigned int mdlen; |
| 207 |
|
| 208 |
EVP_MD_CTX_init(&tmp_ctx); |
| 209 |
ret = EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) && |
| 210 |
EVP_DigestFinal_ex(&tmp_ctx, md, &mdlen) && |
| 211 |
EVP_PKEY_verify(ctx->pctx, sig, sig_len, md, mdlen); |
| 212 |
EVP_MD_CTX_cleanup(&tmp_ctx); |
| 213 |
|
| 214 |
FIPS_service_indicator_unlock_state(); |
| 215 |
if (ret > 0) { |
| 216 |
EVP_DigestVerify_verify_service_indicator(ctx); |
| 217 |
} |
| 218 |
return ret; |
| 219 |
} |
| 220 |
|
| 221 |
int EVP_DigestSign(EVP_MD_CTX *ctx, uint8_t *out_sig, size_t *out_sig_len, |
| 222 |
const uint8_t *data, size_t data_len) { |
| 223 |
GUARD_PTR(ctx->pctx); |
| 224 |
// We have to avoid the underlying |EVP_DigestSignFinal| services updating |
| 225 |
// the indicator state, so we lock the state here. |
| 226 |
FIPS_service_indicator_lock_state(); |
| 227 |
SET_DIT_AUTO_RESET; |
| 228 |
int ret = 0; |
| 229 |
|
| 230 |
if (uses_prehash(ctx, evp_sign) || used_for_hmac(ctx)) { |
| 231 |
// If |out_sig| is NULL, the caller is only querying the maximum output |
| 232 |
// length. |data| should only be incorporated in the final call. |
| 233 |
if (out_sig != NULL && !EVP_DigestSignUpdate(ctx, data, data_len)) { |
| 234 |
goto end; |
| 235 |
} |
| 236 |
|
| 237 |
ret = EVP_DigestSignFinal(ctx, out_sig, out_sig_len); |
| 238 |
goto end; |
| 239 |
} |
| 240 |
|
| 241 |
if (ctx->pctx->pmeth->sign_message == NULL) { |
| 242 |
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 243 |
goto end; |
| 244 |
} |
| 245 |
|
| 246 |
// This is executed when |uses_prehash| is not true, which is the case for |
| 247 |
// Ed25519 and ML-DSA when in pure mode. |
| 248 |
ret = ctx->pctx->pmeth->sign_message(ctx->pctx, out_sig, out_sig_len, data, |
| 249 |
data_len); |
| 250 |
end: |
| 251 |
FIPS_service_indicator_unlock_state(); |
| 252 |
if (ret > 0 && out_sig != NULL) { |
| 253 |
// Indicator should only be set if we performed crypto, don't set if we only |
| 254 |
// performed a size check. |
| 255 |
EVP_DigestSign_verify_service_indicator(ctx); |
| 256 |
} |
| 257 |
return ret; |
| 258 |
} |
| 259 |
|
| 260 |
int EVP_DigestVerify(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, |
| 261 |
const uint8_t *data, size_t len) { |
| 262 |
GUARD_PTR(ctx->pctx); |
| 263 |
// We have to avoid the underlying |EVP_DigestSignFinal| services updating |
| 264 |
// the indicator state, so we lock the state here. |
| 265 |
FIPS_service_indicator_lock_state(); |
| 266 |
SET_DIT_AUTO_RESET; |
| 267 |
int ret = 0; |
| 268 |
|
| 269 |
if (uses_prehash(ctx, evp_verify) && !used_for_hmac(ctx)) { |
| 270 |
ret = EVP_DigestVerifyUpdate(ctx, data, len) && |
| 271 |
EVP_DigestVerifyFinal(ctx, sig, sig_len); |
| 272 |
goto end; |
| 273 |
} |
| 274 |
|
| 275 |
if (ctx->pctx->pmeth->verify_message == NULL) { |
| 276 |
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 277 |
goto end; |
| 278 |
} |
| 279 |
|
| 280 |
// This is executed when |uses_prehash| is not true, which is the case for |
| 281 |
// Ed25519 and ML-DSA when in pure mode. |
| 282 |
ret = ctx->pctx->pmeth->verify_message(ctx->pctx, sig, sig_len, data, len); |
| 283 |
|
| 284 |
end: |
| 285 |
FIPS_service_indicator_unlock_state(); |
| 286 |
if (ret > 0) { |
| 287 |
EVP_DigestVerify_verify_service_indicator(ctx); |
| 288 |
} |
| 289 |
return ret; |
| 290 |
} |
| 291 |
|
| 292 |
void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx) { |
| 293 |
SET_DIT_AUTO_RESET; |
| 294 |
// |pctx| could be null, so we have to deal with the cleanup job here. |
| 295 |
if (!(ctx->flags & EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) { |
| 296 |
EVP_PKEY_CTX_free(ctx->pctx); |
| 297 |
} |
| 298 |
|
| 299 |
ctx->pctx = pctx; |
| 300 |
ctx->pctx_ops = EVP_MD_pctx_ops(); |
| 301 |
|
| 302 |
if (pctx != NULL) { |
| 303 |
// make sure |pctx| is not freed when destroying |EVP_MD_CTX| |
| 304 |
ctx->flags |= EVP_MD_CTX_FLAG_KEEP_PKEY_CTX; |
| 305 |
} else { |
| 306 |
// if |pctx| is null, we remove the flag. |
| 307 |
ctx->flags &= ~EVP_MD_CTX_FLAG_KEEP_PKEY_CTX; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx) { |
| 312 |
SET_DIT_AUTO_RESET; |
| 313 |
if(ctx == NULL) { |
| 314 |
return NULL; |
| 315 |
} |
| 316 |
return ctx->pctx; |
| 317 |
} |
| 318 |
|
| 319 |
EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx) { |
| 320 |
return EVP_MD_CTX_get_pkey_ctx(ctx); |
| 321 |
} |