diff --git a/configure.ac b/configure.ac index 395d75ddb..f06822354 100644 --- a/configure.ac +++ b/configure.ac @@ -213,6 +213,8 @@ then enable_aeskeywrap=yes enable_x963kdf=yes enable_scrypt=yes + + AM_CFLAGS="-DHAVE_AES_DECRYPT $AM_CFLAGS" fi AM_CONDITIONAL([BUILD_ALL], [test "x$ENABLED_ALL" = "xyes"]) diff --git a/src/internal.c b/src/internal.c index 6d8c3afee..d7915572f 100755 --- a/src/internal.c +++ b/src/internal.c @@ -3038,6 +3038,13 @@ int RsaSign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, WOLFSSL_ENTER("RsaSign"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #if defined(WC_RSA_PSS) if (sigAlgo == rsa_pss_sa_algo) { enum wc_HashType hashType = WC_HASH_TYPE_NONE; @@ -3072,9 +3079,9 @@ int RsaSign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, ret = wc_RsaSSL_Sign(in, inSz, out, *outSz, key, ssl->rng); /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3104,6 +3111,13 @@ int RsaVerify(WOLFSSL* ssl, byte* in, word32 inSz, byte** out, int sigAlgo, WOLFSSL_ENTER("RsaVerify"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #if defined(WC_RSA_PSS) if (sigAlgo == rsa_pss_sa_algo) { enum wc_HashType hashType = WC_HASH_TYPE_NONE; @@ -3135,9 +3149,9 @@ int RsaVerify(WOLFSSL* ssl, byte* in, word32 inSz, byte** out, int sigAlgo, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3168,6 +3182,13 @@ int VerifyRsaSign(WOLFSSL* ssl, byte* verifySig, word32 sigSz, return BUFFER_E; } +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #if defined(WC_RSA_PSS) if (sigAlgo == rsa_pss_sa_algo) { enum wc_HashType hashType = WC_HASH_TYPE_NONE; @@ -3200,9 +3221,9 @@ int VerifyRsaSign(WOLFSSL* ssl, byte* verifySig, word32 sigSz, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3223,6 +3244,13 @@ int RsaDec(WOLFSSL* ssl, byte* in, word32 inSz, byte** out, word32* outSz, WOLFSSL_ENTER("RsaDec"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #ifdef HAVE_PK_CALLBACKS if (ssl->ctx->RsaDecCb) { ret = ssl->ctx->RsaDecCb(ssl, in, inSz, out, keyBuf, keySz, @@ -3240,9 +3268,9 @@ int RsaDec(WOLFSSL* ssl, byte* in, word32 inSz, byte** out, word32* outSz, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3269,6 +3297,13 @@ int RsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, word32* outSz, WOLFSSL_ENTER("RsaEnc"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #ifdef HAVE_PK_CALLBACKS if (ssl->ctx->RsaEncCb) { ret = ssl->ctx->RsaEncCb(ssl, in, inSz, out, outSz, keyBuf, keySz, @@ -3281,9 +3316,9 @@ int RsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, word32* outSz, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3314,6 +3349,13 @@ int EccSign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, WOLFSSL_ENTER("EccSign"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #if defined(HAVE_PK_CALLBACKS) if (ssl->ctx->EccSignCb) { ret = ssl->ctx->EccSignCb(ssl, in, inSz, out, outSz, keyBuf, @@ -3326,9 +3368,9 @@ int EccSign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3350,6 +3392,13 @@ int EccVerify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* out, WOLFSSL_ENTER("EccVerify"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #ifdef HAVE_PK_CALLBACKS if (ssl->ctx->EccVerifyCb) { ret = ssl->ctx->EccVerifyCb(ssl, in, inSz, out, outSz, keyBuf, keySz, @@ -3362,9 +3411,9 @@ int EccVerify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* out, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } else #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3436,9 +3485,13 @@ int EccSharedSecret(WOLFSSL* ssl, ecc_key* priv_key, ecc_key* pub_key, int side, void* ctx) { int ret; -#ifdef WOLFSSL_ASYNC_CRYPT - WC_ASYNC_DEV* asyncDev; +#ifdef HAVE_PK_CALLBACKS + ecc_key* otherKey = NULL; #endif +#ifdef WOLFSSL_ASYNC_CRYPT + WC_ASYNC_DEV* asyncDev = &priv_key->asyncDev; +#endif + (void)ssl; (void)pubKeyDer; (void)pubKeySz; @@ -3449,30 +3502,37 @@ int EccSharedSecret(WOLFSSL* ssl, ecc_key* priv_key, ecc_key* pub_key, #ifdef HAVE_PK_CALLBACKS if (ssl->ctx->EccSharedSecretCb) { - ecc_key* otherKey = NULL; - ret = EccGetKey(ssl, &otherKey); - if (ret == 0) { - ret = ssl->ctx->EccSharedSecretCb(ssl, otherKey, pubKeyDer, - pubKeySz, out, outlen, side, ctx); - } + if (ret != 0) + return ret; #ifdef WOLFSSL_ASYNC_CRYPT asyncDev = &otherKey->asyncDev; #endif } +#endif + +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + +#ifdef HAVE_PK_CALLBACKS + if (ssl->ctx->EccSharedSecretCb) { + ret = ssl->ctx->EccSharedSecretCb(ssl, otherKey, pubKeyDer, + pubKeySz, out, outlen, side, ctx); + } else #endif { ret = wc_ecc_shared_secret(priv_key, pub_key, out, outlen); - #ifdef WOLFSSL_ASYNC_CRYPT - asyncDev = &priv_key->asyncDev; - #endif } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3488,6 +3548,13 @@ int EccMakeKey(WOLFSSL* ssl, ecc_key* key, ecc_key* peer) WOLFSSL_ENTER("EccMakeKey"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_NONE); + if (ret != 0) + return ret; +#endif + if (peer == NULL) { keySz = ssl->eccTempKeySz; } @@ -3506,9 +3573,9 @@ int EccMakeKey(WOLFSSL* ssl, ecc_key* key, ecc_key* peer) } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_NONE); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3544,6 +3611,13 @@ int Ed25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, WOLFSSL_ENTER("Ed25519Sign"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #if defined(HAVE_PK_CALLBACKS) if (ssl->ctx->Ed25519SignCb) { ret = ssl->ctx->Ed25519SignCb(ssl, in, inSz, out, outSz, keyBuf, @@ -3556,9 +3630,9 @@ int Ed25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3592,6 +3666,13 @@ int Ed25519Verify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* msg, WOLFSSL_ENTER("Ed25519Verify"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #ifdef HAVE_PK_CALLBACKS if (ssl->ctx->Ed25519VerifyCb) { ret = ssl->ctx->Ed25519VerifyCb(ssl, in, inSz, msg, msgSz, keyBuf, @@ -3605,9 +3686,9 @@ int Ed25519Verify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* msg, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } else #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3673,6 +3754,13 @@ static int X25519SharedSecret(WOLFSSL* ssl, curve25519_key* priv_key, WOLFSSL_ENTER("X25519SharedSecret"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + return ret; +#endif + #ifdef HAVE_PK_CALLBACKS if (ssl->ctx->X25519SharedSecretCb) { curve25519_key* otherKey = NULL; @@ -3691,10 +3779,9 @@ static int X25519SharedSecret(WOLFSSL* ssl, curve25519_key* priv_key, } /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &priv_key->asyncDev, - WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &priv_key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3712,14 +3799,21 @@ static int X25519MakeKey(WOLFSSL* ssl, curve25519_key* key, WOLFSSL_ENTER("X25519MakeKey"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_NONE); + if (ret != 0) + return ret; +#endif + ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key); if (ret == 0) ssl->ecdhCurveOID = ECC_X25519_OID; /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &key->asyncDev, WC_ASYNC_FLAG_NONE); + ret = wolfSSL_AsyncPush(ssl, &key->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3742,12 +3836,19 @@ int DhGenKeyPair(WOLFSSL* ssl, DhKey* dhKey, WOLFSSL_ENTER("DhGenKeyPair"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE); + if (ret != 0) + return ret; +#endif + ret = wc_DhGenerateKeyPair(dhKey, ssl->rng, priv, privSz, pub, pubSz); /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE); + ret = wolfSSL_AsyncPush(ssl, &dhKey->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -3767,12 +3868,19 @@ int DhAgree(WOLFSSL* ssl, DhKey* dhKey, WOLFSSL_ENTER("DhAgree"); +#ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE); + if (ret != 0) + return ret; +#endif + ret = wc_DhAgree(dhKey, agree, agreeSz, priv, privSz, otherPub, otherPubSz); /* Handle async pending response */ -#if defined(WOLFSSL_ASYNC_CRYPT) +#ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE); + ret = wolfSSL_AsyncPush(ssl, &dhKey->asyncDev); } #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -7686,6 +7794,7 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, { int ret = 0; #ifdef WOLFSSL_ASYNC_CRYPT + WC_ASYNC_DEV* asyncDev; ProcPeerCertArgs* args = (ProcPeerCertArgs*)ssl->async.args; typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1]; (void)sizeof(args_test); @@ -7916,18 +8025,29 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, args->dCertInit = 1; } - ret = ParseCertRelative(args->dCert, CERT_TYPE, 0, - ssl->ctx->cm); - if (ret != 0) { - #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, - args->dCert->sigCtx.asyncDev, + #ifdef WOLFSSL_ASYNC_CRYPT + do { + /* intialize event */ + asyncDev = args->dCert->sigCtx.asyncDev; + if (asyncDev) { + ret = wolfSSL_AsyncInit(ssl, asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + break; } - #endif - goto exit_ppc; - } + #endif + ret = ParseCertRelative(args->dCert, CERT_TYPE, 0, + ssl->ctx->cm); + if (ret != 0 && ret != WC_PENDING_E) + goto exit_ppc; + + #ifdef WOLFSSL_ASYNC_CRYPT + if (asyncDev && ret == WC_PENDING_E) { + ret = wolfSSL_AsyncPush(ssl, asyncDev); + goto exit_ppc; + } + } while (ret == WC_PENDING_E && asyncDev == NULL) + #endif #ifndef NO_SKID if (args->dCert->extAuthKeyIdSet) { @@ -7981,18 +8101,29 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, args->dCertInit = 1; } - ret = ParseCertRelative(args->dCert, CERT_TYPE, 0, - ssl->ctx->cm); - if (ret != 0) { - #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, - args->dCert->sigCtx.asyncDev, - WC_ASYNC_FLAG_CALL_AGAIN); + #ifdef WOLFSSL_ASYNC_CRYPT + do { + /* intialize event */ + asyncDev = args->dCert->sigCtx.asyncDev; + if (asyncDev) { + ret = wolfSSL_AsyncInit(ssl, asyncDev, + WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + goto exit_ppc; } - #endif - goto exit_ppc; - } + #endif + ret = ParseCertRelative(args->dCert, CERT_TYPE, 0, + ssl->ctx->cm); + if (ret != 0 && ret != WC_PENDING_E) { + goto exit_ppc; + } + #ifdef WOLFSSL_ASYNC_CRYPT + if (asyncDev && ret == WC_PENDING_E) { + ret = wolfSSL_AsyncPush(ssl, asyncDev); + goto exit_ppc; + } + } while (ret == WC_PENDING_E && asyncDev == NULL); + #endif #ifndef NO_SKID subjectHash = args->dCert->extSubjKeyId; @@ -8025,15 +8156,25 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, args->dCertInit = 1; } - ret = ParseCertRelative(args->dCert, CERT_TYPE, + #ifdef WOLFSSL_ASYNC_CRYPT + do { + /* intialize event */ + asyncDev = args->dCert->sigCtx.asyncDev; + if (asyncDev) { + ret = wolfSSL_AsyncInit(ssl, asyncDev, + WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + goto exit_ppc; + } + #endif + ret = ParseCertRelative(args->dCert, CERT_TYPE, !ssl->options.verifyNone, ssl->ctx->cm); #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, - args->dCert->sigCtx.asyncDev, - WC_ASYNC_FLAG_CALL_AGAIN); - goto exit_ppc; - } + if (asyncDev && ret == WC_PENDING_E) { + ret = wolfSSL_AsyncPush(ssl, asyncDev); + goto exit_ppc; + } + } while (ret == WC_PENDING_E && asyncDev == NULL); #endif #ifndef NO_SKID @@ -8217,16 +8358,27 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, #ifdef WOLFSSL_TRUST_PEER_CERT if (!haveTrustPeer) #endif - { /* only parse if not already present in dCert from above */ - ret = ParseCertRelative(args->dCert, CERT_TYPE, + { + #ifdef WOLFSSL_ASYNC_CRYPT + do { + /* intialize event */ + asyncDev = args->dCert->sigCtx.asyncDev; + if (asyncDev) { + ret = wolfSSL_AsyncInit(ssl, asyncDev, + WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + goto exit_ppc; + } + #endif + /* only parse if not already present in dCert from above */ + ret = ParseCertRelative(args->dCert, CERT_TYPE, !ssl->options.verifyNone, ssl->ctx->cm); #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, - args->dCert->sigCtx.asyncDev, - WC_ASYNC_FLAG_CALL_AGAIN); - goto exit_ppc; - } + if (asyncDev && ret == WC_PENDING_E) { + ret = wolfSSL_AsyncPush(ssl, asyncDev); + goto exit_ppc; + } + } while (ret == WC_PENDING_E && asyncDev == NULL); #endif } @@ -10478,12 +10630,18 @@ static INLINE int EncryptDo(WOLFSSL* ssl, byte* out, const byte* input, #ifdef BUILD_DES3 case wolfssl_triple_des: + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + asyncDev = &ssl->encrypt.des3->asyncDev; + ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); + if (ret != 0) + break; + #endif + ret = wc_Des3_CbcEncrypt(ssl->encrypt.des3, out, input, sz); #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) { - asyncDev = &ssl->encrypt.des3->asyncDev; - if (asyncOkay) - ret = wolfSSL_AsyncPush(ssl, asyncDev, event_flags); + if (ret == WC_PENDING_E && asyncOkay) { + ret = wolfSSL_AsyncPush(ssl, asyncDev); } #endif break; @@ -10491,13 +10649,18 @@ static INLINE int EncryptDo(WOLFSSL* ssl, byte* out, const byte* input, #ifdef BUILD_AES case wolfssl_aes: + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + asyncDev = &ssl->encrypt.aes->asyncDev; + ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); + if (ret != 0) + break; + #endif + ret = wc_AesCbcEncrypt(ssl->encrypt.aes, out, input, sz); #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) { - asyncDev = &ssl->encrypt.aes->asyncDev; - if (asyncOkay) - ret = wolfSSL_AsyncPush(ssl, asyncDev, event_flags); - break; + if (ret == WC_PENDING_E && asyncOkay) { + ret = wolfSSL_AsyncPush(ssl, asyncDev); } #endif break; @@ -10509,6 +10672,15 @@ static INLINE int EncryptDo(WOLFSSL* ssl, byte* out, const byte* input, { wc_AesAuthEncryptFunc aes_auth_fn; const byte* additionalSrc; + + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + asyncDev = &ssl->encrypt.aes->asyncDev; + ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); + if (ret != 0) + break; + #endif + #if defined(BUILD_AESGCM) && defined(HAVE_AESCCM) aes_auth_fn = (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm) ? wc_AesGcmEncrypt : wc_AesCcmEncrypt; @@ -10550,10 +10722,8 @@ static INLINE int EncryptDo(WOLFSSL* ssl, byte* out, const byte* input, ssl->specs.aead_mac_size, ssl->encrypt.additional, AEAD_AUTH_DATA_SZ); #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) { - asyncDev = &ssl->encrypt.aes->asyncDev; - if (asyncOkay) - ret = wolfSSL_AsyncPush(ssl, asyncDev, event_flags); + if (ret == WC_PENDING_E && asyncOkay) { + ret = wolfSSL_AsyncPush(ssl, asyncDev); } #endif } @@ -10724,11 +10894,18 @@ static INLINE int DecryptDo(WOLFSSL* ssl, byte* plain, const byte* input, #ifdef BUILD_DES3 case wolfssl_triple_des: + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.des3->asyncDev, + WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + break; + #endif + ret = wc_Des3_CbcDecrypt(ssl->decrypt.des3, plain, input, sz); #ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.des3->asyncDev, - WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.des3->asyncDev); } #endif break; @@ -10736,11 +10913,18 @@ static INLINE int DecryptDo(WOLFSSL* ssl, byte* plain, const byte* input, #ifdef BUILD_AES case wolfssl_aes: + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, + WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + break; + #endif + ret = wc_AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz); #ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev, - WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev); } #endif break; @@ -10751,6 +10935,15 @@ static INLINE int DecryptDo(WOLFSSL* ssl, byte* plain, const byte* input, case wolfssl_aes_ccm: /* GCM AEAD macros use same size as CCM */ { wc_AesAuthDecryptFunc aes_auth_fn; + + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, + WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + break; + #endif + #if defined(BUILD_AESGCM) && defined(HAVE_AESCCM) aes_auth_fn = (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm) ? wc_AesGcmDecrypt : wc_AesCcmDecrypt; @@ -10785,9 +10978,7 @@ static INLINE int DecryptDo(WOLFSSL* ssl, byte* plain, const byte* input, ssl->decrypt.additional, AEAD_AUTH_DATA_SZ)) < 0) { #ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, - &ssl->decrypt.aes->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); - break; + ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev); } #endif } @@ -24433,7 +24624,7 @@ int wolfSSL_AsyncPop(WOLFSSL* ssl, byte* state) return ret; } -int wolfSSL_AsyncPush(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev, word32 flags) +int wolfSSL_AsyncInit(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev, word32 flags) { int ret; WOLF_EVENT* event; @@ -24447,13 +24638,30 @@ int wolfSSL_AsyncPush(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev, word32 flags) /* init event */ ret = wolfAsync_EventInit(event, WOLF_EVENT_TYPE_ASYNC_WOLFSSL, ssl, flags); - if (ret == 0) { - ssl->async.dev = asyncDev; - /* place event into queue */ - ret = wolfAsync_EventQueuePush(&ssl->ctx->event_queue, event); + WOLFSSL_LEAVE("wolfSSL_AsyncInit", ret); + + return ret; +} + +int wolfSSL_AsyncPush(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev) +{ + int ret; + WOLF_EVENT* event; + + if (ssl == NULL || asyncDev == NULL) { + return BAD_FUNC_ARG; } + /* grab event pointer */ + event = &asyncDev->event; + + /* store reference to active async operation */ + ssl->async.dev = asyncDev; + + /* place event into queue */ + ret = wolfAsync_EventQueuePush(&ssl->ctx->event_queue, event); + /* success means return WC_PENDING_E */ if (ret == 0) { ret = WC_PENDING_E; diff --git a/src/tls13.c b/src/tls13.c index 7f2d00bb6..309cc881b 100644 --- a/src/tls13.c +++ b/src/tls13.c @@ -1618,25 +1618,35 @@ static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input, switch (ssl->specs.bulk_cipher_algorithm) { #ifdef BUILD_AESGCM case wolfssl_aes_gcm: + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + asyncDev = &ssl->encrypt.aes->asyncDev; + ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); + if (ret != 0) + break; + #endif + nonceSz = AESGCM_NONCE_SZ; ret = wc_AesGcmEncrypt(ssl->encrypt.aes, output, input, dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, macSz, NULL, 0); - #ifdef WOLFSSL_ASYNC_CRYPT - asyncDev = &ssl->encrypt.aes->asyncDev; - #endif break; #endif #ifdef HAVE_AESCCM case wolfssl_aes_ccm: + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + asyncDev = &ssl->encrypt.aes->asyncDev; + ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); + if (ret != 0) + break; + #endif + nonceSz = AESCCM_NONCE_SZ; ret = wc_AesCcmEncrypt(ssl->encrypt.aes, output, input, dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, macSz, NULL, 0); - #ifdef WOLFSSL_ASYNC_CRYPT - asyncDev = &ssl->encrypt.aes->asyncDev; - #endif break; #endif @@ -1663,9 +1673,7 @@ static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input, } else { /* If pending, then leave and return will resume below */ - ret = wolfSSL_AsyncPush(ssl, asyncDev, event_flags); - - return ret; + return wolfSSL_AsyncPush(ssl, asyncDev); } } #endif @@ -1824,14 +1832,21 @@ int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input, word16 sz) switch (ssl->specs.bulk_cipher_algorithm) { #ifdef BUILD_AESGCM case wolfssl_aes_gcm: + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, + WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + break; + #endif + nonceSz = AESGCM_NONCE_SZ; ret = wc_AesGcmDecrypt(ssl->decrypt.aes, output, input, dataSz, ssl->decrypt.nonce, nonceSz, input + dataSz, macSz, NULL, 0); #ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev, - WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev); } #endif break; @@ -1839,14 +1854,21 @@ int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input, word16 sz) #ifdef HAVE_AESCCM case wolfssl_aes_ccm: + #ifdef WOLFSSL_ASYNC_CRYPT + /* intialize event */ + ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, + WC_ASYNC_FLAG_CALL_AGAIN); + if (ret != 0) + break; + #endif + nonceSz = AESCCM_NONCE_SZ; ret = wc_AesCcmDecrypt(ssl->decrypt.aes, output, input, dataSz, ssl->decrypt.nonce, nonceSz, input + dataSz, macSz, NULL, 0); #ifdef WOLFSSL_ASYNC_CRYPT if (ret == WC_PENDING_E) { - ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev, - WC_ASYNC_FLAG_CALL_AGAIN); + ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev); } #endif break; diff --git a/tests/api.c b/tests/api.c index 9e826a308..0e1538bee 100644 --- a/tests/api.c +++ b/tests/api.c @@ -4969,9 +4969,15 @@ static int test_wc_Md5HmacFinal (void) printf(testingFmt, "wc_HmacFinal() with MD5"); - ret = wc_HmacSetKey(&hmac, MD5, (byte*)key, (word32)XSTRLEN(key)); - if (ret != 0) { + ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); + if (ret != 0) flag = ret; + + if (!flag) { + ret = wc_HmacSetKey(&hmac, MD5, (byte*)key, (word32)XSTRLEN(key)); + if (ret != 0) { + flag = ret; + } } if (!flag) { @@ -5010,6 +5016,9 @@ static int test_wc_Md5HmacFinal (void) } } #endif + + wc_HmacFree(&hmac); + printf(resultFmt, flag == 0 ? passed : failed); #endif @@ -5042,9 +5051,15 @@ static int test_wc_ShaHmacFinal (void) printf(testingFmt, "wc_HmacFinal() with SHA"); - ret = wc_HmacSetKey(&hmac, SHA, (byte*)key, (word32)XSTRLEN(key)); - if (ret != 0) { + ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); + if (ret != 0) flag = ret; + + if (!flag) { + ret = wc_HmacSetKey(&hmac, SHA, (byte*)key, (word32)XSTRLEN(key)); + if (ret != 0) { + flag = ret; + } } if (!flag) { @@ -5083,6 +5098,9 @@ static int test_wc_ShaHmacFinal (void) } } #endif + + wc_HmacFree(&hmac); + printf(resultFmt, flag == 0 ? passed : failed); #endif @@ -5116,9 +5134,15 @@ static int test_wc_Sha224HmacFinal (void) printf(testingFmt, "wc_HmacFinal() with SHA224"); - ret = wc_HmacSetKey(&hmac, SHA224, (byte*)key, (word32)XSTRLEN(key)); - if (ret != 0) { + ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); + if (ret != 0) flag = ret; + + if (!flag) { + ret = wc_HmacSetKey(&hmac, SHA224, (byte*)key, (word32)XSTRLEN(key)); + if (ret != 0) { + flag = ret; + } } if (!flag) { @@ -5157,6 +5181,9 @@ static int test_wc_Sha224HmacFinal (void) } } #endif + + wc_HmacFree(&hmac); + printf(resultFmt, flag == 0 ? passed : failed); #endif @@ -5189,9 +5216,15 @@ static int test_wc_Sha256HmacFinal (void) printf(testingFmt, "wc_HmacFinal() with SHA256"); - ret = wc_HmacSetKey(&hmac, SHA256, (byte*)key, (word32)XSTRLEN(key)); - if (ret != 0) { + ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); + if (ret != 0) flag = ret; + + if (!flag) { + ret = wc_HmacSetKey(&hmac, SHA256, (byte*)key, (word32)XSTRLEN(key)); + if (ret != 0) { + flag = ret; + } } if (!flag) { @@ -5230,6 +5263,9 @@ static int test_wc_Sha256HmacFinal (void) } } #endif + + wc_HmacFree(&hmac); + printf(resultFmt, flag == 0 ? passed : failed); #endif @@ -5263,9 +5299,15 @@ static int test_wc_Sha384HmacFinal (void) printf(testingFmt, "wc_HmacFinal() with SHA384"); - ret = wc_HmacSetKey(&hmac, SHA384, (byte*)key, (word32)XSTRLEN(key)); - if (ret != 0) { + ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); + if (ret != 0) flag = ret; + + if (!flag) { + ret = wc_HmacSetKey(&hmac, SHA384, (byte*)key, (word32)XSTRLEN(key)); + if (ret != 0) { + flag = ret; + } } if (!flag) { @@ -5303,6 +5345,9 @@ static int test_wc_Sha384HmacFinal (void) } } #endif + + wc_HmacFree(&hmac); + printf(resultFmt, flag == 0 ? passed : failed); #endif diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 9e3ad4200..3c6d831e9 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -136,9 +136,9 @@ #define INIT_CYCLE_COUNTER #define BEGIN_INTEL_CYCLES total_cycles = get_intel_cycles(); #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles; - #define SHOW_INTEL_CYCLES printf(" Cycles per byte = %6.2f", \ - count == 0 ? 0 : \ - (float)total_cycles / ((word64)count*BENCH_SIZE)); + #define SHOW_INTEL_CYCLES(b, n) \ + XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " Cycles per byte = %6.2f\n", \ + count == 0 ? 0 : (float)total_cycles / ((word64)count*BENCH_SIZE)) #elif defined(LINUX_CYCLE_COUNT) #include #include @@ -161,14 +161,15 @@ total_cycles = total_cycles - begin_cycles; \ } while (0); - #define SHOW_INTEL_CYCLES printf(" Cycles per byte = %6.2f", \ - (float)total_cycles / (count*BENCH_SIZE)); + #define SHOW_INTEL_CYCLES(b, n) \ + XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " Cycles per byte = %6.2f\n", \ + (float)total_cycles / (count*BENCH_SIZE)) #else #define INIT_CYCLE_COUNTER #define BEGIN_INTEL_CYCLES #define END_INTEL_CYCLES - #define SHOW_INTEL_CYCLES + #define SHOW_INTEL_CYCLES(b, n) b[XSTRLEN(b)] = '\n' #endif /* let's use buffers, we have them */ @@ -218,18 +219,22 @@ static THREAD_LS_T WC_RNG rng; #endif +#if defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || defined(HAVE_ECC) || \ + defined(HAVE_ECC) || defined(HAVE_NTRU) || !defined(NO_DH) || \ + !defined(NO_RSA) || defined(HAVE_SCRYPT) + + #define BENCH_ASYM +#endif /* Asynchronous helper macros */ static THREAD_LS_T int devId = INVALID_DEVID; #ifdef WOLFSSL_ASYNC_CRYPT - static THREAD_LS_T WOLF_EVENT_QUEUE eventQueue; - static THREAD_LS_T int asyncPending; + static WOLF_EVENT_QUEUE eventQueue; #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev) #define BENCH_ASYNC_GET_NAME(doAsync) (doAsync) ? "HW" : "SW" - #define BENCH_ASYNC_IS_PEND() (asyncPending > 0) #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING) #ifndef WC_NO_ASYNC_THREADING @@ -240,114 +245,116 @@ static THREAD_LS_T int devId = INVALID_DEVID; static int g_threadCount; #endif - static INLINE int bench_async_begin(void) { - /* init event queue */ - asyncPending = 0; - return wolfEventQueue_Init(&eventQueue); - } - - static INLINE void bench_async_end(void) { - /* free event queue */ - wolfEventQueue_Free(&eventQueue); - } - - static INLINE void bench_async_complete(int* ret, WC_ASYNC_DEV* asyncDev, - int* times) - { - *ret = asyncDev->event.ret; - if (*ret >= 0) { - (*times)++; - asyncDev->event.done = 0; /* reset done flag */ - } - } - - static INLINE int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev, - int callAgain, int* times, int limit) + static int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev, + int callAgain, int* times, int limit, int* pending) { int allowNext = 0; + /* this state can be set from a different thread */ + WOLF_EVENT_STATE state = asyncDev->event.state; + /* if algo doesn't require calling again then use this flow */ - if (!callAgain) { - if (asyncDev->event.done) { - /* operation completed */ - bench_async_complete(ret, asyncDev, times); - } - } - /* if algo does require calling again then use this flow */ - else { - if (asyncDev->event.done) { + if (state == WOLF_EVENT_STATE_DONE) { + if (callAgain) { + /* needs called again, so allow it and handle completion in bench_async_handle */ allowNext = 1; } + else { + *ret = asyncDev->event.ret; + asyncDev->event.state = WOLF_EVENT_STATE_READY; + (*times)++; + if (*pending > 0) /* to support case where async blocks */ + (*pending)--; + + if ((*times + *pending) < limit) + allowNext = 1; + } } - if (asyncDev->event.pending == 0 && - (*times + asyncPending) < limit) { + /* if slot is available and we haven't reached limit, start another */ + else if (state == WOLF_EVENT_STATE_READY && (*times + *pending) < limit) { allowNext = 1; } return allowNext; } - static INLINE int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev, - int callAgain, int* times) + static int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev, + int callAgain, int* times, int* pending) { + WOLF_EVENT_STATE state = asyncDev->event.state; + if (*ret == WC_PENDING_E) { - *ret = wc_AsyncHandle(asyncDev, &eventQueue, - callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE); - if (*ret == 0) - asyncPending++; + if (state == WOLF_EVENT_STATE_DONE) { + *ret = asyncDev->event.ret; + asyncDev->event.state = WOLF_EVENT_STATE_READY; + (*times)++; + (*pending)--; + } + else { + (*pending)++; + *ret = wc_AsyncHandle(asyncDev, &eventQueue, + callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE); + } } else if (*ret >= 0) { - /* operation completed */ - bench_async_complete(ret, asyncDev, times); + *ret = asyncDev->event.ret; + asyncDev->event.state = WOLF_EVENT_STATE_READY; + (*times)++; + if (*pending > 0) /* to support case where async blocks */ + (*pending)--; } return (*ret >= 0) ? 1 : 0; } - static INLINE void bench_async_poll(void) + static INLINE int bench_async_poll(int* pending) { - /* poll until there are events done */ - if (asyncPending > 0) { - int ret, asyncDone = 0; - do { - ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0, + int ret, asyncDone = 0; + + ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0, WOLF_POLL_FLAG_CHECK_HW, &asyncDone); - if (ret != 0) { - printf("Async poll failed %d\n", ret); - return; - } - } while (asyncDone == 0); - asyncPending -= asyncDone; + if (ret != 0) { + printf("Async poll failed %d\n", ret); + return ret; } + + if (asyncDone == 0) { + #ifndef WC_NO_ASYNC_THREADING + /* give time to other threads */ + wc_AsyncThreadYield(); + #endif + } + + (void)pending; + + return asyncDone; } #else #define BENCH_MAX_PENDING (1) #define BENCH_ASYNC_GET_NAME(doAsync) "" #define BENCH_ASYNC_GET_DEV(obj) NULL - #define BENCH_ASYNC_IS_PEND() (0) - - #define bench_async_begin() - #define bench_async_end() (void)doAsync; static INLINE int bench_async_check(int* ret, void* asyncDev, - int callAgain, int* times, int limit) + int callAgain, int* times, int limit, int* pending) { (void)ret; (void)asyncDev; (void)callAgain; (void)times; (void)limit; + (void)pending; return 1; } static INLINE int bench_async_handle(int* ret, void* asyncDev, - int callAgain, int* times) + int callAgain, int* times, int* pending) { (void)asyncDev; (void)callAgain; + (void)pending; if (*ret >= 0) { /* operation completed */ @@ -356,7 +363,7 @@ static THREAD_LS_T int devId = INVALID_DEVID; } return 0; } - #define bench_async_poll() + #define bench_async_poll(p) #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -447,6 +454,7 @@ static byte* bench_iv = (byte*)bench_iv_buf; int doAsync; int finishCount; bench_stat_type_t type; + int lastRet; } bench_stats_t; static bench_stats_t* bench_stats_head; static bench_stats_t* bench_stats_tail; @@ -454,10 +462,11 @@ static byte* bench_iv = (byte*)bench_iv_buf; static bench_stats_t* bench_stats_add(bench_stat_type_t type, const char* algo, int strength, const char* desc, int doAsync, - double perfsec) + double perfsec, int ret) { bench_stats_t* stat; + /* protect bench_stats_head and bench_stats_tail access */ pthread_mutex_lock(&bench_lock); /* locate existing in list */ @@ -496,6 +505,8 @@ static byte* bench_iv = (byte*)bench_iv_buf; stat->doAsync = doAsync; stat->perfsec += perfsec; stat->finishCount++; + if (stat->lastRet > ret) + stat->lastRet = ret; /* track last error */ if (stat->finishCount == g_threadCount) { isLast = 1; @@ -511,7 +522,7 @@ static byte* bench_iv = (byte*)bench_iv_buf; /* print final stat */ if (isLast) { if (stat->type == BENCH_STAT_SYM) { - printf("%-8s%s %8.3f MB/s\n", stat->desc, + printf("%-12s%s %8.3f MB/s\n", stat->desc, BENCH_ASYNC_GET_NAME(stat->doAsync), stat->perfsec); } else { @@ -550,10 +561,11 @@ static INLINE int bench_stats_sym_check(double start) return ((current_time(0) - start) < BENCH_MIN_RUNTIME_SEC); } -static void bench_stats_sym_finish(const char* desc, int doAsync, int count, double start) +static void bench_stats_sym_finish(const char* desc, int doAsync, int count, double start, int ret) { double total, persec = 0, blocks = count; const char* blockType; + char msg[128] = {0}; END_INTEL_CYCLES total = current_time(0) - start; @@ -579,26 +591,29 @@ static void bench_stats_sym_finish(const char* desc, int doAsync, int count, dou persec = (1 / total) * blocks; } - printf("%-12s%s %5.0f %s took %5.3f seconds, %8.3f %s/s", + XSNPRINTF(msg, sizeof(msg), "%-12s%s %5.0f %s took %5.3f seconds, %8.3f %s/s", desc, BENCH_ASYNC_GET_NAME(doAsync), blocks, blockType, total, persec, blockType); - SHOW_INTEL_CYCLES - printf("\n"); + SHOW_INTEL_CYCLES(msg, sizeof(msg)); + printf("%s", msg); + + /* show errors */ + if (ret < 0) { + printf("Benchmark %s failed: %d\n", desc, ret); + } #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING) /* Add to thread stats */ - bench_stats_add(BENCH_STAT_SYM, NULL, 0, desc, doAsync, persec); + bench_stats_add(BENCH_STAT_SYM, NULL, 0, desc, doAsync, persec, ret); #endif + (void)doAsync; + (void)ret; } -/* declare here rather than creating a static function to avoid warning of not - * used in the case of something like a leanpsk only build */ -void bench_stats_asym_finish(const char* algo, int strength, - const char* desc, int doAsync, int count, double start); - -void bench_stats_asym_finish(const char* algo, int strength, - const char* desc, int doAsync, int count, double start) +#ifdef BENCH_ASYM +static void bench_stats_asym_finish(const char* algo, int strength, + const char* desc, int doAsync, int count, double start, int ret) { double total, each = 0, opsSec, milliEach; @@ -611,13 +626,21 @@ void bench_stats_asym_finish(const char* algo, int strength, printf("%-5s %4d %-9s %s %6d ops took %5.3f sec, avg %5.3f ms," " %.3f ops/sec\n", algo, strength, desc, BENCH_ASYNC_GET_NAME(doAsync), count, total, milliEach, opsSec); - (void)doAsync; + + /* show errors */ + if (ret < 0) { + printf("Benchmark %s %s %d failed: %d\n", algo, desc, strength, ret); + } #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING) /* Add to thread stats */ - bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, doAsync, opsSec); + bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, doAsync, opsSec, ret); #endif + + (void)doAsync; + (void)ret; } +#endif /* BENCH_ASYM */ static INLINE void bench_stats_free(void) { @@ -654,6 +677,12 @@ static void* benchmarks_do(void* args) (void)args; +#ifdef WOLFSSL_ASYNC_CRYPT + if (wolfEventQueue_Init(&eventQueue) != 0) { + printf("Async event queue init failure!\n"); + } +#endif + #if defined(HAVE_LOCAL_RNG) { int rngRet; @@ -898,6 +927,11 @@ static void* benchmarks_do(void* args) bench_ed25519KeySign(); #endif +#ifdef WOLFSSL_ASYNC_CRYPT + /* free event queue */ + wolfEventQueue_Free(&eventQueue); +#endif + #if defined(HAVE_LOCAL_RNG) wc_FreeRng(&rng); #endif @@ -1103,11 +1137,7 @@ void bench_rng(void) count += i; } while (bench_stats_sym_check(start)); exit_rng: - bench_stats_sym_finish("RNG", 0, count, start); - - if (ret < 0) { - printf("wc_RNG_GenerateBlock failed %d\n", ret); - } + bench_stats_sym_finish("RNG", 0, count, start, ret); wc_FreeRng(&myrng); } @@ -1119,12 +1149,10 @@ exit_rng: #ifdef HAVE_AES_CBC void bench_aescbc(int doAsync) { - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; Aes enc[BENCH_MAX_PENDING]; double start; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); @@ -1145,15 +1173,15 @@ void bench_aescbc(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_AesCbcEncrypt(&enc[i], bench_plain, bench_cipher, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_enc; } } @@ -1162,7 +1190,7 @@ void bench_aescbc(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_aes_enc: - bench_stats_sym_finish("AES-Enc", doAsync, count, start); + bench_stats_sym_finish("AES-Enc", doAsync, count, start, ret); if (ret < 0) { goto exit; @@ -1180,15 +1208,15 @@ exit_aes_enc: bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_AesCbcDecrypt(&enc[i], bench_plain, bench_cipher, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_dec; } } @@ -1197,36 +1225,28 @@ exit_aes_enc: count += times; } while (bench_stats_sym_check(start)); exit_aes_dec: - bench_stats_sym_finish("AES-Dec", doAsync, count, start); + bench_stats_sym_finish("AES-Dec", doAsync, count, start, ret); #endif /* HAVE_AES_DECRYPT */ exit: - if (ret < 0) { - printf("bench_aescbc failed: %d\n", ret); - } - for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_AesFree(&enc[i]); } - - bench_async_end(); } #endif /* HAVE_AES_CBC */ #ifdef HAVE_AESGCM void bench_aesgcm(int doAsync) { - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; Aes enc[BENCH_MAX_PENDING]; double start; DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT); DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); #ifdef WOLFSSL_ASYNC_CRYPT @@ -1256,17 +1276,17 @@ void bench_aesgcm(int doAsync) /* GCM uses same routine in backend for both encrypt and decrypt */ bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_AesGcmEncrypt(&enc[i], bench_cipher, bench_plain, BENCH_SIZE, bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ, bench_additional, AES_AUTH_ADD_SZ); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_gcm; } } @@ -1275,22 +1295,22 @@ void bench_aesgcm(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_aes_gcm: - bench_stats_sym_finish("AES-GCM-Enc", doAsync, count, start); + bench_stats_sym_finish("AES-GCM-Enc", doAsync, count, start, ret); /* GCM uses same routine in backend for both encrypt and decrypt */ bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_AesGcmDecrypt(&enc[i], bench_plain, bench_cipher, BENCH_SIZE, bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ, bench_additional, AES_AUTH_ADD_SZ); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_gcm_dec; } } @@ -1299,7 +1319,7 @@ exit_aes_gcm: count += times; } while (bench_stats_sym_check(start)); exit_aes_gcm_dec: - bench_stats_sym_finish("AES-GCM-Dec", doAsync, count, start); + bench_stats_sym_finish("AES-GCM-Dec", doAsync, count, start, ret); exit: @@ -1313,8 +1333,6 @@ exit: FREE_VAR(bench_additional, HEAP_HINT); FREE_VAR(bench_tag, HEAP_HINT); - - bench_async_end(); } #endif /* HAVE_AESGCM */ @@ -1338,7 +1356,7 @@ void bench_aesctr(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CTR", 0, count, start); + bench_stats_sym_finish("AES-CTR", 0, count, start, ret); } #endif /* WOLFSSL_AES_COUNTER */ @@ -1367,7 +1385,7 @@ void bench_aesccm(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CCM", 0, count, start); + bench_stats_sym_finish("AES-CCM", 0, count, start, ret); FREE_VAR(bench_additional, HEAP_HINT); FREE_VAR(bench_tag, HEAP_HINT); @@ -1402,7 +1420,7 @@ void bench_poly1305() wc_Poly1305Final(&enc, mac); count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("POLY1305", 0, count, start); + bench_stats_sym_finish("POLY1305", 0, count, start, ret); } #endif /* HAVE_POLY1305 */ @@ -1425,14 +1443,14 @@ void bench_camellia(void) for (i = 0; i < numBlocks; i++) { ret = wc_CamelliaCbcEncrypt(&cam, bench_plain, bench_cipher, BENCH_SIZE); - if (ret <= 0) { + if (ret < 0) { printf("CamelliaCbcEncrypt failed: %d\n", ret); return; } } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("Camellia", 0, count, start); + bench_stats_sym_finish("Camellia", 0, count, start, ret); } #endif @@ -1440,12 +1458,10 @@ void bench_camellia(void) #ifndef NO_DES3 void bench_des(int doAsync) { - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; Des3 enc[BENCH_MAX_PENDING]; double start; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); @@ -1466,15 +1482,15 @@ void bench_des(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_Des3_CbcEncrypt(&enc[i], bench_plain, bench_cipher, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_3des; } } @@ -1483,19 +1499,13 @@ void bench_des(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_3des: - bench_stats_sym_finish("3DES", doAsync, count, start); + bench_stats_sym_finish("3DES", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_des failed: %d\n", ret); - } - for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Des3Free(&enc[i]); } - - bench_async_end(); } #endif /* !NO_DES3 */ @@ -1521,7 +1531,7 @@ void bench_idea(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("IDEA", 0, count, start); + bench_stats_sym_finish("IDEA", 0, count, start, ret); } #endif /* HAVE_IDEA */ @@ -1529,12 +1539,10 @@ void bench_idea(void) #ifndef NO_RC4 void bench_arc4(int doAsync) { - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; Arc4 enc[BENCH_MAX_PENDING]; double start; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); @@ -1555,15 +1563,15 @@ void bench_arc4(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_Arc4Process(&enc[i], bench_cipher, bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_arc4; } } @@ -1572,19 +1580,13 @@ void bench_arc4(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_arc4: - bench_stats_sym_finish("ARC4", doAsync, count, start); + bench_stats_sym_finish("ARC4", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_arc4 failed: %d\n", ret); - } - for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Arc4Free(&enc[i]); } - - bench_async_end(); } #endif /* !NO_RC4 */ @@ -1605,7 +1607,7 @@ void bench_hc128(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("HC128", 0, count, start); + bench_stats_sym_finish("HC128", 0, count, start, 0); } #endif /* HAVE_HC128 */ @@ -1626,7 +1628,7 @@ void bench_rabbit(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("RABBIT", 0, count, start); + bench_stats_sym_finish("RABBIT", 0, count, start, 0); } #endif /* NO_RABBIT */ @@ -1648,7 +1650,7 @@ void bench_chacha(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("CHACHA", 0, count, start); + bench_stats_sym_finish("CHACHA", 0, count, start, 0); } #endif /* HAVE_CHACHA*/ @@ -1656,7 +1658,7 @@ void bench_chacha(void) void bench_chacha20_poly1305_aead(void) { double start; - int ret, i, count; + int ret = 0, i, count; byte authTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]; XMEMSET(authTag, 0, sizeof(authTag)); @@ -1673,7 +1675,7 @@ void bench_chacha20_poly1305_aead(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("CHA-POLY", 0, count, start); + bench_stats_sym_finish("CHA-POLY", 0, count, start, ret); } #endif /* HAVE_CHACHA && HAVE_POLY1305 */ @@ -1683,11 +1685,9 @@ void bench_md5(int doAsync) { Md5 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, MD5_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -1703,15 +1703,15 @@ void bench_md5(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Md5Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_md5; } } @@ -1721,27 +1721,23 @@ void bench_md5(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Md5Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_md5; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_md5: - bench_stats_sym_finish("MD5", doAsync, count, start); + bench_stats_sym_finish("MD5", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_md5 failed: %d\n", ret); - } - #ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Md5Free(&hash[i]); @@ -1749,8 +1745,6 @@ exit: #endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* !NO_MD5 */ @@ -1760,11 +1754,9 @@ void bench_sha(int doAsync) { Sha hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -1780,15 +1772,15 @@ void bench_sha(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_ShaUpdate(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha; } } @@ -1798,36 +1790,28 @@ void bench_sha(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_ShaFinal(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha: - bench_stats_sym_finish("SHA", doAsync, count, start); + bench_stats_sym_finish("SHA", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_ShaFree(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* NO_SHA */ @@ -1837,11 +1821,9 @@ void bench_sha224(int doAsync) { Sha224 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA224_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -1857,15 +1839,15 @@ void bench_sha224(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha224Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha224; } } @@ -1875,35 +1857,27 @@ void bench_sha224(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha224Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha224; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha224: - bench_stats_sym_finish("SHA-224", doAsync, count, start); + bench_stats_sym_finish("SHA-224", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha224 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha224Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif @@ -1912,11 +1886,9 @@ void bench_sha256(int doAsync) { Sha256 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA256_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -1932,15 +1904,15 @@ void bench_sha256(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha256Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha256; } } @@ -1950,35 +1922,27 @@ void bench_sha256(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha256Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha256; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha256: - bench_stats_sym_finish("SHA-256", doAsync, count, start); + bench_stats_sym_finish("SHA-256", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha256 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha256Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif @@ -1987,11 +1951,9 @@ void bench_sha384(int doAsync) { Sha384 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA384_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2007,15 +1969,15 @@ void bench_sha384(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha384Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha384; } } @@ -2025,35 +1987,27 @@ void bench_sha384(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha384Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha384; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha384: - bench_stats_sym_finish("SHA-384", doAsync, count, start); + bench_stats_sym_finish("SHA-384", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha384 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha384Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif @@ -2062,11 +2016,9 @@ void bench_sha512(int doAsync) { Sha512 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA512_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2082,15 +2034,15 @@ void bench_sha512(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha512Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha512; } } @@ -2100,35 +2052,27 @@ void bench_sha512(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha512Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha512; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha512: - bench_stats_sym_finish("SHA-512", doAsync, count, start); + bench_stats_sym_finish("SHA-512", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha512 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha512Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif @@ -2139,11 +2083,9 @@ void bench_sha3_224(int doAsync) { Sha3 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA3_224_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2159,15 +2101,15 @@ void bench_sha3_224(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_224_Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_224; } } @@ -2177,35 +2119,27 @@ void bench_sha3_224(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_224_Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_224; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_224: - bench_stats_sym_finish("SHA3-224", doAsync, count, start); + bench_stats_sym_finish("SHA3-224", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha3_224 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha3_224_Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* WOLFSSL_NOSHA3_224 */ @@ -2214,11 +2148,9 @@ void bench_sha3_256(int doAsync) { Sha3 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA3_256_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2234,15 +2166,15 @@ void bench_sha3_256(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_256_Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_256; } } @@ -2252,35 +2184,27 @@ void bench_sha3_256(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_256_Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_256; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_256: - bench_stats_sym_finish("SHA3-256", doAsync, count, start); + bench_stats_sym_finish("SHA3-256", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha3_256 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha3_256_Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* WOLFSSL_NOSHA3_256 */ @@ -2289,11 +2213,9 @@ void bench_sha3_384(int doAsync) { Sha3 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA3_384_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2309,15 +2231,15 @@ void bench_sha3_384(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_384_Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_384; } } @@ -2327,35 +2249,27 @@ void bench_sha3_384(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_384_Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_384; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_384: - bench_stats_sym_finish("SHA3-384", doAsync, count, start); + bench_stats_sym_finish("SHA3-384", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha3_384 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha3_384_Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* WOLFSSL_NOSHA3_384 */ @@ -2364,11 +2278,9 @@ void bench_sha3_512(int doAsync) { Sha3 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA3_512_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2384,15 +2296,15 @@ void bench_sha3_512(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_512_Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_512; } } @@ -2402,35 +2314,27 @@ void bench_sha3_512(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_512_Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_512; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_512: - bench_stats_sym_finish("SHA3-512", doAsync, count, start); + bench_stats_sym_finish("SHA3-512", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha3_512 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha3_512_Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* WOLFSSL_NOSHA3_512 */ #endif @@ -2464,7 +2368,7 @@ int bench_ripemd(void) count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("RIPEMD", 0, count, start); + bench_stats_sym_finish("RIPEMD", 0, count, start, ret); return 0; } @@ -2501,7 +2405,7 @@ void bench_blake2(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("BLAKE2b", 0, count, start); + bench_stats_sym_finish("BLAKE2b", 0, count, start, ret); } #endif @@ -2539,7 +2443,7 @@ void bench_cmac(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CMAC", 0, count, start); + bench_stats_sym_finish("AES-CMAC", 0, count, start, ret); } #endif /* WOLFSSL_CMAC */ @@ -2565,7 +2469,7 @@ void bench_scrypt(void) count += i; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("scrypt", 0, "", 0, count, start); + bench_stats_asym_finish("scrypt", 0, "", 0, count, start, ret); } #endif /* HAVE_SCRYPT */ @@ -2577,13 +2481,11 @@ void bench_rsaKeyGen(int doAsync) { RsaKey genKey[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; int k, keySz; const int keySizes[2] = {1024, 2048}; const long rsa_e_val = 65537; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(genKey, 0, sizeof(genKey)); @@ -2593,11 +2495,11 @@ void bench_rsaKeyGen(int doAsync) bench_stats_start(&count, &start); do { /* while free pending slots in queue, submit ops */ - for (times = 0; times < genTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < genTimes || pending > 0; ) { + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, genTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, genTimes, &pending)) { wc_FreeRsaKey(&genKey[i]); ret = wc_InitRsaKey_ex(&genKey[i], HEAP_HINT, @@ -2607,7 +2509,7 @@ void bench_rsaKeyGen(int doAsync) } ret = wc_MakeRsaKey(&genKey[i], keySz, rsa_e_val, &rng); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, &pending)) { goto exit; } } @@ -2616,10 +2518,9 @@ void bench_rsaKeyGen(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("RSA", keySz, "key gen", doAsync, count, start); + bench_stats_asym_finish("RSA", keySz, "key gen", doAsync, count, start, ret); if (ret < 0) { - printf("bench_rsaKeyGen failed: %d\n", ret); break; } } @@ -2628,8 +2529,6 @@ void bench_rsaKeyGen(int doAsync) for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_FreeRsaKey(&genKey[i]); } - - bench_async_end(); } #endif /* WOLFSSL_KEY_GEN */ @@ -2649,7 +2548,7 @@ void bench_rsaKeyGen(int doAsync) void bench_rsa(int doAsync) { - int ret, i, times, count = 0; + int ret, i, times, count = 0, pending = 0; size_t bytes; word32 idx = 0; const byte* tmp; @@ -2674,8 +2573,6 @@ void bench_rsa(int doAsync) #error "need a cert buffer size" #endif /* USE_CERT_BUFFERS */ - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(rsaKey, 0, sizeof(rsaKey)); @@ -2705,15 +2602,15 @@ void bench_rsa(int doAsync) /* begin public RSA */ bench_stats_start(&count, &start); do { - for (times = 0; times < ntimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < ntimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, ntimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, ntimes, &pending)) { ret = wc_RsaPublicEncrypt(message, len, enc[i], RSA_BUF_SIZE, &rsaKey[i], &rng); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, &pending)) { goto exit_rsa_pub; } } @@ -2722,35 +2619,27 @@ void bench_rsa(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_rsa_pub: - bench_stats_asym_finish("RSA", rsaKeySz, "public", doAsync, count, start); + bench_stats_asym_finish("RSA", rsaKeySz, "public", doAsync, count, start, ret); if (ret < 0) { goto exit; } -#ifdef WOLFSSL_ASYNC_CRYPT - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&rsaKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; -#endif - /* capture resulting encrypt length */ idx = rsaKeySz/8; /* begin private async RSA */ bench_stats_start(&count, &start); do { - for (times = 0; times < ntimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < ntimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, ntimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, ntimes, &pending)) { ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i], RSA_BUF_SIZE, &rsaKey[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, &pending)) { goto exit; } } @@ -2759,11 +2648,7 @@ exit_rsa_pub: count += times; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("RSA", rsaKeySz, "private", doAsync, count, start); - - if (ret < 0) { - printf("bench_rsa failed: %d\n", ret); - } + bench_stats_asym_finish("RSA", rsaKeySz, "private", doAsync, count, start, ret); /* cleanup */ for (i = 0; i < BENCH_MAX_PENDING; i++) { @@ -2773,8 +2658,6 @@ exit: FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT); FREE_ARRAY(out, BENCH_MAX_PENDING, HEAP_HINT); FREE_VAR(message, HEAP_HINT); - - bench_async_end(); } #endif /* !NO_RSA */ @@ -2802,7 +2685,7 @@ exit: void bench_dh(int doAsync) { int ret, i; - int count = 0, times; + int count = 0, times, pending = 0; const byte* tmp = NULL; double start = 0.0f; DhKey dhKey[BENCH_MAX_PENDING]; @@ -2839,8 +2722,6 @@ void bench_dh(int doAsync) #error "need to define a cert buffer size" #endif /* USE_CERT_BUFFERS */ - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(dhKey, 0, sizeof(dhKey)); @@ -2869,15 +2750,15 @@ void bench_dh(int doAsync) bench_stats_start(&count, &start); do { /* while free pending slots in queue, submit ops */ - for (times = 0; times < genTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < genTimes || pending > 0; ) { + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, genTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, genTimes, &pending)) { privSz[i] = 0; ret = wc_DhGenerateKeyPair(&dhKey[i], &rng, priv[i], &privSz[i], pub[i], &pubSz[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, &pending)) { goto exit_dh_gen; } } @@ -2886,7 +2767,7 @@ void bench_dh(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_dh_gen: - bench_stats_asym_finish("DH", dhKeySz, "key gen", doAsync, count, start); + bench_stats_asym_finish("DH", dhKeySz, "key gen", doAsync, count, start, ret); if (ret < 0) { goto exit; @@ -2896,26 +2777,20 @@ exit_dh_gen: ret = wc_DhGenerateKeyPair(&dhKey[0], &rng, priv2, &privSz2, pub2, &pubSz2); #ifdef WOLFSSL_ASYNC_CRYPT ret = wc_AsyncWait(ret, &dhKey[0].asyncDev, WC_ASYNC_FLAG_NONE); - - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&dhKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; #endif /* Key Agree */ bench_stats_start(&count, &start); do { - for (times = 0; times < agreeTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < agreeTimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, agreeTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, agreeTimes, &pending)) { ret = wc_DhAgree(&dhKey[i], agree[i], &agreeSz[i], priv[i], privSz[i], pub2, pubSz2); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, &pending)) { goto exit; } } @@ -2924,11 +2799,7 @@ exit_dh_gen: count += times; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("DH", dhKeySz, "key agree", doAsync, count, start); - - if (ret < 0) { - printf("bench_dh failed: %d\n", ret); - } + bench_stats_asym_finish("DH", dhKeySz, "key agree", doAsync, count, start, ret); /* cleanup */ for (i = 0; i < BENCH_MAX_PENDING; i++) { @@ -2940,8 +2811,6 @@ exit: FREE_ARRAY(priv, BENCH_MAX_PENDING, HEAP_HINT); FREE_VAR(priv2, HEAP_HINT); FREE_ARRAY(agree, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* !NO_DH */ @@ -3059,7 +2928,7 @@ void bench_ntru(void) return; } } - bench_stats_asym_finish("NTRU", ntruBits, "encryption", 0, i, start); + bench_stats_asym_finish("NTRU", ntruBits, "encryption", 0, i, start, ret); ret = ntru_crypto_drbg_uninstantiate(drbg); if (ret != DRBG_OK) { @@ -3088,7 +2957,7 @@ void bench_ntru(void) return; } } - bench_stats_asym_finish("NTRU", ntruBits, "decryption", 0, i, start); + bench_stats_asym_finish("NTRU", ntruBits, "decryption", 0, i, start, ret); } } @@ -3141,10 +3010,9 @@ void bench_ntruKeyGen(void) public_key, &private_key_len, private_key); } - bench_stats_asym_finish("NTRU", ntruBits, "key gen", 0, i, start); + bench_stats_asym_finish("NTRU", ntruBits, "key gen", 0, i, start, ret); if (ret != NTRU_OK) { - printf("keygen failed\n"); return; } @@ -3159,17 +3027,18 @@ void bench_ntruKeyGen(void) #endif #ifdef HAVE_ECC -#define BENCH_ECC_SIZE 32 + +#ifndef BENCH_ECC_SIZE + #define BENCH_ECC_SIZE 32 +#endif void bench_eccMakeKey(int doAsync) { - int ret, i, times, count; + int ret = 0, i, times, count, pending = 0; const int keySize = BENCH_ECC_SIZE; ecc_key genKey[BENCH_MAX_PENDING]; double start; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(&genKey, 0, sizeof(genKey)); @@ -3177,11 +3046,11 @@ void bench_eccMakeKey(int doAsync) bench_stats_start(&count, &start); do { /* while free pending slots in queue, submit ops */ - for (times = 0; times < genTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < genTimes || pending > 0; ) { + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, genTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, genTimes, &pending)) { wc_ecc_free(&genKey[i]); ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT, doAsync ? devId : INVALID_DEVID); @@ -3190,7 +3059,7 @@ void bench_eccMakeKey(int doAsync) } ret = wc_ecc_make_key(&rng, keySize, &genKey[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, &pending)) { goto exit; } } @@ -3199,23 +3068,17 @@ void bench_eccMakeKey(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("ECC", keySize * 8, "key gen", doAsync, count, start); - - if (ret < 0) { - printf("bench_eccMakeKey failed: %d\n", ret); - } + bench_stats_asym_finish("ECC", keySize * 8, "key gen", doAsync, count, start, ret); /* cleanup */ for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_ecc_free(&genKey[i]); } - - bench_async_end(); } void bench_ecc(int doAsync) { - int ret, i, times, count; + int ret, i, times, count, pending = 0; const int keySize = BENCH_ECC_SIZE; ecc_key genKey[BENCH_MAX_PENDING]; #ifdef HAVE_ECC_DHE @@ -3237,8 +3100,6 @@ void bench_ecc(int doAsync) #endif DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, BENCH_ECC_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(&genKey, 0, sizeof(genKey)); #ifdef HAVE_ECC_DHE @@ -3271,26 +3132,19 @@ void bench_ecc(int doAsync) } #ifdef HAVE_ECC_DHE -#ifdef WOLFSSL_ASYNC_CRYPT - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&genKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; -#endif /* ECC Shared Secret */ bench_stats_start(&count, &start); do { - for (times = 0; times < agreeTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < agreeTimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes, &pending)) { x[i] = keySize; ret = wc_ecc_shared_secret(&genKey[i], &genKey2[i], shared[i], &x[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, &pending)) { goto exit_ecdhe; } } @@ -3299,7 +3153,7 @@ void bench_ecc(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_ecdhe: - bench_stats_asym_finish("ECDHE", keySize * 8, "agree", doAsync, count, start); + bench_stats_asym_finish("ECDHE", keySize * 8, "agree", doAsync, count, start, ret); if (ret < 0) { goto exit; @@ -3307,13 +3161,6 @@ exit_ecdhe: #endif /* HAVE_ECC_DHE */ #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN) -#ifdef WOLFSSL_ASYNC_CRYPT - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&genKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; -#endif /* Init digest to sign */ for (i = 0; i < BENCH_MAX_PENDING; i++) { @@ -3325,17 +3172,17 @@ exit_ecdhe: /* ECC Sign */ bench_stats_start(&count, &start); do { - for (times = 0; times < agreeTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < agreeTimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes, &pending)) { if (genKey[i].state == 0) x[i] = ECC_MAX_SIG_SIZE; ret = wc_ecc_sign_hash(digest[i], keySize, sig[i], &x[i], &rng, &genKey[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, &pending)) { goto exit_ecdsa_sign; } } @@ -3344,35 +3191,28 @@ exit_ecdhe: count += times; } while (bench_stats_sym_check(start)); exit_ecdsa_sign: - bench_stats_asym_finish("ECDSA", keySize * 8, "sign", doAsync, count, start); + bench_stats_asym_finish("ECDSA", keySize * 8, "sign", doAsync, count, start, ret); if (ret < 0) { goto exit; } #ifdef HAVE_ECC_VERIFY -#ifdef WOLFSSL_ASYNC_CRYPT - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&genKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; -#endif /* ECC Verify */ bench_stats_start(&count, &start); do { - for (times = 0; times < agreeTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < agreeTimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes, &pending)) { if (genKey[i].state == 0) verify[i] = 0; ret = wc_ecc_verify_hash(sig[i], x[i], digest[i], keySize, &verify[i], &genKey[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, &pending)) { goto exit_ecdsa_verify; } } @@ -3381,16 +3221,12 @@ exit_ecdsa_sign: count += times; } while (bench_stats_sym_check(start)); exit_ecdsa_verify: - bench_stats_asym_finish("ECDSA", keySize * 8, "verify", doAsync, count, start); + bench_stats_asym_finish("ECDSA", keySize * 8, "verify", doAsync, count, start, ret); #endif /* HAVE_ECC_VERIFY */ #endif /* !NO_ASN && HAVE_ECC_SIGN */ exit: - if (ret < 0) { - printf("bench_ecc failed: %d\n", ret); - } - /* cleanup */ for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_ecc_free(&genKey[i]); @@ -3406,8 +3242,6 @@ exit: FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT); #endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } @@ -3464,7 +3298,7 @@ void bench_eccEncrypt(void) count += i; } while (bench_stats_sym_check(start)); exit_enc: - bench_stats_asym_finish("ECC", keySize * 8, "encrypt", 0, count, start); + bench_stats_asym_finish("ECC", keySize * 8, "encrypt", 0, count, start, ret); bench_stats_start(&count, &start); do { @@ -3479,14 +3313,10 @@ exit_enc: count += i; } while (bench_stats_sym_check(start)); exit_dec: - bench_stats_asym_finish("ECC", keySize * 8, "decrypt", 0, count, start); + bench_stats_asym_finish("ECC", keySize * 8, "decrypt", 0, count, start, ret); exit: - if (ret != 0) { - printf("bench_eccEncrypt failed! %d\n", ret); - } - /* cleanup */ wc_ecc_free(&userB); wc_ecc_free(&userA); @@ -3499,7 +3329,7 @@ void bench_curve25519KeyGen(void) { curve25519_key genKey; double start; - int ret, i, count; + int ret = 0, i, count; /* Key Gen */ bench_stats_start(&count, &start); @@ -3514,7 +3344,7 @@ void bench_curve25519KeyGen(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_asym_finish("CURVE", 25519, "key gen", 0, count, start); + bench_stats_asym_finish("CURVE", 25519, "key gen", 0, count, start, ret); } #ifdef HAVE_CURVE25519_SHARED_SECRET @@ -3555,7 +3385,7 @@ void bench_curve25519KeyAgree(void) count += i; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("CURVE", 25519, "key agree", 0, count, start); + bench_stats_asym_finish("CURVE", 25519, "key agree", 0, count, start, ret); wc_curve25519_free(&genKey2); wc_curve25519_free(&genKey); @@ -3580,7 +3410,7 @@ void bench_ed25519KeyGen(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_asym_finish("ED", 25519, "key gen", 0, count, start); + bench_stats_asym_finish("ED", 25519, "key gen", 0, count, start, 0); } @@ -3622,7 +3452,7 @@ void bench_ed25519KeySign(void) count += i; } while (bench_stats_sym_check(start)); exit_ed_sign: - bench_stats_asym_finish("ED", 25519, "sign", 0, count, start); + bench_stats_asym_finish("ED", 25519, "sign", 0, count, start, ret); #ifdef HAVE_ED25519_VERIFY bench_stats_start(&count, &start); @@ -3639,7 +3469,7 @@ exit_ed_sign: count += i; } while (bench_stats_sym_check(start)); exit_ed_verify: - bench_stats_asym_finish("ED", 25519, "verify", 0, count, start); + bench_stats_asym_finish("ED", 25519, "verify", 0, count, start, ret); #endif /* HAVE_ED25519_VERIFY */ #endif /* HAVE_ED25519_SIGN */ diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 39beee02d..c59fa616d 100755 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -2822,9 +2822,8 @@ int wc_AesSetIV(Aes* aes, const byte* iv) (const byte*)aes->asyncKey, aes->keylen, (const byte*)aes->asyncIv, AES_BLOCK_SIZE); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &aes->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_AES_CBC_ENCRYPT; + if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_CBC_ENCRYPT)) { + WC_ASYNC_TEST* testDev = &aes->asyncDev.test; testDev->aes.aes = aes; testDev->aes.out = out; testDev->aes.in = in; @@ -2913,9 +2912,8 @@ int wc_AesSetIV(Aes* aes, const byte* iv) (const byte*)aes->asyncKey, aes->keylen, (const byte*)aes->asyncIv, AES_BLOCK_SIZE); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &aes->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_AES_CBC_DECRYPT; + if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_CBC_DECRYPT)) { + WC_ASYNC_TEST* testDev = &aes->asyncDev.test; testDev->aes.aes = aes; testDev->aes.out = out; testDev->aes.in = in; @@ -3165,7 +3163,7 @@ int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) byte out_block[AES_BLOCK_SIZE]; int odd, even, blocks; byte *tmp; - + if (aes == NULL || out == NULL || in == NULL) { return BAD_FUNC_ARG; } @@ -5494,7 +5492,7 @@ static int AES_GCM_decrypt(const unsigned char *in, unsigned char *out, for (; i < nbytes/16/8; i++) { r0 = _mm_setzero_si128(); r1 = _mm_setzero_si128(); - + tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64); tmp2 = _mm_add_epi32(ctr1, ONE); tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_EPI64); @@ -7078,9 +7076,8 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz, (const byte*)aes->asyncKey, aes->keylen, iv, ivSz, authTag, authTagSz, authIn, authInSz); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &aes->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_AES_GCM_ENCRYPT; + if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_GCM_ENCRYPT)) { + WC_ASYNC_TEST* testDev = &aes->asyncDev.test; testDev->aes.aes = aes; testDev->aes.out = out; testDev->aes.in = in; @@ -7091,6 +7088,7 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz, testDev->aes.authTagSz = authTagSz; testDev->aes.authIn = authIn; testDev->aes.authInSz = authInSz; + return WC_PENDING_E; } #endif } @@ -7340,9 +7338,8 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz, (const byte*)aes->asyncKey, aes->keylen, iv, ivSz, authTag, authTagSz, authIn, authInSz); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &aes->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_AES_GCM_DECRYPT; + if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_GCM_DECRYPT)) { + WC_ASYNC_TEST* testDev = &aes->asyncDev.test; testDev->aes.aes = aes; testDev->aes.out = out; testDev->aes.in = in; @@ -7900,6 +7897,10 @@ int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz, XMEMCPY(r, in, inSz); XMEMSET(t, 0, sizeof(t)); + ret = wc_AesInit(&aes, NULL, INVALID_DEVID); + if (ret != 0) + return ret; + ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_ENCRYPTION); if (ret != 0) return ret; @@ -7926,6 +7927,8 @@ int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz, /* C[0] = A */ XMEMCPY(out, tmp, KEYWRAP_BLOCK_SIZE); + wc_AesFree(&aes); + return inSz + KEYWRAP_BLOCK_SIZE; } @@ -7967,6 +7970,10 @@ int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz, XMEMCPY(out, in + KEYWRAP_BLOCK_SIZE, inSz - KEYWRAP_BLOCK_SIZE); XMEMSET(t, 0, sizeof(t)); + ret = wc_AesInit(&aes, NULL, INVALID_DEVID); + if (ret != 0) + return ret; + ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_DECRYPTION); if (ret != 0) return ret; @@ -7992,6 +7999,8 @@ int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz, } } + wc_AesFree(&aes); + /* verify IV */ if (XMEMCMP(tmp, expIv, KEYWRAP_BLOCK_SIZE) != 0) return BAD_KEYWRAP_IV_E; diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index ff8059cf1..a2a396941 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -4566,25 +4566,25 @@ static int ConfirmSignature(SignatureCtx* sigCtx, if (sigCtx->key.rsa == NULL || sigCtx->plain == NULL) { ERROR_OUT(MEMORY_E, exit_cs); } - if ((ret = wc_InitRsaKey_ex(sigCtx->key.rsa, sigCtx->heap, sigCtx->devId)) != 0) { goto exit_cs; } - if (sigSz > MAX_ENCODED_SIG_SZ) { WOLFSSL_MSG("Verify Signature is too big"); ERROR_OUT(BUFFER_E, exit_cs); } - if ((ret = wc_RsaPublicKeyDecode(key, &idx, sigCtx->key.rsa, keySz)) != 0) { WOLFSSL_MSG("ASN Key decode error RSA"); goto exit_cs; } - XMEMCPY(sigCtx->plain, sig, sigSz); sigCtx->out = NULL; + + #ifdef WOLFSSL_ASYNC_CRYPT + sigCtx->asyncDev = &sigCtx->key.rsa->asyncDev; + #endif break; } #endif /* !NO_RSA */ @@ -4597,7 +4597,6 @@ static int ConfirmSignature(SignatureCtx* sigCtx, if (sigCtx->key.ecc == NULL) { ERROR_OUT(MEMORY_E, exit_cs); } - if ((ret = wc_ecc_init_ex(sigCtx->key.ecc, sigCtx->heap, sigCtx->devId)) < 0) { goto exit_cs; @@ -4607,6 +4606,9 @@ static int ConfirmSignature(SignatureCtx* sigCtx, WOLFSSL_MSG("ASN Key import error ECC"); goto exit_cs; } + #ifdef WOLFSSL_ASYNC_CRYPT + sigCtx->asyncDev = &sigCtx->key.ecc->asyncDev; + #endif break; } #endif /* HAVE_ECC */ @@ -4620,7 +4622,6 @@ static int ConfirmSignature(SignatureCtx* sigCtx, if (sigCtx->key.ed25519 == NULL) { ERROR_OUT(MEMORY_E, exit_cs); } - if ((ret = wc_ed25519_init(sigCtx->key.ed25519)) < 0) { goto exit_cs; } @@ -4629,6 +4630,9 @@ static int ConfirmSignature(SignatureCtx* sigCtx, WOLFSSL_MSG("ASN Key import error ED25519"); goto exit_cs; } + #ifdef WOLFSSL_ASYNC_CRYPT + sigCtx->asyncDev = &sigCtx->key.ed25519->asyncDev; + #endif break; } #endif @@ -4643,6 +4647,15 @@ static int ConfirmSignature(SignatureCtx* sigCtx, } sigCtx->state = SIG_STATE_DO; + + #ifdef WOLFSSL_ASYNC_CRYPT + if (sigCtx->devId != INVALID_DEVID) { + /* always return here, so we can properly init the async + context back in SSL world */ + ret = WC_PENDING_E; + goto exit_cs; + } + #endif } /* SIG_STATE_KEY */ FALL_THROUGH; @@ -4654,10 +4667,6 @@ static int ConfirmSignature(SignatureCtx* sigCtx, { ret = wc_RsaSSL_VerifyInline(sigCtx->plain, sigSz, &sigCtx->out, sigCtx->key.rsa); - #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) - sigCtx->asyncDev = &sigCtx->key.rsa->asyncDev; - #endif break; } #endif /* !NO_RSA */ @@ -4666,10 +4675,6 @@ static int ConfirmSignature(SignatureCtx* sigCtx, { ret = wc_ecc_verify_hash(sig, sigSz, sigCtx->digest, sigCtx->digestSz, &sigCtx->verify, sigCtx->key.ecc); - #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) - sigCtx->asyncDev = &sigCtx->key.ecc->asyncDev; - #endif break; } #endif /* HAVE_ECC */ @@ -4678,10 +4683,6 @@ static int ConfirmSignature(SignatureCtx* sigCtx, { ret = wc_ed25519_verify_msg(sig, sigSz, buf, bufSz, &sigCtx->verify, sigCtx->key.ed25519); - #ifdef WOLFSSL_ASYNC_CRYPT - if (ret == WC_PENDING_E) - sigCtx->asyncDev = &sigCtx->key.ecc->asyncDev; - #endif break; } #endif diff --git a/wolfcrypt/src/des3.c b/wolfcrypt/src/des3.c index dd85bb0e7..82e201fbf 100755 --- a/wolfcrypt/src/des3.c +++ b/wolfcrypt/src/des3.c @@ -1582,9 +1582,8 @@ return IntelQaSymDes3CbcEncrypt(&des->asyncDev, out, in, sz, des->key_raw, DES3_KEYLEN, (byte*)des->iv_raw, DES3_IVLEN); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &des->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_DES3_CBC_ENCRYPT; + if (wc_AsyncTestInit(&des->asyncDev, ASYNC_TEST_DES3_CBC_ENCRYPT)) { + WC_ASYNC_TEST* testDev = &des->asyncDev.test; testDev->des.des = des; testDev->des.out = out; testDev->des.in = in; @@ -1625,9 +1624,8 @@ return IntelQaSymDes3CbcDecrypt(&des->asyncDev, out, in, sz, des->key_raw, DES3_KEYLEN, (byte*)des->iv_raw, DES3_IVLEN); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &des->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_DES3_CBC_DECRYPT; + if (wc_AsyncTestInit(&des->asyncDev, ASYNC_TEST_DES3_CBC_DECRYPT)) { + WC_ASYNC_TEST* testDev = &des->asyncDev.test; testDev->des.des = des; testDev->des.out = out; testDev->des.in = in; diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 5756db6e1..5be449cb2 100755 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -687,9 +687,8 @@ static int wc_DhGenerateKeyPair_Async(DhKey* key, WC_RNG* rng, /* TODO: Not implemented - use software for now */ #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_DH_GEN; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_DH_GEN)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->dhGen.key = key; testDev->dhGen.rng = rng; testDev->dhGen.priv = priv; @@ -836,9 +835,8 @@ static int wc_DhAgree_Async(DhKey* key, byte* agree, word32* agreeSz, ret = IntelQaDhAgree(&key->asyncDev, &key->p.raw, agree, agreeSz, priv, privSz, otherPub, pubSz); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_DH_AGREE; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_DH_AGREE)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->dhAgree.key = key; testDev->dhAgree.agree = agree; testDev->dhAgree.agreeSz = agreeSz; diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 947274dcd..8659a09fd 100755 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -1117,12 +1117,23 @@ static int wc_ecc_curve_load(const ecc_set_type* dp, ecc_curve_spec** pCurve, if (x == ECC_CURVE_INVALID) return ECC_BAD_ARG_E; +#if !defined(SINGLE_THREADED) + ret = wc_LockMutex(&ecc_curve_cache_mutex); + if (ret != 0) { + return ret; + } +#endif + /* make sure cache has been allocated */ if (ecc_curve_spec_cache[x] == NULL) { ecc_curve_spec_cache[x] = (ecc_curve_spec*)XMALLOC( sizeof(ecc_curve_spec), NULL, DYNAMIC_TYPE_ECC); - if (ecc_curve_spec_cache[x] == NULL) + if (ecc_curve_spec_cache[x] == NULL) { + #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED) + wc_UnLockMutex(&ecc_curve_cache_mutex); + #endif return MEMORY_E; + } XMEMSET(ecc_curve_spec_cache[x], 0, sizeof(ecc_curve_spec)); } @@ -1149,13 +1160,6 @@ static int wc_ecc_curve_load(const ecc_set_type* dp, ecc_curve_spec** pCurve, } curve->dp = dp; /* set dp info */ -#if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED) - ret = wc_LockMutex(&ecc_curve_cache_mutex); - if (ret != 0) { - return MEMORY_E; - } -#endif - /* determine items to load */ load_items = (~curve->load_mask & load_mask); curve->load_mask |= load_items; @@ -2803,9 +2807,8 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key, &curve->Af->raw, &curve->Bf->raw, &curve->prime->raw, private_key->dp->cofactor); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &private_key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_ECC_SHARED_SEC; + if (wc_AsyncTestInit(&private_key->asyncDev, ASYNC_TEST_ECC_SHARED_SEC)) { + WC_ASYNC_TEST* testDev = &private_key->asyncDev.test; testDev->eccSharedSec.private_key = private_key; testDev->eccSharedSec.public_point = point; testDev->eccSharedSec.out = out; @@ -2895,13 +2898,6 @@ int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point, case ECC_STATE_SHARED_SEC_RES: private_key->state = ECC_STATE_SHARED_SEC_RES; err = 0; - #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) - if (private_key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) { - #if defined(HAVE_CAVIUM) || defined(HAVE_INTEL_QA) - err = private_key->asyncDev.event.ret; - #endif - } - #endif break; default: @@ -3017,9 +3013,8 @@ int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) #elif defined(HAVE_INTEL_QA) /* TODO: Not implemented */ #else - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_ECC_MAKE; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_MAKE)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->eccMake.rng = rng; testDev->eccMake.key = key; testDev->eccMake.size = keysize; @@ -3465,9 +3460,8 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng, #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \ defined(WOLFSSL_ASYNC_CRYPT_TEST) if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) { - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_ECC_SIGN; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_SIGN)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->eccSign.in = in; testDev->eccSign.inSz = inlen; testDev->eccSign.rng = rng; @@ -4011,9 +4005,8 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash, #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \ defined(WOLFSSL_ASYNC_CRYPT_TEST) if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) { - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_ECC_VERIFY; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_VERIFY)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->eccVerify.r = r; testDev->eccVerify.s = s; testDev->eccVerify.hash = hash; @@ -7175,15 +7168,10 @@ int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg, } #endif - #if defined(WOLFSSL_ASYNC_CRYPT) - ret = 0; - #endif - do { - #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); - #endif - ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret, &sharedSz); - } while (ret == WC_PENDING_E); + ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret, &sharedSz); +#if defined(WOLFSSL_ASYNC_CRYPT) + ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); +#endif if (ret == 0) { switch (ctx->kdfAlgo) { case ecHKDF_SHA256 : @@ -7338,15 +7326,10 @@ int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg, } #endif - #if defined(WOLFSSL_ASYNC_CRYPT) - ret = 0; - #endif - do { - #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); - #endif - ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret, &sharedSz); - } while (ret == WC_PENDING_E); + ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret, &sharedSz); +#if defined(WOLFSSL_ASYNC_CRYPT) + ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); +#endif if (ret == 0) { switch (ctx->kdfAlgo) { case ecHKDF_SHA256 : diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index fb4426aec..7779d95a0 100755 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -1289,9 +1289,8 @@ static int wc_RsaFunctionAsync(const byte* in, word32 inLen, byte* out, (void)rng; #ifdef WOLFSSL_ASYNC_CRYPT_TEST - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_RSA_FUNC; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_RSA_FUNC)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->rsaFunc.in = in; testDev->rsaFunc.inSz = inLen; testDev->rsaFunc.out = out; @@ -1870,9 +1869,8 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) #elif defined(HAVE_INTEL_QA) /* TODO: Not implemented */ #else - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_RSA_MAKE; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_RSA_MAKE)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->rsaMake.rng = rng; testDev->rsaMake.key = key; testDev->rsaMake.size = size; diff --git a/wolfcrypt/src/wolfevent.c b/wolfcrypt/src/wolfevent.c index 6e3eae2b3..e5f74fe5b 100644 --- a/wolfcrypt/src/wolfevent.c +++ b/wolfcrypt/src/wolfevent.c @@ -41,8 +41,8 @@ int wolfEvent_Init(WOLF_EVENT* event, WOLF_EVENT_TYPE type, void* context) return BAD_FUNC_ARG; } - if (event->pending) { - WOLFSSL_MSG("event already pending!"); + if (event->state == WOLF_EVENT_STATE_PENDING) { + WOLFSSL_MSG("Event already pending!"); return BAD_COND_E; } @@ -99,10 +99,6 @@ int wolfEventQueue_Push(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event) } #endif - /* Setup event */ - event->next = NULL; - event->pending = 1; - ret = wolfEventQueue_Add(queue, event); #ifndef SINGLE_THREADED @@ -145,6 +141,8 @@ int wolfEventQueue_Add(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event) return BAD_FUNC_ARG; } + event->next = NULL; /* added to end */ + event->prev = NULL; if (queue->tail == NULL) { queue->head = event; } @@ -218,7 +216,7 @@ int wolfEventQueue_Poll(WOLF_EVENT_QUEUE* queue, void* context_filter, if (ret < 0) break; /* exit for */ /* If event is done then process */ - if (event->done) { + if (event->state == WOLF_EVENT_STATE_DONE) { /* remove from queue */ ret = wolfEventQueue_Remove(queue, event); if (ret < 0) break; /* exit for */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 836953241..698c71e4c 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -6748,6 +6748,11 @@ int rsa_test(void) DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT); DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT); +#ifdef WOLFSSL_ASYNC_CRYPT + if (in == NULL) + return MEMORY_E; +#endif + /* initialize stack structures */ XMEMSET(&rng, 0, sizeof(rng)); XMEMSET(&key, 0, sizeof(key)); diff --git a/wolfssl/internal.h b/wolfssl/internal.h index e62e364d3..17690ee68 100755 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -3820,9 +3820,9 @@ WOLFSSL_LOCAL int AllocKey(WOLFSSL* ssl, int type, void** pKey); WOLFSSL_LOCAL void FreeKey(WOLFSSL* ssl, int type, void** pKey); #ifdef WOLFSSL_ASYNC_CRYPT + WOLFSSL_LOCAL int wolfSSL_AsyncInit(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev, word32 flags); WOLFSSL_LOCAL int wolfSSL_AsyncPop(WOLFSSL* ssl, byte* state); - WOLFSSL_LOCAL int wolfSSL_AsyncPush(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev, - word32 flags); + WOLFSSL_LOCAL int wolfSSL_AsyncPush(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev); #endif diff --git a/wolfssl/test.h b/wolfssl/test.h index 96e6fa2f3..a5b3961a8 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -1385,7 +1385,7 @@ static INLINE void CaCb(unsigned char* der, int sz, int type) #ifdef HAVE_STACK_SIZE typedef THREAD_RETURN WOLFSSL_THREAD (*thread_func)(void* args); - +#define STACK_CHECK_VAL 0x01 static INLINE int StackSizeCheck(func_args* args, thread_func tf) { @@ -1405,7 +1405,7 @@ static INLINE int StackSizeCheck(func_args* args, thread_func tf) if (ret != 0 || myStack == NULL) err_sys("posix_memalign failed\n"); - XMEMSET(myStack, 0x01, stackSize); + XMEMSET(myStack, STACK_CHECK_VAL, stackSize); ret = pthread_attr_init(&myAttr); if (ret != 0) @@ -1426,7 +1426,7 @@ static INLINE int StackSizeCheck(func_args* args, thread_func tf) err_sys("pthread_join failed"); for (i = 0; i < stackSize; i++) { - if (myStack[i] != 0x01) { + if (myStack[i] != STACK_CHECK_VAL) { break; } } diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 30ef7a944..917e0f2b1 100755 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -257,7 +257,7 @@ VAR_TYPE* VAR_NAME = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * VAR_SIZE, HEAP, DYNAMIC_TYPE_WOLF_BIGINT); #define DECLARE_VAR_INIT(VAR_NAME, VAR_TYPE, VAR_SIZE, INIT_VALUE, HEAP) \ VAR_TYPE* VAR_NAME = ({ \ - VAR_TYPE* ptr = XMALLOC(sizeof(VAR_TYPE) * VAR_SIZE, HEAP, DYNAMIC_TYPE_WOLF_BIGINT); \ + VAR_TYPE* ptr = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * VAR_SIZE, HEAP, DYNAMIC_TYPE_WOLF_BIGINT); \ if (ptr && INIT_VALUE) { \ XMEMCPY(ptr, INIT_VALUE, sizeof(VAR_TYPE) * VAR_SIZE); \ } \ diff --git a/wolfssl/wolfcrypt/wolfevent.h b/wolfssl/wolfcrypt/wolfevent.h index af984c8cd..221c1d7d3 100644 --- a/wolfssl/wolfcrypt/wolfevent.h +++ b/wolfssl/wolfcrypt/wolfevent.h @@ -55,6 +55,12 @@ typedef enum WOLF_EVENT_TYPE { #endif /* WOLFSSL_ASYNC_CRYPT */ } WOLF_EVENT_TYPE; +typedef enum WOLF_EVENT_STATE { + WOLF_EVENT_STATE_READY, + WOLF_EVENT_STATE_PENDING, + WOLF_EVENT_STATE_DONE, +} WOLF_EVENT_STATE; + struct WOLF_EVENT { /* double linked list */ WOLF_EVENT* next; @@ -69,15 +75,14 @@ struct WOLF_EVENT { } dev; #ifdef HAVE_CAVIUM CavReqId reqId; +#endif +#ifndef WC_NO_ASYNC_THREADING + pthread_t threadId; #endif int ret; /* Async return code */ unsigned int flags; WOLF_EVENT_TYPE type; - - /* event flags */ - WOLF_EVENT_FLAG pending:1; - WOLF_EVENT_FLAG done:1; - /* Future event flags can go here */ + WOLF_EVENT_STATE state; }; enum WOLF_POLL_FLAGS {