Skip to content

Commit f0cf51c

Browse files
committed
Fail ecc-sign when a round produces an invalid signature
1 parent ad25487 commit f0cf51c

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

ecc/ecc-sign.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <wolfssl/wolfcrypt/sha256.h>
2727
#include <wolfssl/wolfcrypt/ecc.h>
2828
#include <wolfssl/wolfcrypt/random.h>
29+
#include <wolfssl/wolfcrypt/error-crypt.h>
2930

3031
#define MAX_FIRMWARE_LEN (1024 * 1024)
3132
static const int gFwLen = MAX_FIRMWARE_LEN;
@@ -146,6 +147,9 @@ static int SignFirmware(byte* hashBuf, word32 hashLen, byte* sigBuf, word32* sig
146147
ret = wc_ecc_verify_hash(sigBuf, *sigLen, hashBuf, hashLen,
147148
&is_valid_sig, &gMyKey);
148149
printf("Verify ret %d, is_valid_sig %d\n", ret, is_valid_sig);
150+
/* a bad signature also returns 0 here: the result is in is_valid_sig */
151+
if (ret == 0 && is_valid_sig != 1)
152+
ret = SIG_VERIFY_E;
149153
}
150154

151155
wc_FreeRng(&rng);
@@ -156,6 +160,7 @@ static int SignFirmware(byte* hashBuf, word32 hashLen, byte* sigBuf, word32* sig
156160
int main(void)
157161
{
158162
int ret;
163+
int firstErr = 0;
159164
byte hashBuf[WC_SHA256_DIGEST_SIZE];
160165
word32 hashLen = WC_SHA256_DIGEST_SIZE;
161166
byte sigBuf[ECC_MAX_SIG_SIZE];
@@ -179,6 +184,10 @@ int main(void)
179184

180185
printf("Firmware Signature %d: Ret %d, HashLen %d, SigLen %d\n", i, ret, hashLen, sigLen);
181186

187+
/* keep the first error: ret alone would report only the last round */
188+
if (ret != 0 && firstErr == 0)
189+
firstErr = ret;
190+
182191
#ifdef ENABLE_BUF_PRINT
183192
PrintBuffer(hashBuf, hashLen);
184193
printf("\n");
@@ -191,5 +200,6 @@ int main(void)
191200
gMyKeyInit = 0;
192201
}
193202

194-
return ret;
203+
/* not firstErr: an exit code is masked to 8 bits, so -256 would read as 0 */
204+
return (firstErr == 0) ? 0 : 1;
195205
}

0 commit comments

Comments
 (0)