Skip to content

Commit 864a9d0

Browse files
committed
Dilithium: fixes
TLS uses DER API now and needs to be protected with the right #ifdefs. Do the right check of size in wc_Dilithium_PrivateKeyDecode(). Don't require public key when doing private DER.
1 parent 85552d0 commit 864a9d0

4 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/internal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28394,7 +28394,8 @@ int DecodePrivateKey(WOLFSSL *ssl, word32* length)
2839428394
}
2839528395
}
2839628396
#endif /* HAVE_FALCON */
28397-
#if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN)
28397+
#if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) && \
28398+
!defined(WOLFSSL_DILITHIUM_NO_ASN1)
2839828399
#if !defined(NO_RSA) || defined(HAVE_ECC)
2839928400
FreeKey(ssl, ssl->hsType, (void**)&ssl->hsKey);
2840028401
#endif

src/ssl_load.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,8 @@ static int ProcessBufferTryDecodeFalcon(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
942942
}
943943
#endif
944944

945-
#if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN)
945+
#if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) && \
946+
!defined(WOLFSSL_DILITHIUM_NO_ASN1)
946947
/* See if DER data is an Dilithium private key.
947948
*
948949
* Checks size meets minimum Falcon key size.
@@ -1151,7 +1152,8 @@ static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
11511152
keyType, keySz);
11521153
}
11531154
#endif /* HAVE_FALCON */
1154-
#if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN)
1155+
#if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) && \
1156+
!defined(WOLFSSL_DILITHIUM_NO_ASN1)
11551157
/* Try Falcon if key format is Dilithium level 2k, 3k or 5k or yet unknown.
11561158
*/
11571159
if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == DILITHIUM_LEVEL2k) ||

wolfcrypt/src/dilithium.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7074,8 +7074,7 @@ int wc_dilithium_export_key(dilithium_key* key, byte* priv, word32 *privSz,
70747074

70757075
#ifndef WOLFSSL_DILITHIUM_NO_ASN1
70767076

7077-
#if defined(WOLFSSL_DILITHIUM_PRIVATE_KEY) && \
7078-
defined(WOLFSSL_DILITHIUM_PUBLIC_KEY)
7077+
#if defined(WOLFSSL_DILITHIUM_PRIVATE_KEY)
70797078

70807079
/* Decode the DER encoded Dilithium key.
70817080
*
@@ -7135,13 +7134,13 @@ int wc_Dilithium_PrivateKeyDecode(const byte* input, word32* inOutIdx,
71357134
privKeyLen -= DILITHIUM_LEVEL2_PUB_KEY_SIZE;
71367135
}
71377136
else if ((key->level == 3) &&
7138-
(privKeyLen != DILITHIUM_LEVEL3_PRV_KEY_SIZE)) {
7137+
(privKeyLen == DILITHIUM_LEVEL3_PRV_KEY_SIZE)) {
71397138
pubKey = privKey + DILITHIUM_LEVEL3_KEY_SIZE;
71407139
pubKeyLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE;
71417140
privKeyLen -= DILITHIUM_LEVEL3_PUB_KEY_SIZE;
71427141
}
71437142
else if ((key->level == 5) &&
7144-
(privKeyLen != DILITHIUM_LEVEL5_PRV_KEY_SIZE)) {
7143+
(privKeyLen == DILITHIUM_LEVEL5_PRV_KEY_SIZE)) {
71457144
pubKey = privKey + DILITHIUM_LEVEL5_KEY_SIZE;
71467145
pubKeyLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE;
71477146
privKeyLen -= DILITHIUM_LEVEL5_PUB_KEY_SIZE;
@@ -7150,17 +7149,25 @@ int wc_Dilithium_PrivateKeyDecode(const byte* input, word32* inOutIdx,
71507149

71517150
if (ret == 0) {
71527151
/* Check whether public key data was found. */
7153-
if (pubKeyLen == 0) {
7152+
#if defined(WOLFSSL_DILITHIUM_PUBLIC_KEY)
7153+
if (pubKeyLen == 0)
7154+
#endif
7155+
{
71547156
/* No public key data, only import private key data. */
71557157
ret = wc_dilithium_import_private(privKey, privKeyLen, key);
71567158
}
7159+
#if defined(WOLFSSL_DILITHIUM_PUBLIC_KEY)
71577160
else {
71587161
/* Import private and public key data. */
71597162
ret = wc_dilithium_import_key(privKey, privKeyLen, pubKey,
71607163
pubKeyLen, key);
71617164
}
7165+
#endif
71627166
}
71637167

7168+
(void)pubKey;
7169+
(void)pubKeyLen;
7170+
71647171
return ret;
71657172
}
71667173

wolfssl/wolfcrypt/dilithium.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,7 @@ int wc_dilithium_export_key(dilithium_key* key, byte* priv, word32 *privSz,
675675
#endif
676676

677677
#ifndef WOLFSSL_DILITHIUM_NO_ASN1
678-
#if defined(WOLFSSL_DILITHIUM_PRIVATE_KEY) && \
679-
defined(WOLFSSL_DILITHIUM_PUBLIC_KEY)
678+
#if defined(WOLFSSL_DILITHIUM_PRIVATE_KEY)
680679
WOLFSSL_API int wc_Dilithium_PrivateKeyDecode(const byte* input,
681680
word32* inOutIdx, dilithium_key* key, word32 inSz);
682681
#endif
@@ -689,8 +688,7 @@ WOLFSSL_API int wc_Dilithium_PublicKeyDecode(const byte* input,
689688
WOLFSSL_API int wc_Dilithium_PublicKeyToDer(dilithium_key* key, byte* output,
690689
word32 inLen, int withAlg);
691690
#endif
692-
#if defined(WOLFSSL_DILITHIUM_PRIVATE_KEY) && \
693-
defined(WOLFSSL_DILITHIUM_PUBLIC_KEY)
691+
#if defined(WOLFSSL_DILITHIUM_PRIVATE_KEY)
694692
WOLFSSL_API int wc_Dilithium_KeyToDer(dilithium_key* key, byte* output,
695693
word32 inLen);
696694
#endif

0 commit comments

Comments
 (0)