@@ -31,10 +31,10 @@ uint8_t const DER_PREFIX[] = {0x30, 0x56, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x
31
31
0x06 , 0x05 , 0x2b , 0x81 , 0x04 , 0x00 , 0x0a , 0x03 , 0x42 , 0x00 };
32
32
33
33
#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)
35
35
36
36
#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)
38
38
39
39
#define SUBACCOUNT_PREFIX_SIZE 11u
40
40
@@ -74,7 +74,9 @@ zxerr_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t
74
74
}
75
75
76
76
//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 ) {
78
80
if (principalLen != DFINITY_PRINCIPAL_LEN || subaccountLen != DFINITY_SUBACCOUNT_LEN || maxoutLen < DFINITY_ADDR_LEN ){
79
81
return zxerr_invalid_crypto_settings ;
80
82
}
@@ -105,7 +107,7 @@ zxerr_t crypto_principalToSubaccount(uint8_t *principal, uint16_t principalLen,
105
107
// 00 // no padding
106
108
// 047060f720298ffa0f48d9606abdb0 ... // point on curve, uncompressed
107
109
108
- zxerr_t crypto_computePrincipal (uint8_t * pubKey , uint8_t * address ) {
110
+ zxerr_t crypto_computePrincipal (const uint8_t * pubKey , uint8_t * principal ) {
109
111
uint8_t DER [DER_INPUT_SIZE ];
110
112
MEMZERO (DER , sizeof (DER ));
111
113
MEMCPY (DER , DER_PREFIX , DER_PREFIX_SIZE );
@@ -117,7 +119,7 @@ zxerr_t crypto_computePrincipal(uint8_t *pubKey, uint8_t *address) {
117
119
cx_hash (& ctx .header , CX_LAST , DER , DER_INPUT_SIZE , buf , 224 );
118
120
119
121
buf [DFINITY_PRINCIPAL_LEN - 1 ] = 0x02 ;
120
- MEMCPY (address , buf , DFINITY_PRINCIPAL_LEN );
122
+ MEMCPY (principal , buf , DFINITY_PRINCIPAL_LEN );
121
123
return zxerr_ok ;
122
124
}
123
125
@@ -192,7 +194,10 @@ zxerr_t crypto_getDigest(uint8_t *digest, txtype_e txtype){
192
194
HASH_U64 ("ingress_expiry" ,fields -> ingress_expiry , tmpdigest );
193
195
HASH_BYTES_INTERMEDIATE ("method_name" , fields -> method_name , tmpdigest );
194
196
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
+ }
196
201
HASH_BYTES_END ("arg" , fields -> arg , tmpdigest , digest );
197
202
return zxerr_ok ;
198
203
}
@@ -293,13 +298,14 @@ zxerr_t crypto_sign(uint8_t *signatureBuffer,
293
298
#include <hexutils.h>
294
299
#include "picohash.h"
295
300
296
- char * crypto_testPubKey ;
297
-
298
301
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
+
299
305
return zxerr_ok ;
300
306
}
301
307
302
- zxerr_t crypto_computePrincipal (uint8_t * pubKey , uint8_t * address ) {
308
+ zxerr_t crypto_computePrincipal (const uint8_t * pubKey , uint8_t * principal ) {
303
309
uint8_t DER [DER_INPUT_SIZE ];
304
310
MEMZERO (DER , sizeof (DER ));
305
311
MEMCPY (DER , DER_PREFIX , DER_PREFIX_SIZE );
@@ -314,7 +320,7 @@ zxerr_t crypto_computePrincipal(uint8_t *pubKey, uint8_t *address) {
314
320
picohash_final (& ctx , buf );
315
321
316
322
buf [DFINITY_PRINCIPAL_LEN - 1 ] = 0x02 ;
317
- MEMCPY (address , buf , DFINITY_PRINCIPAL_LEN );
323
+ MEMCPY (principal , buf , DFINITY_PRINCIPAL_LEN );
318
324
return zxerr_ok ;
319
325
}
320
326
@@ -326,18 +332,23 @@ zxerr_t crypto_sign(uint8_t *signature,
326
332
return zxerr_ok ;
327
333
}
328
334
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 ) {
331
341
return zxerr_invalid_crypto_settings ;
332
342
}
343
+
333
344
uint8_t hashinput [SUBACCOUNT_PREFIX_SIZE + DFINITY_PRINCIPAL_LEN + DFINITY_SUBACCOUNT_LEN ];
334
345
MEMZERO (hashinput , sizeof (hashinput ));
335
346
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 );
337
348
MEMCPY (hashinput + SUBACCOUNT_PREFIX_SIZE , principal , DFINITY_PRINCIPAL_LEN );
338
349
MEMCPY (hashinput + SUBACCOUNT_PREFIX_SIZE + DFINITY_PRINCIPAL_LEN , subAccount , DFINITY_SUBACCOUNT_LEN );
339
- uint8_t buf [32 ];
340
350
351
+ uint8_t buf [32 ];
341
352
picohash_ctx_t ctx ;
342
353
343
354
picohash_init_sha224 (& ctx );
@@ -347,11 +358,11 @@ zxerr_t crypto_principalToSubaccount(uint8_t *principal, uint16_t principalLen,
347
358
MEMCPY (address + 4 , buf , 28 );
348
359
349
360
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 );
355
366
return zxerr_ok ;
356
367
}
357
368
@@ -372,35 +383,35 @@ void crc32_small(const void *data, uint8_t n_bytes, uint32_t *crc) {
372
383
}
373
384
}
374
385
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 ) {
376
387
uint8_t input [33 ];
377
388
uint32_t crc = 0 ;
378
- crc32_small (address , addressLen , & crc );
389
+ crc32_small (address_in , addressLen , & crc );
379
390
input [0 ] = (uint8_t ) ((crc & 0xFF000000 ) >> 24 );
380
391
input [1 ] = (uint8_t ) ((crc & 0x00FF0000 ) >> 16 );
381
392
input [2 ] = (uint8_t ) ((crc & 0x0000FF00 ) >> 8 );
382
393
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 );
385
396
if (enc_len == 0 ) {
386
397
return zxerr_unknown ;
387
398
}
388
399
* outLen = enc_len ;
389
400
return zxerr_ok ;
390
401
}
391
402
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 );
394
405
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 ) {
397
408
return zxerr_unknown ;
398
409
}
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 );
401
412
offset += 5 ;
402
- if (index + 5 < textLen ) {
403
- s [offset ] = '-' ;
413
+ if (index + 5 < text_in_len ) {
414
+ s_out [offset ] = '-' ;
404
415
offset += 1 ;
405
416
}
406
417
}
@@ -454,7 +465,7 @@ typedef struct {
454
465
uint8_t publicKey [SECP256K1_PK_LEN ];
455
466
uint8_t principalBytes [DFINITY_PRINCIPAL_LEN ];
456
467
uint8_t subAccountBytes [DFINITY_ADDR_LEN ];
457
- unsigned char addrText [DFINITY_TEXTUAL_SIZE ];
468
+ char addrText [DFINITY_TEXTUAL_SIZE ];
458
469
459
470
} __attribute__((packed )) answer_t ;
460
471
@@ -476,7 +487,9 @@ zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t buffer_len, uint16_t *addrL
476
487
uint8_t zero_subaccount [DFINITY_SUBACCOUNT_LEN ];
477
488
MEMZERO (zero_subaccount , DFINITY_SUBACCOUNT_LEN );
478
489
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 )));
480
493
481
494
uint16_t outLen = 0 ;
482
495
0 commit comments