diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index af1c9c244..39684a232 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -12711,14 +12711,24 @@ int sp_tohex(sp_int* a, char* str) i = a->used - 1; #ifndef WC_DISABLE_RADIX_ZERO_PAD for (j = SP_WORD_SIZE - 8; j >= 0; j -= 8) { - if (((a->dp[i] >> j) & 0xff) != 0) + if (((a->dp[i] >> j) & 0xff) != 0) { break; + } + else if (j == 0) { + j = SP_WORD_SIZE - 8; + --i; + } } j += 4; #else for (j = SP_WORD_SIZE - 4; j >= 0; j -= 4) { - if (((a->dp[i] >> j) & 0xf) != 0) + if (((a->dp[i] >> j) & 0xf) != 0) { break; + } + else if (j == 0) { + j = SP_WORD_SIZE - 4; + --i; + } } #endif /* WC_DISABLE_RADIX_ZERO_PAD */ for (; j >= 0; j -= 4) {