bip32.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /**
  2. * Copyright (c) 2013-2016 Tomas Dzetkulic
  3. * Copyright (c) 2013-2016 Pavol Rusnak
  4. * Copyright (c) 2015-2016 Jochen Hoenicke
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  20. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include "address.h"
  27. #if USE_NEM
  28. #include "aes/aes.h"
  29. #endif
  30. #include "base58.h"
  31. #include "bignum.h"
  32. #include "bip32.h"
  33. #include "cardano.h"
  34. #include "curves.h"
  35. #include "ecdsa.h"
  36. #include "ed25519_donna/ed25519_sha3.h"
  37. #include "ed25519_donna/ed25519.h"
  38. #include "hmac.h"
  39. #include "nist256p1.h"
  40. #include "secp256k1.h"
  41. #include "sha2.h"
  42. #include "sha3.h"
  43. #if USE_KECCAK
  44. #include "ed25519_donna/ed25519_keccak.h"
  45. #endif
  46. #if USE_NEM
  47. #include "nem.h"
  48. #endif
  49. #include "memzero.h"
  50. const curve_info ed25519_info = {
  51. .bip32_name = ED25519_SEED_NAME,
  52. .params = NULL,
  53. .hasher_base58 = HASHER_SHA2D,
  54. .hasher_sign = HASHER_SHA2D,
  55. .hasher_pubkey = HASHER_SHA2_RIPEMD,
  56. .hasher_script = HASHER_SHA2,
  57. };
  58. const curve_info ed25519_sha3_info = {
  59. .bip32_name = "ed25519-sha3 seed",
  60. .params = NULL,
  61. .hasher_base58 = HASHER_SHA2D,
  62. .hasher_sign = HASHER_SHA2D,
  63. .hasher_pubkey = HASHER_SHA2_RIPEMD,
  64. .hasher_script = HASHER_SHA2,
  65. };
  66. #if USE_KECCAK
  67. const curve_info ed25519_keccak_info = {
  68. .bip32_name = "ed25519-keccak seed",
  69. .params = NULL,
  70. .hasher_base58 = HASHER_SHA2D,
  71. .hasher_sign = HASHER_SHA2D,
  72. .hasher_pubkey = HASHER_SHA2_RIPEMD,
  73. .hasher_script = HASHER_SHA2,
  74. };
  75. #endif
  76. const curve_info curve25519_info = {
  77. .bip32_name = "curve25519 seed",
  78. .params = NULL,
  79. .hasher_base58 = HASHER_SHA2D,
  80. .hasher_sign = HASHER_SHA2D,
  81. .hasher_pubkey = HASHER_SHA2_RIPEMD,
  82. .hasher_script = HASHER_SHA2,
  83. };
  84. int hdnode_from_xpub(
  85. uint32_t depth,
  86. uint32_t child_num,
  87. const uint8_t* chain_code,
  88. const uint8_t* public_key,
  89. const char* curve,
  90. HDNode* out) {
  91. const curve_info* info = get_curve_by_name(curve);
  92. if(info == 0) {
  93. return 0;
  94. }
  95. if(public_key[0] != 0x02 && public_key[0] != 0x03) { // invalid pubkey
  96. return 0;
  97. }
  98. out->curve = info;
  99. out->depth = depth;
  100. out->child_num = child_num;
  101. memcpy(out->chain_code, chain_code, 32);
  102. memzero(out->private_key, 32);
  103. memzero(out->private_key_extension, 32);
  104. memcpy(out->public_key, public_key, 33);
  105. return 1;
  106. }
  107. int hdnode_from_xprv(
  108. uint32_t depth,
  109. uint32_t child_num,
  110. const uint8_t* chain_code,
  111. const uint8_t* private_key,
  112. const char* curve,
  113. HDNode* out) {
  114. bool failed = false;
  115. const curve_info* info = get_curve_by_name(curve);
  116. if(info == 0) {
  117. failed = true;
  118. } else if(info->params) {
  119. bignum256 a = {0};
  120. bn_read_be(private_key, &a);
  121. if(bn_is_zero(&a)) { // == 0
  122. failed = true;
  123. } else {
  124. if(!bn_is_less(&a, &info->params->order)) { // >= order
  125. failed = true;
  126. }
  127. }
  128. memzero(&a, sizeof(a));
  129. }
  130. if(failed) {
  131. return 0;
  132. }
  133. out->curve = info;
  134. out->depth = depth;
  135. out->child_num = child_num;
  136. memcpy(out->chain_code, chain_code, 32);
  137. memcpy(out->private_key, private_key, 32);
  138. memzero(out->public_key, sizeof(out->public_key));
  139. memzero(out->private_key_extension, sizeof(out->private_key_extension));
  140. return 1;
  141. }
  142. int hdnode_from_seed(const uint8_t* seed, int seed_len, const char* curve, HDNode* out) {
  143. static CONFIDENTIAL uint8_t I[32 + 32];
  144. memzero(out, sizeof(HDNode));
  145. out->depth = 0;
  146. out->child_num = 0;
  147. out->curve = get_curve_by_name(curve);
  148. if(out->curve == 0) {
  149. return 0;
  150. }
  151. static CONFIDENTIAL HMAC_SHA512_CTX ctx;
  152. hmac_sha512_Init(&ctx, (const uint8_t*)out->curve->bip32_name, strlen(out->curve->bip32_name));
  153. hmac_sha512_Update(&ctx, seed, seed_len);
  154. hmac_sha512_Final(&ctx, I);
  155. if(out->curve->params) {
  156. bignum256 a = {0};
  157. while(true) {
  158. bn_read_be(I, &a);
  159. if(!bn_is_zero(&a) // != 0
  160. && bn_is_less(&a, &out->curve->params->order)) { // < order
  161. break;
  162. }
  163. hmac_sha512_Init(
  164. &ctx, (const uint8_t*)out->curve->bip32_name, strlen(out->curve->bip32_name));
  165. hmac_sha512_Update(&ctx, I, sizeof(I));
  166. hmac_sha512_Final(&ctx, I);
  167. }
  168. memzero(&a, sizeof(a));
  169. }
  170. memcpy(out->private_key, I, 32);
  171. memcpy(out->chain_code, I + 32, 32);
  172. memzero(out->public_key, sizeof(out->public_key));
  173. memzero(I, sizeof(I));
  174. return 1;
  175. }
  176. uint32_t hdnode_fingerprint(HDNode* node) {
  177. uint8_t digest[32] = {0};
  178. uint32_t fingerprint = 0;
  179. hdnode_fill_public_key(node);
  180. hasher_Raw(node->curve->hasher_pubkey, node->public_key, 33, digest);
  181. fingerprint = ((uint32_t)digest[0] << 24) + (digest[1] << 16) + (digest[2] << 8) + digest[3];
  182. memzero(digest, sizeof(digest));
  183. return fingerprint;
  184. }
  185. int hdnode_private_ckd_bip32(HDNode* inout, uint32_t i) {
  186. static CONFIDENTIAL uint8_t data[1 + 32 + 4];
  187. static CONFIDENTIAL uint8_t I[32 + 32];
  188. static CONFIDENTIAL bignum256 a, b;
  189. #if USE_CARDANO
  190. if(inout->curve == &ed25519_cardano_info) {
  191. return 0;
  192. }
  193. #endif
  194. if(i & 0x80000000) { // private derivation
  195. data[0] = 0;
  196. memcpy(data + 1, inout->private_key, 32);
  197. } else { // public derivation
  198. if(!inout->curve->params) {
  199. return 0;
  200. }
  201. if(hdnode_fill_public_key(inout) != 0) {
  202. return 0;
  203. }
  204. memcpy(data, inout->public_key, 33);
  205. }
  206. write_be(data + 33, i);
  207. bn_read_be(inout->private_key, &a);
  208. static CONFIDENTIAL HMAC_SHA512_CTX ctx;
  209. hmac_sha512_Init(&ctx, inout->chain_code, 32);
  210. hmac_sha512_Update(&ctx, data, sizeof(data));
  211. hmac_sha512_Final(&ctx, I);
  212. if(inout->curve->params) {
  213. while(true) {
  214. bool failed = false;
  215. bn_read_be(I, &b);
  216. if(!bn_is_less(&b, &inout->curve->params->order)) { // >= order
  217. failed = true;
  218. } else {
  219. bn_add(&b, &a);
  220. bn_mod(&b, &inout->curve->params->order);
  221. if(bn_is_zero(&b)) {
  222. failed = true;
  223. }
  224. }
  225. if(!failed) {
  226. bn_write_be(&b, inout->private_key);
  227. break;
  228. }
  229. data[0] = 1;
  230. memcpy(data + 1, I + 32, 32);
  231. hmac_sha512_Init(&ctx, inout->chain_code, 32);
  232. hmac_sha512_Update(&ctx, data, sizeof(data));
  233. hmac_sha512_Final(&ctx, I);
  234. }
  235. } else {
  236. memcpy(inout->private_key, I, 32);
  237. }
  238. memcpy(inout->chain_code, I + 32, 32);
  239. inout->depth++;
  240. inout->child_num = i;
  241. memzero(inout->public_key, sizeof(inout->public_key));
  242. // making sure to wipe our memory
  243. memzero(&a, sizeof(a));
  244. memzero(&b, sizeof(b));
  245. memzero(I, sizeof(I));
  246. memzero(data, sizeof(data));
  247. return 1;
  248. }
  249. int hdnode_private_ckd(HDNode* inout, uint32_t i) {
  250. #if USE_CARDANO
  251. if(inout->curve == &ed25519_cardano_info) {
  252. return hdnode_private_ckd_cardano(inout, i);
  253. } else
  254. #endif
  255. {
  256. return hdnode_private_ckd_bip32(inout, i);
  257. }
  258. }
  259. int hdnode_public_ckd_cp(
  260. const ecdsa_curve* curve,
  261. const curve_point* parent,
  262. const uint8_t* parent_chain_code,
  263. uint32_t i,
  264. curve_point* child,
  265. uint8_t* child_chain_code) {
  266. uint8_t data[(1 + 32) + 4] = {0};
  267. uint8_t I[32 + 32] = {0};
  268. bignum256 c = {0};
  269. if(i & 0x80000000) { // private derivation
  270. return 0;
  271. }
  272. data[0] = 0x02 | (parent->y.val[0] & 0x01);
  273. bn_write_be(&parent->x, data + 1);
  274. write_be(data + 33, i);
  275. while(true) {
  276. hmac_sha512(parent_chain_code, 32, data, sizeof(data), I);
  277. bn_read_be(I, &c);
  278. if(bn_is_less(&c, &curve->order)) { // < order
  279. scalar_multiply(curve, &c, child); // b = c * G
  280. point_add(curve, parent, child); // b = a + b
  281. if(!point_is_infinity(child)) {
  282. if(child_chain_code) {
  283. memcpy(child_chain_code, I + 32, 32);
  284. }
  285. // Wipe all stack data.
  286. memzero(data, sizeof(data));
  287. memzero(I, sizeof(I));
  288. memzero(&c, sizeof(c));
  289. return 1;
  290. }
  291. }
  292. data[0] = 1;
  293. memcpy(data + 1, I + 32, 32);
  294. }
  295. }
  296. int hdnode_public_ckd(HDNode* inout, uint32_t i) {
  297. curve_point parent = {0}, child = {0};
  298. if(!ecdsa_read_pubkey(inout->curve->params, inout->public_key, &parent)) {
  299. return 0;
  300. }
  301. if(!hdnode_public_ckd_cp(
  302. inout->curve->params, &parent, inout->chain_code, i, &child, inout->chain_code)) {
  303. return 0;
  304. }
  305. memzero(inout->private_key, 32);
  306. inout->depth++;
  307. inout->child_num = i;
  308. inout->public_key[0] = 0x02 | (child.y.val[0] & 0x01);
  309. bn_write_be(&child.x, inout->public_key + 1);
  310. // Wipe all stack data.
  311. memzero(&parent, sizeof(parent));
  312. memzero(&child, sizeof(child));
  313. return 1;
  314. }
  315. void hdnode_public_ckd_address_optimized(
  316. const curve_point* pub,
  317. const uint8_t* chain_code,
  318. uint32_t i,
  319. uint32_t version,
  320. HasherType hasher_pubkey,
  321. HasherType hasher_base58,
  322. char* addr,
  323. int addrsize,
  324. int addrformat) {
  325. uint8_t child_pubkey[33] = {0};
  326. curve_point b = {0};
  327. hdnode_public_ckd_cp(&secp256k1, pub, chain_code, i, &b, NULL);
  328. child_pubkey[0] = 0x02 | (b.y.val[0] & 0x01);
  329. bn_write_be(&b.x, child_pubkey + 1);
  330. switch(addrformat) {
  331. case 1: // Segwit-in-P2SH
  332. ecdsa_get_address_segwit_p2sh(
  333. child_pubkey, version, hasher_pubkey, hasher_base58, addr, addrsize);
  334. break;
  335. default: // normal address
  336. ecdsa_get_address(child_pubkey, version, hasher_pubkey, hasher_base58, addr, addrsize);
  337. break;
  338. }
  339. }
  340. #if USE_BIP32_CACHE
  341. static bool private_ckd_cache_root_set = false;
  342. static CONFIDENTIAL HDNode private_ckd_cache_root;
  343. static int private_ckd_cache_index = 0;
  344. static CONFIDENTIAL struct {
  345. bool set;
  346. size_t depth;
  347. uint32_t i[BIP32_CACHE_MAXDEPTH];
  348. HDNode node;
  349. } private_ckd_cache[BIP32_CACHE_SIZE];
  350. void bip32_cache_clear(void) {
  351. private_ckd_cache_root_set = false;
  352. private_ckd_cache_index = 0;
  353. memzero(&private_ckd_cache_root, sizeof(private_ckd_cache_root));
  354. memzero(private_ckd_cache, sizeof(private_ckd_cache));
  355. }
  356. int hdnode_private_ckd_cached(
  357. HDNode* inout,
  358. const uint32_t* i,
  359. size_t i_count,
  360. uint32_t* fingerprint) {
  361. if(i_count == 0) {
  362. // no way how to compute parent fingerprint
  363. return 1;
  364. }
  365. if(i_count == 1) {
  366. if(fingerprint) {
  367. *fingerprint = hdnode_fingerprint(inout);
  368. }
  369. if(hdnode_private_ckd(inout, i[0]) == 0) return 0;
  370. return 1;
  371. }
  372. bool found = false;
  373. // if root is not set or not the same
  374. if(!private_ckd_cache_root_set ||
  375. memcmp(&private_ckd_cache_root, inout, sizeof(HDNode)) != 0) {
  376. // clear the cache
  377. private_ckd_cache_index = 0;
  378. memzero(private_ckd_cache, sizeof(private_ckd_cache));
  379. // setup new root
  380. memcpy(&private_ckd_cache_root, inout, sizeof(HDNode));
  381. private_ckd_cache_root_set = true;
  382. } else {
  383. // try to find parent
  384. int j = 0;
  385. for(j = 0; j < BIP32_CACHE_SIZE; j++) {
  386. if(private_ckd_cache[j].set && private_ckd_cache[j].depth == i_count - 1 &&
  387. memcmp(private_ckd_cache[j].i, i, (i_count - 1) * sizeof(uint32_t)) == 0 &&
  388. private_ckd_cache[j].node.curve == inout->curve) {
  389. memcpy(inout, &(private_ckd_cache[j].node), sizeof(HDNode));
  390. found = true;
  391. break;
  392. }
  393. }
  394. }
  395. // else derive parent
  396. if(!found) {
  397. size_t k = 0;
  398. for(k = 0; k < i_count - 1; k++) {
  399. if(hdnode_private_ckd(inout, i[k]) == 0) return 0;
  400. }
  401. // and save it
  402. memzero(
  403. &(private_ckd_cache[private_ckd_cache_index]),
  404. sizeof(private_ckd_cache[private_ckd_cache_index]));
  405. private_ckd_cache[private_ckd_cache_index].set = true;
  406. private_ckd_cache[private_ckd_cache_index].depth = i_count - 1;
  407. memcpy(private_ckd_cache[private_ckd_cache_index].i, i, (i_count - 1) * sizeof(uint32_t));
  408. memcpy(&(private_ckd_cache[private_ckd_cache_index].node), inout, sizeof(HDNode));
  409. private_ckd_cache_index = (private_ckd_cache_index + 1) % BIP32_CACHE_SIZE;
  410. }
  411. if(fingerprint) {
  412. *fingerprint = hdnode_fingerprint(inout);
  413. }
  414. if(hdnode_private_ckd(inout, i[i_count - 1]) == 0) return 0;
  415. return 1;
  416. }
  417. #endif
  418. int hdnode_get_address_raw(HDNode* node, uint32_t version, uint8_t* addr_raw) {
  419. if(hdnode_fill_public_key(node) != 0) {
  420. return 1;
  421. }
  422. ecdsa_get_address_raw(node->public_key, version, node->curve->hasher_pubkey, addr_raw);
  423. return 0;
  424. }
  425. int hdnode_get_address(HDNode* node, uint32_t version, char* addr, int addrsize) {
  426. if(hdnode_fill_public_key(node) != 0) {
  427. return 1;
  428. }
  429. ecdsa_get_address(
  430. node->public_key,
  431. version,
  432. node->curve->hasher_pubkey,
  433. node->curve->hasher_base58,
  434. addr,
  435. addrsize);
  436. return 0;
  437. }
  438. int hdnode_fill_public_key(HDNode* node) {
  439. if(node->public_key[0] != 0) return 0;
  440. #if USE_BIP32_25519_CURVES
  441. if(node->curve->params) {
  442. if(ecdsa_get_public_key33(node->curve->params, node->private_key, node->public_key) != 0) {
  443. return 1;
  444. }
  445. } else {
  446. node->public_key[0] = 1;
  447. if(node->curve == &ed25519_info) {
  448. ed25519_publickey(node->private_key, node->public_key + 1);
  449. } else if(node->curve == &ed25519_sha3_info) {
  450. ed25519_publickey_sha3(node->private_key, node->public_key + 1);
  451. #if USE_KECCAK
  452. } else if(node->curve == &ed25519_keccak_info) {
  453. ed25519_publickey_keccak(node->private_key, node->public_key + 1);
  454. #endif
  455. } else if(node->curve == &curve25519_info) {
  456. curve25519_scalarmult_basepoint(node->public_key + 1, node->private_key);
  457. #if USE_CARDANO
  458. } else if(node->curve == &ed25519_cardano_info) {
  459. ed25519_publickey_ext(node->private_key, node->public_key + 1);
  460. #endif
  461. }
  462. }
  463. #else
  464. if(ecdsa_get_public_key33(node->curve->params, node->private_key, node->public_key) != 0) {
  465. return 1;
  466. }
  467. #endif
  468. return 0;
  469. }
  470. #if USE_ETHEREUM
  471. int hdnode_get_ethereum_pubkeyhash(const HDNode* node, uint8_t* pubkeyhash) {
  472. uint8_t buf[65] = {0};
  473. //SHA3_CTX ctx = {0};
  474. SHA3_CTX* ctx = malloc(sizeof(SHA3_CTX));
  475. memzero(ctx, sizeof(SHA3_CTX));
  476. /* get uncompressed public key */
  477. if(ecdsa_get_public_key65(node->curve->params, node->private_key, buf) != 0) {
  478. memzero(ctx, sizeof(SHA3_CTX));
  479. free(ctx);
  480. return 0;
  481. }
  482. /* compute sha3 of x and y coordinate without 04 prefix */
  483. sha3_256_Init(ctx);
  484. sha3_Update(ctx, buf + 1, 64);
  485. keccak_Final(ctx, buf);
  486. memzero(ctx, sizeof(SHA3_CTX));
  487. free(ctx);
  488. /* result are the least significant 160 bits */
  489. memcpy(pubkeyhash, buf + 12, 20);
  490. return 1;
  491. }
  492. #endif
  493. #if USE_NEM
  494. int hdnode_get_nem_address(HDNode* node, uint8_t version, char* address) {
  495. if(node->curve != &ed25519_keccak_info) {
  496. return 0;
  497. }
  498. if(hdnode_fill_public_key(node) != 0) {
  499. return 0;
  500. }
  501. return nem_get_address(&node->public_key[1], version, address);
  502. }
  503. int hdnode_get_nem_shared_key(
  504. const HDNode* node,
  505. const ed25519_public_key peer_public_key,
  506. const uint8_t* salt,
  507. ed25519_public_key mul,
  508. uint8_t* shared_key) {
  509. if(node->curve != &ed25519_keccak_info) {
  510. return 0;
  511. }
  512. // sizeof(ed25519_public_key) == SHA3_256_DIGEST_LENGTH
  513. if(mul == NULL) mul = shared_key;
  514. if(ed25519_scalarmult_keccak(mul, node->private_key, peer_public_key)) {
  515. return 0;
  516. }
  517. for(size_t i = 0; i < 32; i++) {
  518. shared_key[i] = mul[i] ^ salt[i];
  519. }
  520. keccak_256(shared_key, 32, shared_key);
  521. return 1;
  522. }
  523. int hdnode_nem_encrypt(
  524. const HDNode* node,
  525. const ed25519_public_key public_key,
  526. const uint8_t* iv_immut,
  527. const uint8_t* salt,
  528. const uint8_t* payload,
  529. size_t size,
  530. uint8_t* buffer) {
  531. uint8_t last_block[AES_BLOCK_SIZE] = {0};
  532. uint8_t remainder = size % AES_BLOCK_SIZE;
  533. // Round down to last whole block
  534. size -= remainder;
  535. // Copy old last block
  536. memcpy(last_block, &payload[size], remainder);
  537. // Pad new last block with number of missing bytes
  538. memset(&last_block[remainder], AES_BLOCK_SIZE - remainder, AES_BLOCK_SIZE - remainder);
  539. // the IV gets mutated, so we make a copy not to touch the original
  540. uint8_t iv[AES_BLOCK_SIZE] = {0};
  541. memcpy(iv, iv_immut, AES_BLOCK_SIZE);
  542. uint8_t shared_key[SHA3_256_DIGEST_LENGTH] = {0};
  543. if(!hdnode_get_nem_shared_key(node, public_key, salt, NULL, shared_key)) {
  544. return 0;
  545. }
  546. aes_encrypt_ctx ctx = {0};
  547. int ret = aes_encrypt_key256(shared_key, &ctx);
  548. memzero(shared_key, sizeof(shared_key));
  549. if(ret != EXIT_SUCCESS) {
  550. return 0;
  551. }
  552. if(aes_cbc_encrypt(payload, buffer, size, iv, &ctx) != EXIT_SUCCESS) {
  553. return 0;
  554. }
  555. if(aes_cbc_encrypt(last_block, &buffer[size], sizeof(last_block), iv, &ctx) != EXIT_SUCCESS) {
  556. return 0;
  557. }
  558. return 1;
  559. }
  560. int hdnode_nem_decrypt(
  561. const HDNode* node,
  562. const ed25519_public_key public_key,
  563. uint8_t* iv,
  564. const uint8_t* salt,
  565. const uint8_t* payload,
  566. size_t size,
  567. uint8_t* buffer) {
  568. uint8_t shared_key[SHA3_256_DIGEST_LENGTH] = {0};
  569. if(!hdnode_get_nem_shared_key(node, public_key, salt, NULL, shared_key)) {
  570. return 0;
  571. }
  572. aes_decrypt_ctx ctx = {0};
  573. int ret = aes_decrypt_key256(shared_key, &ctx);
  574. memzero(shared_key, sizeof(shared_key));
  575. if(ret != EXIT_SUCCESS) {
  576. return 0;
  577. }
  578. if(aes_cbc_decrypt(payload, buffer, size, iv, &ctx) != EXIT_SUCCESS) {
  579. return 0;
  580. }
  581. return 1;
  582. }
  583. #endif
  584. // msg is a data to be signed
  585. // msg_len is the message length
  586. int hdnode_sign(
  587. HDNode* node,
  588. const uint8_t* msg,
  589. uint32_t msg_len,
  590. HasherType hasher_sign,
  591. uint8_t* sig,
  592. uint8_t* pby,
  593. int (*is_canonical)(uint8_t by, uint8_t sig[64])) {
  594. if(node->curve->params) {
  595. return ecdsa_sign(
  596. node->curve->params,
  597. hasher_sign,
  598. node->private_key,
  599. msg,
  600. msg_len,
  601. sig,
  602. pby,
  603. is_canonical);
  604. } else if(node->curve == &curve25519_info) {
  605. return 1; // signatures are not supported
  606. } else {
  607. if(node->curve == &ed25519_info) {
  608. ed25519_sign(msg, msg_len, node->private_key, sig);
  609. } else if(node->curve == &ed25519_sha3_info) {
  610. ed25519_sign_sha3(msg, msg_len, node->private_key, sig);
  611. #if USE_KECCAK
  612. } else if(node->curve == &ed25519_keccak_info) {
  613. ed25519_sign_keccak(msg, msg_len, node->private_key, sig);
  614. #endif
  615. } else {
  616. return 1; // unknown or unsupported curve
  617. }
  618. return 0;
  619. }
  620. }
  621. int hdnode_sign_digest(
  622. HDNode* node,
  623. const uint8_t* digest,
  624. uint8_t* sig,
  625. uint8_t* pby,
  626. int (*is_canonical)(uint8_t by, uint8_t sig[64])) {
  627. if(node->curve->params) {
  628. return ecdsa_sign_digest(
  629. node->curve->params, node->private_key, digest, sig, pby, is_canonical);
  630. } else if(node->curve == &curve25519_info) {
  631. return 1; // signatures are not supported
  632. } else {
  633. return hdnode_sign(node, digest, 32, 0, sig, pby, is_canonical);
  634. }
  635. }
  636. int hdnode_get_shared_key(
  637. const HDNode* node,
  638. const uint8_t* peer_public_key,
  639. uint8_t* session_key,
  640. int* result_size) {
  641. // Use elliptic curve Diffie-Helman to compute shared session key
  642. if(node->curve->params) {
  643. if(ecdh_multiply(node->curve->params, node->private_key, peer_public_key, session_key) !=
  644. 0) {
  645. return 1;
  646. }
  647. *result_size = 65;
  648. return 0;
  649. } else if(node->curve == &curve25519_info) {
  650. session_key[0] = 0x04;
  651. if(peer_public_key[0] != 0x40) {
  652. return 1; // Curve25519 public key should start with 0x40 byte.
  653. }
  654. curve25519_scalarmult(session_key + 1, node->private_key, peer_public_key + 1);
  655. *result_size = 33;
  656. return 0;
  657. } else {
  658. *result_size = 0;
  659. return 1; // ECDH is not supported
  660. }
  661. }
  662. static int hdnode_serialize(
  663. const HDNode* node,
  664. uint32_t fingerprint,
  665. uint32_t version,
  666. bool use_private,
  667. char* str,
  668. int strsize) {
  669. uint8_t node_data[78] = {0};
  670. write_be(node_data, version);
  671. node_data[4] = node->depth;
  672. write_be(node_data + 5, fingerprint);
  673. write_be(node_data + 9, node->child_num);
  674. memcpy(node_data + 13, node->chain_code, 32);
  675. if(use_private) {
  676. node_data[45] = 0;
  677. memcpy(node_data + 46, node->private_key, 32);
  678. } else {
  679. memcpy(node_data + 45, node->public_key, 33);
  680. }
  681. int ret = base58_encode_check(
  682. node_data, sizeof(node_data), node->curve->hasher_base58, str, strsize);
  683. memzero(node_data, sizeof(node_data));
  684. return ret;
  685. }
  686. int hdnode_serialize_public(
  687. const HDNode* node,
  688. uint32_t fingerprint,
  689. uint32_t version,
  690. char* str,
  691. int strsize) {
  692. return hdnode_serialize(node, fingerprint, version, false, str, strsize);
  693. }
  694. int hdnode_serialize_private(
  695. const HDNode* node,
  696. uint32_t fingerprint,
  697. uint32_t version,
  698. char* str,
  699. int strsize) {
  700. return hdnode_serialize(node, fingerprint, version, true, str, strsize);
  701. }
  702. // check for validity of curve point in case of public data not performed
  703. static int hdnode_deserialize(
  704. const char* str,
  705. uint32_t version,
  706. bool use_private,
  707. const char* curve,
  708. HDNode* node,
  709. uint32_t* fingerprint) {
  710. uint8_t node_data[78] = {0};
  711. memzero(node, sizeof(HDNode));
  712. node->curve = get_curve_by_name(curve);
  713. if(base58_decode_check(str, node->curve->hasher_base58, node_data, sizeof(node_data)) !=
  714. sizeof(node_data)) {
  715. return -1;
  716. }
  717. uint32_t ver = read_be(node_data);
  718. if(ver != version) {
  719. return -3; // invalid version
  720. }
  721. if(use_private) {
  722. // invalid data
  723. if(node_data[45]) {
  724. return -2;
  725. }
  726. memcpy(node->private_key, node_data + 46, 32);
  727. memzero(node->public_key, sizeof(node->public_key));
  728. } else {
  729. memzero(node->private_key, sizeof(node->private_key));
  730. memcpy(node->public_key, node_data + 45, 33);
  731. }
  732. node->depth = node_data[4];
  733. if(fingerprint) {
  734. *fingerprint = read_be(node_data + 5);
  735. }
  736. node->child_num = read_be(node_data + 9);
  737. memcpy(node->chain_code, node_data + 13, 32);
  738. return 0;
  739. }
  740. int hdnode_deserialize_public(
  741. const char* str,
  742. uint32_t version,
  743. const char* curve,
  744. HDNode* node,
  745. uint32_t* fingerprint) {
  746. return hdnode_deserialize(str, version, false, curve, node, fingerprint);
  747. }
  748. int hdnode_deserialize_private(
  749. const char* str,
  750. uint32_t version,
  751. const char* curve,
  752. HDNode* node,
  753. uint32_t* fingerprint) {
  754. return hdnode_deserialize(str, version, true, curve, node, fingerprint);
  755. }
  756. const curve_info* get_curve_by_name(const char* curve_name) {
  757. if(curve_name == 0) {
  758. return 0;
  759. }
  760. if(strcmp(curve_name, SECP256K1_NAME) == 0) {
  761. return &secp256k1_info;
  762. }
  763. if(strcmp(curve_name, SECP256K1_DECRED_NAME) == 0) {
  764. return &secp256k1_decred_info;
  765. }
  766. if(strcmp(curve_name, SECP256K1_GROESTL_NAME) == 0) {
  767. return &secp256k1_groestl_info;
  768. }
  769. if(strcmp(curve_name, SECP256K1_SMART_NAME) == 0) {
  770. return &secp256k1_smart_info;
  771. }
  772. if(strcmp(curve_name, NIST256P1_NAME) == 0) {
  773. return &nist256p1_info;
  774. }
  775. if(strcmp(curve_name, ED25519_NAME) == 0) {
  776. return &ed25519_info;
  777. }
  778. #if USE_CARDANO
  779. if(strcmp(curve_name, ED25519_CARDANO_NAME) == 0) {
  780. return &ed25519_cardano_info;
  781. }
  782. #endif
  783. if(strcmp(curve_name, ED25519_SHA3_NAME) == 0) {
  784. return &ed25519_sha3_info;
  785. }
  786. #if USE_KECCAK
  787. if(strcmp(curve_name, ED25519_KECCAK_NAME) == 0) {
  788. return &ed25519_keccak_info;
  789. }
  790. #endif
  791. if(strcmp(curve_name, CURVE25519_NAME) == 0) {
  792. return &curve25519_info;
  793. }
  794. return 0;
  795. }