ecc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "includes.h"
  2. #include "ecc.h"
  3. #include "dbutil.h"
  4. #include "bignum.h"
  5. #if DROPBEAR_ECC
  6. /* .dp members are filled out by dropbear_ecc_fill_dp() at startup */
  7. #if DROPBEAR_ECC_256
  8. struct dropbear_ecc_curve ecc_curve_nistp256 = {
  9. 32, /* .ltc_size */
  10. NULL, /* .dp */
  11. &sha256_desc, /* .hash_desc */
  12. "nistp256" /* .name */
  13. };
  14. #endif
  15. #if DROPBEAR_ECC_384
  16. struct dropbear_ecc_curve ecc_curve_nistp384 = {
  17. 48, /* .ltc_size */
  18. NULL, /* .dp */
  19. &sha384_desc, /* .hash_desc */
  20. "nistp384" /* .name */
  21. };
  22. #endif
  23. #if DROPBEAR_ECC_521
  24. struct dropbear_ecc_curve ecc_curve_nistp521 = {
  25. 66, /* .ltc_size */
  26. NULL, /* .dp */
  27. &sha512_desc, /* .hash_desc */
  28. "nistp521" /* .name */
  29. };
  30. #endif
  31. struct dropbear_ecc_curve *dropbear_ecc_curves[] = {
  32. #if DROPBEAR_ECC_256
  33. &ecc_curve_nistp256,
  34. #endif
  35. #if DROPBEAR_ECC_384
  36. &ecc_curve_nistp384,
  37. #endif
  38. #if DROPBEAR_ECC_521
  39. &ecc_curve_nistp521,
  40. #endif
  41. NULL
  42. };
  43. void dropbear_ecc_fill_dp() {
  44. struct dropbear_ecc_curve **curve;
  45. /* libtomcrypt guarantees they're ordered by size */
  46. const ltc_ecc_set_type *dp = ltc_ecc_sets;
  47. for (curve = dropbear_ecc_curves; *curve; curve++) {
  48. for (;dp->size > 0; dp++) {
  49. if (dp->size == (*curve)->ltc_size) {
  50. (*curve)->dp = dp;
  51. break;
  52. }
  53. }
  54. if (!(*curve)->dp) {
  55. dropbear_exit("Missing ECC params %s", (*curve)->name);
  56. }
  57. }
  58. }
  59. struct dropbear_ecc_curve* curve_for_dp(const ltc_ecc_set_type *dp) {
  60. struct dropbear_ecc_curve **curve = NULL;
  61. for (curve = dropbear_ecc_curves; *curve; curve++) {
  62. if ((*curve)->dp == dp) {
  63. break;
  64. }
  65. }
  66. assert(*curve);
  67. return *curve;
  68. }
  69. ecc_key * new_ecc_key(void) {
  70. ecc_key *key = m_malloc(sizeof(*key));
  71. m_mp_alloc_init_multi((mp_int**)&key->pubkey.x, (mp_int**)&key->pubkey.y,
  72. (mp_int**)&key->pubkey.z, (mp_int**)&key->k, NULL);
  73. return key;
  74. }
  75. /* Copied from libtomcrypt ecc_import.c (version there is static), modified
  76. for different mp_int pointer without LTC_SOURCE */
  77. static int ecc_is_point(const ecc_key *key)
  78. {
  79. mp_int *prime, *b, *t1, *t2;
  80. int err;
  81. m_mp_alloc_init_multi(&prime, &b, &t1, &t2, NULL);
  82. /* load prime and b */
  83. if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; }
  84. if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; }
  85. /* compute y^2 */
  86. if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; }
  87. /* compute x^3 */
  88. if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; }
  89. if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; }
  90. if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; }
  91. /* compute y^2 - x^3 */
  92. if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; }
  93. /* compute y^2 - x^3 + 3x */
  94. if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
  95. if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
  96. if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
  97. if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; }
  98. while (mp_cmp_d(t1, 0) == LTC_MP_LT) {
  99. if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; }
  100. }
  101. while (mp_cmp(t1, prime) != LTC_MP_LT) {
  102. if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; }
  103. }
  104. /* compare to b */
  105. if (mp_cmp(t1, b) != LTC_MP_EQ) {
  106. err = CRYPT_INVALID_PACKET;
  107. } else {
  108. err = CRYPT_OK;
  109. }
  110. error:
  111. mp_clear_multi(prime, b, t1, t2, NULL);
  112. m_free(prime);
  113. m_free(b);
  114. m_free(t1);
  115. m_free(t2);
  116. return err;
  117. }
  118. /* For the "ephemeral public key octet string" in ECDH (rfc5656 section 4) */
  119. void buf_put_ecc_raw_pubkey_string(buffer *buf, ecc_key *key) {
  120. unsigned long len = key->dp->size*2 + 1;
  121. int err;
  122. buf_putint(buf, len);
  123. err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len);
  124. if (err != CRYPT_OK) {
  125. dropbear_exit("ECC error");
  126. }
  127. buf_incrwritepos(buf, len);
  128. }
  129. /* For the "ephemeral public key octet string" in ECDH (rfc5656 section 4) */
  130. ecc_key * buf_get_ecc_raw_pubkey(buffer *buf, const struct dropbear_ecc_curve *curve) {
  131. ecc_key *key = NULL;
  132. int ret = DROPBEAR_FAILURE;
  133. const unsigned int size = curve->dp->size;
  134. unsigned char first;
  135. TRACE(("enter buf_get_ecc_raw_pubkey"))
  136. buf_setpos(buf, 0);
  137. first = buf_getbyte(buf);
  138. if (first == 2 || first == 3) {
  139. dropbear_log(LOG_WARNING, "Dropbear doesn't support ECC point compression");
  140. return NULL;
  141. }
  142. if (first != 4 || buf->len != 1+2*size) {
  143. TRACE(("leave, wrong size"))
  144. return NULL;
  145. }
  146. key = new_ecc_key();
  147. key->dp = curve->dp;
  148. if (mp_from_ubin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) {
  149. TRACE(("failed to read x"))
  150. goto out;
  151. }
  152. buf_incrpos(buf, size);
  153. if (mp_from_ubin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) {
  154. TRACE(("failed to read y"))
  155. goto out;
  156. }
  157. buf_incrpos(buf, size);
  158. mp_set(key->pubkey.z, 1);
  159. if (ecc_is_point(key) != CRYPT_OK) {
  160. TRACE(("failed, not a point"))
  161. goto out;
  162. }
  163. /* SEC1 3.2.3.1 Check that Q != 0 */
  164. if (mp_cmp_d(key->pubkey.x, 0) == LTC_MP_EQ) {
  165. TRACE(("failed, x == 0"))
  166. goto out;
  167. }
  168. if (mp_cmp_d(key->pubkey.y, 0) == LTC_MP_EQ) {
  169. TRACE(("failed, y == 0"))
  170. goto out;
  171. }
  172. ret = DROPBEAR_SUCCESS;
  173. out:
  174. if (ret == DROPBEAR_FAILURE) {
  175. if (key) {
  176. ecc_free(key);
  177. m_free(key);
  178. key = NULL;
  179. }
  180. }
  181. return key;
  182. }
  183. /* a modified version of libtomcrypt's "ecc_shared_secret" to output
  184. a mp_int instead. */
  185. mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, const ecc_key *private_key)
  186. {
  187. ecc_point *result = NULL;
  188. mp_int *prime = NULL, *shared_secret = NULL;
  189. int err = DROPBEAR_FAILURE;
  190. /* type valid? */
  191. if (private_key->type != PK_PRIVATE) {
  192. goto out;
  193. }
  194. if (private_key->dp != public_key->dp) {
  195. goto out;
  196. }
  197. /* make new point */
  198. result = ltc_ecc_new_point();
  199. if (result == NULL) {
  200. goto out;
  201. }
  202. prime = m_malloc(sizeof(*prime));
  203. m_mp_init(prime);
  204. if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) {
  205. goto out;
  206. }
  207. if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) {
  208. goto out;
  209. }
  210. shared_secret = m_malloc(sizeof(*shared_secret));
  211. m_mp_init(shared_secret);
  212. if (mp_copy(result->x, shared_secret) != CRYPT_OK) {
  213. goto out;
  214. }
  215. mp_clear(prime);
  216. m_free(prime);
  217. ltc_ecc_del_point(result);
  218. err = DROPBEAR_SUCCESS;
  219. out:
  220. if (err == DROPBEAR_FAILURE) {
  221. dropbear_exit("ECC error");
  222. }
  223. return shared_secret;
  224. }
  225. #endif