Skip to content

Commit

Permalink
fix memory problems by using malloc with aligned memory allocation
Browse files Browse the repository at this point in the history
refactor: compare_bytes_with_hex
  • Loading branch information
gorazdko committed Aug 9, 2020
1 parent 0e43d4d commit 2c99de7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
5 changes: 3 additions & 2 deletions seedtool/keystore.ino
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ bool Keystore::check_derivation_path(const char *path) {
}
if (derivation_tmp)
free(derivation_tmp);
derivation_tmp = (uint32_t *)calloc(derivationLen_tmp, sizeof(uint32_t));

derivation_tmp = (uint32_t *)calloc_aligned(derivationLen_tmp, sizeof(uint32_t));
size_t current = 0;
for(size_t i=0; i<len; i++){
if(cur[i] == '/'){ // next
Expand Down Expand Up @@ -114,7 +115,7 @@ bool Keystore::save_derivation_path(const char *path) {
derivationLen = derivationLen_tmp;
if (derivation)
free(derivation);
derivation = (uint32_t *)calloc(derivationLen, sizeof(uint32_t));
derivation = (uint32_t *)calloc_aligned(derivationLen, sizeof(uint32_t));
memcpy(derivation, derivation_tmp, derivationLen*sizeof(uint32_t));

standard_derivation_path = false;
Expand Down
1 change: 1 addition & 0 deletions seedtool/selftest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ bool test_bip32(void) {

keystore.update_root_key(seed, sizeof(seed), MAINNET);

// STUB derivation path
bool retval = keystore.save_derivation_path(derivation_path.c_str());
if (retval == false) {
Serial.println(F("save derivation path failed"));
Expand Down
9 changes: 6 additions & 3 deletions seedtool/ur.ino
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ bool ur_encode_crypto_seed(uint8_t *seed, size_t seed_len, String &seed_ur, uint
seed_ur.toUpperCase();

// @FIXME: free also on premature exit
//free(cbor_seed);
free(cbor_seed);

return true;
}

bool test_ur(void) {

int ret;
{ // scope here, otherwise problems with RAM
{ // scope for every test case
// source: https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2020-012-bytewords.md#exampletest-vector
uint8_t seeds[] = {0xd9, 0x01, 0x2c, 0xa2, 0x01, 0x50, 0xc7, 0x09, 0x85, 0x80, 0x12, 0x5e, 0x2a, 0xb0, 0x98, 0x12,
0x53, 0x46, 0x8b, 0x2d, 0xbc, 0x52, 0x02, 0xd8, 0x64, 0x19, 0x47, 0xda};
Expand All @@ -166,6 +166,8 @@ bool test_ur(void) {
Serial.println("bytewords failed");
return false;
}

free(seed_bytewords);
}
{
// https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2020-006-urtypes.md#exampletest-vector-1
Expand Down Expand Up @@ -235,7 +237,8 @@ bool test_ur(void) {
free(cbor_xpub);

// CLEAN STUB
keystore.save_derivation_path(keystore.default_derivation);
stdDerivation stdDer = SINGLE_NATIVE_SEGWIT;
keystore.save_standard_derivation_path(&stdDer, network.get_network());
}
return true;
}
13 changes: 13 additions & 0 deletions seedtool/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ void serial_printf(const char *format, ...);
void print_hex(uint8_t *data, size_t len);
bool compare_bytes_with_hex(uint8_t *data, size_t len, const char * hex);

/**
* @brief allocates memory chunk word aligned.
* Allocating memory with malloc may lead to problems, e.g.
* mallocing 3 bytes in the keystore constructor will cause selftest
* to fail.
*/
void * malloc_aligned(size_t _size);

/**
* @brief allocates memory chunk word aligned and 0 initialized
*/
void * calloc_aligned(size_t nitmes, size_t _size);

struct Point {
int x;
int y;
Expand Down
32 changes: 22 additions & 10 deletions seedtool/util.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,32 @@ void print_hex(uint8_t *data, size_t len) {

bool compare_bytes_with_hex(uint8_t *data, size_t len, const char * hex) {
bool ret;
char *str = (char *)malloc(4*len);
char str[3] = {0};

for (int i=0; i < len; i++) {
sprintf(str + i*2, "%02x", data[i]);
sprintf(str, "%02x", data[i]);
if (memcmp(str, hex + 2*i, 2) != 0) {
return false;
}
}

str[2*len] = 0;

if (strcmp(str, hex) == 0)
ret = true;
else
ret = false;
return true;
}

free(str);
void * malloc_aligned(size_t _size) {
size_t allocate_size = _size;
while (allocate_size % 8 != 0) {
allocate_size++;
}
return malloc(allocate_size);
}

return ret;
void * calloc_aligned(size_t nitmes, size_t _size) {
size_t allocate_size = nitmes*_size;
while (allocate_size % 8 != 0) {
allocate_size++;
}
void * ret = malloc(allocate_size);
memset(ret, 0, allocate_size);
return ret;
}

0 comments on commit 2c99de7

Please sign in to comment.