cardano.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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,
  54. uint8_t *dst) {
  55. uint16_t r = 0;
  56. for (int i = 0; i < 32; i++) {
  57. r = r + (uint16_t)src1[i] + (uint16_t)src2[i];
  58. dst[i] = r & 0xff;
  59. r >>= 8;
  60. }
  61. }
  62. static void cardano_ed25519_tweak_bits(uint8_t private_key[32]) {
  63. private_key[0] &= 0xf8;
  64. private_key[31] &= 0x1f;
  65. private_key[31] |= 0x40;
  66. }
  67. int hdnode_private_ckd_cardano(HDNode *inout, uint32_t index) {
  68. if (inout->curve != &ed25519_cardano_info) {
  69. return 0;
  70. }
  71. if (inout->depth >= CARDANO_MAX_NODE_DEPTH) {
  72. return 0;
  73. }
  74. // checks for hardened/non-hardened derivation, keysize 32 means we are
  75. // dealing with public key and thus non-h, keysize 64 is for private key
  76. int keysize = 32;
  77. if (index & 0x80000000) {
  78. keysize = 64;
  79. }
  80. static CONFIDENTIAL uint8_t data[1 + 64 + 4];
  81. static CONFIDENTIAL uint8_t z[32 + 32];
  82. static CONFIDENTIAL uint8_t priv_key[64];
  83. static CONFIDENTIAL uint8_t res_key[64];
  84. write_le(data + keysize + 1, index);
  85. memcpy(priv_key, inout->private_key, 32);
  86. memcpy(priv_key + 32, inout->private_key_extension, 32);
  87. if (keysize == 64) { // private derivation
  88. data[0] = 0;
  89. memcpy(data + 1, inout->private_key, 32);
  90. memcpy(data + 1 + 32, inout->private_key_extension, 32);
  91. } else { // public derivation
  92. if (hdnode_fill_public_key(inout) != 0) {
  93. return 0;
  94. }
  95. data[0] = 2;
  96. memcpy(data + 1, inout->public_key + 1, 32);
  97. }
  98. static CONFIDENTIAL HMAC_SHA512_CTX ctx;
  99. hmac_sha512_Init(&ctx, inout->chain_code, 32);
  100. hmac_sha512_Update(&ctx, data, 1 + keysize + 4);
  101. hmac_sha512_Final(&ctx, z);
  102. static CONFIDENTIAL uint8_t zl8[32];
  103. memzero(zl8, 32);
  104. /* get 8 * Zl */
  105. scalar_multiply8(z, 28, zl8);
  106. /* Kl = 8*Zl + parent(K)l */
  107. scalar_add_256bits(zl8, priv_key, res_key);
  108. /* Kr = Zr + parent(K)r */
  109. scalar_add_256bits(z + 32, priv_key + 32, res_key + 32);
  110. memcpy(inout->private_key, res_key, 32);
  111. memcpy(inout->private_key_extension, res_key + 32, 32);
  112. if (keysize == 64) {
  113. data[0] = 1;
  114. } else {
  115. data[0] = 3;
  116. }
  117. hmac_sha512_Init(&ctx, inout->chain_code, 32);
  118. hmac_sha512_Update(&ctx, data, 1 + keysize + 4);
  119. hmac_sha512_Final(&ctx, z);
  120. memcpy(inout->chain_code, z + 32, 32);
  121. inout->depth++;
  122. inout->child_num = index;
  123. memzero(inout->public_key, sizeof(inout->public_key));
  124. // making sure to wipe our memory
  125. memzero(z, sizeof(z));
  126. memzero(data, sizeof(data));
  127. memzero(priv_key, sizeof(priv_key));
  128. memzero(res_key, sizeof(res_key));
  129. return 1;
  130. }
  131. int hdnode_from_secret_cardano(const uint8_t secret[CARDANO_SECRET_LENGTH],
  132. HDNode *out) {
  133. memzero(out, sizeof(HDNode));
  134. out->depth = 0;
  135. out->child_num = 0;
  136. out->curve = &ed25519_cardano_info;
  137. memcpy(out->private_key, secret, 32);
  138. memcpy(out->private_key_extension, secret + 32, 32);
  139. memcpy(out->chain_code, secret + 64, 32);
  140. cardano_ed25519_tweak_bits(out->private_key);
  141. out->public_key[0] = 0;
  142. if (hdnode_fill_public_key(out) != 0) {
  143. return 0;
  144. }
  145. return 1;
  146. }
  147. // Derives the root Cardano secret from a master secret, aka seed, as defined in
  148. // SLIP-0023.
  149. int secret_from_seed_cardano_slip23(const uint8_t *seed, 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,
  154. strlen(ED25519_CARDANO_NAME));
  155. hmac_sha512_Update(&ctx, seed, seed_len);
  156. hmac_sha512_Final(&ctx, I);
  157. sha512_Raw(I, 32, secret_out);
  158. memcpy(secret_out + SHA512_DIGEST_LENGTH, I + 32, 32);
  159. cardano_ed25519_tweak_bits(secret_out);
  160. memzero(I, sizeof(I));
  161. memzero(&ctx, sizeof(ctx));
  162. return 1;
  163. }
  164. // Derives the root Cardano secret from a BIP-32 master secret via the Ledger
  165. // derivation:
  166. // https://github.com/cardano-foundation/CIPs/blob/09d7d8ee1bd64f7e6b20b5a6cae088039dce00cb/CIP-0003/Ledger.md
  167. int secret_from_seed_cardano_ledger(const uint8_t *seed, int seed_len,
  168. uint8_t secret_out[CARDANO_SECRET_LENGTH]) {
  169. static CONFIDENTIAL uint8_t chain_code[SHA256_DIGEST_LENGTH];
  170. static CONFIDENTIAL uint8_t root_key[SHA512_DIGEST_LENGTH];
  171. static CONFIDENTIAL HMAC_SHA256_CTX ctx;
  172. static CONFIDENTIAL HMAC_SHA512_CTX sctx;
  173. const uint8_t *intermediate_result = seed;
  174. int intermediate_result_len = seed_len;
  175. do {
  176. // STEP 1: derive a master secret like in BIP-32/SLIP-10
  177. hmac_sha512_Init(&sctx, (const uint8_t *)ED25519_SEED_NAME,
  178. 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,
  189. strlen(ED25519_SEED_NAME));
  190. hmac_sha256_Update(&ctx, (const unsigned char *)"\x01", 1);
  191. hmac_sha256_Update(&ctx, seed, seed_len);
  192. hmac_sha256_Final(&ctx, chain_code);
  193. // STEP 4: extract information into output
  194. _Static_assert(
  195. SHA512_DIGEST_LENGTH + SHA256_DIGEST_LENGTH == CARDANO_SECRET_LENGTH,
  196. "Invalid configuration of Cardano secret size");
  197. memcpy(secret_out, root_key, SHA512_DIGEST_LENGTH);
  198. memcpy(secret_out + SHA512_DIGEST_LENGTH, chain_code, SHA256_DIGEST_LENGTH);
  199. // STEP 5: tweak bits of the private key
  200. cardano_ed25519_tweak_bits(secret_out);
  201. memzero(&ctx, sizeof(ctx));
  202. memzero(&sctx, sizeof(sctx));
  203. memzero(root_key, sizeof(root_key));
  204. memzero(chain_code, sizeof(chain_code));
  205. return 1;
  206. }
  207. #define CARDANO_ICARUS_STEPS 32
  208. _Static_assert(
  209. CARDANO_ICARUS_PBKDF2_ROUNDS % CARDANO_ICARUS_STEPS == 0,
  210. "CARDANO_ICARUS_STEPS does not divide CARDANO_ICARUS_PBKDF2_ROUNDS");
  211. #define CARDANO_ICARUS_ROUNDS_PER_STEP \
  212. (CARDANO_ICARUS_PBKDF2_ROUNDS / CARDANO_ICARUS_STEPS)
  213. // Derives the root Cardano HDNode from a passphrase and the entropy encoded in
  214. // a BIP-0039 mnemonic using the Icarus derivation scheme, aka V2 derivation
  215. // scheme:
  216. // https://github.com/cardano-foundation/CIPs/blob/09d7d8ee1bd64f7e6b20b5a6cae088039dce00cb/CIP-0003/Icarus.md
  217. int secret_from_entropy_cardano_icarus(
  218. const uint8_t *pass, int pass_len, const uint8_t *entropy, int entropy_len,
  219. uint8_t secret_out[CARDANO_SECRET_LENGTH],
  220. void (*progress_callback)(uint32_t, uint32_t)) {
  221. static CONFIDENTIAL PBKDF2_HMAC_SHA512_CTX pctx;
  222. static CONFIDENTIAL uint8_t digest[SHA512_DIGEST_LENGTH];
  223. uint32_t progress = 0;
  224. // PASS 1: first 64 bytes
  225. pbkdf2_hmac_sha512_Init(&pctx, pass, pass_len, entropy, entropy_len, 1);
  226. if (progress_callback) {
  227. progress_callback(progress, CARDANO_ICARUS_PBKDF2_ROUNDS * 2);
  228. }
  229. for (int i = 0; i < CARDANO_ICARUS_STEPS; i++) {
  230. pbkdf2_hmac_sha512_Update(&pctx, CARDANO_ICARUS_ROUNDS_PER_STEP);
  231. if (progress_callback) {
  232. progress += CARDANO_ICARUS_ROUNDS_PER_STEP;
  233. progress_callback(progress, CARDANO_ICARUS_PBKDF2_ROUNDS * 2);
  234. }
  235. }
  236. pbkdf2_hmac_sha512_Final(&pctx, digest);
  237. memcpy(secret_out, digest, SHA512_DIGEST_LENGTH);
  238. // PASS 2: remaining 32 bytes
  239. pbkdf2_hmac_sha512_Init(&pctx, pass, pass_len, entropy, entropy_len, 2);
  240. if (progress_callback) {
  241. progress_callback(progress, CARDANO_ICARUS_PBKDF2_ROUNDS * 2);
  242. }
  243. for (int i = 0; i < CARDANO_ICARUS_STEPS; i++) {
  244. pbkdf2_hmac_sha512_Update(&pctx, CARDANO_ICARUS_ROUNDS_PER_STEP);
  245. if (progress_callback) {
  246. progress += CARDANO_ICARUS_ROUNDS_PER_STEP;
  247. progress_callback(progress, CARDANO_ICARUS_PBKDF2_ROUNDS * 2);
  248. }
  249. }
  250. pbkdf2_hmac_sha512_Final(&pctx, digest);
  251. memcpy(secret_out + SHA512_DIGEST_LENGTH, digest,
  252. CARDANO_SECRET_LENGTH - SHA512_DIGEST_LENGTH);
  253. cardano_ed25519_tweak_bits(secret_out);
  254. memzero(&pctx, sizeof(pctx));
  255. memzero(digest, sizeof(digest));
  256. return 1;
  257. }
  258. #endif // USE_CARDANO