Skip to content

Commit 165a6e1

Browse files
authored
Merge pull request #205 from miyazakh/f-638_NullPointer
F-638 : Fix Null pointer check
2 parents 6fbbc8d + 0c910c8 commit 165a6e1

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

src/genkey/clu_genkey.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ int wolfCLU_genKey_RSA(WC_RNG* rng, char* fName, int directive, int fmt, int
10391039
int wolfCLU_genKey_Dilithium(WC_RNG* rng, char* fName, int directive, int fmt,
10401040
int keySz, int level, int withAlg)
10411041
{
1042-
#ifdef HAVE_DILITHIUM
1042+
#ifdef HAVE_DILITHIUM
10431043
int ret = WOLFCLU_SUCCESS;
10441044

10451045
XFILE file = NULL;
@@ -1076,11 +1076,11 @@ int wolfCLU_genKey_Dilithium(WC_RNG* rng, char* fName, int directive, int fmt,
10761076

10771077
/* init the dilithium key */
10781078
if (wc_dilithium_init(key) != 0) {
1079-
wolfCLU_LogError("Failed to initialize Dilithium Key.\nRET: %d", ret);
1079+
wolfCLU_LogError("Failed to initialize Dilithium Key.");
10801080
#ifdef WOLFSSL_SMALL_STACK
10811081
XFREE(key, HEAP_HINT, DYNAMIC_TYPE_DILITHIUM);
10821082
#endif
1083-
return ret;
1083+
return WOLFCLU_FATAL_ERROR;
10841084
}
10851085

10861086
/* set the level of the dilithium key */
@@ -1113,12 +1113,6 @@ int wolfCLU_genKey_Dilithium(WC_RNG* rng, char* fName, int directive, int fmt,
11131113
if (ret == WOLFCLU_SUCCESS) {
11141114
XMEMSET(fOutNameBuf, 0, fNameSz + fExtSz);
11151115
XMEMCPY(fOutNameBuf, fName, fNameSz);
1116-
1117-
derBuf = (byte*)XMALLOC(keySz, HEAP_HINT,
1118-
DYNAMIC_TYPE_TMP_BUFFER);
1119-
if (derBuf == NULL) {
1120-
ret = MEMORY_E;
1121-
}
11221116
}
11231117

11241118
if (ret == WOLFCLU_SUCCESS) {
@@ -1129,6 +1123,13 @@ int wolfCLU_genKey_Dilithium(WC_RNG* rng, char* fName, int directive, int fmt,
11291123
case PRIV_ONLY_FILE:
11301124
/* add on the final part of the file name ".priv" */
11311125
XMEMCPY(fOutNameBuf + fNameSz, fExtPriv, fExtSz);
1126+
1127+
derBuf = (byte*)XMALLOC(keySz, HEAP_HINT,
1128+
DYNAMIC_TYPE_TMP_BUFFER);
1129+
if (derBuf == NULL) {
1130+
ret = MEMORY_E;
1131+
break;
1132+
}
11321133
WOLFCLU_LOG(WOLFCLU_L0, "Private key file = %s", fOutNameBuf);
11331134

11341135
/* Private key to der */
@@ -1184,16 +1185,17 @@ int wolfCLU_genKey_Dilithium(WC_RNG* rng, char* fName, int directive, int fmt,
11841185

11851186
FALL_THROUGH;
11861187
case PUB_ONLY_FILE:
1187-
/* add on the final part of the file name ".priv" */
1188+
/* add on the final part of the file name ".pub" */
11881189
XMEMCPY(fOutNameBuf + fNameSz, fExtPub, fExtSz);
11891190
WOLFCLU_LOG(WOLFCLU_L0, "Public key file = %s", fOutNameBuf);
11901191

11911192
derBuf = (byte*)XMALLOC(keySz, HEAP_HINT,
11921193
DYNAMIC_TYPE_TMP_BUFFER);
11931194
if (derBuf == NULL) {
11941195
ret = MEMORY_E;
1196+
break;
11951197
}
1196-
1198+
11971199
derBufSz = wc_Dilithium_PublicKeyToDer(key, derBuf,
11981200
(word32)keySz, withAlg);
11991201
if (derBufSz < 0) {

tests/genkey_sign_ver/genkey-sign-ver-test.sh

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ cleanup_genkey_sign_ver(){
4040
rm rsakey.pub
4141
rm mldsakey.priv
4242
rm mldsakey.pub
43+
rm mldsakey_pub.pub
44+
rm mldsakey_pub.priv
45+
rm mldsakey_priv.pub
46+
rm mldsakey_priv.priv
4347
rm ecc-signed.sig
4448
rm ed-signed.sig
4549
rm rsa-signed.sig
@@ -89,7 +93,7 @@ rsa_compare_decrypted(){
8993
else
9094
printf '%s\n' "Decrypted mismatch with original, FAILURE!"
9195
printf '%s\n' "DECRYPTED --> ${1}"
92-
printf '%s\n' "ORIGINAL --> ${2}" && exit -1
96+
printf '%s\n' "ORIGINAL --> ${2}" && exit 99
9397
fi
9498
}
9599

@@ -109,13 +113,13 @@ gen_key_sign_ver_test(){
109113
printf '%s\n' "genkey RESULT - $RESULT"
110114
[ $RESULT -ne 0 ] && printf '%s\n' "Failed $1 genkey" && \
111115
printf '%s\n' "Before running this test please configure wolfssl with" && \
112-
printf '%s\n' "--enable-keygen" && exit -1
116+
printf '%s\n' "--enable-keygen" && exit 99
113117

114118
# test signing with priv key
115119
./wolfssl -$1 -sign -inkey $2.priv -inform $4 -in sign-this.txt -out $3
116120
RESULT=$?
117121
printf '%s\n' "sign RESULT - $RESULT"
118-
[ $RESULT -ne 0 ] && printf '%s\n' "Failed $1 sign" && exit -1
122+
[ $RESULT -ne 0 ] && printf '%s\n' "Failed $1 sign" && exit 99
119123

120124
# test verifying with priv key
121125
if [ "${1}" = "rsa" ]; then
@@ -130,7 +134,7 @@ gen_key_sign_ver_test(){
130134
fi
131135
RESULT=$?
132136
printf '%s\n' "private verify RESULT - $RESULT"
133-
[ $RESULT -ne 0 ] && printf '%s\n' "Failed $1 private verify" && exit -1
137+
[ $RESULT -ne 0 ] && printf '%s\n' "Failed $1 private verify" && exit 99
134138

135139
# test verifying with pub key
136140
if [ "${1}" = "rsa" ]; then
@@ -141,7 +145,7 @@ gen_key_sign_ver_test(){
141145
fi
142146
RESULT=$?
143147
printf '%s\n' "public verify RESULT - $RESULT"
144-
[ $RESULT -ne 0 ] && printf '%s\n' "Failed $1 public verify " && exit -1
148+
[ $RESULT -ne 0 ] && printf '%s\n' "Failed $1 public verify " && exit 99
145149

146150
if [ $1 = "rsa" ]; then
147151
ORIGINAL=`cat -A sign-this.txt`
@@ -238,6 +242,20 @@ do
238242
gen_key_sign_ver_test ${ALGORITHM} ${KEYFILENAME} ${SIGOUTNAME} ${DERPEMRAW} ${level}
239243
done
240244

245+
# Verifies that -output PUB generates only the public key file.
246+
./wolfssl -genkey dilithium -level 2 -out mldsakey_pub -outform der -output pub
247+
RESULT=$?
248+
[ $RESULT -ne 0 ] && printf '%s\n' "Failed dilithium genkey -output PUB" && exit 99
249+
[ ! -f mldsakey_pub.pub ] && printf '%s\n' "dilithium -output PUB: .pub file missing" && exit 99
250+
[ -f mldsakey_pub.priv ] && printf '%s\n' "dilithium -output PUB: .priv unexpectedly created" && exit 99
251+
252+
# Verifies that -output PRIV generates only the private key file.
253+
./wolfssl -genkey dilithium -level 2 -out mldsakey_priv -outform der -output priv
254+
RESULT=$?
255+
[ $RESULT -ne 0 ] && printf '%s\n' "Failed dilithium genkey -output PRIV" && exit 99
256+
[ ! -f mldsakey_priv.priv ] && printf '%s\n' "dilithium -output PRIV: .priv file missing" && exit 99
257+
[ -f mldsakey_priv.pub ] && printf '%s\n' "dilithium -output PRIV: .pub unexpectedly created" && exit 99
258+
241259
# Dilithium sign to an unwritable path must fail gracefully
242260
./wolfssl -genkey dilithium -level 2 -out mldsakey -outform der -output keypair
243261
./wolfssl -dilithium -sign -inkey mldsakey.priv -inform der \

0 commit comments

Comments
 (0)