cardano.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * Copyright (c) 2013-2021 SatoshiLabs
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include "bignum.h"
  26. #include "bip32.h"
  27. #include "cardano.h"
  28. #include "curves.h"
  29. #include "hasher.h"
  30. #include "hmac.h"
  31. #include "memzero.h"
  32. #include "options.h"
  33. #include "pbkdf2.h"
  34. #include "sha2.h"
  35. #if USE_CARDANO
  36. #define CARDANO_MAX_NODE_DEPTH 1048576
  37. const curve_info ed25519_cardano_info = {
  38. .bip32_name = ED25519_CARDANO_NAME,
  39. .params = NULL,
  40. .hasher_base58 = HASHER_SHA2D,
  41. .hasher_sign = HASHER_SHA2D,
  42. .hasher_pubkey = HASHER_SHA2_RIPEMD,
  43. .hasher_script = HASHER_SHA2,
  44. };
  45. static void scalar_multiply8(const uint8_t* src, int bytes, uint8_t* dst) {
  46. uint8_t prev_acc = 0;
  47. for(int i = 0; i < bytes; i++) {
  48. dst[i] = (src[i] << 3) + (prev_acc & 0x7);
  49. prev_acc = src[i] >> 5;
  50. }
  51. dst[bytes] = src[bytes - 1] >> 5;
  52. }
  53. static void scalar_add_256bits(const uint8_t* src1, const uint8_t* src2, uint8_t* dst) {
  54. uint16_t r = 0;
  55. for(int i = 0; i < 32; i++) {
  56. r = r + (uint16_t)src1[i] + (uint16_t)src2[i];
  57. dst[i] = r & 0xff;
  58. r >>= 8;
  59. }
  60. }
  61. static void cardano_ed25519_tweak_bits(uint8_t private_key[32]) {
  62. private_key[0] &= 0xf8;
  63. private_key[31] &= 0x1f;
  64. private_key[31] |= 0x40;
  65. }
  66. int hdnode_private_ckd_cardano(HDNode* inout, uint32_t index) {
  67. if(inout->curve != &ed25519_cardano_info) {
  68. return 0;
  69. }
  70. if(inout->depth >= CARDANO_MAX_NODE_DEPTH) {
  71. return 0;
  72. }
  73. // checks for hardened/non-hardened derivation, keysize 32 means we are
  74. // dealing with public key and thus non-h, keysize 64 is for private key
  75. int keysize = 32;
  76. if(index & 0x80000000) {
  77. keysize = 64;
  78. }
  79. static CONFIDENTIAL uint8_t data[1 + 64 + 4];
  80. static CONFIDENTIAL uint8_t z[32 + 32];
  81. static CONFIDENTIAL uint8_t priv_key[64];
  82. static CONFIDENTIAL uint8_t res_key[64];
  83. write_le(data + keysize + 1, index);
  84. memcpy(priv_key, inout->private_key, 32);
  85. memcpy(priv_key + 32, inout->private_key_extension, 32);
  86. if(keysize == 64) { // private derivation
  87. data[0] = 0;
  88. memcpy(data + 1, inout->private_key, 32);
  89. memcpy(data + 1 + 32, inout->private_key_extension, 32);
  90. } else { // public derivation
  91. if(hdnode_fill_public_key(inout) != 0) {
  92. return 0;
  93. }
  94. data[0] = 2;
  95. memcpy(data + 1, inout->public_key + 1, 32);
  96. }
  97. static CONFIDENTIAL HMAC_SHA512_CTX ctx;
  98. hmac_sha512_Init(&ctx, inout->chain_code, 32);
  99. hmac_sha512_Update(&ctx, data, 1 + keysize + 4);
  100. hmac_sha512_Final(&ctx, z);
  101. static CONFIDENTIAL uint8_t zl8[32];
  102. memzero(zl8, 32);
  103. /* get 8 * Zl */
  104. scalar_multiply8(z, 28, zl8);
  105. /* Kl = 8*Zl + parent(K)l */
  106. scalar_add_256bits(zl8, priv_key, res_key);
  107. /* Kr = Zr + parent(K)r */
  108. scalar_add_256bits(z + 32, priv_key + 32, res_key + 32);
  109. memcpy(inout->private_key, res_key, 32);
  110. memcpy(inout->private_key_extension, res_key + 32, 32);
  111. if(keysize == 64) {
  112. data[0] = 1;
  113. } else {
  114. data[0] = 3;
  115. }
  116. hmac_sha512_Init(&ctx, inout->chain_code, 32);
  117. hmac_sha512_Update(&ctx, data, 1 + keysize + 4);
  118. hmac_sha512_Final(&ctx, z);
  119. memcpy(inout->chain_code, z + 32, 32);
  120. inout->depth++;
  121. inout->child_num = index;
  122. memzero(inout->public_key, sizeof(inout->public_key));
  123. // making sure to wipe our memory
  124. memzero(z, sizeof(z));
  125. memzero(data, sizeof(data));
  126. memzero(priv_key, sizeof(priv_key));
  127. memzero(res_key, sizeof(res_key));
  128. return 1;
  129. }
  130. int hdnode_from_secret_cardano(const uint8_t secret[CARDANO_SECRET_LENGTH], HDNode* out) {
  131. memzero(out, sizeof(HDNode));
  132. out->depth = 0;
  133. out->child_num = 0;
  134. out->curve = &ed25519_cardano_info;
  135. memcpy(out->private_key, secret, 32);
  136. memcpy(out->private_key_extension, secret + 32, 32);
  137. memcpy(out->chain_code, secret + 64, 32);
  138. cardano_ed25519_tweak_bits(out->private_key);
  139. out->public_key[0] = 0;
  140. if(hdnode_fill_public_key(out) != 0) {
  141. return 0;
  142. }
  143. return 1;
  144. }
  145. // Derives the root Cardano secret from a master secret, aka seed, as defined in
  146. // SLIP-0023.
  147. int secret_from_seed_cardano_slip23(
  148. const uint8_t* seed,
  149. int seed_len,
  150. uint8_t secret_out[CARDANO_SECRET_LENGTH]) {
  151. static CONFIDENTIAL uint8_t I[SHA512_DIGEST_LENGTH];
  152. static CONFIDENTIAL HMAC_SHA512_CTX ctx;
  153. hmac_sha512_Init(&ctx, (const uint8_t*)ED25519_CARDANO_NAME, strlen(ED25519_CARDANO_NAME));
  154. hmac_sha512_Update(&ctx, seed, seed_len);
  155. hmac_sha512_Final(&ctx, I);
  156. sha512_Raw(I, 32, secret_out);
  157. memcpy(secret_out + SHA512_DIGEST_LENGTH, I + 32, 32);
  158. cardano_ed25519_tweak_bits(secret_out);
  159. memzero(I, sizeof(I));
  160. memzero(&ctx, sizeof(ctx));
  161. return 1;
  162. }
  163. // Derives the root Cardano secret from a BIP-32 master secret via the Ledger
  164. // derivation:
  165. // https://github.com/cardano-foundation/CIPs/blob/09d7d8ee1bd64f7e6b20b5a6cae088039dce00cb/CIP-0003/Ledger.md
  166. int secret_from_seed_cardano_ledger(
  167. const uint8_t* seed,
  168. int seed_len,
  169. uint8_t secret_out[CARDANO_SECRET_LENGTH]) {
  170. static CONFIDENTIAL uint8_t chain_code[SHA256_DIGEST_LENGTH];
  171. static CONFIDENTIAL uint8_t root_key[SHA512_DIGEST_LENGTH];
  172. static CONFIDENTIAL HMAC_SHA256_CTX ctx;
  173. static CONFIDENTIAL HMAC_SHA512_CTX sctx;
  174. const uint8_t* intermediate_result = seed;
  175. int intermediate_result_len = seed_len;
  176. do {
  177. // STEP 1: derive a master secret like in BIP-32/SLIP-10
  178. hmac_sha512_Init(&sctx, (const uint8_t*)ED25519_SEED_NAME, strlen(ED25519_SEED_NAME));
  179. hmac_sha512_Update(&sctx, intermediate_result, intermediate_result_len);
  180. hmac_sha512_Final(&sctx, root_key);
  181. // STEP 2: check that the resulting key does not have a particular bit set,
  182. // otherwise iterate like in SLIP-10
  183. intermediate_result = root_key;
  184. intermediate_result_len = sizeof(root_key);
  185. } while(root_key[31] & 0x20);
  186. // STEP 3: calculate the chain code as a HMAC-SHA256 of "\x01" + seed,
  187. // key is "ed25519 seed"
  188. hmac_sha256_Init(&ctx, (const unsigned char*)ED25519_SEED_NAME, strlen(ED25519_SEED_NAME));
  189. hmac_sha256_Update(&ctx, (const unsigned char*)"\x01", 1);
  190. hmac_sha256_Update(&ctx, seed, seed_len);
  191. hmac_sha256_Final(&ctx, chain_code);
  192. // STEP 4: extract information into output
  193. _Static_assert(
  194. SHA512_DIGEST_LENGTH + SHA256_DIGEST_LENGTH == CARDANO_SECRET_LENGTH,
  195. "Invalid configuration of Cardano secret size");
  196. memcpy(secret_out, root_key, SHA512_DIGEST_LENGTH);
  197. memcpy(secret_out + SHA512_DIGEST_LENGTH, chain_code, SHA256_DIGEST_LENGTH);
  198. // STEP 5: tweak bits of the private key
  199. cardano_ed25519_tweak_bits(secret_out);
  200. memzero(&ctx, sizeof(ctx));
  201. memzero(&sctx, sizeof(sctx));
  202. memzero(root_key, sizeof(root_key));
  203. memzero(chain_code, sizeof(chain_code));
  204. return 1;
  205. }
  206. #define CARDANO_ICARUS_STEPS 32
  207. _Static_assert(
  208. CARDANO_ICARUS_PBKDF2_ROUNDS % CARDANO_ICARUS_STEPS == 0,
  209. "CARDANO_ICARUS_STEPS does not divide CARDANO_ICARUS_PBKDF2_ROUNDS");
  210. #define CARDANO_ICARUS_ROUNDS_PER_STEP (CARDANO_ICARUS_PBKDF2_ROUNDS / CARDANO_ICARUS_STEPS)
  211. // Derives the root Cardano HDNode from a passphrase and the entropy encoded in
  212. // a BIP-0039 mnemonic using the Icarus derivation scheme, aka V2 derivation
  213. // scheme:
  214. // https://github.com/cardano-foundation/CIPs/blob/09d7d8ee1bd64f7e6b20b5a6cae088039dce00cb/CIP-0003/Icarus.md
  215. int secret_from_entropy_cardano_icarus(
  216. const uint8_t* pass,
  217. int pass_len,
  218. const uint8_t* entropy,
  219. int entropy_len,
  220. uint8_t secret_out[CARDANO_SECRET_LENGTH],
  221. void (*progress_callback)(uint32_t, uint32_t)) {
  222. static CONFIDENTIAL PBKDF2_HMAC_SHA512_CTX pctx;
  223. static CONFIDENTIAL uint8_t digest[SHA512_DIGEST_LENGTH];
  224. uint32_t progress = 0;
  225. // PASS 1: first 64 bytes
  226. pbkdf2_hmac_sha512_Init(&pctx, pass, pass_len, entropy, entropy_len, 1);
  227. if(progress_callback) {
  228. progress_callback(progress, CARDANO_ICARUS_PBKDF2_ROUNDS * 2);
  229. }
  230. for(int i = 0; i < CARDANO_ICARUS_STEPS; i++) {
  231. pbkdf2_hmac_sha512_Update(&pctx, CARDANO_ICARUS_ROUNDS_PER_STEP);
  232. if(progress_callback) {
  233. progress += CARDANO_ICARUS_ROUNDS_PER_STEP;
  234. progress_callback(progress, CARDANO_ICARUS_PBKDF2_ROUNDS * 2);
  235. }
  236. }
  237. pbkdf2_hmac_sha512_Final(&pctx, digest);
  238. memcpy(secret_out, digest, SHA512_DIGEST_LENGTH);
  239. // PASS 2: remaining 32 bytes
  240. pbkdf2_hmac_sha512_Init(&pctx, pass, pass_len, entropy, entropy_len, 2);
  241. if(progress_callback) {
  242. progress_callback(progress, CARDANO_ICARUS_PBKDF2_ROUNDS * 2);
  243. }
  244. for(int i = 0; i < CARDANO_ICARUS_STEPS; i++) {
  245. pbkdf2_hmac_sha512_Update(&pctx, CARDANO_ICARUS_ROUNDS_PER_STEP);
  246. if(progress_callback) {
  247. progress += CARDANO_ICARUS_ROUNDS_PER_STEP;
  248. progress_callback(progress, CARDANO_ICARUS_PBKDF2_ROUNDS * 2);
  249. }
  250. }
  251. pbkdf2_hmac_sha512_Final(&pctx, digest);
  252. memcpy(
  253. secret_out + SHA512_DIGEST_LENGTH, digest, CARDANO_SECRET_LENGTH - SHA512_DIGEST_LENGTH);
  254. cardano_ed25519_tweak_bits(secret_out);
  255. memzero(&pctx, sizeof(pctx));
  256. memzero(digest, sizeof(digest));
  257. return 1;
  258. }
  259. #endif // USE_CARDANO