mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-23 13:51:00 +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 =
|
static const b58_almostmaxint_t b58_almostmaxint_mask =
|
||||||
((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1);
|
((((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) {
|
bool b58tobin(void *bin, size_t *binszp, const char *b58) {
|
||||||
size_t binsz = *binszp;
|
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;
|
binu = bin;
|
||||||
for (i = 0; i < binsz; ++i) {
|
for (i = 0; i < binsz; ++i) {
|
||||||
if (binu[i]) {
|
if (binu[i]) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepend the correct number of null-bytes
|
||||||
if (zerocount > i) {
|
if (zerocount > i) {
|
||||||
/* result too large */
|
/* result too large */
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
*binszp = binsz - i + zerocount;
|
||||||
break;
|
|
||||||
}
|
|
||||||
--*binszp;
|
|
||||||
}
|
|
||||||
*binszp += zerocount;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user