Skip to content

Commit

Permalink
Fix for new 'wc_HashAlg' structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Fekete committed Oct 9, 2024
1 parent 7244149 commit 299d9b1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 63 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, enum wc_HashType hashType);
int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst);

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

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

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

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

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)) {
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)) {
ok = 0;
}
if ((ctx->hashType == WC_HASH_TYPE_SHA) && (op == EVP_PKEY_OP_SIGN)) {
if ((ctx->hash.type == WC_HASH_TYPE_SHA) && (op == EVP_PKEY_OP_SIGN)) {
ok = 0;
}

if (ok) {
rc = wc_HashInit_ex(&ctx->hash, ctx->hashType, NULL, INVALID_DEVID);
rc = wc_HashInit_ex(&ctx->hash, ctx->hash.type, NULL, INVALID_DEVID);
if (rc != 0) {
ok = 0;
}
Expand Down Expand Up @@ -475,7 +471,7 @@ 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->hashType, data, (word32)dataLen);
int rc = wc_HashUpdate(&ctx->hash, ctx->hash.type, data, (word32)dataLen);
if (rc != 0) {
ok = 0;
}
Expand Down Expand Up @@ -533,15 +529,15 @@ 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->hashType, digest);
int rc = wc_HashFinal(&ctx->hash, ctx->hash.type, digest);
if (rc != 0) {
ok = 0;
}
}

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

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

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

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

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

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

if (ok && (!wp_hash_copy(&srcCtx->hash, &dstCtx->hash,
srcCtx->hashType))) {
if (ok && (!wp_hash_copy(&srcCtx->hash, &dstCtx->hash))) {
ok = 0;
}
if (ok && (!wp_ecx_up_ref(srcCtx->ecx))) {
ok = 0;
}
if (ok) {
dstCtx->ecx = srcCtx->ecx;
dstCtx->hashType = srcCtx->hashType;
dstCtx->op = srcCtx->op;
XMEMCPY(dstCtx->mdName, srcCtx->mdName, sizeof(srcCtx->mdName));
}
Expand Down
30 changes: 16 additions & 14 deletions src/wp_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,62 +248,62 @@ 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, enum wc_HashType hashType)
int wp_hash_copy(wc_HashAlg* src, wc_HashAlg* dst)
{
int ok = 1;
int rc = 0;

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

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

0 comments on commit 299d9b1

Please sign in to comment.