Skip to content

Commit

Permalink
fixed OOM
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaib79 committed Oct 12, 2020
1 parent 5bf09d8 commit f75b2c2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
3 changes: 0 additions & 3 deletions src/signMsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,11 @@ bool sign_message(void) {
}

void handleSignMsg(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, volatile unsigned int *flags, volatile unsigned int *tx) {
uint8_t hashMessage[32];
if (p1 == P1_FIRST) {
char tmp[11];
uint32_t index;
uint32_t base = 10;
uint8_t pos = 0;
uint32_t i;
cx_sha3_t sha;
// first 4 bytes from dataBuffer should be the message length (big endian uint32)
if (dataLength < 4)
THROW(ERR_INVALID_MESSAGE);
Expand Down
29 changes: 4 additions & 25 deletions src/signTx.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "signTx.h"
#include "os.h"
#include "ux.h"
#include "utils.h"
#include "../deps/jsmn/jsmn.h"
#include <uint256.h>
#include "base64.h"
Expand All @@ -27,25 +24,6 @@
VERSION_FIELD \
)

// additional space for "0." and " eGLD"
#define PRETTY_SIZE (2 + MAX_TICKER_LEN)

typedef struct {
char buffer[MAX_BUFFER_LEN]; // buffer to hold large transactions that are composed from multiple APDUs
uint16_t bufLen;

char receiver[FULL_ADDRESS_LENGTH];
char amount[MAX_AMOUNT_LEN + PRETTY_SIZE];
uint64_t gas_limit;
uint64_t gas_price;
char fee[MAX_AMOUNT_LEN + PRETTY_SIZE];
char data[MAX_DISPLAY_DATA_SIZE + DATA_SIZE_LEN];
uint16_t data_size;
char signature[64];
} tx_context_t;

static tx_context_t tx_context;

static uint8_t setResultSignature();
void makeFeePretty(network_t network);
static int jsoneq(const char *json, jsmntok_t *tok, const char *s);
Expand Down Expand Up @@ -132,15 +110,15 @@ static bool gas_to_fee(uint64_t gas_limit, uint64_t gas_price, char *fee, size_t
return true;
}

static bool isdigit(char c) {
static bool is_digit(char c) {
return c >= '0' && c <= '9';
}

static bool parse_int(char *str, size_t size, uint64_t *result) {
uint64_t min = 0, n = 0;

for (size_t i = 0; i < size; i++) {
if (!isdigit(str[i])) {
if (!is_digit(str[i])) {
return false;
}
n = n * 10 + str[i] - '0';
Expand All @@ -158,7 +136,7 @@ static bool parse_int(char *str, size_t size, uint64_t *result) {

static bool valid_amount(char *amount, size_t size) {
for (size_t i = 0; i < size; i++) {
if (!isdigit(amount[i])) {
if (!is_digit(amount[i])) {
return false;
}
}
Expand Down Expand Up @@ -236,6 +214,7 @@ static void computeDataSize(char *base64, int b64len) {
if (base64[b64len - 2] == '=')
tx_context.data_size--;
}
tx_context.data[tx_context.data_size] = '\0';
int len = sizeof(tx_context.data);
// prepare the first display page, which contains the data field size
char str_size[DATA_SIZE_LEN] = "[ Size: 000 ] ";
Expand Down
25 changes: 21 additions & 4 deletions src/signTx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,34 @@
#ifndef _SIGN_TX_H_
#define _SIGN_TX_H_

void handleSignTx(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, volatile unsigned int *flags, volatile unsigned int *tx);

#define MAX_AMOUNT_LEN 32
#define MAX_BUFFER_LEN 1024
#define MAX_DATA_SIZE 800 // 800 in base64 = 600 in ASCII
#define MAX_BUFFER_LEN 700
#define MAX_DATA_SIZE 600 // 600 in base64 = 450 in ASCII
#define MAX_DISPLAY_DATA_SIZE 64 // must be multiple of 4
#define DATA_SIZE_LEN 17
#define MAX_CHAINID_LEN 32
#define MAX_TICKER_LEN 5
#define MAX_UINT32_LEN 10 // len(f"{0xffffffff:d}")
#define MAX_UINT64_LEN 20 // len(f"{0xffffffffffffffff:d}")
#define MAX_UINT128_LEN 39 // len(f"{0xffffffffffffffffffffffffffffffff:d}")
#define PRETTY_SIZE (2 + MAX_TICKER_LEN) // additional space for "0." and " eGLD"

typedef struct {
uint8_t buffer[MAX_BUFFER_LEN]; // buffer to hold large transactions that are composed from multiple APDUs
uint16_t bufLen;

char receiver[FULL_ADDRESS_LENGTH];
char amount[MAX_AMOUNT_LEN + PRETTY_SIZE];
uint64_t gas_limit;
uint64_t gas_price;
char fee[MAX_AMOUNT_LEN + PRETTY_SIZE];
char data[MAX_DISPLAY_DATA_SIZE + DATA_SIZE_LEN];
uint16_t data_size;
uint8_t signature[64];
} tx_context_t;

static tx_context_t tx_context;

void handleSignTx(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, volatile unsigned int *flags, volatile unsigned int *tx);

#endif
6 changes: 3 additions & 3 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ bool getPublicKey(uint32_t accountNumber, uint32_t index, uint8_t *publicKeyArra
return true;
}

bool getPrivateKey(uint32_t accountNumber, uint32_t index, cx_ecfp_private_key_t *privateKey) {
bool getPrivateKey(uint32_t account, uint32_t addressIndex, cx_ecfp_private_key_t *privateKey) {
uint8_t privateKeyData[32];
uint32_t bip32Path[BIP32_PATH];
bool success = true;

os_memmove(bip32Path, derivePath, sizeof(derivePath));

bip32Path[2] = accountNumber | HARDENED_OFFSET;
bip32Path[4] = index | HARDENED_OFFSET;
bip32Path[2] = account | HARDENED_OFFSET;
bip32Path[4] = addressIndex | HARDENED_OFFSET;

BEGIN_TRY {
TRY {
Expand Down

0 comments on commit f75b2c2

Please sign in to comment.