Advertisement
Guest User

Custom EC curve NID_secp256k1

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