Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

129 lines
4.2 KiB

  1. /**
  2. * @file x25519.h
  3. * @copyright
  4. * Copyright (c) 2016 Cryptography Research, Inc. \n
  5. * Released under the MIT License. See LICENSE.txt for license information.
  6. * @author Mike Hamburg
  7. * @brief X25519 key exchange and signatures.
  8. */
  9. #ifndef __X25519_H__
  10. #define __X25519_H__
  11. #define X25519_BYTES (256/8)
  12. /* The base point (9) */
  13. extern const unsigned char X25519_BASE_POINT[X25519_BYTES];
  14. /** Number of bytes in an EC public key */
  15. #define EC_PUBLIC_BYTES 32
  16. /** Number of bytes in an EC private key */
  17. #define EC_PRIVATE_BYTES 32
  18. /**
  19. * Number of bytes in a Schnorr challenge.
  20. * Could be set to 16 in a pinch. (FUTURE?)
  21. */
  22. #define EC_CHALLENGE_BYTES 32
  23. /** Enough bytes to get a uniform sample mod #E. For eg a Brainpool
  24. * curve this would need to be more than for a private key, but due
  25. * to the special prime used by Curve25519, the same size is enough.
  26. */
  27. #define EC_UNIFORM_BYTES 32
  28. /* x25519 scalar multiplication. Sets out to scalar*base.
  29. *
  30. * If clamp is set (and supported by X25519_INTEROP_SUPPORT_CLAMP)
  31. * then the scalar will be "clamped" like a Curve25519 secret key.
  32. * This adds almost no security, but permits interop with other x25519
  33. * implementations without manually clamping the keys.
  34. *
  35. * Per RFC 7748, this function returns failure (-1) if the output
  36. * is zero and clamp is set. This indicates "non-contributory behavior",
  37. * meaning that one party might steer the key so that the other party's
  38. * contribution doesn't matter, or contributes only a little entropy.
  39. *
  40. * WARNING: however, this function differs from RFC 7748 in another way:
  41. * it pays attention to the high bit base[EC_PUBLIC_BYTES-1] & 0x80, but
  42. * RFC 7748 says to ignore this bit. For compatibility with RFC 7748,
  43. * you must also clear this bit by running base[EC_PUBLIC_BYTES-1] &= 0x7F.
  44. * This library won't clear it for you because it takes the base point as
  45. * const, and (depending on build flags) dosen't copy it.
  46. *
  47. * If clamp==0, or if X25519_INTEROP_SUPPORT_CLAMP==0, then this function
  48. * always returns 0.
  49. */
  50. int x25519 (
  51. unsigned char out[EC_PUBLIC_BYTES],
  52. const unsigned char scalar[EC_PRIVATE_BYTES],
  53. const unsigned char base[EC_PUBLIC_BYTES],
  54. int clamp
  55. );
  56. /**
  57. * Returns 0 on success, -1 on failure.
  58. *
  59. * Per RFC 7748, this function returns failure if the output
  60. * is zero and clamp is set. This usually doesn't matter for
  61. * base scalarmuls.
  62. *
  63. * If clamp==0, or if X25519_INTEROP_SUPPORT_CLAMP==0, then this function
  64. * always returns 0.
  65. *
  66. * Same as x255(out,scalar,X255_BASE_POINT), except that
  67. * other implementations may optimize it.
  68. */
  69. static inline int x25519_base (
  70. unsigned char out[EC_PUBLIC_BYTES],
  71. const unsigned char scalar[EC_PRIVATE_BYTES],
  72. int clamp
  73. ) {
  74. return x25519(out,scalar,X25519_BASE_POINT,clamp);
  75. }
  76. /**
  77. * As x25519_base, but with a scalar that's EC_UNIFORM_BYTES long,
  78. * and clamp always 0 (and thus, no return value).
  79. *
  80. * This is used for signing. Implementors must replace it for
  81. * curves that require more bytes for uniformity (Brainpool).
  82. */
  83. static inline void x25519_base_uniform (
  84. unsigned char out[EC_PUBLIC_BYTES],
  85. const unsigned char scalar[EC_UNIFORM_BYTES]
  86. ) {
  87. (void)x25519_base(out,scalar,0);
  88. }
  89. /**
  90. * STROBE-compatible Schnorr signatures using curve25519 (not ed25519)
  91. *
  92. * The user will call x25519_base_uniform(eph,eph_secret) to schedule
  93. * a random ephemeral secret key. They then call a Schnorr oracle to
  94. * get a challenge, and compute the response using this function.
  95. */
  96. void x25519_sign_p2 (
  97. unsigned char response[EC_PRIVATE_BYTES],
  98. const unsigned char challenge[EC_CHALLENGE_BYTES],
  99. const unsigned char eph_secret[EC_UNIFORM_BYTES],
  100. const unsigned char secret[EC_PRIVATE_BYTES]
  101. );
  102. /**
  103. * STROBE-compatible signature verification using curve25519 (not ed25519).
  104. * This function is the public equivalent x25519_sign_p2, taking the long-term
  105. * and ephemeral public keys instead of secret ones.
  106. *
  107. * Returns -1 on failure and 0 on success.
  108. */
  109. int x25519_verify_p2 (
  110. const unsigned char response[X25519_BYTES],
  111. const unsigned char challenge[X25519_BYTES],
  112. const unsigned char eph[X25519_BYTES],
  113. const unsigned char pub[X25519_BYTES]
  114. );
  115. #endif /* __X25519_H__ */