mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-16 17:42:02 +00:00
crypto: sync base58.c with upstream
This commit is contained in:
parent
8524ff9832
commit
8eee90fd9a
@ -24,7 +24,6 @@
|
|||||||
#include "base58.h"
|
#include "base58.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include "memzero.h"
|
#include "memzero.h"
|
||||||
#include "ripemd160.h"
|
#include "ripemd160.h"
|
||||||
#include "sha2.h"
|
#include "sha2.h"
|
||||||
@ -41,6 +40,12 @@ const int8_t b58digits_map[] = {
|
|||||||
49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
|
49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef uint64_t b58_maxint_t;
|
||||||
|
typedef uint32_t b58_almostmaxint_t;
|
||||||
|
#define b58_almostmaxint_bits (sizeof(b58_almostmaxint_t) * 8)
|
||||||
|
static const b58_almostmaxint_t b58_almostmaxint_mask =
|
||||||
|
((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1);
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@ -50,17 +55,18 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {
|
|||||||
|
|
||||||
const unsigned char *b58u = (const unsigned char *)b58;
|
const unsigned char *b58u = (const unsigned char *)b58;
|
||||||
unsigned char *binu = bin;
|
unsigned char *binu = bin;
|
||||||
size_t outisz = (binsz + 3) / 4;
|
size_t outisz =
|
||||||
uint32_t outi[outisz];
|
(binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);
|
||||||
uint64_t t;
|
b58_almostmaxint_t outi[outisz];
|
||||||
uint32_t c;
|
b58_maxint_t t;
|
||||||
|
b58_almostmaxint_t c;
|
||||||
size_t i, j;
|
size_t i, j;
|
||||||
uint8_t bytesleft = binsz % 4;
|
uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t);
|
||||||
uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0;
|
b58_almostmaxint_t zeromask =
|
||||||
|
bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0;
|
||||||
unsigned zerocount = 0;
|
unsigned zerocount = 0;
|
||||||
size_t b58sz;
|
|
||||||
|
|
||||||
b58sz = strlen(b58);
|
size_t b58sz = strlen(b58);
|
||||||
|
|
||||||
memzero(outi, sizeof(outi));
|
memzero(outi, sizeof(outi));
|
||||||
|
|
||||||
@ -76,9 +82,9 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {
|
|||||||
return false;
|
return false;
|
||||||
c = (unsigned)b58digits_map[b58u[i]];
|
c = (unsigned)b58digits_map[b58u[i]];
|
||||||
for (j = outisz; j--;) {
|
for (j = outisz; j--;) {
|
||||||
t = ((uint64_t)outi[j]) * 58 + c;
|
t = ((b58_maxint_t)outi[j]) * 58 + c;
|
||||||
c = (t & 0x3f00000000) >> 32;
|
c = t >> b58_almostmaxint_bits;
|
||||||
outi[j] = t & 0xffffffff;
|
outi[j] = t & b58_almostmaxint_mask;
|
||||||
}
|
}
|
||||||
if (c)
|
if (c)
|
||||||
// Output number too big (carry to the next int32)
|
// Output number too big (carry to the next int32)
|
||||||
@ -89,26 +95,17 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
j = 0;
|
j = 0;
|
||||||
switch (bytesleft) {
|
if (bytesleft) {
|
||||||
case 3:
|
for (i = bytesleft; i > 0; --i) {
|
||||||
*(binu++) = (outi[0] & 0xff0000) >> 16;
|
*(binu++) = (outi[0] >> (8 * (i - 1))) & 0xff;
|
||||||
//-fallthrough
|
}
|
||||||
case 2:
|
++j;
|
||||||
*(binu++) = (outi[0] & 0xff00) >> 8;
|
|
||||||
//-fallthrough
|
|
||||||
case 1:
|
|
||||||
*(binu++) = (outi[0] & 0xff);
|
|
||||||
++j;
|
|
||||||
//-fallthrough
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; j < outisz; ++j) {
|
for (; j < outisz; ++j) {
|
||||||
*(binu++) = (outi[j] >> 0x18) & 0xff;
|
for (i = sizeof(*outi); i > 0; --i) {
|
||||||
*(binu++) = (outi[j] >> 0x10) & 0xff;
|
*(binu++) = (outi[j] >> (8 * (i - 1))) & 0xff;
|
||||||
*(binu++) = (outi[j] >> 8) & 0xff;
|
}
|
||||||
*(binu++) = (outi[j] >> 0) & 0xff;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count canonical base58 byte count
|
// Count canonical base58 byte count
|
||||||
@ -119,6 +116,7 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {
|
|||||||
/* result too large */
|
/* result too large */
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
--*binszp;
|
--*binszp;
|
||||||
@ -149,24 +147,28 @@ int b58check(const void *bin, size_t binsz, HasherType hasher_type,
|
|||||||
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) {
|
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) {
|
||||||
const uint8_t *bin = data;
|
const uint8_t *bin = data;
|
||||||
int carry;
|
int carry;
|
||||||
ssize_t i, j, high, zcount = 0;
|
size_t i, j, high, zcount = 0;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
|
||||||
while (zcount < (ssize_t)binsz && !bin[zcount]) ++zcount;
|
while (zcount < binsz && !bin[zcount]) ++zcount;
|
||||||
|
|
||||||
size = (binsz - zcount) * 138 / 100 + 1;
|
size = (binsz - zcount) * 138 / 100 + 1;
|
||||||
uint8_t buf[size];
|
uint8_t buf[size];
|
||||||
memzero(buf, size);
|
memzero(buf, size);
|
||||||
|
|
||||||
for (i = zcount, high = size - 1; i < (ssize_t)binsz; ++i, high = j) {
|
for (i = zcount, high = size - 1; i < binsz; ++i, high = j) {
|
||||||
for (carry = bin[i], j = size - 1; (j > high) || carry; --j) {
|
for (carry = bin[i], j = size - 1; (j > high) || carry; --j) {
|
||||||
carry += 256 * buf[j];
|
carry += 256 * buf[j];
|
||||||
buf[j] = carry % 58;
|
buf[j] = carry % 58;
|
||||||
carry /= 58;
|
carry /= 58;
|
||||||
|
if (!j) {
|
||||||
|
// Otherwise j wraps to maxint which is > high
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < (ssize_t)size && !buf[j]; ++j)
|
for (j = 0; j < size && !buf[j]; ++j)
|
||||||
;
|
;
|
||||||
|
|
||||||
if (*b58sz <= zcount + size - j) {
|
if (*b58sz <= zcount + size - j) {
|
||||||
@ -175,8 +177,7 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (zcount) memset(b58, '1', zcount);
|
if (zcount) memset(b58, '1', zcount);
|
||||||
for (i = zcount; j < (ssize_t)size; ++i, ++j)
|
for (i = zcount; j < size; ++i, ++j) b58[i] = b58digits_ordered[buf[j]];
|
||||||
b58[i] = b58digits_ordered[buf[j]];
|
|
||||||
b58[i] = '\0';
|
b58[i] = '\0';
|
||||||
*b58sz = i + 1;
|
*b58sz = i + 1;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user