Advertisement
Guest User

Custom EC curve NID_secp256k1 for bitcoin-0.8.5-linux

a guest
Dec 9th, 2013
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.09 KB | None | 0 0
  1. Subject: [PATCH] Custom EC curve NID_secp256k1
  2.  
  3. Patch for bitcoin-0.8.5-linux with openssl,
  4. which is supporting ECC, but missing NID_secp256k1.
  5.  
  6. Donate to 1MyBTCJepaaM6Bs4iJgew3tVZbuUWunBV7
  7.  
  8. diff -ur bitcoin-0.8.5-linux/src/src/key.cpp bitcoin-0.8.5-linux-new/src/src/key.cpp
  9. --- bitcoin-0.8.5-linux/src/src/key.cpp 2013-09-12 12:43:31.000000000 +0000
  10. +++ bitcoin-0.8.5-linux-new/src/src/key.cpp 2013-12-09 20:19:55.513842379 +0000
  11. @@ -6,9 +6,171 @@
  12.  
  13.  #include <openssl/ecdsa.h>
  14.  #include <openssl/obj_mac.h>
  15. +#include <openssl/err.h>
  16.  
  17.  #include "key.h"
  18.  
  19. +typedef struct {
  20. +   int field_type, /* either NID_X9_62_prime_field or
  21. +                * NID_X9_62_characteristic_two_field */
  22. +       seed_len,
  23. +       param_len;
  24. +   unsigned int cofactor;  /* promoted to BN_ULONG */
  25. +} EC_CURVE_DATA;
  26. +
  27. +static const struct { EC_CURVE_DATA h; unsigned char data[0+32*6]; }
  28. +   _EC_SECG_PRIME_256K1 = {
  29. +                { NID_X9_62_prime_field,0,32,1 },
  30. +                {                          /* no seed */
  31. +                        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, /* p */
  32. +                        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  33. +                        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,
  34. +                        0xFC,0x2F,
  35. +                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* a */
  36. +                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37. +                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38. +                        0x00,0x00,
  39. +                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* b */
  40. +                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41. +                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42. +                        0x00,0x07,
  43. +                        0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0, /* x */
  44. +                        0x62,0x95,0xCE,0x87,0x0B,0x07,0x02,0x9B,0xFC,0xDB,
  45. +                        0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
  46. +                        0x17,0x98,
  47. +                        0x48,0x3a,0xda,0x77,0x26,0xa3,0xc4,0x65,0x5d,0xa4, /* y */
  48. +                        0xfb,0xfc,0x0e,0x11,0x08,0xa8,0xfd,0x17,0xb4,0x48,
  49. +                        0xa6,0x85,0x54,0x19,0x9c,0x47,0xd0,0x8f,0xfb,0x10,
  50. +                        0xd4,0xb8,
  51. +                        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, /* order */
  52. +                        0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,
  53. +                        0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,0x8C,0xD0,0x36,
  54. +                        0x41,0x41 }
  55. +   };
  56. +
  57. +static EC_GROUP *ec_group_new_from_data(const EC_CURVE_DATA *data)
  58. +{
  59. +   EC_GROUP *group=NULL;
  60. +   EC_POINT *P=NULL;
  61. +   BN_CTX   *ctx=NULL;
  62. +   BIGNUM   *p=NULL, *a=NULL, *b=NULL, *x=NULL, *y=NULL, *order=NULL;
  63. +   int  ok=0;
  64. +   int  seed_len,param_len;
  65. +   const unsigned char *params;
  66. +
  67. +   if ((ctx = BN_CTX_new()) == NULL)
  68. +       {
  69. +                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_MALLOC_FAILURE);
  70. +                        goto err;
  71. +       }
  72. +
  73. +   seed_len  = data->seed_len;
  74. +   param_len = data->param_len;
  75. +   params    = (const unsigned char *)(data+1);    /* skip header */
  76. +   params   += seed_len;               /* skip seed   */
  77. +
  78. +   if (!(p = BN_bin2bn(params+0*param_len, param_len, NULL))
  79. +            || !(a = BN_bin2bn(params+1*param_len, param_len, NULL))
  80. +            || !(b = BN_bin2bn(params+2*param_len, param_len, NULL)))
  81. +       {
  82. +                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
  83. +                        goto err;
  84. +       }
  85. +
  86. +        if ((group = EC_GROUP_new_curve_GFp(p, a, b, ctx)) == NULL)
  87. +                {
  88. +                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
  89. +                        goto err;
  90. +                }
  91. +
  92. +   if ((P = EC_POINT_new(group)) == NULL)
  93. +       {
  94. +                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
  95. +                        goto err;
  96. +       }
  97. +
  98. +   if (!(x = BN_bin2bn(params+3*param_len, param_len, NULL))
  99. +            || !(y = BN_bin2bn(params+4*param_len, param_len, NULL)))
  100. +       {
  101. +                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
  102. +                        goto err;
  103. +       }
  104. +   if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
  105. +       {
  106. +                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
  107. +                        goto err;
  108. +       }
  109. +   if (!(order = BN_bin2bn(params+5*param_len, param_len, NULL))
  110. +            || !BN_set_word(x, (BN_ULONG)data->cofactor))
  111. +       {
  112. +                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
  113. +                        goto err;
  114. +       }
  115. +   if (!EC_GROUP_set_generator(group, P, order, x))
  116. +       {
  117. +                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
  118. +                        goto err;
  119. +       }
  120. +   if (seed_len)
  121. +       {
  122. +                        if (!EC_GROUP_set_seed(group, params-seed_len, seed_len))
  123. +                                {
  124. +                                        ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
  125. +                                        goto err;
  126. +                                }
  127. +       }
  128. +   ok=1;
  129. + err:
  130. +   if (!ok)
  131. +       {
  132. +                        EC_GROUP_free(group);
  133. +                        group = NULL;
  134. +       }
  135. +   if (P)
  136. +       EC_POINT_free(P);
  137. +   if (ctx)
  138. +       BN_CTX_free(ctx);
  139. +   if (p)
  140. +       BN_free(p);
  141. +   if (a)
  142. +       BN_free(a);
  143. +   if (b)
  144. +       BN_free(b);
  145. +   if (order)
  146. +       BN_free(order);
  147. +   if (x)
  148. +       BN_free(x);
  149. +   if (y)
  150. +       BN_free(y);
  151. +   return group;
  152. +}
  153. +
  154. +
  155. +
  156. +EC_KEY *EC_KEY_new_by_curve_name_NID_secp256k1(void)
  157. +{
  158. +        static EC_GROUP *group = NULL;
  159. +   EC_KEY *ret = NULL;
  160. +
  161. +        if (group == NULL) {
  162. +#ifdef HAVE_NID_secp256k1
  163. +                group = EC_GROUP_new_by_curve_name(NID_secp256k1);
  164. +#else
  165. +                group = ec_group_new_from_data(&_EC_SECG_PRIME_256K1.h);
  166. +#endif
  167. +                if (group == NULL)
  168. +                        return NULL;
  169. +        }
  170. +
  171. +   ret = EC_KEY_new();
  172. +   if (ret == NULL)
  173. +       return NULL;
  174. +
  175. +        EC_KEY_set_group(ret, group);
  176. +
  177. +   return ret;
  178. +}
  179. +
  180.  // Generate a private key from just the secret parameter
  181.  int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key)
  182.  {
  183. @@ -131,7 +293,7 @@
  184.      fCompressedPubKey = false;
  185.      if (pkey != NULL)
  186.          EC_KEY_free(pkey);
  187. -    pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
  188. +    pkey = EC_KEY_new_by_curve_name_NID_secp256k1();
  189.      if (pkey == NULL)
  190.          throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed");
  191.      fSet = false;
  192. @@ -209,7 +371,7 @@
  193.  bool CKey::SetSecret(const CSecret& vchSecret, bool fCompressed)
  194.  {
  195.      EC_KEY_free(pkey);
  196. -    pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
  197. +    pkey = EC_KEY_new_by_curve_name_NID_secp256k1();
  198.      if (pkey == NULL)
  199.          throw key_error("CKey::SetSecret() : EC_KEY_new_by_curve_name failed");
  200.      if (vchSecret.size() != 32)
  201. @@ -355,7 +517,7 @@
  202.      BN_bin2bn(&vchSig[33],32,sig->s);
  203.  
  204.      EC_KEY_free(pkey);
  205. -    pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
  206. +    pkey = EC_KEY_new_by_curve_name_NID_secp256k1();
  207.      if (nV >= 31)
  208.      {
  209.          SetCompressedPubKey();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement