mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-23 05:40:57 +00:00
crypto: Fix buffer overflow in b58tobin.
This commit is contained in:
parent
f34e2382fa
commit
20dd1ddc60
@ -46,6 +46,9 @@ typedef uint32_t b58_almostmaxint_t;
|
||||
static const b58_almostmaxint_t b58_almostmaxint_mask =
|
||||
((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1);
|
||||
|
||||
// Decodes a null-terminated Base58 string `b58` to binary and writes the result
|
||||
// at the end of the buffer `bin` of size `*binszp`. On success `*binszp` is set
|
||||
// to the number of valid bytes at the end of the buffer.
|
||||
bool b58tobin(void *bin, size_t *binszp, const char *b58) {
|
||||
size_t binsz = *binszp;
|
||||
|
||||
@ -108,20 +111,18 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {
|
||||
}
|
||||
}
|
||||
|
||||
// Count canonical base58 byte count
|
||||
// locate the most significant byte
|
||||
binu = bin;
|
||||
for (i = 0; i < binsz; ++i) {
|
||||
if (binu[i]) {
|
||||
if (zerocount > i) {
|
||||
/* result too large */
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
--*binszp;
|
||||
if (binu[i]) break;
|
||||
}
|
||||
*binszp += zerocount;
|
||||
|
||||
// prepend the correct number of null-bytes
|
||||
if (zerocount > i) {
|
||||
/* result too large */
|
||||
return false;
|
||||
}
|
||||
*binszp = binsz - i + zerocount;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user