Skip to content

Commit

Permalink
update base58 lib
Browse files Browse the repository at this point in the history
  • Loading branch information
TamtamHero authored and BTChip github committed Jan 21, 2020
1 parent c697e78 commit 6382147
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dep
obj
src/glyphs.c
src/glyphs.h

.vscode
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ APPNAME = XRP
APP_LOAD_PARAMS=--appFlags 0x240 --path "44'/144'" --curve secp256k1 --curve ed25519 $(COMMON_LOAD_PARAMS)

APPVERSION_M=1
APPVERSION_N=0
APPVERSION_P=9
APPVERSION_N=1
APPVERSION_P=0
APPVERSION=$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)
DEFINES += UNUSED\(x\)=\(void\)x
DEFINES += APPVERSION=\"$(APPVERSION)\"
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,7 @@ unsigned int io_seproxyhal_touch_tx_ok(const bagl_element_t *e) {
os_memset(privateKeyData, 0, sizeof(privateKeyData));
if (tmpCtx.transactionContext.curve == CX_CURVE_256K1) {
cx_hash_sha512(tmpCtx.transactionContext.rawTx, tmpCtx.transactionContext.rawTxLength, privateKeyData, 64);
PRINTF("Hash to sign:\n%.*H\n", 32, privateKeyData);
tx = cx_ecdsa_sign(&privateKey, CX_RND_RFC6979 | CX_LAST, CX_SHA256,
privateKeyData,
32, G_io_apdu_buffer, sizeof(G_io_apdu_buffer), NULL);
Expand Down
199 changes: 110 additions & 89 deletions src/xrpBase58.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include "xrpBase58.h"

#define MAX_DEC_INPUT_SIZE 164
#define MAX_ENC_INPUT_SIZE 120

static const unsigned char const BASE58TABLE[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Expand All @@ -38,99 +41,117 @@ static const unsigned char const BASE58ALPHABET[] = {
};


unsigned char xrp_decode_base58(unsigned char WIDE *in, unsigned char length,
unsigned char *out,
unsigned char maxoutlen) {
unsigned char tmp[164];
unsigned char buffer[164];
unsigned char i;
unsigned char j;
unsigned char startAt;
unsigned char zeroCount = 0;
if (length > sizeof(tmp)) {
THROW(INVALID_PARAMETER);
}
os_memmove(tmp, in, length);
for (i = 0; i < length; i++) {
if (in[i] > 128) {
THROW(EXCEPTION);
}
tmp[i] = BASE58TABLE[in[i]];
if (tmp[i] == 0xff) {
THROW(EXCEPTION);
}
}
while ((zeroCount < length) && (tmp[zeroCount] == 0)) {
++zeroCount;
int xrp_decode_base58(const char *in, size_t length,
unsigned char *out, size_t *outlen) {
unsigned char tmp[MAX_DEC_INPUT_SIZE];
unsigned char buffer[MAX_DEC_INPUT_SIZE] = {0};
unsigned char i;
unsigned char j;
unsigned char startAt;
unsigned char zeroCount = 0;
if (length > MAX_DEC_INPUT_SIZE) {
return -1;
}
os_memmove(tmp, in, length);
PRINTF("To decode\n%s\n",tmp);
for (i = 0; i < length; i++) {
if (in[i] >= sizeof(BASE58TABLE)) {
return -1;
}
j = length;
startAt = zeroCount;
while (startAt < length) {
unsigned short remainder = 0;
unsigned char divLoop;
for (divLoop = startAt; divLoop < length; divLoop++) {
unsigned short digit256 = (unsigned short)(tmp[divLoop] & 0xff);
unsigned short tmpDiv = remainder * 58 + digit256;
tmp[divLoop] = (unsigned char)(tmpDiv / 256);
remainder = (tmpDiv % 256);
}
if (tmp[startAt] == 0) {
++startAt;
}
buffer[--j] = (unsigned char)remainder;
tmp[i] = BASE58TABLE[(int)in[i]];
if (tmp[i] == 0xff) {
return -1;
}
while ((j < length) && (buffer[j] == 0)) {
++j;
}
while ((zeroCount < length) && (tmp[zeroCount] == 0)) {
++zeroCount;
}
j = length;
startAt = zeroCount;
while (startAt < length) {
unsigned short remainder = 0;
unsigned char divLoop;
for (divLoop = startAt; divLoop < length; divLoop++) {
unsigned short digit256 = (unsigned short)(tmp[divLoop] & 0xff);
unsigned short tmpDiv = remainder * 58 + digit256;
tmp[divLoop] = (unsigned char)(tmpDiv / 256);
remainder = (tmpDiv % 256);
}
length = length - (j - zeroCount);
if (maxoutlen < length) {
THROW(EXCEPTION_OVERFLOW);
if (tmp[startAt] == 0) {
++startAt;
}
os_memmove(out, buffer + j - zeroCount, length);
return length;
buffer[--j] = (unsigned char)remainder;
}
while ((j < length) && (buffer[j] == 0)) {
++j;
}
length = length - (j - zeroCount);
if (*outlen < length) {
PRINTF("Decode overflow %d %d\n", length, *outlen);
return -1;
}

os_memmove(out, buffer + j - zeroCount, length);
out[length] = '\0';
PRINTF("Decoded\n%.*H\n",length,out);
*outlen = length;
return 0;
}

unsigned char xrp_encode_base58(unsigned char WIDE *in, unsigned char length,
unsigned char *out,
unsigned char maxoutlen) {
unsigned char tmp[164];
unsigned char buffer[164];
unsigned char j;
unsigned char startAt;
unsigned char zeroCount = 0;
if (length > sizeof(tmp)) {
THROW(INVALID_PARAMETER);
}
os_memmove(tmp, in, length);
while ((zeroCount < length) && (tmp[zeroCount] == 0)) {
++zeroCount;
}
j = 2 * length;
startAt = zeroCount;
while (startAt < length) {
unsigned short remainder = 0;
unsigned char divLoop;
for (divLoop = startAt; divLoop < length; divLoop++) {
unsigned short digit256 = (unsigned short)(tmp[divLoop] & 0xff);
unsigned short tmpDiv = remainder * 256 + digit256;
tmp[divLoop] = (unsigned char)(tmpDiv / 58);
remainder = (tmpDiv % 58);
}
if (tmp[startAt] == 0) {
++startAt;
}
buffer[--j] = (unsigned char)BASE58ALPHABET[remainder];
}
while ((j < (2 * length)) && (buffer[j] == BASE58ALPHABET[0])) {
++j;
}
while (zeroCount-- > 0) {
buffer[--j] = BASE58ALPHABET[0];
}
length = 2 * length - j;
if (maxoutlen < length) {
THROW(EXCEPTION_OVERFLOW);
int xrp_encode_base58(const unsigned char *in, size_t length,
unsigned char *out, size_t *outlen) {
unsigned char buffer[MAX_ENC_INPUT_SIZE * 138 / 100 + 1] = {0};
size_t i = 0, j;
size_t startAt, stopAt;
size_t zeroCount = 0;
size_t outputSize;

if (length > MAX_ENC_INPUT_SIZE) {
return -1;
}

PRINTF("Length to encode %d\n", length);
PRINTF("To encode\n%.*H\n",length,in);

while ((zeroCount < length) && (in[zeroCount] == 0)) {
++zeroCount;
}

outputSize = (length - zeroCount) * 138 / 100 + 1;
stopAt = outputSize - 1;
for (startAt = zeroCount; startAt < length; startAt++) {
int carry = in[startAt];
for (j = outputSize - 1; (int)j >= 0; j--) {
carry += 256 * buffer[j];
buffer[j] = carry % 58;
carry /= 58;

if (j <= stopAt - 1 && carry == 0) {
break;
}
}
os_memmove(out, (buffer + j), length);
return length;
}
stopAt = j;
}

j = 0;
while (j < outputSize && buffer[j] == 0) {
j += 1;
}

if (*outlen < zeroCount + outputSize - j) {
*outlen = zeroCount + outputSize - j;
return -1;
}

os_memset(out, BASE58ALPHABET[0], zeroCount);

i = zeroCount;
while (j < outputSize) {
out[i++] = BASE58ALPHABET[buffer[j++]];
}
out[i] = '\0';
*outlen = i;
PRINTF("Length encoded %d\n", i);
PRINTF("Encoded\n%.*H\n",i,out);
return 0;
}
8 changes: 2 additions & 6 deletions src/xrpBase58.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
#include "os.h"
#include "cx.h"

unsigned char xrp_decode_base58(unsigned char WIDE *in, unsigned char length,
unsigned char *out,
unsigned char maxoutlen);
int xrp_decode_base58(const char *in, size_t length, unsigned char *out, size_t *outlen);

unsigned char xrp_encode_base58(unsigned char WIDE *in, unsigned char length,
unsigned char *out,
unsigned char maxoutlen);
int xrp_encode_base58(const unsigned char *in, size_t length, unsigned char *out, size_t *outlen);
17 changes: 12 additions & 5 deletions src/xrpHelpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ unsigned short xrp_public_key_to_encoded_base58(
unsigned char checksumBuffer[32];
cx_sha256_t hash;
unsigned char versionSize = (version > 255 ? 2 : 1);
size_t size = outlen;

if (version > 255) {
tmpBuffer[0] = (version >> 8);
Expand All @@ -61,7 +62,10 @@ unsigned short xrp_public_key_to_encoded_base58(
cx_hash(&hash.header, CX_LAST, checksumBuffer, 32, checksumBuffer, 32);

os_memmove(tmpBuffer + 20 + versionSize, checksumBuffer, 4);
return xrp_encode_base58(tmpBuffer, 24 + versionSize, out, outlen);
if(xrp_encode_base58(tmpBuffer, 24 + versionSize, out, &size)){
return 0;
}
return size;
}

unsigned short xrp_decode_base58_address(unsigned char WIDE *in,
Expand All @@ -70,19 +74,22 @@ unsigned short xrp_decode_base58_address(unsigned char WIDE *in,
unsigned short outlen) {
unsigned char hashBuffer[32];
cx_sha256_t hash;
outlen = xrp_decode_base58(in, inlen, out, outlen);
size_t size = outlen;
if(xrp_decode_base58(in, inlen, out, &size)){
THROW(EXCEPTION);
}

// Compute hash to verify address
cx_sha256_init(&hash);
cx_hash(&hash.header, CX_LAST, out, outlen - 4, hashBuffer, 32);
cx_hash(&hash.header, CX_LAST, out, size - 4, hashBuffer, 32);
cx_sha256_init(&hash);
cx_hash(&hash.header, CX_LAST, hashBuffer, 32, hashBuffer, 32);

if (os_memcmp(out + outlen - 4, hashBuffer, 4)) {
if (os_memcmp(out + size - 4, hashBuffer, 4)) {
THROW(INVALID_CHECKSUM);
}

return outlen;
return size;
}

unsigned short xrp_compress_public_key(cx_ecfp_public_key_t *publicKey, uint8_t *out, uint32_t outlen) {
Expand Down

0 comments on commit 6382147

Please sign in to comment.