ecdsa.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright (c) 2013-2014 Tomas Dzetkulic
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef __ECDSA_H__
  24. #define __ECDSA_H__
  25. #include <stdint.h>
  26. #include "bignum.h"
  27. #include "hasher.h"
  28. #include "options.h"
  29. // curve point x and y
  30. typedef struct {
  31. bignum256 x, y;
  32. } curve_point;
  33. typedef struct {
  34. bignum256 prime; // prime order of the finite field
  35. curve_point G; // initial curve point
  36. bignum256 order; // order of G
  37. bignum256 order_half; // order of G divided by 2
  38. int a; // coefficient 'a' of the elliptic curve
  39. bignum256 b; // coefficient 'b' of the elliptic curve
  40. #if USE_PRECOMPUTED_CP
  41. const curve_point cp[64][8];
  42. #endif
  43. } ecdsa_curve;
  44. // 4 byte prefix + 40 byte data (segwit)
  45. // 1 byte prefix + 64 byte data (cashaddr)
  46. #define MAX_ADDR_RAW_SIZE 65
  47. // bottle neck is cashaddr
  48. // segwit is at most 90 characters plus NUL separator
  49. // cashaddr: human readable prefix + 1 separator + 104 data + 8 checksum + 1 NUL
  50. // we choose 130 as maximum (including NUL character)
  51. #define MAX_ADDR_SIZE 130
  52. // 4 byte prefix + 32 byte privkey + 1 byte compressed marker
  53. #define MAX_WIF_RAW_SIZE (4 + 32 + 1)
  54. // (4 + 32 + 1 + 4 [checksum]) * 8 / log2(58) plus NUL.
  55. #define MAX_WIF_SIZE (57)
  56. void point_copy(const curve_point* cp1, curve_point* cp2);
  57. void point_add(const ecdsa_curve* curve, const curve_point* cp1, curve_point* cp2);
  58. void point_double(const ecdsa_curve* curve, curve_point* cp);
  59. int point_multiply(
  60. const ecdsa_curve* curve,
  61. const bignum256* k,
  62. const curve_point* p,
  63. curve_point* res);
  64. void point_set_infinity(curve_point* p);
  65. int point_is_infinity(const curve_point* p);
  66. int point_is_equal(const curve_point* p, const curve_point* q);
  67. int point_is_negative_of(const curve_point* p, const curve_point* q);
  68. int scalar_multiply(const ecdsa_curve* curve, const bignum256* k, curve_point* res);
  69. int ecdh_multiply(
  70. const ecdsa_curve* curve,
  71. const uint8_t* priv_key,
  72. const uint8_t* pub_key,
  73. uint8_t* session_key);
  74. void compress_coords(const curve_point* cp, uint8_t* compressed);
  75. void uncompress_coords(const ecdsa_curve* curve, uint8_t odd, const bignum256* x, bignum256* y);
  76. int ecdsa_uncompress_pubkey(
  77. const ecdsa_curve* curve,
  78. const uint8_t* pub_key,
  79. uint8_t* uncompressed);
  80. int ecdsa_sign(
  81. const ecdsa_curve* curve,
  82. HasherType hasher_sign,
  83. const uint8_t* priv_key,
  84. const uint8_t* msg,
  85. uint32_t msg_len,
  86. uint8_t* sig,
  87. uint8_t* pby,
  88. int (*is_canonical)(uint8_t by, uint8_t sig[64]));
  89. int ecdsa_sign_digest(
  90. const ecdsa_curve* curve,
  91. const uint8_t* priv_key,
  92. const uint8_t* digest,
  93. uint8_t* sig,
  94. uint8_t* pby,
  95. int (*is_canonical)(uint8_t by, uint8_t sig[64]));
  96. int ecdsa_get_public_key33(const ecdsa_curve* curve, const uint8_t* priv_key, uint8_t* pub_key);
  97. int ecdsa_get_public_key65(const ecdsa_curve* curve, const uint8_t* priv_key, uint8_t* pub_key);
  98. void ecdsa_get_pubkeyhash(const uint8_t* pub_key, HasherType hasher_pubkey, uint8_t* pubkeyhash);
  99. void ecdsa_get_address_raw(
  100. const uint8_t* pub_key,
  101. uint32_t version,
  102. HasherType hasher_pubkey,
  103. uint8_t* addr_raw);
  104. void ecdsa_get_address(
  105. const uint8_t* pub_key,
  106. uint32_t version,
  107. HasherType hasher_pubkey,
  108. HasherType hasher_base58,
  109. char* addr,
  110. int addrsize);
  111. void ecdsa_get_address_segwit_p2sh_raw(
  112. const uint8_t* pub_key,
  113. uint32_t version,
  114. HasherType hasher_pubkey,
  115. uint8_t* addr_raw);
  116. void ecdsa_get_address_segwit_p2sh(
  117. const uint8_t* pub_key,
  118. uint32_t version,
  119. HasherType hasher_pubkey,
  120. HasherType hasher_base58,
  121. char* addr,
  122. int addrsize);
  123. void ecdsa_get_wif(
  124. const uint8_t* priv_key,
  125. uint32_t version,
  126. HasherType hasher_base58,
  127. char* wif,
  128. int wifsize);
  129. int ecdsa_address_decode(
  130. const char* addr,
  131. uint32_t version,
  132. HasherType hasher_base58,
  133. uint8_t* out);
  134. int ecdsa_read_pubkey(const ecdsa_curve* curve, const uint8_t* pub_key, curve_point* pub);
  135. int ecdsa_validate_pubkey(const ecdsa_curve* curve, const curve_point* pub);
  136. int ecdsa_verify(
  137. const ecdsa_curve* curve,
  138. HasherType hasher_sign,
  139. const uint8_t* pub_key,
  140. const uint8_t* sig,
  141. const uint8_t* msg,
  142. uint32_t msg_len);
  143. int ecdsa_verify_digest(
  144. const ecdsa_curve* curve,
  145. const uint8_t* pub_key,
  146. const uint8_t* sig,
  147. const uint8_t* digest);
  148. int ecdsa_recover_pub_from_sig(
  149. const ecdsa_curve* curve,
  150. uint8_t* pub_key,
  151. const uint8_t* sig,
  152. const uint8_t* digest,
  153. int recid);
  154. int ecdsa_sig_to_der(const uint8_t* sig, uint8_t* der);
  155. int ecdsa_sig_from_der(const uint8_t* der, size_t der_len, uint8_t sig[64]);
  156. #endif