bip32.c 24 KB

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