Skip to content

Commit 009ea66

Browse files
authored
Merge pull request #7493 from SparkiDev/sm3_benchmark_fix
Benchmark, SM3: fix full hash testing
2 parents 6b79e53 + add7428 commit 009ea66

7 files changed

Lines changed: 63 additions & 46 deletions

File tree

src/internal.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28015,6 +28015,12 @@ int DecodePrivateKey(WOLFSSL *ssl, word32* length)
2801528015
(ecc_key*)ssl->hsKey,
2801628016
ssl->buffers.key->length);
2801728017
}
28018+
#endif
28019+
#ifdef WOLFSSL_SM2
28020+
if ((ret == 0) && (ssl->buffers.keyType == sm2_sa_algo)) {
28021+
ret = wc_ecc_set_curve((ecc_key*)ssl->hsKey,
28022+
WOLFSSL_SM2_KEY_BITS / 8, ECC_SM2P256V1);
28023+
}
2801828024
#endif
2801928025
if (ret == 0) {
2802028026
WOLFSSL_MSG("Using ECC private key");
@@ -34568,7 +34574,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
3456834574
{
3456934575
word32 keySz;
3457034576

34571-
ssl->buffers.keyType = ecc_dsa_sa_algo;
34577+
ssl->buffers.keyType = ssl->options.sigAlgo;
3457234578
ret = DecodePrivateKey(ssl, &keySz);
3457334579
if (ret != 0) {
3457434580
goto exit_sske;

src/ssl_load.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@
116116
* @param [in, out] info Info for encryption.
117117
* @param [in] heap Dynamic memory allocation hint.
118118
* @param [out] der Holds DER encoded data.
119+
* @param [out] algId Algorithm identifier for private keys.
119120
* @return 0 on success.
120121
* @return NOT_COMPILED_IN when format is PEM and PEM not supported.
121122
* @return ASN_PARSE_E when format is ASN.1 and invalid DER encoding.
122123
* @return MEMORY_E when dynamic memory allocation fails.
123124
*/
124125
static int DataToDerBuffer(const unsigned char* buff, word32 len, int format,
125-
int type, EncryptedInfo* info, void* heap, DerBuffer** der)
126+
int type, EncryptedInfo* info, void* heap, DerBuffer** der, int* algId)
126127
{
127128
int ret;
128129

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

342343
/* Get a certificate as DER. */
343344
ret = DataToDerBuffer(buff + consumed, (word32)(sz - consumed),
344-
format, type, info, heap, &part);
345+
format, type, info, heap, &part, NULL);
345346
if (ret == 0) {
346347
/* Process the user certificate. */
347348
ret = ProcessUserCert(ctx->cm, &part, type, verify,
@@ -604,6 +605,12 @@ static int ProcessBufferTryDecodeEcc(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
604605
idx = 0;
605606
ret = wc_EccPublicKeyDecode(der->buffer, &idx, key, der->length);
606607
}
608+
#endif
609+
#ifdef WOLFSSL_SM2
610+
if (*keyFormat == SM2k) {
611+
ret = wc_ecc_set_curve(key, WOLFSSL_SM2_KEY_BITS / 8,
612+
ECC_SM2P256V1);
613+
}
607614
#endif
608615
if (ret == 0) {
609616
/* Get the minimum ECC key size from SSL or SSL context object. */
@@ -1317,52 +1324,53 @@ static void ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
13171324
* @param [in] heap Dynamic memory allocation hint.
13181325
* @param [in] type Type of data:
13191326
* PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
1327+
* @param [in] algId Algorithm id of key.
13201328
* @return 0 on success.
13211329
* @return WOLFSSL_BAD_FILE when not able to decode.
13221330
*/
13231331
static int ProcessBufferPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
1324-
DerBuffer* der, int format, EncryptedInfo* info, void* heap, int type)
1332+
DerBuffer* der, int format, EncryptedInfo* info, void* heap, int type,
1333+
int algId)
13251334
{
13261335
int ret;
1327-
int keyFormat = 0;
13281336
#if (defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)) || \
13291337
defined(HAVE_PKCS8)
1330-
word32 algId = 0;
1338+
word32 p8AlgId = 0;
13311339
#endif
13321340

13331341
(void)info;
13341342
(void)format;
13351343

13361344
#ifdef HAVE_PKCS8
13371345
/* Try and remove PKCS8 header and get algorithm id. */
1338-
ret = ToTraditional_ex(der->buffer, der->length, &algId);
1346+
ret = ToTraditional_ex(der->buffer, der->length, &p8AlgId);
13391347
if (ret > 0) {
13401348
/* Header stripped inline. */
13411349
der->length = ret;
1342-
keyFormat = algId;
1350+
algId = p8AlgId;
13431351
}
13441352
#endif
13451353

13461354
/* Put the data into the SSL or SSL context object. */
13471355
ProcessBufferPrivKeyHandleDer(ctx, ssl, &der, type);
13481356
/* Try to decode the DER data. */
1349-
ret = ProcessBufferTryDecode(ctx, ssl, der, &keyFormat, heap, type);
1357+
ret = ProcessBufferTryDecode(ctx, ssl, der, &algId, heap, type);
13501358

13511359
#if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
13521360
/* If private key type PKCS8 header wasn't already removed (algId == 0). */
1353-
if (((ret != 0) || (keyFormat == 0)) && (format != WOLFSSL_FILETYPE_PEM) &&
1361+
if (((ret != 0) || (algId == 0)) && (format != WOLFSSL_FILETYPE_PEM) &&
13541362
(info->passwd_cb != NULL) && (algId == 0)) {
13551363
/* Try to decrypt DER data as a PKCS#8 private key. */
13561364
ret = ProcessBufferPrivPkcs8Dec(info, der, heap);
13571365
if (ret >= 0) {
13581366
/* Try to decode decrypted data. */
1359-
ret = ProcessBufferTryDecode(ctx, ssl, der, &keyFormat, heap, type);
1367+
ret = ProcessBufferTryDecode(ctx, ssl, der, &algId, heap, type);
13601368
}
13611369
}
13621370
#endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */
13631371

1364-
/* Check if we were able to determine key format. */
1365-
if ((ret == 0) && (keyFormat == 0)) {
1372+
/* Check if we were able to determine algorithm id. */
1373+
if ((ret == 0) && (algId == 0)) {
13661374
#ifdef OPENSSL_EXTRA
13671375
/* Decryption password is probably wrong. */
13681376
if (info->passwd_cb) {
@@ -2265,6 +2273,7 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
22652273
#else
22662274
EncryptedInfo info[1];
22672275
#endif
2276+
int algId = 0;
22682277

22692278
WOLFSSL_ENTER("ProcessBuffer");
22702279

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

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

23222332
if ((ret == 0) && IS_PRIVKEY_TYPE(type)) {
23232333
/* Process the private key. */
2324-
ret = ProcessBufferPrivateKey(ctx, ssl, der, format, info, heap, type);
2334+
ret = ProcessBufferPrivateKey(ctx, ssl, der, format, info, heap, type,
2335+
algId);
23252336
#ifdef WOLFSSL_SMALL_STACK
23262337
/* Info no longer needed - keep max memory usage down. */
23272338
XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);

wolfcrypt/benchmark/benchmark.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5588,7 +5588,7 @@ void bench_sm4_gcm(void)
55885588
#endif
55895589

55905590
#ifdef WOLFSSL_SM4_CCM
5591-
void bench_sm4_ccm()
5591+
void bench_sm4_ccm(void)
55925592
{
55935593
wc_Sm4 enc;
55945594
double start;
@@ -7554,12 +7554,12 @@ void bench_sm3(int useDeviceID)
75547554
bench_stats_start(&count, &start);
75557555
do {
75567556
for (times = 0; times < numBlocks; times++) {
7557-
ret = wc_InitSm3(hash, HEAP_HINT,
7557+
ret = wc_InitSm3(hash[0], HEAP_HINT,
75587558
useDeviceID ? devId: INVALID_DEVID);
75597559
if (ret == 0)
7560-
ret = wc_Sm3Update(hash, bench_plain, bench_size);
7560+
ret = wc_Sm3Update(hash[0], bench_plain, bench_size);
75617561
if (ret == 0)
7562-
ret = wc_Sm3Final(hash, digest[0]);
7562+
ret = wc_Sm3Final(hash[0], digest[0]);
75637563
if (ret != 0)
75647564
goto exit_sm3;
75657565
RECORD_MULTI_VALUE_STATS();
@@ -11016,13 +11016,13 @@ void bench_eccEncrypt(int curveId)
1101611016
#ifdef WOLFSSL_SM2
1101711017
static void bench_sm2_MakeKey(int useDeviceID)
1101811018
{
11019-
int ret = 0, i, times, count, pending = 0;
11019+
int ret = 0, i, times, count = 0, pending = 0;
1102011020
int deviceID;
1102111021
int keySize;
1102211022
WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
1102311023
sizeof(ecc_key), HEAP_HINT);
1102411024
char name[BENCH_ECC_NAME_SZ];
11025-
double start;
11025+
double start = 0;
1102611026
const char**desc = bench_desc_words[lng_index];
1102711027
DECLARE_MULTI_VALUE_STATS_VARS()
1102811028

wolfcrypt/src/asn.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23790,13 +23790,19 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm)
2379023790
if (cert->ca) {
2379123791
if (verify == VERIFY || verify == VERIFY_OCSP ||
2379223792
verify == VERIFY_SKIP_DATE) {
23793+
word32 keyOID = cert->ca->keyOID;
23794+
#if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
23795+
if (cert->selfSigned && (cert->signatureOID == CTC_SM3wSM2)) {
23796+
keyOID = SM2k;
23797+
}
23798+
#endif
2379323799
/* try to confirm/verify signature */
2379423800
if ((ret = ConfirmSignature(&cert->sigCtx,
2379523801
cert->source + cert->certBegin,
2379623802
cert->sigIndex - cert->certBegin,
2379723803
cert->ca->publicKey, cert->ca->pubKeySize,
23798-
cert->ca->keyOID, cert->signature,
23799-
cert->sigLength, cert->signatureOID,
23804+
keyOID, cert->signature, cert->sigLength,
23805+
cert->signatureOID,
2380023806
#ifdef WC_RSA_PSS
2380123807
cert->source + cert->sigParamsIndex,
2380223808
cert->sigParamsLength,

wolfcrypt/src/port/arm/armv8-aes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,7 @@ void GHASH(Gcm* gcm, const byte* a, word32 aSz, const byte* c,
15321532
"USHR v7.2d, v7.2d, #56 \n"
15331533

15341534
"# AAD \n"
1535+
"CBZ %[a], 20f \n"
15351536
"CBZ %w[aSz], 20f \n"
15361537
"MOV w12, %w[aSz] \n"
15371538

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

17031704
"20: \n"
17041705
"# Cipher Text \n"
1706+
"CBZ %[c], 120f \n"
17051707
"CBZ %w[cSz], 120f \n"
17061708
"MOV w12, %w[cSz] \n"
17071709

wolfcrypt/test/test.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29850,21 +29850,19 @@ static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
2985029850
#ifdef WOLFSSL_SM2
2985129851
#ifdef HAVE_ECC_VERIFY
2985229852
#if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
29853-
#ifdef WOLFSSL_SM2
29854-
#ifdef HAVE_OID_ENCODING
29855-
#define CODED_SM2P256V1 {1,2,156,10197,1,301}
29856-
#define CODED_SM2P256V1_SZ 6
29857-
#else
29858-
#define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
29859-
#define CODED_SM2P256V1_SZ 10
29860-
#endif
29861-
#ifndef WOLFSSL_ECC_CURVE_STATIC
29862-
static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
29863-
#else
29864-
#define ecc_oid_sm2p256v1 CODED_SM2P256V1
29865-
#endif
29866-
#define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
29867-
#endif /* WOLFSSL_SM2 */
29853+
#ifdef HAVE_OID_ENCODING
29854+
#define CODED_SM2P256V1 {1,2,156,10197,1,301}
29855+
#define CODED_SM2P256V1_SZ 6
29856+
#else
29857+
#define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
29858+
#define CODED_SM2P256V1_SZ 10
29859+
#endif
29860+
#ifndef WOLFSSL_ECC_CURVE_STATIC
29861+
static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
29862+
#else
29863+
#define ecc_oid_sm2p256v1 CODED_SM2P256V1
29864+
#endif
29865+
#define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
2986829866
#define ECC_SM2P256V1_TEST 102
2986929867
static int test_sm2_verify_caseA2(void)
2987029868
{
@@ -30041,9 +30039,7 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3004130039
WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
3004230040
WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
3004330041
int i;
30044-
#ifdef HAVE_ECC_VERIFY
3004530042
int verify;
30046-
#endif /* HAVE_ECC_VERIFY */
3004730043
#endif /* HAVE_ECC_SIGN */
3004830044
int ret;
3004930045
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
@@ -30239,7 +30235,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3023930235
if (ret != 0)
3024030236
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
3024130237

30242-
#ifdef HAVE_ECC_VERIFY
3024330238
for (i = 0; i < testVerifyCount; i++) {
3024430239
verify = 0;
3024530240
ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
@@ -30249,7 +30244,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3024930244
if (verify != 1)
3025030245
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
3025130246
}
30252-
#endif /* HAVE_ECC_VERIFY */
3025330247
#endif /* ECC_SHAMIR */
3025430248

3025530249
/* test DSA sign hash with sequence (0,1,2,3,4,...) */
@@ -30262,7 +30256,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3026230256
if (ret != 0)
3026330257
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
3026430258

30265-
#ifdef HAVE_ECC_VERIFY
3026630259
for (i = 0; i < testVerifyCount; i++) {
3026730260
verify = 0;
3026830261
ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
@@ -30272,7 +30265,6 @@ static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
3027230265
if (verify != 1)
3027330266
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
3027430267
}
30275-
#endif /* HAVE_ECC_VERIFY */
3027630268
#endif /* HAVE_ECC_SIGN */
3027730269
#endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
3027830270

wolfssl/wolfcrypt/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ typedef struct w64wrapper {
592592
#endif
593593

594594
#define WC_DECLARE_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
595-
VAR_TYPE* VAR_NAME[VAR_ITEMS]; \
595+
VAR_TYPE* VAR_NAME[VAR_ITEMS] = { NULL, }; \
596596
int idx##VAR_NAME = 0, inner_idx_##VAR_NAME
597597
#define WC_HEAP_ARRAY_ARG(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE) \
598598
VAR_TYPE* VAR_NAME[VAR_ITEMS]

0 commit comments

Comments
 (0)