Advertisement
FlyFar

libs/TinyECDH/ecdh.h

Mar 24th, 2024
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | Cybersecurity | 0 0
  1. /*
  2.  
  3.   Crypto using elliptic curves defined over the finite binary field GF(2^m) where m is prime.
  4.  
  5.   The curves used are the anomalous binary curves (ABC-curves) or also called Koblitz curves.
  6.  
  7.   This class of curves was chosen because it yields efficient implementation of operations.
  8.  
  9.  
  10.  
  11.   Curves available - their different NIST/SECG names and eqivalent symmetric security level:
  12.  
  13.       NIST      SEC Group     strength
  14.     ------------------------------------
  15.       K-163     sect163k1      80 bit
  16.       B-163     sect163r2      80 bit
  17.       K-233     sect233k1     112 bit
  18.       B-233     sect233r1     112 bit
  19.       K-283     sect283k1     128 bit
  20.       B-283     sect283r1     128 bit
  21.       K-409     sect409k1     192 bit
  22.       B-409     sect409r1     192 bit
  23.       K-571     sect571k1     256 bit
  24.       B-571     sect571r1     256 bit
  25.  
  26.  
  27.  
  28.   Curve parameters from:
  29.  
  30.     http://www.secg.org/sec2-v2.pdf
  31.     http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
  32.  
  33.  
  34.   Reference:
  35.  
  36.     https://www.ietf.org/rfc/rfc4492.txt
  37. */
  38.  
  39. #ifndef _ECDH_H__
  40. #define _ECDH_H__
  41.  
  42.  
  43. /* for size-annotated integer types: uint8_t, uint32_t etc. */
  44. #include <stdint.h>
  45.  
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif /* __cplusplus */
  49.  
  50. #define NIST_B163  1
  51. #define NIST_K163  2
  52. #define NIST_B233  3
  53. #define NIST_K233  4
  54. #define NIST_B283  5
  55. #define NIST_K283  6
  56. #define NIST_B409  7
  57. #define NIST_K409  8
  58. #define NIST_B571  9
  59. #define NIST_K571 10
  60.  
  61. /* What is the default curve to use? */
  62. #ifndef ECC_CURVE
  63.  #define ECC_CURVE NIST_K571
  64. #endif
  65.  
  66. #if defined(ECC_CURVE) && (ECC_CURVE != 0)
  67.  #if   (ECC_CURVE == NIST_K163) || (ECC_CURVE == NIST_B163)
  68.   #define CURVE_DEGREE       163
  69.   #define ECC_PRV_KEY_SIZE   24
  70.  #elif (ECC_CURVE == NIST_K233) || (ECC_CURVE == NIST_B233)
  71.   #define CURVE_DEGREE       233
  72.   #define ECC_PRV_KEY_SIZE   32
  73.  #elif (ECC_CURVE == NIST_K283) || (ECC_CURVE == NIST_B283)
  74.   #define CURVE_DEGREE       283
  75.   #define ECC_PRV_KEY_SIZE   36
  76.  #elif (ECC_CURVE == NIST_K409) || (ECC_CURVE == NIST_B409)
  77.   #define CURVE_DEGREE       409
  78.   #define ECC_PRV_KEY_SIZE   52
  79.  #elif (ECC_CURVE == NIST_K571) || (ECC_CURVE == NIST_B571)
  80.   #define CURVE_DEGREE       571
  81.   #define ECC_PRV_KEY_SIZE   72
  82.  #endif
  83. #else
  84.  #error Must define a curve to use
  85. #endif
  86.  
  87. #define ECC_PUB_KEY_SIZE     (2 * ECC_PRV_KEY_SIZE)
  88.  
  89.  
  90. /******************************************************************************/
  91.  
  92.  
  93. /* NOTE: assumes private is filled with random data before calling */
  94. int ecdh_generate_keys(uint8_t* public_key, uint8_t* private_key);
  95.  
  96. /* input: own private key + other party's public key, output: shared secret */
  97. int ecdh_shared_secret(const uint8_t* private_key, const uint8_t* others_pub, uint8_t* output);
  98.  
  99.  
  100. /******************************************************************************/
  101.  
  102. #ifdef __cplusplus
  103. }
  104. #endif /* __cplusplus */
  105.  
  106. #endif /* #ifndef _ECDH_H__ */
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement