Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark, SM3: fix full hash testing #7493

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -28007,6 +28007,12 @@ int DecodePrivateKey(WOLFSSL *ssl, word32* length)
(ecc_key*)ssl->hsKey,
ssl->buffers.key->length);
}
#endif
#ifdef WOLFSSL_SM2
if ((ret == 0) && (ssl->buffers.keyType == sm2_sa_algo)) {
ret = wc_ecc_set_curve((ecc_key*)ssl->hsKey,
WOLFSSL_SM2_KEY_BITS / 8, ECC_SM2P256V1);
}
#endif
if (ret == 0) {
WOLFSSL_MSG("Using ECC private key");
Expand Down Expand Up @@ -34542,7 +34548,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
{
word32 keySz;

ssl->buffers.keyType = ecc_dsa_sa_algo;
ssl->buffers.keyType = ssl->options.sigAlgo;
ret = DecodePrivateKey(ssl, &keySz);
if (ret != 0) {
goto exit_sske;
Expand Down
41 changes: 26 additions & 15 deletions src/ssl_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@
* @param [in, out] info Info for encryption.
* @param [in] heap Dynamic memory allocation hint.
* @param [out] der Holds DER encoded data.
* @param [out] algId Algorithm identifier for private keys.
* @return 0 on success.
* @return NOT_COMPILED_IN when format is PEM and PEM not supported.
* @return ASN_PARSE_E when format is ASN.1 and invalid DER encoding.
* @return MEMORY_E when dynamic memory allocation fails.
*/
static int DataToDerBuffer(const unsigned char* buff, word32 len, int format,
int type, EncryptedInfo* info, void* heap, DerBuffer** der)
int type, EncryptedInfo* info, void* heap, DerBuffer** der, int* algId)
{
int ret;

Expand All @@ -131,7 +132,7 @@ static int DataToDerBuffer(const unsigned char* buff, word32 len, int format,
/* Data in buffer has PEM format - extract DER data. */
if (format == WOLFSSL_FILETYPE_PEM) {
#ifdef WOLFSSL_PEM_TO_DER
ret = PemToDer(buff, len, type, der, heap, info, NULL);
ret = PemToDer(buff, len, type, der, heap, info, algId);
if (ret != 0) {
FreeDer(der);
}
Expand Down Expand Up @@ -341,7 +342,7 @@ static int ProcessUserChain(WOLFSSL_CTX* ctx, WOLFSSL* ssl,

/* Get a certificate as DER. */
ret = DataToDerBuffer(buff + consumed, (word32)(sz - consumed),
format, type, info, heap, &part);
format, type, info, heap, &part, NULL);
if (ret == 0) {
/* Process the user certificate. */
ret = ProcessUserCert(ctx->cm, &part, type, verify,
Expand Down Expand Up @@ -604,6 +605,12 @@ static int ProcessBufferTryDecodeEcc(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
idx = 0;
ret = wc_EccPublicKeyDecode(der->buffer, &idx, key, der->length);
}
#endif
#ifdef WOLFSSL_SM2
if (*keyFormat == SM2k) {
ret = wc_ecc_set_curve(key, WOLFSSL_SM2_KEY_BITS / 8,
ECC_SM2P256V1);
}
#endif
if (ret == 0) {
/* Get the minimum ECC key size from SSL or SSL context object. */
Expand Down Expand Up @@ -1317,52 +1324,53 @@ static void ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
* @param [in] heap Dynamic memory allocation hint.
* @param [in] type Type of data:
* PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
* @param [in] algId Algorithm id of key.
* @return 0 on success.
* @return WOLFSSL_BAD_FILE when not able to decode.
*/
static int ProcessBufferPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
DerBuffer* der, int format, EncryptedInfo* info, void* heap, int type)
DerBuffer* der, int format, EncryptedInfo* info, void* heap, int type,
int algId)
{
int ret;
int keyFormat = 0;
#if (defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)) || \
defined(HAVE_PKCS8)
word32 algId = 0;
word32 p8AlgId = 0;
#endif

(void)info;
(void)format;

#ifdef HAVE_PKCS8
/* Try and remove PKCS8 header and get algorithm id. */
ret = ToTraditional_ex(der->buffer, der->length, &algId);
ret = ToTraditional_ex(der->buffer, der->length, &p8AlgId);
if (ret > 0) {
/* Header stripped inline. */
der->length = ret;
keyFormat = algId;
algId = p8AlgId;
}
#endif

/* Put the data into the SSL or SSL context object. */
ProcessBufferPrivKeyHandleDer(ctx, ssl, &der, type);
/* Try to decode the DER data. */
ret = ProcessBufferTryDecode(ctx, ssl, der, &keyFormat, heap, type);
ret = ProcessBufferTryDecode(ctx, ssl, der, &algId, heap, type);

#if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
/* If private key type PKCS8 header wasn't already removed (algId == 0). */
if (((ret != 0) || (keyFormat == 0)) && (format != WOLFSSL_FILETYPE_PEM) &&
if (((ret != 0) || (algId == 0)) && (format != WOLFSSL_FILETYPE_PEM) &&
(info->passwd_cb != NULL) && (algId == 0)) {
/* Try to decrypt DER data as a PKCS#8 private key. */
ret = ProcessBufferPrivPkcs8Dec(info, der, heap);
if (ret >= 0) {
/* Try to decode decrypted data. */
ret = ProcessBufferTryDecode(ctx, ssl, der, &keyFormat, heap, type);
ret = ProcessBufferTryDecode(ctx, ssl, der, &algId, heap, type);
}
}
#endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */

/* Check if we were able to determine key format. */
if ((ret == 0) && (keyFormat == 0)) {
/* Check if we were able to determine algorithm id. */
if ((ret == 0) && (algId == 0)) {
#ifdef OPENSSL_EXTRA
/* Decryption password is probably wrong. */
if (info->passwd_cb) {
Expand Down Expand Up @@ -2265,6 +2273,7 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
#else
EncryptedInfo info[1];
#endif
int algId = 0;

WOLFSSL_ENTER("ProcessBuffer");

Expand Down Expand Up @@ -2306,7 +2315,8 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
#endif

/* Get the DER data for a private key or certificate. */
ret = DataToDerBuffer(buff, (word32)sz, format, type, info, heap, &der);
ret = DataToDerBuffer(buff, (word32)sz, format, type, info, heap, &der,
&algId);
if (used != NULL) {
/* Update to amount used/consumed. */
*used = info->consumed;
Expand All @@ -2321,7 +2331,8 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,

if ((ret == 0) && IS_PRIVKEY_TYPE(type)) {
/* Process the private key. */
ret = ProcessBufferPrivateKey(ctx, ssl, der, format, info, heap, type);
ret = ProcessBufferPrivateKey(ctx, ssl, der, format, info, heap, type,
algId);
#ifdef WOLFSSL_SMALL_STACK
/* Info no longer needed - keep max memory usage down. */
XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);
Expand Down
12 changes: 6 additions & 6 deletions wolfcrypt/benchmark/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -5536,7 +5536,7 @@ void bench_sm4_gcm(void)
#endif

#ifdef WOLFSSL_SM4_CCM
void bench_sm4_ccm()
void bench_sm4_ccm(void)
{
wc_Sm4 enc;
double start;
Expand Down Expand Up @@ -7475,12 +7475,12 @@ void bench_sm3(int useDeviceID)
bench_stats_start(&count, &start);
do {
for (times = 0; times < numBlocks; times++) {
ret = wc_InitSm3(hash, HEAP_HINT,
ret = wc_InitSm3(hash[0], HEAP_HINT,
useDeviceID ? devId: INVALID_DEVID);
if (ret == 0)
ret = wc_Sm3Update(hash, bench_plain, bench_size);
ret = wc_Sm3Update(hash[0], bench_plain, bench_size);
if (ret == 0)
ret = wc_Sm3Final(hash, digest[0]);
ret = wc_Sm3Final(hash[0], digest[0]);
if (ret != 0)
goto exit_sm3;
RECORD_MULTI_VALUE_STATS();
Expand Down Expand Up @@ -10939,13 +10939,13 @@ void bench_eccEncrypt(int curveId)
#ifdef WOLFSSL_SM2
static void bench_sm2_MakeKey(int useDeviceID)
{
int ret = 0, i, times, count, pending = 0;
int ret = 0, i, times, count = 0, pending = 0;
int deviceID;
int keySize;
WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
sizeof(ecc_key), HEAP_HINT);
char name[BENCH_ECC_NAME_SZ];
double start;
double start = 0;
const char**desc = bench_desc_words[lng_index];
DECLARE_MULTI_VALUE_STATS_VARS()

Expand Down
10 changes: 8 additions & 2 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -23790,13 +23790,19 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm)
if (cert->ca) {
if (verify == VERIFY || verify == VERIFY_OCSP ||
verify == VERIFY_SKIP_DATE) {
word32 keyOID = cert->ca->keyOID;
#if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
if (cert->selfSigned && (cert->signatureOID == CTC_SM3wSM2)) {
keyOID = SM2k;
}
#endif
/* try to confirm/verify signature */
if ((ret = ConfirmSignature(&cert->sigCtx,
cert->source + cert->certBegin,
cert->sigIndex - cert->certBegin,
cert->ca->publicKey, cert->ca->pubKeySize,
cert->ca->keyOID, cert->signature,
cert->sigLength, cert->signatureOID,
keyOID, cert->signature, cert->sigLength,
cert->signatureOID,
#ifdef WC_RSA_PSS
cert->source + cert->sigParamsIndex,
cert->sigParamsLength,
Expand Down
2 changes: 2 additions & 0 deletions wolfcrypt/src/port/arm/armv8-aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,7 @@ void GHASH(Gcm* gcm, const byte* a, word32 aSz, const byte* c,
"USHR v7.2d, v7.2d, #56 \n"

"# AAD \n"
"CBZ %[a], 20f \n"
"CBZ %w[aSz], 20f \n"
"MOV w12, %w[aSz] \n"

Expand Down Expand Up @@ -1702,6 +1703,7 @@ void GHASH(Gcm* gcm, const byte* a, word32 aSz, const byte* c,

"20: \n"
"# Cipher Text \n"
"CBZ %[c], 120f \n"
"CBZ %w[cSz], 120f \n"
"MOV w12, %w[cSz] \n"

Expand Down
34 changes: 13 additions & 21 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29740,21 +29740,19 @@ static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
#ifdef WOLFSSL_SM2
#ifdef HAVE_ECC_VERIFY
#if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
#ifdef WOLFSSL_SM2
#ifdef HAVE_OID_ENCODING
#define CODED_SM2P256V1 {1,2,156,10197,1,301}
#define CODED_SM2P256V1_SZ 6
#else
#define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
#define CODED_SM2P256V1_SZ 10
#endif
#ifndef WOLFSSL_ECC_CURVE_STATIC
static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
#else
#define ecc_oid_sm2p256v1 CODED_SM2P256V1
#endif
#define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
#endif /* WOLFSSL_SM2 */
#ifdef HAVE_OID_ENCODING
#define CODED_SM2P256V1 {1,2,156,10197,1,301}
#define CODED_SM2P256V1_SZ 6
#else
#define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
#define CODED_SM2P256V1_SZ 10
#endif
#ifndef WOLFSSL_ECC_CURVE_STATIC
static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
#else
#define ecc_oid_sm2p256v1 CODED_SM2P256V1
#endif
#define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
#define ECC_SM2P256V1_TEST 102
static int test_sm2_verify_caseA2(void)
{
Expand Down Expand Up @@ -29931,9 +29929,7 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
int i;
#ifdef HAVE_ECC_VERIFY
int verify;
#endif /* HAVE_ECC_VERIFY */
#endif /* HAVE_ECC_SIGN */
int ret;
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
Expand Down Expand Up @@ -30129,7 +30125,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);

#ifdef HAVE_ECC_VERIFY
for (i = 0; i < testVerifyCount; i++) {
verify = 0;
ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
Expand All @@ -30139,7 +30134,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
if (verify != 1)
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
}
#endif /* HAVE_ECC_VERIFY */
#endif /* ECC_SHAMIR */

/* test DSA sign hash with sequence (0,1,2,3,4,...) */
Expand All @@ -30152,7 +30146,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);

#ifdef HAVE_ECC_VERIFY
for (i = 0; i < testVerifyCount; i++) {
verify = 0;
ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
Expand All @@ -30162,7 +30155,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
if (verify != 1)
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
}
#endif /* HAVE_ECC_VERIFY */
#endif /* HAVE_ECC_SIGN */
#endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */

Expand Down
2 changes: 1 addition & 1 deletion wolfssl/wolfcrypt/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ typedef struct w64wrapper {
#endif

#define WC_DECLARE_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
VAR_TYPE* VAR_NAME[VAR_ITEMS]; \
VAR_TYPE* VAR_NAME[VAR_ITEMS] = { NULL, }; \
int idx##VAR_NAME = 0, inner_idx_##VAR_NAME
#define WC_HEAP_ARRAY_ARG(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE) \
VAR_TYPE* VAR_NAME[VAR_ITEMS]
Expand Down