| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/evp.h> |
| 5 |
|
| 6 |
#include <limits.h> |
| 7 |
|
| 8 |
#include <openssl/digest.h> |
| 9 |
#include <openssl/err.h> |
| 10 |
|
| 11 |
#include "../fipsmodule/evp/internal.h" |
| 12 |
|
| 13 |
|
| 14 |
int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) { |
| 15 |
return EVP_DigestInit_ex(ctx, type, impl); |
| 16 |
} |
| 17 |
|
| 18 |
int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type) { |
| 19 |
return EVP_DigestInit(ctx, type); |
| 20 |
} |
| 21 |
|
| 22 |
int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 23 |
return EVP_DigestUpdate(ctx, data, len); |
| 24 |
} |
| 25 |
|
| 26 |
int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig, unsigned *out_sig_len, |
| 27 |
EVP_PKEY *pkey) { |
| 28 |
uint8_t m[EVP_MAX_MD_SIZE]; |
| 29 |
unsigned m_len; |
| 30 |
int ret = 0; |
| 31 |
EVP_MD_CTX tmp_ctx; |
| 32 |
EVP_PKEY_CTX *pkctx = NULL; |
| 33 |
size_t sig_len = EVP_PKEY_size(pkey); |
| 34 |
|
| 35 |
// Ensure the final result will fit in |unsigned|. |
| 36 |
if (sig_len > UINT_MAX) { |
| 37 |
sig_len = UINT_MAX; |
| 38 |
} |
| 39 |
|
| 40 |
*out_sig_len = 0; |
| 41 |
EVP_MD_CTX_init(&tmp_ctx); |
| 42 |
if (!EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) || |
| 43 |
!EVP_DigestFinal_ex(&tmp_ctx, m, &m_len)) { |
| 44 |
goto out; |
| 45 |
} |
| 46 |
EVP_MD_CTX_cleanup(&tmp_ctx); |
| 47 |
|
| 48 |
pkctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 49 |
if (!pkctx || // |
| 50 |
!EVP_PKEY_sign_init(pkctx) || |
| 51 |
!EVP_PKEY_CTX_set_signature_md(pkctx, ctx->digest) || |
| 52 |
!EVP_PKEY_sign(pkctx, sig, &sig_len, m, m_len)) { |
| 53 |
goto out; |
| 54 |
} |
| 55 |
*out_sig_len = (unsigned)sig_len; |
| 56 |
ret = 1; |
| 57 |
|
| 58 |
out: |
| 59 |
EVP_PKEY_CTX_free(pkctx); |
| 60 |
return ret; |
| 61 |
} |
| 62 |
|
| 63 |
int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) { |
| 64 |
return EVP_DigestInit_ex(ctx, type, impl); |
| 65 |
} |
| 66 |
|
| 67 |
int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type) { |
| 68 |
return EVP_DigestInit(ctx, type); |
| 69 |
} |
| 70 |
|
| 71 |
int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 72 |
return EVP_DigestUpdate(ctx, data, len); |
| 73 |
} |
| 74 |
|
| 75 |
int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, |
| 76 |
EVP_PKEY *pkey) { |
| 77 |
uint8_t m[EVP_MAX_MD_SIZE]; |
| 78 |
unsigned m_len; |
| 79 |
int ret = 0; |
| 80 |
EVP_MD_CTX tmp_ctx; |
| 81 |
EVP_PKEY_CTX *pkctx = NULL; |
| 82 |
|
| 83 |
EVP_MD_CTX_init(&tmp_ctx); |
| 84 |
if (!EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) || |
| 85 |
!EVP_DigestFinal_ex(&tmp_ctx, m, &m_len)) { |
| 86 |
EVP_MD_CTX_cleanup(&tmp_ctx); |
| 87 |
goto out; |
| 88 |
} |
| 89 |
EVP_MD_CTX_cleanup(&tmp_ctx); |
| 90 |
|
| 91 |
pkctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 92 |
if (!pkctx || |
| 93 |
!EVP_PKEY_verify_init(pkctx) || |
| 94 |
!EVP_PKEY_CTX_set_signature_md(pkctx, ctx->digest)) { |
| 95 |
goto out; |
| 96 |
} |
| 97 |
ret = EVP_PKEY_verify(pkctx, sig, sig_len, m, m_len); |
| 98 |
|
| 99 |
out: |
| 100 |
EVP_PKEY_CTX_free(pkctx); |
| 101 |
return ret; |
| 102 |
} |