Skip to content

Commit 8bba660

Browse files
committed
Dilithium: fix public and private key decode
Fixes to decoding to prevent accessing NULL key.
1 parent 5793f62 commit 8bba660

1 file changed

Lines changed: 27 additions & 33 deletions

File tree

wolfcrypt/src/dilithium.c

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7126,8 +7126,7 @@ int wc_Dilithium_PrivateKeyDecode(const byte* input, word32* inOutIdx,
71267126
ret = DecodeAsymKey_Assign(input, inOutIdx, inSz, &privKey, &privKeyLen,
71277127
&pubKey, &pubKeyLen, keytype);
71287128
}
7129-
7130-
if ((pubKey == NULL) && (pubKeyLen == 0)) {
7129+
if ((ret == 0) && (pubKey == NULL) && (pubKeyLen == 0)) {
71317130
/* Check if the public key is included in the private key. */
71327131
if ((key->level == 2) &&
71337132
(privKeyLen == DILITHIUM_LEVEL2_PRV_KEY_SIZE)) {
@@ -7197,40 +7196,35 @@ int wc_Dilithium_PublicKeyDecode(const byte* input, word32* inOutIdx,
71977196
if (ret == 0) {
71987197
/* Try to import the key directly. */
71997198
ret = wc_dilithium_import_public(input, inSz, key);
7200-
}
7201-
if (ret == 0) {
7202-
return 0;
7203-
}
7204-
else {
7205-
/* Not successful, decode it first. */
7206-
ret = 0;
7207-
}
7199+
if (ret != 0) {
7200+
/* Start again. */
7201+
ret = 0;
72087202

7209-
if (ret == 0) {
7210-
/* Get OID sum for level. */
7211-
if (key->level == 2) {
7212-
keytype = DILITHIUM_LEVEL2k;
7213-
}
7214-
else if (key->level == 3) {
7215-
keytype = DILITHIUM_LEVEL3k;
7216-
}
7217-
else if (key->level == 5) {
7218-
keytype = DILITHIUM_LEVEL5k;
7219-
}
7220-
else {
7221-
/* Level not set. */
7222-
ret = BAD_FUNC_ARG;
7203+
/* Get OID sum for level. */
7204+
if (key->level == 2) {
7205+
keytype = DILITHIUM_LEVEL2k;
7206+
}
7207+
else if (key->level == 3) {
7208+
keytype = DILITHIUM_LEVEL3k;
7209+
}
7210+
else if (key->level == 5) {
7211+
keytype = DILITHIUM_LEVEL5k;
7212+
}
7213+
else {
7214+
/* Level not set. */
7215+
ret = BAD_FUNC_ARG;
7216+
}
7217+
if (ret == 0) {
7218+
/* Decode the asymmetric key and get out public key data. */
7219+
ret = DecodeAsymKeyPublic_Assign(input, inOutIdx, inSz, &pubKey,
7220+
&pubKeyLen, keytype);
7221+
}
7222+
if (ret == 0) {
7223+
/* Import public key data. */
7224+
ret = wc_dilithium_import_public(pubKey, pubKeyLen, key);
7225+
}
72237226
}
72247227
}
7225-
if (ret == 0) {
7226-
/* Decode the asymmetric key and get out public key data. */
7227-
ret = DecodeAsymKeyPublic_Assign(input, inOutIdx, inSz, &pubKey,
7228-
&pubKeyLen, keytype);
7229-
}
7230-
if (ret == 0) {
7231-
/* Import public key data. */
7232-
ret = wc_dilithium_import_public(pubKey, pubKeyLen, key);
7233-
}
72347228
return ret;
72357229
}
72367230

0 commit comments

Comments
 (0)