Initial import
This commit is contained in:
339
src_common/ethUstream.c
Normal file
339
src_common/ethUstream.c
Normal file
@@ -0,0 +1,339 @@
|
||||
/*******************************************************************************
|
||||
* Ledger Blue
|
||||
* (c) 2016 Ledger
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
********************************************************************************/
|
||||
|
||||
#include "ethUstream.h"
|
||||
#include "ethUtils.h"
|
||||
|
||||
#define MAX_INT256 32
|
||||
#define MAX_ADDRESS 20
|
||||
|
||||
void initTx(txContext_t *context, app_cx_sha3_t *sha3, txContent_t *content,
|
||||
ustreamProcess_t customProcessor, void *extra) {
|
||||
os_memset(context, 0, sizeof(txContext_t));
|
||||
context->sha3 = sha3;
|
||||
context->content = content;
|
||||
context->customProcessor = customProcessor;
|
||||
context->extra = extra;
|
||||
context->currentField = TX_RLP_CONTENT;
|
||||
app_cx_sha3_init(context->sha3, 256);
|
||||
}
|
||||
|
||||
uint8_t readTxByte(txContext_t *context) {
|
||||
uint8_t data;
|
||||
if (context->commandLength < 1) {
|
||||
screen_printf("readTxByte Underflow\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
data = *context->workBuffer;
|
||||
context->workBuffer++;
|
||||
context->commandLength--;
|
||||
if (context->processingField) {
|
||||
context->currentFieldPos++;
|
||||
}
|
||||
if (!(context->processingField && context->fieldSingleByte)) {
|
||||
app_cx_hash((cx_hash_t*)context->sha3, 0, &data, 1, NULL);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
void copyTxData(txContext_t *context, uint8_t *out, uint32_t length) {
|
||||
if (context->commandLength < length) {
|
||||
screen_printf("copyTxData Underflow\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (out != NULL) {
|
||||
os_memmove(out, context->workBuffer, length);
|
||||
}
|
||||
if (!(context->processingField && context->fieldSingleByte)) {
|
||||
app_cx_hash((cx_hash_t*)context->sha3, 0, context->workBuffer, length, NULL);
|
||||
}
|
||||
context->workBuffer += length;
|
||||
context->commandLength -= length;
|
||||
if (context->processingField) {
|
||||
context->currentFieldPos += length;
|
||||
}
|
||||
}
|
||||
|
||||
static void processContent(txContext_t *context) {
|
||||
// Keep the full length for sanity checks, move to the next field
|
||||
if (!context->currentFieldIsList) {
|
||||
screen_printf("Invalid type for RLP_CONTENT\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
context->dataLength = context->currentFieldLength;
|
||||
context->currentField++;
|
||||
context->processingField = false;
|
||||
}
|
||||
|
||||
static void processNonce(txContext_t *context) {
|
||||
if (context->currentFieldIsList) {
|
||||
screen_printf("Invalid type for RLP_NONCE\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldLength > MAX_INT256) {
|
||||
screen_printf("Invalid length for RLP_NONCE\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldPos < context->currentFieldLength) {
|
||||
uint32_t copySize =
|
||||
(context->commandLength <
|
||||
((context->currentFieldLength - context->currentFieldPos))
|
||||
? context->commandLength
|
||||
: context->currentFieldLength - context->currentFieldPos);
|
||||
copyTxData(context, NULL, copySize);
|
||||
}
|
||||
if (context->currentFieldPos == context->currentFieldLength) {
|
||||
context->currentField++;
|
||||
context->processingField = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void processStartGas(txContext_t *context) {
|
||||
if (context->currentFieldIsList) {
|
||||
screen_printf("Invalid type for RLP_STARTGAS\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldLength > MAX_INT256) {
|
||||
screen_printf("Invalid length for RLP_STARTGAS %d\n",
|
||||
context->currentFieldLength);
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldPos < context->currentFieldLength) {
|
||||
uint32_t copySize =
|
||||
(context->commandLength <
|
||||
((context->currentFieldLength - context->currentFieldPos))
|
||||
? context->commandLength
|
||||
: context->currentFieldLength - context->currentFieldPos);
|
||||
copyTxData(context,
|
||||
context->content->startgas.value + context->currentFieldPos,
|
||||
copySize);
|
||||
}
|
||||
if (context->currentFieldPos == context->currentFieldLength) {
|
||||
context->content->startgas.length = context->currentFieldLength;
|
||||
context->currentField++;
|
||||
context->processingField = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void processGasprice(txContext_t *context) {
|
||||
if (context->currentFieldIsList) {
|
||||
screen_printf("Invalid type for RLP_GASPRICE\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldLength > MAX_INT256) {
|
||||
screen_printf("Invalid length for RLP_GASPRICE\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldPos < context->currentFieldLength) {
|
||||
uint32_t copySize =
|
||||
(context->commandLength <
|
||||
((context->currentFieldLength - context->currentFieldPos))
|
||||
? context->commandLength
|
||||
: context->currentFieldLength - context->currentFieldPos);
|
||||
copyTxData(context,
|
||||
context->content->gasprice.value + context->currentFieldPos,
|
||||
copySize);
|
||||
}
|
||||
if (context->currentFieldPos == context->currentFieldLength) {
|
||||
context->content->gasprice.length = context->currentFieldLength;
|
||||
context->currentField++;
|
||||
context->processingField = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void processValue(txContext_t *context) {
|
||||
if (context->currentFieldIsList) {
|
||||
screen_printf("Invalid type for RLP_VALUE\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldLength > MAX_INT256) {
|
||||
screen_printf("Invalid length for RLP_VALUE\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldPos < context->currentFieldLength) {
|
||||
uint32_t copySize =
|
||||
(context->commandLength <
|
||||
((context->currentFieldLength - context->currentFieldPos))
|
||||
? context->commandLength
|
||||
: context->currentFieldLength - context->currentFieldPos);
|
||||
copyTxData(context,
|
||||
context->content->value.value + context->currentFieldPos,
|
||||
copySize);
|
||||
}
|
||||
if (context->currentFieldPos == context->currentFieldLength) {
|
||||
context->content->value.length = context->currentFieldLength;
|
||||
context->currentField++;
|
||||
context->processingField = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void processTo(txContext_t *context) {
|
||||
if (context->currentFieldIsList) {
|
||||
screen_printf("Invalid type for RLP_TO\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldLength > MAX_ADDRESS) {
|
||||
screen_printf("Invalid length for RLP_TO\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldPos < context->currentFieldLength) {
|
||||
uint32_t copySize =
|
||||
(context->commandLength <
|
||||
((context->currentFieldLength - context->currentFieldPos))
|
||||
? context->commandLength
|
||||
: context->currentFieldLength - context->currentFieldPos);
|
||||
copyTxData(context,
|
||||
context->content->destination + context->currentFieldPos,
|
||||
copySize);
|
||||
}
|
||||
if (context->currentFieldPos == context->currentFieldLength) {
|
||||
context->currentField++;
|
||||
context->processingField = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void processData(txContext_t *context) {
|
||||
if (context->currentFieldIsList) {
|
||||
screen_printf("Invalid type for RLP_DATA\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldLength > MAX_INT256) {
|
||||
screen_printf("Invalid length for RLP_DATA\n");
|
||||
THROW(EXCEPTION);
|
||||
}
|
||||
if (context->currentFieldPos < context->currentFieldLength) {
|
||||
uint32_t copySize =
|
||||
(context->commandLength <
|
||||
((context->currentFieldLength - context->currentFieldPos))
|
||||
? context->commandLength
|
||||
: context->currentFieldLength - context->currentFieldPos);
|
||||
copyTxData(context, NULL, copySize);
|
||||
}
|
||||
if (context->currentFieldPos == context->currentFieldLength) {
|
||||
context->currentField++;
|
||||
context->processingField = false;
|
||||
}
|
||||
}
|
||||
|
||||
static parserStatus_e processTxInternal(txContext_t *context) {
|
||||
for (;;) {
|
||||
bool processedCustom = false;
|
||||
if (context->currentField == TX_RLP_DONE) {
|
||||
return USTREAM_FINISHED;
|
||||
}
|
||||
if (context->commandLength == 0) {
|
||||
return USTREAM_PROCESSING;
|
||||
}
|
||||
if (!context->processingField) {
|
||||
bool canDecode = false;
|
||||
uint32_t offset;
|
||||
while (context->commandLength != 0) {
|
||||
bool valid;
|
||||
// Feed the RLP buffer until the length can be decoded
|
||||
context->rlpBuffer[context->rlpBufferPos++] =
|
||||
readTxByte(context);
|
||||
if (rlpCanDecode(context->rlpBuffer, context->rlpBufferPos,
|
||||
&valid)) {
|
||||
// Can decode now, if valid
|
||||
if (!valid) {
|
||||
screen_printf("RLP pre-decode error\n");
|
||||
return USTREAM_FAULT;
|
||||
}
|
||||
canDecode = true;
|
||||
break;
|
||||
}
|
||||
// Cannot decode yet
|
||||
// Sanity check
|
||||
if (context->rlpBufferPos == sizeof(context->rlpBuffer)) {
|
||||
screen_printf("RLP pre-decode logic error\n");
|
||||
return USTREAM_FAULT;
|
||||
}
|
||||
}
|
||||
if (!canDecode) {
|
||||
return USTREAM_PROCESSING;
|
||||
}
|
||||
// Ready to process this field
|
||||
if (!rlpDecodeLength(context->rlpBuffer, context->rlpBufferPos,
|
||||
&context->currentFieldLength, &offset,
|
||||
&context->currentFieldIsList)) {
|
||||
screen_printf("RLP decode error\n");
|
||||
return USTREAM_FAULT;
|
||||
}
|
||||
if (offset == 0) {
|
||||
// Hack for single byte, self encoded
|
||||
context->workBuffer--;
|
||||
context->commandLength++;
|
||||
context->fieldSingleByte = true;
|
||||
} else {
|
||||
context->fieldSingleByte = false;
|
||||
}
|
||||
context->currentFieldPos = 0;
|
||||
context->rlpBufferPos = 0;
|
||||
context->processingField = true;
|
||||
}
|
||||
if (context->customProcessor != NULL) {
|
||||
processedCustom = context->customProcessor(context);
|
||||
}
|
||||
if (!processedCustom) {
|
||||
switch (context->currentField) {
|
||||
case TX_RLP_CONTENT:
|
||||
processContent(context);
|
||||
break;
|
||||
case TX_RLP_NONCE:
|
||||
processNonce(context);
|
||||
break;
|
||||
case TX_RLP_GASPRICE:
|
||||
processGasprice(context);
|
||||
break;
|
||||
case TX_RLP_STARTGAS:
|
||||
processStartGas(context);
|
||||
break;
|
||||
case TX_RLP_VALUE:
|
||||
processValue(context);
|
||||
break;
|
||||
case TX_RLP_TO:
|
||||
processTo(context);
|
||||
break;
|
||||
case TX_RLP_DATA:
|
||||
processData(context);
|
||||
break;
|
||||
default:
|
||||
screen_printf("Invalid RLP decoder context\n");
|
||||
return USTREAM_FAULT;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parserStatus_e processTx(txContext_t *context, uint8_t *buffer,
|
||||
uint32_t length) {
|
||||
parserStatus_e result;
|
||||
BEGIN_TRY {
|
||||
TRY {
|
||||
context->workBuffer = buffer;
|
||||
context->commandLength = length;
|
||||
result = processTxInternal(context);
|
||||
}
|
||||
CATCH_OTHER(e) {
|
||||
result = USTREAM_FAULT;
|
||||
}
|
||||
FINALLY {
|
||||
}
|
||||
}
|
||||
END_TRY;
|
||||
return result;
|
||||
}
|
||||
80
src_common/ethUstream.h
Normal file
80
src_common/ethUstream.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/*******************************************************************************
|
||||
* Ledger Blue
|
||||
* (c) 2016 Ledger
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
********************************************************************************/
|
||||
|
||||
#include "os.h"
|
||||
#include "cx.h"
|
||||
#include <stdbool.h>
|
||||
#include "app_cx_sha3.h"
|
||||
|
||||
struct txContext_t;
|
||||
|
||||
typedef bool (*ustreamProcess_t)(struct txContext_t *context);
|
||||
|
||||
typedef enum rlpTxField_e {
|
||||
TX_RLP_NONE = 0,
|
||||
TX_RLP_CONTENT,
|
||||
TX_RLP_NONCE,
|
||||
TX_RLP_GASPRICE,
|
||||
TX_RLP_STARTGAS,
|
||||
TX_RLP_TO,
|
||||
TX_RLP_VALUE,
|
||||
TX_RLP_DATA,
|
||||
TX_RLP_DONE
|
||||
} rlpTxField_e;
|
||||
|
||||
typedef enum parserStatus_e {
|
||||
USTREAM_PROCESSING,
|
||||
USTREAM_FINISHED,
|
||||
USTREAM_FAULT
|
||||
} parserStatus_e;
|
||||
|
||||
typedef struct txInt256_t {
|
||||
uint8_t value[32];
|
||||
uint8_t length;
|
||||
} txInt256_t;
|
||||
|
||||
typedef struct txContent_t {
|
||||
txInt256_t gasprice;
|
||||
txInt256_t startgas;
|
||||
txInt256_t value;
|
||||
uint8_t destination[20];
|
||||
} txContent_t;
|
||||
|
||||
typedef struct txContext_t {
|
||||
rlpTxField_e currentField;
|
||||
app_cx_sha3_t *sha3;
|
||||
uint32_t currentFieldLength;
|
||||
uint32_t currentFieldPos;
|
||||
bool currentFieldIsList;
|
||||
bool processingField;
|
||||
bool fieldSingleByte;
|
||||
uint32_t dataLength;
|
||||
uint8_t rlpBuffer[5];
|
||||
uint32_t rlpBufferPos;
|
||||
uint8_t *workBuffer;
|
||||
uint32_t commandLength;
|
||||
ustreamProcess_t customProcessor;
|
||||
txContent_t *content;
|
||||
void *extra;
|
||||
} txContext_t;
|
||||
|
||||
void initTx(txContext_t *context, app_cx_sha3_t *sha3, txContent_t *content,
|
||||
ustreamProcess_t customProcessor, void *extra);
|
||||
parserStatus_e processTx(txContext_t *context, uint8_t *buffer,
|
||||
uint32_t length);
|
||||
void copyTxData(txContext_t *context, uint8_t *out, uint32_t length);
|
||||
uint8_t readTxByte(txContext_t *context);
|
||||
269
src_common/ethUtils.c
Normal file
269
src_common/ethUtils.c
Normal file
@@ -0,0 +1,269 @@
|
||||
/*******************************************************************************
|
||||
* Ledger Blue
|
||||
* (c) 2016 Ledger
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
********************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Utilities for an Ethereum Hardware Wallet logic
|
||||
* @file ethUtils.h
|
||||
* @author Ledger Firmware Team <hello@ledger.fr>
|
||||
* @version 1.0
|
||||
* @date 8th of March 2016
|
||||
*/
|
||||
|
||||
#include "os.h"
|
||||
#include "cx.h"
|
||||
#include <stdbool.h>
|
||||
#include "ethUtils.h"
|
||||
|
||||
bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid) {
|
||||
if (*buffer <= 0x7f) {
|
||||
} else if (*buffer <= 0xb7) {
|
||||
} else if (*buffer <= 0xbf) {
|
||||
if (bufferLength < (1 + (*buffer - 0xb7))) {
|
||||
return false;
|
||||
}
|
||||
if (*buffer > 0xbb) {
|
||||
*valid = false; // arbitrary 32 bits length limitation
|
||||
return true;
|
||||
}
|
||||
} else if (*buffer <= 0xf7) {
|
||||
} else {
|
||||
if (bufferLength < (1 + (*buffer - 0xf7))) {
|
||||
return false;
|
||||
}
|
||||
if (*buffer > 0xfb) {
|
||||
*valid = false; // arbitrary 32 bits length limitation
|
||||
return true;
|
||||
}
|
||||
}
|
||||
*valid = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rlpDecodeLength(uint8_t *buffer, uint32_t bufferLength,
|
||||
uint32_t *fieldLength, uint32_t *offset, bool *list) {
|
||||
if (*buffer <= 0x7f) {
|
||||
*offset = 0;
|
||||
*fieldLength = 1;
|
||||
*list = false;
|
||||
} else if (*buffer <= 0xb7) {
|
||||
*offset = 1;
|
||||
*fieldLength = *buffer - 0x80;
|
||||
*list = false;
|
||||
} else if (*buffer <= 0xbf) {
|
||||
*offset = 1 + (*buffer - 0xb7);
|
||||
*list = false;
|
||||
switch (*buffer) {
|
||||
case 0xb8:
|
||||
*fieldLength = *(buffer + 1);
|
||||
break;
|
||||
case 0xb9:
|
||||
*fieldLength = (*(buffer + 1) << 8) + *(buffer + 2);
|
||||
break;
|
||||
case 0xba:
|
||||
*fieldLength =
|
||||
(*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3);
|
||||
break;
|
||||
case 0xbb:
|
||||
*fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) +
|
||||
(*(buffer + 3) << 8) + *(buffer + 4);
|
||||
break;
|
||||
default:
|
||||
return false; // arbitrary 32 bits length limitation
|
||||
}
|
||||
} else if (*buffer <= 0xf7) {
|
||||
*offset = 1;
|
||||
*fieldLength = *buffer - 0xc0;
|
||||
*list = true;
|
||||
} else {
|
||||
*offset = 1 + (*buffer - 0xf7);
|
||||
*list = true;
|
||||
switch (*buffer) {
|
||||
case 0xf8:
|
||||
*fieldLength = *(buffer + 1);
|
||||
break;
|
||||
case 0xf9:
|
||||
*fieldLength = (*(buffer + 1) << 8) + *(buffer + 2);
|
||||
break;
|
||||
case 0xfa:
|
||||
*fieldLength =
|
||||
(*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3);
|
||||
break;
|
||||
case 0xfb:
|
||||
*fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) +
|
||||
(*(buffer + 3) << 8) + *(buffer + 4);
|
||||
break;
|
||||
default:
|
||||
return false; // arbitrary 32 bits length limitation
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
|
||||
app_cx_sha3_t *sha3Context) {
|
||||
uint8_t hashAddress[32];
|
||||
app_cx_sha3_init(sha3Context, 256);
|
||||
app_cx_hash((cx_hash_t*)sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress);
|
||||
os_memmove(out, hashAddress + 12, 20);
|
||||
}
|
||||
|
||||
static const uint8_t const HEXDIGITS[] = "0123456789abcdef";
|
||||
|
||||
#ifdef CHECKSUM_1
|
||||
|
||||
static const uint8_t const MASK[] = {0x80, 0x40, 0x20, 0x10,
|
||||
0x08, 0x04, 0x02, 0x01};
|
||||
|
||||
char convertDigit(uint8_t *address, uint8_t index, uint8_t *hash) {
|
||||
unsigned char digit = address[index / 2];
|
||||
if ((index % 2) == 0) {
|
||||
digit = (digit >> 4) & 0x0f;
|
||||
} else {
|
||||
digit = digit & 0x0f;
|
||||
}
|
||||
if (digit < 10) {
|
||||
return HEXDIGITS[digit];
|
||||
} else {
|
||||
unsigned char data = hash[index / 8];
|
||||
if (((data & MASK[index % 8]) != 0) && (digit > 9)) {
|
||||
return HEXDIGITS[digit] - 'a' + 'A';
|
||||
} else {
|
||||
return HEXDIGITS[digit];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
|
||||
app_cx_sha3_t *sha3Context) {
|
||||
uint8_t hashAddress[32];
|
||||
app_cx_sha3_init(sha3Context, 256);
|
||||
app_cx_hash((cx_hash_t*)sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress);
|
||||
getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context);
|
||||
}
|
||||
|
||||
void getEthAddressStringFromBinary(uint8_t *address, uint8_t *out,
|
||||
app_cx_sha3_t *sha3Context) {
|
||||
uint8_t hashChecksum[32];
|
||||
uint8_t i;
|
||||
app_cx_sha3_init(sha3Context, 256);
|
||||
app_cx_hash((cx_hash_t*)sha3Context, CX_LAST, address, 20, hashChecksum);
|
||||
for (i = 0; i < 40; i++) {
|
||||
out[i] = convertDigit(address, i, hashChecksum);
|
||||
}
|
||||
out[40] = '\0';
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
|
||||
app_cx_sha3_t *sha3Context) {
|
||||
uint8_t hashAddress[32];
|
||||
app_cx_sha3_init(sha3Context, 256);
|
||||
app_cx_hash((cx_hash_t*)sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress);
|
||||
getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context);
|
||||
}
|
||||
|
||||
void getEthAddressStringFromBinary(uint8_t *address, uint8_t *out,
|
||||
app_cx_sha3_t *sha3Context) {
|
||||
uint8_t hashChecksum[32];
|
||||
uint8_t tmp[40];
|
||||
uint8_t i;
|
||||
for (i = 0; i < 20; i++) {
|
||||
uint8_t digit = address[i];
|
||||
tmp[2 * i] = HEXDIGITS[(digit >> 4) & 0x0f];
|
||||
tmp[2 * i + 1] = HEXDIGITS[digit & 0x0f];
|
||||
}
|
||||
app_cx_sha3_init(sha3Context, 256);
|
||||
app_cx_hash((cx_hash_t*)sha3Context, CX_LAST, tmp, 40, hashChecksum);
|
||||
for (i = 0; i < 40; i++) {
|
||||
uint8_t hashDigit = hashChecksum[i / 2];
|
||||
if ((i % 2) == 0) {
|
||||
hashDigit = (hashDigit >> 4) & 0x0f;
|
||||
} else {
|
||||
hashDigit = hashDigit & 0x0f;
|
||||
}
|
||||
if ((hashDigit > 7) && (tmp[i] > '9')) {
|
||||
out[i] = tmp[i] - 'a' + 'A';
|
||||
} else {
|
||||
out[i] = tmp[i];
|
||||
}
|
||||
}
|
||||
out[40] = '\0';
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool adjustDecimals(char *src, uint32_t srcLength, char *target,
|
||||
uint32_t targetLength, uint8_t decimals) {
|
||||
uint32_t startOffset;
|
||||
uint32_t lastZeroOffset = 0;
|
||||
uint32_t offset = 0;
|
||||
if (srcLength == decimals) {
|
||||
if (targetLength < srcLength + 1) {
|
||||
return false;
|
||||
}
|
||||
for (uint32_t i = 0; i < srcLength; i++) {
|
||||
target[i] = src[i];
|
||||
}
|
||||
target[srcLength] = '\0';
|
||||
return true;
|
||||
}
|
||||
if (srcLength < decimals) {
|
||||
uint32_t delta = decimals - srcLength;
|
||||
if (targetLength < srcLength + 1 + 2 + delta) {
|
||||
return false;
|
||||
}
|
||||
target[offset++] = '0';
|
||||
target[offset++] = '.';
|
||||
for (uint32_t i = 0; i < delta; i++) {
|
||||
target[offset++] = '0';
|
||||
}
|
||||
startOffset = offset;
|
||||
for (uint32_t i = 0; i < srcLength; i++) {
|
||||
target[offset++] = src[i];
|
||||
}
|
||||
target[offset] = '\0';
|
||||
} else {
|
||||
uint32_t sourceOffset = 0;
|
||||
uint32_t delta = srcLength - decimals;
|
||||
if (targetLength < srcLength + 1 + 1) {
|
||||
return false;
|
||||
}
|
||||
while (offset < delta) {
|
||||
target[offset++] = src[sourceOffset++];
|
||||
}
|
||||
target[offset++] = '.';
|
||||
startOffset = offset;
|
||||
while (sourceOffset < srcLength) {
|
||||
target[offset++] = src[sourceOffset++];
|
||||
}
|
||||
}
|
||||
for (uint32_t i = startOffset; i < offset; i++) {
|
||||
if (target[i] == '0') {
|
||||
if (lastZeroOffset == 0) {
|
||||
lastZeroOffset = i;
|
||||
}
|
||||
} else {
|
||||
lastZeroOffset = 0;
|
||||
}
|
||||
}
|
||||
if (lastZeroOffset != 0) {
|
||||
target[lastZeroOffset] = '\0';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
49
src_common/ethUtils.h
Normal file
49
src_common/ethUtils.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*******************************************************************************
|
||||
* Ledger Blue
|
||||
* (c) 2016 Ledger
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
********************************************************************************/
|
||||
|
||||
#include "os.h"
|
||||
#include "cx.h"
|
||||
#include "app_cx_sha3.h"
|
||||
|
||||
/**
|
||||
* @brief Decode an RLP encoded field - see
|
||||
* https://github.com/ethereum/wiki/wiki/RLP
|
||||
* @param [in] buffer buffer containing the RLP encoded field to decode
|
||||
* @param [in] bufferLength size of the buffer
|
||||
* @param [out] fieldLength length of the RLP encoded field
|
||||
* @param [out] offset offset to the beginning of the RLP encoded field from the
|
||||
* buffer
|
||||
* @param [out] list true if the field encodes a list, false if it encodes a
|
||||
* string
|
||||
* @return true if the RLP header is consistent
|
||||
*/
|
||||
bool rlpDecodeLength(uint8_t *buffer, uint32_t bufferLength,
|
||||
uint32_t *fieldLength, uint32_t *offset, bool *list);
|
||||
|
||||
bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid);
|
||||
|
||||
void getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
|
||||
app_cx_sha3_t *sha3Context);
|
||||
|
||||
void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out,
|
||||
app_cx_sha3_t *sha3Context);
|
||||
|
||||
void getEthAddressStringFromBinary(uint8_t *address, uint8_t *out,
|
||||
app_cx_sha3_t *sha3Context);
|
||||
|
||||
bool adjustDecimals(char *src, uint32_t srcLength, char *target,
|
||||
uint32_t targetLength, uint8_t decimals);
|
||||
409
src_common/uint256.c
Normal file
409
src_common/uint256.c
Normal file
@@ -0,0 +1,409 @@
|
||||
/*******************************************************************************
|
||||
* Ledger Blue
|
||||
* (c) 2016 Ledger
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
********************************************************************************/
|
||||
|
||||
// Adapted from https://github.com/calccrypto/uint256_t
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "uint256.h"
|
||||
|
||||
static const char HEXDIGITS[] = "0123456789abcdef";
|
||||
|
||||
static uint64_t readUint64BE(uint8_t *buffer) {
|
||||
return (((uint64_t)buffer[0]) << 56) | (((uint64_t)buffer[1]) << 48) |
|
||||
(((uint64_t)buffer[2]) << 40) | (((uint64_t)buffer[3]) << 32) |
|
||||
(((uint64_t)buffer[4]) << 24) | (((uint64_t)buffer[5]) << 16) |
|
||||
(((uint64_t)buffer[6]) << 8) | (((uint64_t)buffer[7]));
|
||||
}
|
||||
|
||||
void readu128BE(uint8_t *buffer, uint128_t *target) {
|
||||
UPPER_P(target) = readUint64BE(buffer);
|
||||
LOWER_P(target) = readUint64BE(buffer + 8);
|
||||
}
|
||||
|
||||
void readu256BE(uint8_t *buffer, uint256_t *target) {
|
||||
readu128BE(buffer, &UPPER_P(target));
|
||||
readu128BE(buffer + 16, &LOWER_P(target));
|
||||
}
|
||||
|
||||
bool zero128(uint128_t *number) {
|
||||
return ((LOWER_P(number) == 0) && (UPPER_P(number) == 0));
|
||||
}
|
||||
|
||||
bool zero256(uint256_t *number) {
|
||||
return (zero128(&LOWER_P(number)) && zero128(&UPPER_P(number)));
|
||||
}
|
||||
|
||||
void copy128(uint128_t *target, uint128_t *number) {
|
||||
UPPER_P(target) = UPPER_P(number);
|
||||
LOWER_P(target) = LOWER_P(number);
|
||||
}
|
||||
|
||||
void copy256(uint256_t *target, uint256_t *number) {
|
||||
copy128(&UPPER_P(target), &UPPER_P(number));
|
||||
copy128(&LOWER_P(target), &LOWER_P(number));
|
||||
}
|
||||
|
||||
void clear128(uint128_t *target) {
|
||||
UPPER_P(target) = 0;
|
||||
LOWER_P(target) = 0;
|
||||
}
|
||||
|
||||
void clear256(uint256_t *target) {
|
||||
clear128(&UPPER_P(target));
|
||||
clear128(&LOWER_P(target));
|
||||
}
|
||||
|
||||
void shiftl128(uint128_t *number, uint32_t value, uint128_t *target) {
|
||||
if (value >= 128) {
|
||||
clear128(target);
|
||||
} else if (value == 64) {
|
||||
UPPER_P(target) = LOWER_P(number);
|
||||
LOWER_P(target) = 0;
|
||||
} else if (value == 0) {
|
||||
copy128(target, number);
|
||||
} else if (value < 64) {
|
||||
UPPER_P(target) =
|
||||
(UPPER_P(number) << value) + (LOWER_P(number) >> (64 - value));
|
||||
LOWER_P(target) = (LOWER_P(number) << value);
|
||||
} else if ((128 > value) && (value > 64)) {
|
||||
UPPER_P(target) = LOWER_P(number) << (value - 64);
|
||||
LOWER_P(target) = 0;
|
||||
} else {
|
||||
clear128(target);
|
||||
}
|
||||
}
|
||||
|
||||
void shiftl256(uint256_t *number, uint32_t value, uint256_t *target) {
|
||||
if (value >= 256) {
|
||||
clear256(target);
|
||||
} else if (value == 128) {
|
||||
copy128(&UPPER_P(target), &LOWER_P(number));
|
||||
clear128(&LOWER_P(target));
|
||||
} else if (value == 0) {
|
||||
copy256(target, number);
|
||||
} else if (value < 128) {
|
||||
uint128_t tmp1;
|
||||
uint128_t tmp2;
|
||||
uint256_t result;
|
||||
shiftl128(&UPPER_P(number), value, &tmp1);
|
||||
shiftr128(&LOWER_P(number), (128 - value), &tmp2);
|
||||
add128(&tmp1, &tmp2, &UPPER(result));
|
||||
shiftl128(&LOWER_P(number), value, &LOWER(result));
|
||||
copy256(target, &result);
|
||||
} else if ((256 > value) && (value > 128)) {
|
||||
shiftl128(&LOWER_P(number), (value - 128), &UPPER_P(target));
|
||||
clear128(&LOWER_P(target));
|
||||
} else {
|
||||
clear256(target);
|
||||
}
|
||||
}
|
||||
|
||||
void shiftr128(uint128_t *number, uint32_t value, uint128_t *target) {
|
||||
if (value >= 128) {
|
||||
clear128(target);
|
||||
} else if (value == 64) {
|
||||
UPPER_P(target) = 0;
|
||||
LOWER_P(target) = UPPER_P(number);
|
||||
} else if (value == 0) {
|
||||
copy128(target, number);
|
||||
} else if (value < 64) {
|
||||
uint128_t result;
|
||||
UPPER(result) = UPPER_P(number) >> value;
|
||||
LOWER(result) =
|
||||
(UPPER_P(number) << (64 - value)) + (LOWER_P(number) >> value);
|
||||
copy128(target, &result);
|
||||
} else if ((128 > value) && (value > 64)) {
|
||||
LOWER_P(target) = UPPER_P(number) >> (value - 64);
|
||||
UPPER_P(target) = 0;
|
||||
} else {
|
||||
clear128(target);
|
||||
}
|
||||
}
|
||||
|
||||
void shiftr256(uint256_t *number, uint32_t value, uint256_t *target) {
|
||||
if (value >= 256) {
|
||||
clear256(target);
|
||||
} else if (value == 128) {
|
||||
copy128(&LOWER_P(target), &UPPER_P(number));
|
||||
clear128(&UPPER_P(target));
|
||||
} else if (value == 0) {
|
||||
copy256(target, number);
|
||||
} else if (value < 128) {
|
||||
uint128_t tmp1;
|
||||
uint128_t tmp2;
|
||||
uint256_t result;
|
||||
shiftr128(&UPPER_P(number), value, &UPPER(result));
|
||||
shiftr128(&LOWER_P(number), value, &tmp1);
|
||||
shiftl128(&UPPER_P(number), (128 - value), &tmp2);
|
||||
add128(&tmp1, &tmp2, &LOWER(result));
|
||||
copy256(target, &result);
|
||||
} else if ((256 > value) && (value > 128)) {
|
||||
shiftr128(&UPPER_P(number), (value - 128), &LOWER_P(target));
|
||||
clear128(&UPPER_P(target));
|
||||
} else {
|
||||
clear256(target);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t bits128(uint128_t *number) {
|
||||
uint32_t result = 0;
|
||||
if (UPPER_P(number)) {
|
||||
result = 64;
|
||||
uint64_t up = UPPER_P(number);
|
||||
while (up) {
|
||||
up >>= 1;
|
||||
result++;
|
||||
}
|
||||
} else {
|
||||
uint64_t low = LOWER_P(number);
|
||||
while (low) {
|
||||
low >>= 1;
|
||||
result++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t bits256(uint256_t *number) {
|
||||
uint32_t result = 0;
|
||||
if (!zero128(&UPPER_P(number))) {
|
||||
result = 128;
|
||||
uint128_t up;
|
||||
copy128(&up, &UPPER_P(number));
|
||||
while (!zero128(&up)) {
|
||||
shiftr128(&up, 1, &up);
|
||||
result++;
|
||||
}
|
||||
} else {
|
||||
uint128_t low;
|
||||
copy128(&low, &LOWER_P(number));
|
||||
while (!zero128(&low)) {
|
||||
shiftr128(&low, 1, &low);
|
||||
result++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool equal128(uint128_t *number1, uint128_t *number2) {
|
||||
return (UPPER_P(number1) == UPPER_P(number2)) &&
|
||||
(LOWER_P(number1) == LOWER_P(number2));
|
||||
}
|
||||
|
||||
bool equal256(uint256_t *number1, uint256_t *number2) {
|
||||
return (equal128(&UPPER_P(number1), &UPPER_P(number2)) &&
|
||||
equal128(&LOWER_P(number1), &LOWER_P(number2)));
|
||||
}
|
||||
|
||||
bool gt128(uint128_t *number1, uint128_t *number2) {
|
||||
if (UPPER_P(number1) == UPPER_P(number2)) {
|
||||
return (LOWER_P(number1) > LOWER_P(number2));
|
||||
}
|
||||
return (UPPER_P(number1) > UPPER_P(number2));
|
||||
}
|
||||
|
||||
bool gt256(uint256_t *number1, uint256_t *number2) {
|
||||
if (equal128(&UPPER_P(number1), &UPPER_P(number2))) {
|
||||
return gt128(&LOWER_P(number1), &LOWER_P(number2));
|
||||
}
|
||||
return gt128(&UPPER_P(number1), &UPPER_P(number2));
|
||||
}
|
||||
|
||||
bool gte128(uint128_t *number1, uint128_t *number2) {
|
||||
return gt128(number1, number2) || equal128(number1, number2);
|
||||
}
|
||||
|
||||
bool gte256(uint256_t *number1, uint256_t *number2) {
|
||||
return gt256(number1, number2) || equal256(number1, number2);
|
||||
}
|
||||
|
||||
void add128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
|
||||
UPPER_P(target) =
|
||||
UPPER_P(number1) + UPPER_P(number2) +
|
||||
((LOWER_P(number1) + LOWER_P(number2)) < LOWER_P(number1));
|
||||
LOWER_P(target) = LOWER_P(number1) + LOWER_P(number2);
|
||||
}
|
||||
|
||||
void add256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
|
||||
uint128_t tmp;
|
||||
add128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target));
|
||||
add128(&LOWER_P(number1), &LOWER_P(number2), &tmp);
|
||||
if (gt128(&LOWER_P(number1), &tmp)) {
|
||||
uint128_t one;
|
||||
UPPER(one) = 0;
|
||||
LOWER(one) = 1;
|
||||
add128(&UPPER_P(target), &one, &UPPER_P(target));
|
||||
}
|
||||
add128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target));
|
||||
}
|
||||
|
||||
void minus128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
|
||||
UPPER_P(target) =
|
||||
UPPER_P(number1) - UPPER_P(number2) -
|
||||
((LOWER_P(number1) - LOWER_P(number2)) > LOWER_P(number1));
|
||||
LOWER_P(target) = LOWER_P(number1) - LOWER_P(number2);
|
||||
}
|
||||
|
||||
void minus256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
|
||||
uint128_t tmp;
|
||||
minus128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target));
|
||||
minus128(&LOWER_P(number1), &LOWER_P(number2), &tmp);
|
||||
if (gt128(&tmp, &LOWER_P(number1))) {
|
||||
uint128_t one;
|
||||
UPPER(one) = 0;
|
||||
LOWER(one) = 1;
|
||||
minus128(&UPPER_P(target), &one, &UPPER_P(target));
|
||||
}
|
||||
minus128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target));
|
||||
}
|
||||
|
||||
void or128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
|
||||
UPPER_P(target) = UPPER_P(number1) | UPPER_P(number2);
|
||||
LOWER_P(target) = LOWER_P(number1) | LOWER_P(number2);
|
||||
}
|
||||
|
||||
void or256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
|
||||
or128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target));
|
||||
or128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target));
|
||||
}
|
||||
|
||||
void divmod128(uint128_t *l, uint128_t *r, uint128_t *retDiv,
|
||||
uint128_t *retMod) {
|
||||
uint128_t copyd, adder, resDiv, resMod;
|
||||
uint128_t one;
|
||||
UPPER(one) = 0;
|
||||
LOWER(one) = 1;
|
||||
uint32_t diffBits = bits128(l) - bits128(r);
|
||||
clear128(&resDiv);
|
||||
copy128(&resMod, l);
|
||||
if (gt128(r, l)) {
|
||||
copy128(retMod, l);
|
||||
clear128(retDiv);
|
||||
} else {
|
||||
shiftl128(r, diffBits, ©d);
|
||||
shiftl128(&one, diffBits, &adder);
|
||||
if (gt128(©d, &resMod)) {
|
||||
shiftr128(©d, 1, ©d);
|
||||
shiftr128(&adder, 1, &adder);
|
||||
}
|
||||
while (gte128(&resMod, r)) {
|
||||
if (gte128(&resMod, ©d)) {
|
||||
minus128(&resMod, ©d, &resMod);
|
||||
or128(&resDiv, &adder, &resDiv);
|
||||
}
|
||||
shiftr128(©d, 1, ©d);
|
||||
shiftr128(&adder, 1, &adder);
|
||||
}
|
||||
copy128(retDiv, &resDiv);
|
||||
copy128(retMod, &resMod);
|
||||
}
|
||||
}
|
||||
|
||||
void divmod256(uint256_t *l, uint256_t *r, uint256_t *retDiv,
|
||||
uint256_t *retMod) {
|
||||
uint256_t copyd, adder, resDiv, resMod;
|
||||
uint256_t one;
|
||||
clear256(&one);
|
||||
UPPER(LOWER(one)) = 0;
|
||||
LOWER(LOWER(one)) = 1;
|
||||
uint32_t diffBits = bits256(l) - bits256(r);
|
||||
clear256(&resDiv);
|
||||
copy256(&resMod, l);
|
||||
if (gt256(r, l)) {
|
||||
copy256(retMod, l);
|
||||
clear256(retDiv);
|
||||
} else {
|
||||
shiftl256(r, diffBits, ©d);
|
||||
shiftl256(&one, diffBits, &adder);
|
||||
if (gt256(©d, &resMod)) {
|
||||
shiftr256(©d, 1, ©d);
|
||||
shiftr256(&adder, 1, &adder);
|
||||
}
|
||||
while (gte256(&resMod, r)) {
|
||||
if (gte256(&resMod, ©d)) {
|
||||
minus256(&resMod, ©d, &resMod);
|
||||
or256(&resDiv, &adder, &resDiv);
|
||||
}
|
||||
shiftr256(©d, 1, ©d);
|
||||
shiftr256(&adder, 1, &adder);
|
||||
}
|
||||
copy256(retDiv, &resDiv);
|
||||
copy256(retMod, &resMod);
|
||||
}
|
||||
}
|
||||
|
||||
static void reverseString(char *str, uint32_t length) {
|
||||
uint32_t i, j;
|
||||
for (i = 0, j = length - 1; i < j; i++, j--) {
|
||||
uint8_t c;
|
||||
c = str[i];
|
||||
str[i] = str[j];
|
||||
str[j] = c;
|
||||
}
|
||||
}
|
||||
|
||||
bool tostring128(uint128_t *number, uint32_t baseParam, char *out,
|
||||
uint32_t outLength) {
|
||||
uint128_t rDiv;
|
||||
uint128_t rMod;
|
||||
uint128_t base;
|
||||
copy128(&rDiv, number);
|
||||
clear128(&rMod);
|
||||
clear128(&base);
|
||||
LOWER(base) = baseParam;
|
||||
uint32_t offset = 0;
|
||||
if ((baseParam < 2) || (baseParam > 16)) {
|
||||
return false;
|
||||
}
|
||||
do {
|
||||
if (offset > (outLength - 1)) {
|
||||
return false;
|
||||
}
|
||||
divmod128(&rDiv, &base, &rDiv, &rMod);
|
||||
out[offset++] = HEXDIGITS[(uint8_t)LOWER(rMod)];
|
||||
} while (!zero128(&rDiv));
|
||||
out[offset] = '\0';
|
||||
reverseString(out, offset);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tostring256(uint256_t *number, uint32_t baseParam, char *out,
|
||||
uint32_t outLength) {
|
||||
uint256_t rDiv;
|
||||
uint256_t rMod;
|
||||
uint256_t base;
|
||||
copy256(&rDiv, number);
|
||||
clear256(&rMod);
|
||||
clear256(&base);
|
||||
UPPER(LOWER(base)) = 0;
|
||||
LOWER(LOWER(base)) = baseParam;
|
||||
uint32_t offset = 0;
|
||||
if ((baseParam < 2) || (baseParam > 16)) {
|
||||
return false;
|
||||
}
|
||||
do {
|
||||
if (offset > (outLength - 1)) {
|
||||
return false;
|
||||
}
|
||||
divmod256(&rDiv, &base, &rDiv, &rMod);
|
||||
out[offset++] = HEXDIGITS[(uint8_t)LOWER(LOWER(rMod))];
|
||||
} while (!zero256(&rDiv));
|
||||
out[offset] = '\0';
|
||||
reverseString(out, offset);
|
||||
return true;
|
||||
}
|
||||
63
src_common/uint256.h
Normal file
63
src_common/uint256.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*******************************************************************************
|
||||
* Ledger Blue
|
||||
* (c) 2016 Ledger
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
********************************************************************************/
|
||||
|
||||
// Adapted from https://github.com/calccrypto/uint256_t
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct uint128_t { uint64_t elements[2]; } uint128_t;
|
||||
|
||||
typedef struct uint256_t { uint128_t elements[2]; } uint256_t;
|
||||
|
||||
#define UPPER_P(x) x->elements[0]
|
||||
#define LOWER_P(x) x->elements[1]
|
||||
#define UPPER(x) x.elements[0]
|
||||
#define LOWER(x) x.elements[1]
|
||||
|
||||
void readu128BE(uint8_t *buffer, uint128_t *target);
|
||||
void readu256BE(uint8_t *buffer, uint256_t *target);
|
||||
bool zero128(uint128_t *number);
|
||||
bool zero256(uint256_t *number);
|
||||
void copy128(uint128_t *target, uint128_t *number);
|
||||
void copy256(uint256_t *target, uint256_t *number);
|
||||
void clear128(uint128_t *target);
|
||||
void clear256(uint256_t *target);
|
||||
void shiftl128(uint128_t *number, uint32_t value, uint128_t *target);
|
||||
void shiftr128(uint128_t *number, uint32_t value, uint128_t *target);
|
||||
void shiftl256(uint256_t *number, uint32_t value, uint256_t *target);
|
||||
void shiftr256(uint256_t *number, uint32_t value, uint256_t *target);
|
||||
uint32_t bits128(uint128_t *number);
|
||||
uint32_t bits256(uint256_t *number);
|
||||
bool equal128(uint128_t *number1, uint128_t *number2);
|
||||
bool equal256(uint256_t *number1, uint256_t *number2);
|
||||
bool gt128(uint128_t *number1, uint128_t *number2);
|
||||
bool gt256(uint256_t *number1, uint256_t *number2);
|
||||
bool gte128(uint128_t *number1, uint128_t *number2);
|
||||
bool gte256(uint256_t *number1, uint256_t *number2);
|
||||
void add128(uint128_t *number1, uint128_t *number2, uint128_t *target);
|
||||
void add256(uint256_t *number1, uint256_t *number2, uint256_t *target);
|
||||
void minus128(uint128_t *number1, uint128_t *number2, uint128_t *target);
|
||||
void minus256(uint256_t *number1, uint256_t *number2, uint256_t *target);
|
||||
void or128(uint128_t *number1, uint128_t *number2, uint128_t *target);
|
||||
void or256(uint256_t *number1, uint256_t *number2, uint256_t *target);
|
||||
void divmod128(uint128_t *l, uint128_t *r, uint128_t *div, uint128_t *mod);
|
||||
void divmod256(uint256_t *l, uint256_t *r, uint256_t *div, uint256_t *mod);
|
||||
bool tostring128(uint128_t *number, uint32_t base, char *out,
|
||||
uint32_t outLength);
|
||||
bool tostring256(uint256_t *number, uint32_t base, char *out,
|
||||
uint32_t outLength);
|
||||
Reference in New Issue
Block a user