Skip to content

Commit

Permalink
Add version independent fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Fekete committed Oct 9, 2024
1 parent 299d9b1 commit 32b1cba
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 30 deletions.
2 changes: 1 addition & 1 deletion include/wolfprovider/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ enum wc_HashType wp_nid_to_wc_hash_type(int nid);
int wp_name_to_wc_mgf(OSSL_LIB_CTX* libCtx, const char* name,
const char* propQ);
int wp_mgf1_from_hash(int nid);
int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst);
int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst, enum wc_HashType hashType);

int wp_cipher_from_params(const OSSL_PARAM params[], int* cipher,
const char** cipherName);
Expand Down
80 changes: 71 additions & 9 deletions src/wp_ecdsa_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ typedef struct wp_EcdsaSigCtx {

/** wolfSSL hash object. */
wc_HashAlg hash;
#ifndef wc_Hashes
/** Hash algorithm to use on data to be signed. */
enum wc_HashType hashType;
#endif

/** Property query string. */
char* propQuery;
Expand Down Expand Up @@ -138,14 +142,21 @@ static wp_EcdsaSigCtx* wp_ecdsa_dupctx(wp_EcdsaSigCtx* srcCtx)
ok = 0;
}

if (ok && (!wp_hash_copy(&srcCtx->hash, &dstCtx->hash))) {
if (ok && (!wp_hash_copy(&srcCtx->hash, &dstCtx->hash
#ifndef wc_Hashes
,srcCtx->hashType
#endif
))) {
ok = 0;
}
if (ok && (!wp_ecc_up_ref(srcCtx->ecc))) {
ok = 0;
}
if (ok) {
dstCtx->ecc = srcCtx->ecc;
#ifndef wc_Hashes
dstCtx->hashType = srcCtx->hashType;
#endif
dstCtx->op = srcCtx->op;
XMEMCPY(dstCtx->mdName, srcCtx->mdName, sizeof(srcCtx->mdName));
}
Expand Down Expand Up @@ -245,8 +256,14 @@ static int wp_ecdsa_sign(wp_EcdsaSigCtx *ctx, unsigned char *sig,
*sigLen = wc_ecc_sig_size(wp_ecc_get_key(ctx->ecc));
}
else {
#ifdef wc_Hashes
if ((ctx->hash.type != WC_HASH_TYPE_NONE) &&
(tbsLen != (size_t)wc_HashGetDigestSize(ctx->hash.type))) {
(tbsLen != (size_t)wc_HashGetDigestSize(ctx->hash.type)))
#else
if ((ctx->hashType != WC_HASH_TYPE_NONE) &&
(tbsLen != (size_t)wc_HashGetDigestSize(ctx->hashType)))
#endif
{
ok = 0;
}
else if ((ok = wp_ecc_check_usage(ctx->ecc))) {
Expand Down Expand Up @@ -406,17 +423,33 @@ static int wp_ecdsa_setup_md(wp_EcdsaSigCtx *ctx, const char *mdName,
if (mdName != NULL) {
int rc;

#ifdef wc_Hashes
ctx->hash.type = wp_name_to_wc_hash_type(ctx->libCtx, mdName, mdProps);
if ((ctx->hash.type == WC_HASH_TYPE_NONE) ||
(ctx->hash.type == WC_HASH_TYPE_MD5)) {
(ctx->hash.type == WC_HASH_TYPE_MD5))
#else
ctx->hashType = wp_name_to_wc_hash_type(ctx->libCtx, mdName, mdProps);
if ((ctx->hashType == WC_HASH_TYPE_NONE) ||
(ctx->hashType == WC_HASH_TYPE_MD5))
#endif
{
ok = 0;
}
if ((ctx->hash.type == WC_HASH_TYPE_SHA) && (op == EVP_PKEY_OP_SIGN)) {
#ifdef wc_Hashes
if ((ctx->hash.type == WC_HASH_TYPE_SHA) && (op == EVP_PKEY_OP_SIGN))
#else
if ((ctx->hashType == WC_HASH_TYPE_SHA) && (op == EVP_PKEY_OP_SIGN))
#endif
{
ok = 0;
}

if (ok) {
#ifdef wc_Hashes
rc = wc_HashInit_ex(&ctx->hash, ctx->hash.type, NULL, INVALID_DEVID);
#else
rc = wc_HashInit_ex(&ctx->hash, ctx->hashType, NULL, INVALID_DEVID);
#endif
if (rc != 0) {
ok = 0;
}
Expand Down Expand Up @@ -471,7 +504,13 @@ static int wp_ecdsa_digest_signverify_update(wp_EcdsaSigCtx *ctx,
const unsigned char *data, size_t dataLen)
{
int ok = 1;
int rc = wc_HashUpdate(&ctx->hash, ctx->hash.type, data, (word32)dataLen);
int rc = wc_HashUpdate(&ctx->hash,
#ifdef wc_Hashes
ctx->hash.type,
#else
ctx->hashType,
#endif
data, (word32)dataLen);
if (rc != 0) {
ok = 0;
}
Expand Down Expand Up @@ -529,15 +568,27 @@ static int wp_ecdsa_digest_sign_final(wp_EcdsaSigCtx *ctx, unsigned char *sig,
ok = 0;
}
else if (sig != NULL) {
int rc = wc_HashFinal(&ctx->hash, ctx->hash.type, digest);
int rc = wc_HashFinal(&ctx->hash,
#ifdef wc_Hashes
ctx->hash.type,
#else
ctx->hashType,
#endif
digest);
if (rc != 0) {
ok = 0;
}
}

if (ok) {
ok = wp_ecdsa_sign(ctx, sig, sigLen, sigSize, digest,
wc_HashGetDigestSize(ctx->hash.type));
wc_HashGetDigestSize(
#ifdef wc_Hashes
ctx->hash.type
#else
ctx->hashType
#endif
));
}

WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
Expand Down Expand Up @@ -590,15 +641,26 @@ static int wp_ecdsa_digest_verify_final(wp_EcdsaSigCtx *ctx, unsigned char *sig,
ok = 0;
}
else {
int rc = wc_HashFinal(&ctx->hash, ctx->hash.type, digest);
int rc = wc_HashFinal(&ctx->hash,
#ifdef wc_Hashes
ctx->hash.type,
#else
ctx->hashType,
#endif
digest);
if (rc != 0) {
ok = 0;
}
}

if (ok) {
ok = wp_ecdsa_verify(ctx,sig, sigLen, digest,
wc_HashGetDigestSize(ctx->hash.type));
#ifdef wc_Hashes
wc_HashGetDigestSize(ctx->hash.type)
#else
wc_HashGetDigestSize(ctx->hashType)
#endif
);
}

WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
Expand Down
14 changes: 13 additions & 1 deletion src/wp_ecx_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ typedef struct wp_EcxSigCtx {

/** wolfSSL hash object. */
wc_HashAlg hash;
#ifndef wc_Hashes
/** Hash algorithm to use on data to be signed. */
enum wc_HashType hashType;
#endif

/** Property query string. */
char* propQuery;
Expand Down Expand Up @@ -133,14 +137,22 @@ static wp_EcxSigCtx* wp_ecx_dupctx(wp_EcxSigCtx* srcCtx)
ok = 0;
}

if (ok && (!wp_hash_copy(&srcCtx->hash, &dstCtx->hash))) {
#ifdef wc_Hashes
if (ok && (!wp_hash_copy(&srcCtx->hash, &dstCtx->hash)))
#else
if (ok && (!wp_hash_copy(&srcCtx->hash, &dstCtx->hash, srcCtx->hashType)))
#endif
{
ok = 0;
}
if (ok && (!wp_ecx_up_ref(srcCtx->ecx))) {
ok = 0;
}
if (ok) {
dstCtx->ecx = srcCtx->ecx;
#ifndef wc_Hashes
dstCtx->hashType = srcCtx->hashType;
#endif
dstCtx->op = srcCtx->op;
XMEMCPY(dstCtx->mdName, srcCtx->mdName, sizeof(srcCtx->mdName));
}
Expand Down
59 changes: 57 additions & 2 deletions src/wp_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,62 +248,99 @@ int wp_mgf1_from_hash(int nid)
* @return 1 on success.
* @return 0 on failure.
*/
int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst)
int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst, enum wc_HashType hashType)
{
int ok = 1;
int rc = 0;

switch (src->type) {
#ifdef wc_Hashes
switch (src->type)
#else
switch (hashType)
#endif
{
case WC_HASH_TYPE_MD5:
#ifdef WP_HAVE_MD5
#ifdef wc_Hashes
rc = wc_Md5Copy(&src->alg.md5, &dst->alg.md5);
#else
rc = wc_Md5Copy(&src->md5, &dst->md5);
#endif
#else
ok = 0;
#endif
break;
case WC_HASH_TYPE_SHA:
#ifdef WP_HAVE_SHA1
#ifdef wc_Hashes
rc = wc_ShaCopy(&src->alg.sha, &dst->alg.sha);
#else
rc = wc_ShaCopy(&src->sha, &dst->sha);
#endif
#else
ok = 0;
#endif
break;
case WC_HASH_TYPE_SHA224:
#ifdef WP_HAVE_SHA224
#ifdef wc_Hashes
rc = wc_Sha224Copy(&src->alg.sha224, &dst->alg.sha224);
#else
rc = wc_Sha224Copy(&src->sha224, &dst->sha224);
#endif
#else
ok = 0;
#endif
break;
case WC_HASH_TYPE_SHA256:
#ifdef WP_HAVE_SHA256
#ifdef wc_Hashes
rc = wc_Sha256Copy(&src->alg.sha256, &dst->alg.sha256);
#else
rc = wc_Sha256Copy(&src->sha256, &dst->sha256);
#endif
#else
ok = 0;
#endif
break;
case WC_HASH_TYPE_SHA384:
#ifdef WP_HAVE_SHA384
#ifdef wc_Hashes
rc = wc_Sha384Copy(&src->alg.sha384, &dst->alg.sha384);
#else
rc = wc_Sha384Copy(&src->sha384, &dst->sha384);
#endif
#else
ok = 0;
#endif
break;
#ifdef WP_HAVE_SHA512
case WC_HASH_TYPE_SHA512:
#ifdef wc_Hashes
rc = wc_Sha512Copy(&src->alg.sha512, &dst->alg.sha512);
#else
rc = wc_Sha512Copy(&src->sha512, &dst->sha512);
#endif
break;
#if LIBWOLFSSL_VERSION_HEX >= 0x05000000
#if !defined(WOLFSSL_NOSHA512_224) && !defined(HAVE_FIPS) && \
!defined(SELF_TEST)
case WC_HASH_TYPE_SHA512_224:
#ifdef wc_Hashes
rc = wc_Sha512_224Copy(&src->alg.sha512, &dst->alg.sha512);
#else
rc = wc_Sha512_224Copy(&src->sha512, &dst->sha512);
#endif
break;
#endif /* !WOLFSSL_NOSHA512_224 */
#if !defined(WOLFSSL_NOSHA512_256) && !defined(HAVE_FIPS) && \
!defined(SELF_TEST)
case WC_HASH_TYPE_SHA512_256:
#ifdef wc_Hashes
rc = wc_Sha512_256Copy(&src->alg.sha512, &dst->alg.sha512);
#else
rc = wc_Sha512_256Copy(&src->sha512, &dst->sha512);
#endif
break;
#endif /* !WOLFSSL_NOSHA512_256 */
#endif /* LIBWOLFSSL_VERSION_HEX >= 0x05000000 */
Expand All @@ -316,16 +353,32 @@ int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst)
#endif /* WP_HAVE_SHA512 */
#ifdef WP_HAVE_SHA3
case WC_HASH_TYPE_SHA3_224:
#ifdef wc_Hashes
rc = wc_Sha3_224_Copy(&src->alg.sha3, &dst->alg.sha3);
#else
rc = wc_Sha3_224_Copy(&src->sha3, &dst->sha3);
#endif
break;
case WC_HASH_TYPE_SHA3_256:
#ifdef wc_Hashes
rc = wc_Sha3_256_Copy(&src->alg.sha3, &dst->alg.sha3);
#else
rc = wc_Sha3_256_Copy(&src->sha3, &dst->sha3);
#endif
break;
case WC_HASH_TYPE_SHA3_384:
#ifdef wc_Hashes
rc = wc_Sha3_384_Copy(&src->alg.sha3, &dst->alg.sha3);
#else
rc = wc_Sha3_384_Copy(&src->sha3, &dst->sha3);
#endif
break;
case WC_HASH_TYPE_SHA3_512:
#ifdef wc_Hashes
rc = wc_Sha3_512_Copy(&src->alg.sha3, &dst->alg.sha3);
#else
rc = wc_Sha3_512_Copy(&src->sha3, &dst->sha3);
#endif
break;
#else
case WC_HASH_TYPE_SHA3_224:
Expand Down Expand Up @@ -355,8 +408,10 @@ int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst)
}
if (rc != 0) {
ok = 0;
#ifdef wc_Hashes
} else {
dst->type = src->type;
#endif
}

WOLFPROV_LEAVE(WP_LOG_PROVIDER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
Expand Down
Loading

0 comments on commit 32b1cba

Please sign in to comment.