Skip to content

Commit 6a7eb7a

Browse files
authored
Merge pull request #52 from Zondax/dev
preliminary release for v0.3.0
2 parents 3885519 + 42c0136 commit 6a7eb7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+3593
-2820
lines changed

CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ if (ENABLE_FUZZING)
4343
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,bugprone-*,cert-*,clang-analyzer-*,-cert-err58-cpp,misc-*,-bugprone-suspicious-include)
4444

4545
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
46-
# require at least clang 3.2
47-
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0)
48-
message(FATAL_ERROR "Clang version must be at least 10.0!")
46+
# require at least clang 12
47+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0)
48+
message(FATAL_ERROR "Clang version must be at least 11.0!")
4949
endif ()
5050
else ()
5151
message(FATAL_ERROR
52-
"You are using an unsupported compiler! Fuzzing only works with Clang 10.\n"
53-
"1. Install clang-10 \n"
54-
"2. Pass -DCMAKE_C_COMPILER=clang-10 -DCMAKE_CXX_COMPILER=clang++-10")
52+
"You are using an unsupported compiler! Fuzzing only works with Clang 11.\n"
53+
"1. Install clang-12 \n"
54+
"2. Pass -DCMAKE_C_COMPILER=clang-11 -DCMAKE_CXX_COMPILER=clang++-11")
5555
endif ()
5656

5757
string(APPEND CMAKE_C_FLAGS " -fsanitize=fuzzer-no-link")

app/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ COIN=DFN
5151
endif
5252

5353
APPVERSION_M=0
54-
APPVERSION_N=2
54+
APPVERSION_N=3
5555
APPVERSION_P=0
5656

5757
$(info COIN = [$(COIN)])

app/src/addr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ zxerr_t addr_getItem(int8_t displayIdx,
4141
switch (displayIdx) {
4242
case 0:
4343
snprintf(outKey, outKeyLen, "Principal");
44-
CHECK_ZXERR(addr_to_textual(buffer, sizeof(buffer), G_io_apdu_buffer + VIEW_PRINCIPAL_OFFSET_TEXT, action_addrResponseLen - VIEW_PRINCIPAL_OFFSET_TEXT));
44+
CHECK_ZXERR(addr_to_textual(buffer, sizeof(buffer), (const char *) G_io_apdu_buffer + VIEW_PRINCIPAL_OFFSET_TEXT, action_addrResponseLen - VIEW_PRINCIPAL_OFFSET_TEXT));
4545
pageString(outVal, outValLen, buffer, pageIdx, pageCount);
4646
return zxerr_ok;
4747

app/src/base32.c

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,75 +21,28 @@
2121

2222
#include <string.h>
2323

24-
int base32_decode(const uint8_t *encoded, unsigned int encodedSize, uint8_t *result, unsigned int bufSize) {
25-
if (encoded == NULL) {
26-
return 0;
27-
}
28-
29-
unsigned int buffer = 0;
30-
int bitsLeft = 0;
31-
unsigned int count = 0;
32-
for (const uint8_t *ptr = encoded; count < bufSize && *ptr && (ptr - encoded) < encodedSize; ++ptr) {
33-
uint8_t ch = *ptr;
34-
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-') {
35-
continue;
36-
}
37-
buffer <<= 5;
38-
39-
// Deal with commonly mistyped characters
40-
if (ch == '0') {
41-
ch = 'O';
42-
} else if (ch == '1') {
43-
ch = 'L';
44-
} else if (ch == '8') {
45-
ch = 'B';
46-
}
47-
48-
// Look up one base32 digit
49-
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
50-
ch = (ch & 0x1F) - 1;
51-
} else if (ch >= '2' && ch <= '7') {
52-
ch -= '2' - 26;
53-
} else {
54-
return -1;
55-
}
56-
57-
buffer |= ch;
58-
bitsLeft += 5;
59-
if (bitsLeft >= 8) {
60-
result[count++] = buffer >> (bitsLeft - 8);
61-
bitsLeft -= 8;
62-
}
63-
}
64-
if (count < bufSize) {
65-
result[count] = '\000';
66-
}
67-
68-
return count;
69-
}
70-
71-
int base32_encode(const uint8_t *data, unsigned int length, uint8_t *result, unsigned int bufSize) {
24+
uint32_t base32_encode(const uint8_t *data, unsigned int length, char *result, uint32_t bufSize) {
7225
if (length < 0 || length > (1 << 28)) {
7326
return -1;
7427
}
75-
unsigned int count = 0;
28+
uint32_t count = 0;
7629
if (length > 0) {
77-
unsigned int buffer = data[0];
78-
unsigned int next = 1;
79-
int bitsLeft = 8;
30+
uint32_t buffer = data[0];
31+
uint32_t next = 1;
32+
uint32_t bitsLeft = 8;
8033
while (count < bufSize && (bitsLeft > 0 || next < length)) {
8134
if (bitsLeft < 5) {
8235
if (next < length) {
8336
buffer <<= 8;
8437
buffer |= data[next++] & 0xFF;
8538
bitsLeft += 8;
8639
} else {
87-
int pad = 5 - bitsLeft;
40+
uint32_t pad = 5u - bitsLeft;
8841
buffer <<= pad;
8942
bitsLeft += pad;
9043
}
9144
}
92-
int index = 0x1F & (buffer >> (bitsLeft - 5));
45+
uint32_t index = 0x1Fu & (buffer >> (bitsLeft - 5u));
9346
bitsLeft -= 5;
9447
result[count++] = "abcdefghijklmnopqrstuvwxyz234567"[index];
9548
}

app/src/base32.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@
4949
extern "C" {
5050
#endif
5151

52-
int base32_decode(const uint8_t *encoded, unsigned int encodedSize, uint8_t *result, unsigned int bufSize)
53-
__attribute__((visibility("hidden")));
54-
55-
int base32_encode(const uint8_t *data, unsigned int length, uint8_t *result,
56-
unsigned int bufSize) __attribute__((visibility("hidden")));
52+
uint32_t base32_encode(const uint8_t *data, unsigned int length,
53+
char *result, uint32_t bufSize) __attribute__((visibility("hidden")));
5754

5855
#ifdef __cplusplus
5956
}

app/src/coin.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ typedef enum {
6060
#define APPVERSION_LINE1 "Version"
6161
#define APPVERSION_LINE2 "v" APPVERSION
6262

63-
typedef enum {
64-
token_transfer = 0x00,
65-
state_transaction_read = 0x01,
66-
} txtype_e;
67-
6863
#ifdef __cplusplus
6964
}
7065
#endif

app/src/crypto.c

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ uint8_t const DER_PREFIX[] = {0x30, 0x56, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x
3131
0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a, 0x03, 0x42, 0x00};
3232

3333
#define DER_PREFIX_SIZE 23u
34-
#define DER_INPUT_SIZE DER_PREFIX_SIZE + SECP256K1_PK_LEN
34+
#define DER_INPUT_SIZE (DER_PREFIX_SIZE + SECP256K1_PK_LEN)
3535

3636
#define SIGN_PREFIX_SIZE 11u
37-
#define SIGN_PREHASH_SIZE SIGN_PREFIX_SIZE + CX_SHA256_SIZE
37+
#define SIGN_PREHASH_SIZE (SIGN_PREFIX_SIZE + CX_SHA256_SIZE)
3838

3939
#define SUBACCOUNT_PREFIX_SIZE 11u
4040

@@ -74,7 +74,9 @@ zxerr_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t
7474
}
7575

7676
//CRC-32(b) || b with b = SHA-224(“\x0Aaccount-id“ || owner || sub-account), where owner is a (29-byte)
77-
zxerr_t crypto_principalToSubaccount(uint8_t *principal, uint16_t principalLen, uint8_t *subAccount, uint16_t subaccountLen, uint8_t *address, uint16_t maxoutLen){
77+
zxerr_t crypto_principalToSubaccount(const uint8_t *principal, uint16_t principalLen,
78+
uint8_t *subAccount, uint16_t subaccountLen,
79+
uint8_t *address, uint16_t maxoutLen) {
7880
if (principalLen != DFINITY_PRINCIPAL_LEN || subaccountLen != DFINITY_SUBACCOUNT_LEN || maxoutLen < DFINITY_ADDR_LEN){
7981
return zxerr_invalid_crypto_settings;
8082
}
@@ -105,7 +107,7 @@ zxerr_t crypto_principalToSubaccount(uint8_t *principal, uint16_t principalLen,
105107
// 00 // no padding
106108
// 047060f720298ffa0f48d9606abdb0 ... // point on curve, uncompressed
107109

108-
zxerr_t crypto_computePrincipal(uint8_t *pubKey, uint8_t *address) {
110+
zxerr_t crypto_computePrincipal(const uint8_t *pubKey, uint8_t *principal) {
109111
uint8_t DER[DER_INPUT_SIZE];
110112
MEMZERO(DER, sizeof(DER));
111113
MEMCPY(DER, DER_PREFIX, DER_PREFIX_SIZE);
@@ -117,7 +119,7 @@ zxerr_t crypto_computePrincipal(uint8_t *pubKey, uint8_t *address) {
117119
cx_hash(&ctx.header, CX_LAST, DER, DER_INPUT_SIZE, buf, 224);
118120

119121
buf[DFINITY_PRINCIPAL_LEN-1] = 0x02;
120-
MEMCPY(address, buf, DFINITY_PRINCIPAL_LEN);
122+
MEMCPY(principal, buf, DFINITY_PRINCIPAL_LEN);
121123
return zxerr_ok;
122124
}
123125

@@ -192,7 +194,10 @@ zxerr_t crypto_getDigest(uint8_t *digest, txtype_e txtype){
192194
HASH_U64("ingress_expiry",fields->ingress_expiry, tmpdigest);
193195
HASH_BYTES_INTERMEDIATE("method_name", fields->method_name, tmpdigest);
194196
HASH_BYTES_INTERMEDIATE("request_type", parser_tx_obj.request_type, tmpdigest);
195-
HASH_BYTES_INTERMEDIATE("nonce", fields->nonce, tmpdigest);
197+
198+
if(fields->has_nonce){
199+
HASH_BYTES_INTERMEDIATE("nonce", fields->nonce, tmpdigest);
200+
}
196201
HASH_BYTES_END("arg", fields->arg, tmpdigest, digest);
197202
return zxerr_ok;
198203
}
@@ -293,13 +298,14 @@ zxerr_t crypto_sign(uint8_t *signatureBuffer,
293298
#include <hexutils.h>
294299
#include "picohash.h"
295300

296-
char *crypto_testPubKey;
297-
298301
zxerr_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t *pubKey, uint16_t pubKeyLen) {
302+
const char *tmp = "0410d34980a51af89d3331ad5fa80fe30d8868ad87526460b3b3e15596ee58e812422987d8589ba61098264df5bb9c2d3ff6fe061746b4b31a44ec26636632b835";
303+
parseHexString(pubKey, pubKeyLen, tmp);
304+
299305
return zxerr_ok;
300306
}
301307

302-
zxerr_t crypto_computePrincipal(uint8_t *pubKey, uint8_t *address) {
308+
zxerr_t crypto_computePrincipal(const uint8_t *pubKey, uint8_t *principal) {
303309
uint8_t DER[DER_INPUT_SIZE];
304310
MEMZERO(DER, sizeof(DER));
305311
MEMCPY(DER, DER_PREFIX, DER_PREFIX_SIZE);
@@ -314,7 +320,7 @@ zxerr_t crypto_computePrincipal(uint8_t *pubKey, uint8_t *address) {
314320
picohash_final(&ctx, buf);
315321

316322
buf[DFINITY_PRINCIPAL_LEN - 1] = 0x02;
317-
MEMCPY(address, buf, DFINITY_PRINCIPAL_LEN);
323+
MEMCPY(principal, buf, DFINITY_PRINCIPAL_LEN);
318324
return zxerr_ok;
319325
}
320326

@@ -326,18 +332,23 @@ zxerr_t crypto_sign(uint8_t *signature,
326332
return zxerr_ok;
327333
}
328334

329-
zxerr_t crypto_principalToSubaccount(uint8_t *principal, uint16_t principalLen, uint8_t *subAccount, uint16_t subaccountLen, uint8_t *address, uint16_t maxoutLen){
330-
if (principalLen != DFINITY_PRINCIPAL_LEN || subaccountLen != DFINITY_SUBACCOUNT_LEN || maxoutLen < DFINITY_ADDR_LEN){
335+
zxerr_t crypto_principalToSubaccount(const uint8_t *principal, uint16_t principalLen,
336+
uint8_t *subAccount, uint16_t subaccountLen,
337+
uint8_t *address, uint16_t maxoutLen) {
338+
339+
if (principalLen != DFINITY_PRINCIPAL_LEN || subaccountLen != DFINITY_SUBACCOUNT_LEN ||
340+
maxoutLen < DFINITY_ADDR_LEN) {
331341
return zxerr_invalid_crypto_settings;
332342
}
343+
333344
uint8_t hashinput[SUBACCOUNT_PREFIX_SIZE + DFINITY_PRINCIPAL_LEN + DFINITY_SUBACCOUNT_LEN];
334345
MEMZERO(hashinput, sizeof(hashinput));
335346
hashinput[0] = 0x0a;
336-
MEMCPY(&hashinput[1], (uint8_t *)"account-id",SUBACCOUNT_PREFIX_SIZE - 1);
347+
MEMCPY(&hashinput[1], (uint8_t *) "account-id", SUBACCOUNT_PREFIX_SIZE - 1);
337348
MEMCPY(hashinput + SUBACCOUNT_PREFIX_SIZE, principal, DFINITY_PRINCIPAL_LEN);
338349
MEMCPY(hashinput + SUBACCOUNT_PREFIX_SIZE + DFINITY_PRINCIPAL_LEN, subAccount, DFINITY_SUBACCOUNT_LEN);
339-
uint8_t buf[32];
340350

351+
uint8_t buf[32];
341352
picohash_ctx_t ctx;
342353

343354
picohash_init_sha224(&ctx);
@@ -347,11 +358,11 @@ zxerr_t crypto_principalToSubaccount(uint8_t *principal, uint16_t principalLen,
347358
MEMCPY(address + 4, buf, 28);
348359

349360
uint32_t crc = 0;
350-
crc32_small(address+4, DFINITY_ADDR_LEN-4,&crc);
351-
address[0] = (uint8_t)((crc & 0xFF000000) >> 24);
352-
address[1] = (uint8_t)((crc & 0x00FF0000) >> 16);
353-
address[2] = (uint8_t)((crc & 0x0000FF00) >> 8);
354-
address[3] = (uint8_t)((crc & 0x000000FF) >> 0);
361+
crc32_small(address + 4, DFINITY_ADDR_LEN - 4, &crc);
362+
address[0] = (uint8_t) ((crc & 0xFF000000) >> 24);
363+
address[1] = (uint8_t) ((crc & 0x00FF0000) >> 16);
364+
address[2] = (uint8_t) ((crc & 0x0000FF00) >> 8);
365+
address[3] = (uint8_t) ((crc & 0x000000FF) >> 0);
355366
return zxerr_ok;
356367
}
357368

@@ -372,35 +383,35 @@ void crc32_small(const void *data, uint8_t n_bytes, uint32_t *crc) {
372383
}
373384
}
374385

375-
zxerr_t crypto_principalToTextual(uint8_t *address, uint8_t addressLen, unsigned char *textual, uint16_t *outLen){
386+
zxerr_t crypto_principalToTextual(const uint8_t *address_in, uint8_t addressLen, char *textual, uint16_t *outLen) {
376387
uint8_t input[33];
377388
uint32_t crc = 0;
378-
crc32_small(address, addressLen, &crc);
389+
crc32_small(address_in, addressLen, &crc);
379390
input[0] = (uint8_t) ((crc & 0xFF000000) >> 24);
380391
input[1] = (uint8_t) ((crc & 0x00FF0000) >> 16);
381392
input[2] = (uint8_t) ((crc & 0x0000FF00) >> 8);
382393
input[3] = (uint8_t) ((crc & 0x000000FF) >> 0);
383-
MEMCPY(input + 4, address, addressLen);
384-
int enc_len = base32_encode(input, 4 + addressLen, textual, 100);
394+
MEMCPY(input + 4, address_in, addressLen);
395+
uint32_t enc_len = base32_encode(input, 4 + addressLen, textual, 100);
385396
if (enc_len == 0) {
386397
return zxerr_unknown;
387398
}
388399
*outLen = enc_len;
389400
return zxerr_ok;
390401
}
391402

392-
zxerr_t addr_to_textual(char *s, uint16_t max, const char *text, uint16_t textLen) {
393-
MEMZERO(s, max);
403+
zxerr_t addr_to_textual(char *s_out, uint16_t s_max, const char *text_in, uint16_t text_in_len) {
404+
MEMZERO(s_out, s_max);
394405
uint16_t offset = 0;
395-
for (uint16_t index = 0; index < textLen; index += 5) {
396-
if (offset + 6 > max) {
406+
for (uint16_t index = 0; index < text_in_len; index += 5) {
407+
if (offset + 6 > s_max) {
397408
return zxerr_unknown;
398409
}
399-
uint8_t maxLen = (textLen - index) < 5 ? (textLen - index) : 5;
400-
MEMCPY(s + offset, text + index, maxLen);
410+
uint8_t maxLen = (text_in_len - index) < 5 ? (text_in_len - index) : 5;
411+
MEMCPY(s_out + offset, text_in + index, maxLen);
401412
offset += 5;
402-
if (index + 5 < textLen) {
403-
s[offset] = '-';
413+
if (index + 5 < text_in_len) {
414+
s_out[offset] = '-';
404415
offset += 1;
405416
}
406417
}
@@ -454,7 +465,7 @@ typedef struct {
454465
uint8_t publicKey[SECP256K1_PK_LEN];
455466
uint8_t principalBytes[DFINITY_PRINCIPAL_LEN];
456467
uint8_t subAccountBytes[DFINITY_ADDR_LEN];
457-
unsigned char addrText[DFINITY_TEXTUAL_SIZE];
468+
char addrText[DFINITY_TEXTUAL_SIZE];
458469

459470
} __attribute__((packed)) answer_t;
460471

@@ -476,7 +487,9 @@ zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t buffer_len, uint16_t *addrL
476487
uint8_t zero_subaccount[DFINITY_SUBACCOUNT_LEN];
477488
MEMZERO(zero_subaccount, DFINITY_SUBACCOUNT_LEN);
478489

479-
CHECK_ZXERR(crypto_principalToSubaccount(answer->principalBytes, sizeof_field(answer_t, principalBytes), zero_subaccount, DFINITY_SUBACCOUNT_LEN, answer->subAccountBytes, sizeof_field(answer_t, subAccountBytes)));
490+
CHECK_ZXERR(crypto_principalToSubaccount(answer->principalBytes, sizeof_field(answer_t, principalBytes),
491+
zero_subaccount, DFINITY_SUBACCOUNT_LEN, answer->subAccountBytes,
492+
sizeof_field(answer_t, subAccountBytes)));
480493

481494
uint16_t outLen = 0;
482495

app/src/crypto.h

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,30 @@ extern "C" {
2626
#include <sigutils.h>
2727
#include <zxerror.h>
2828

29-
#define CHECKSUM_LENGTH 4
30-
3129
extern uint32_t hdPath[HDPATH_LEN_DEFAULT];
3230

33-
#define ADDRESS_PROTOCOL_LEN 1
34-
35-
#define BLAKE2B_256_SIZE 32
36-
37-
uint16_t formatProtocol(const uint8_t *addressBytes, uint16_t addressSize,
38-
uint8_t *formattedAddress,
39-
uint16_t formattedAddressSize);
40-
4131
bool isTestnet();
4232

43-
int prepareDigestToSign(const unsigned char *in, unsigned int inLen,
44-
unsigned char *out, unsigned int outLen);
33+
zxerr_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT],
34+
uint8_t *pubKey, uint16_t pubKeyLen);
4535

46-
zxerr_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t *pubKey, uint16_t pubKeyLen);
36+
zxerr_t crypto_computePrincipal(const uint8_t *pubKey, uint8_t *principal);
37+
38+
zxerr_t crypto_principalToTextual(const uint8_t *address_in, uint8_t addressLen, char *textual, uint16_t *outLen);
4739

48-
zxerr_t crypto_computePrincipal(uint8_t *pubKey, uint8_t *address);
49-
zxerr_t crypto_principalToTextual(uint8_t *address, uint8_t addressLen, unsigned char *textual, uint16_t *outLen);
5040
zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t bufferLen, uint16_t *addrLen);
5141

52-
zxerr_t addr_to_textual(char *s, uint16_t max, const char *text, uint16_t textLen);
42+
zxerr_t addr_to_textual(char *s_out, uint16_t s_max, const char *text_in, uint16_t text_in_len);
5343

5444
void crc32_small(const void *data, uint8_t n_bytes, uint32_t *crc);
5545

56-
zxerr_t compressLEB128 (const uint64_t input, uint16_t maxSize, uint8_t *output, uint16_t *outLen);
57-
zxerr_t crypto_principalToSubaccount(uint8_t *principal, uint16_t principalLen, uint8_t *subAccount, uint16_t subaccountLen, uint8_t *address, uint16_t maxoutLen);
46+
zxerr_t compressLEB128(uint64_t input, uint16_t maxSize,
47+
uint8_t *output, uint16_t *outLen);
48+
49+
zxerr_t crypto_principalToSubaccount(const uint8_t *principal, uint16_t principalLen,
50+
uint8_t *subAccount, uint16_t subaccountLen,
51+
uint8_t *address, uint16_t maxoutLen);
52+
5853
zxerr_t crypto_sign(uint8_t *signature,
5954
uint16_t signatureMaxlen,
6055
const uint8_t *message,

0 commit comments

Comments
 (0)