bip32.c 25 KB

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