ecdh.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Crypto using elliptic curves defined over the finite binary field GF(2^m) where m is prime.
  3. The curves used are the anomalous binary curves (ABC-curves) or also called Koblitz curves.
  4. This class of curves was chosen because it yields efficient implementation of operations.
  5. Curves available - their different NIST/SECG names and eqivalent symmetric security level:
  6. NIST SEC Group strength
  7. ------------------------------------
  8. K-163 sect163k1 80 bit
  9. B-163 sect163r2 80 bit
  10. K-233 sect233k1 112 bit
  11. B-233 sect233r1 112 bit
  12. K-283 sect283k1 128 bit
  13. B-283 sect283r1 128 bit
  14. K-409 sect409k1 192 bit
  15. B-409 sect409r1 192 bit
  16. K-571 sect571k1 256 bit
  17. B-571 sect571r1 256 bit
  18. Curve parameters from:
  19. http://www.secg.org/sec2-v2.pdf
  20. http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
  21. Reference:
  22. https://www.ietf.org/rfc/rfc4492.txt
  23. */
  24. #ifndef _ECDH_H__
  25. #define _ECDH_H__
  26. /* for size-annotated integer types: uint8_t, uint32_t etc. */
  27. #include <stdint.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif /* __cplusplus */
  31. #define NIST_B163 1
  32. #define NIST_K163 2
  33. #define NIST_B233 3
  34. #define NIST_K233 4
  35. #define NIST_B283 5
  36. #define NIST_K283 6
  37. #define NIST_B409 7
  38. #define NIST_K409 8
  39. #define NIST_B571 9
  40. #define NIST_K571 10
  41. /* 这里设置使哪个曲线 */
  42. #ifndef ECC_CURVE
  43. #define ECC_CURVE NIST_K571
  44. #endif
  45. #if defined(ECC_CURVE) && (ECC_CURVE != 0)
  46. #if (ECC_CURVE == NIST_K163) || (ECC_CURVE == NIST_B163)
  47. #define CURVE_DEGREE 163
  48. #define ECC_PRV_KEY_SIZE 24
  49. #elif (ECC_CURVE == NIST_K233) || (ECC_CURVE == NIST_B233)
  50. #define CURVE_DEGREE 233
  51. #define ECC_PRV_KEY_SIZE 32
  52. #elif (ECC_CURVE == NIST_K283) || (ECC_CURVE == NIST_B283)
  53. #define CURVE_DEGREE 283
  54. #define ECC_PRV_KEY_SIZE 36
  55. #elif (ECC_CURVE == NIST_K409) || (ECC_CURVE == NIST_B409)
  56. #define CURVE_DEGREE 409
  57. #define ECC_PRV_KEY_SIZE 52
  58. #elif (ECC_CURVE == NIST_K571) || (ECC_CURVE == NIST_B571)
  59. #define CURVE_DEGREE 571
  60. #define ECC_PRV_KEY_SIZE 72
  61. #endif
  62. #else
  63. #error Must define a curve to use
  64. #endif
  65. #define ECC_PUB_KEY_SIZE (2 * ECC_PRV_KEY_SIZE)
  66. /******************************************************************************/
  67. /* NOTE: assumes private is filled with random data before calling */
  68. int ecdh_generate_keys(uint8_t* public_key, uint8_t* private_key);
  69. /* input: own private key + other party's public key, output: shared secret */
  70. int ecdh_shared_secret(const uint8_t* private_key, const uint8_t* others_pub, uint8_t* output);
  71. /* Broken :( .... */
  72. int ecdsa_sign(const uint8_t* private_key, uint8_t* hash, uint8_t* random_k, uint8_t* signature);
  73. int ecdsa_verify(const uint8_t* public_key, uint8_t* hash, const uint8_t* signature);
  74. /******************************************************************************/
  75. #ifdef __cplusplus
  76. }
  77. #endif /* __cplusplus */
  78. #endif /* #ifndef _ECDH_H__ */