internal.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2015-2016 the fiat-crypto authors (see the AUTHORS file).
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #ifndef OPENSSL_HEADER_CURVE25519_INTERNAL_H
  23. #define OPENSSL_HEADER_CURVE25519_INTERNAL_H
  24. #if defined(__cplusplus)
  25. extern "C" {
  26. #endif
  27. #if defined(OPENSSL_X86_64) && !defined(OPENSSL_SMALL) && \
  28. !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_NO_ASM)
  29. #define BORINGSSL_X25519_X86_64
  30. void x25519_x86_64(uint8_t out[32], const uint8_t scalar[32],
  31. const uint8_t point[32]);
  32. #endif
  33. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_APPLE)
  34. #define BORINGSSL_X25519_NEON
  35. // x25519_NEON is defined in asm/x25519-arm.S.
  36. void x25519_NEON(uint8_t out[32], const uint8_t scalar[32],
  37. const uint8_t point[32]);
  38. #endif
  39. // fe means field element. Here the field is \Z/(2^255-19). An element t,
  40. // entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
  41. // t[3]+2^102 t[4]+...+2^230 t[9].
  42. // fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc.
  43. // Multiplication and carrying produce fe from fe_loose.
  44. typedef struct fe { uint32_t v[10]; } fe;
  45. // fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc.
  46. // Addition and subtraction produce fe_loose from (fe, fe).
  47. typedef struct fe_loose { uint32_t v[10]; } fe_loose;
  48. /* ge means group element.
  49. * Here the group is the set of pairs (x,y) of field elements (see fe.h)
  50. * satisfying -x^2 + y^2 = 1 + d x^2y^2
  51. * where d = -121665/121666.
  52. *
  53. * Representations:
  54. * ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
  55. * ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
  56. * ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
  57. * ge_precomp (Duif): (y+x,y-x,2dxy) */
  58. typedef struct {
  59. fe X;
  60. fe Y;
  61. fe Z;
  62. } ge_p2;
  63. typedef struct {
  64. fe X;
  65. fe Y;
  66. fe Z;
  67. fe T;
  68. } ge_p3;
  69. typedef struct {
  70. fe_loose X;
  71. fe_loose Y;
  72. fe_loose Z;
  73. fe_loose T;
  74. } ge_p1p1;
  75. typedef struct {
  76. fe_loose yplusx;
  77. fe_loose yminusx;
  78. fe_loose xy2d;
  79. } ge_precomp;
  80. typedef struct {
  81. fe_loose YplusX;
  82. fe_loose YminusX;
  83. fe_loose Z;
  84. fe_loose T2d;
  85. } ge_cached;
  86. void x25519_ge_tobytes(uint8_t *s, const ge_p2 *h);
  87. int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s);
  88. void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p);
  89. void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p);
  90. void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p);
  91. void x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
  92. void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
  93. void x25519_ge_scalarmult_small_precomp(
  94. ge_p3 *h, const uint8_t a[32], const uint8_t precomp_table[15 * 2 * 32]);
  95. void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]);
  96. void x25519_ge_scalarmult(ge_p2 *r, const uint8_t *scalar, const ge_p3 *A);
  97. void x25519_sc_reduce(uint8_t *s);
  98. enum spake2_state_t {
  99. spake2_state_init = 0,
  100. spake2_state_msg_generated,
  101. spake2_state_key_generated,
  102. };
  103. struct spake2_ctx_st {
  104. uint8_t private_key[32];
  105. uint8_t my_msg[32];
  106. uint8_t password_scalar[32];
  107. uint8_t password_hash[64];
  108. uint8_t *my_name;
  109. size_t my_name_len;
  110. uint8_t *their_name;
  111. size_t their_name_len;
  112. enum spake2_role_t my_role;
  113. enum spake2_state_t state;
  114. char disable_password_scalar_hack;
  115. };
  116. #if defined(__cplusplus)
  117. } // extern C
  118. #endif
  119. #endif // OPENSSL_HEADER_CURVE25519_INTERNAL_H