fe_operations.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* fe_operations.h
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef WOLF_CRYPT_FE_OPERATIONS_H
  22. #define WOLF_CRYPT_FE_OPERATIONS_H
  23. #include <wolfssl/wolfcrypt/settings.h>
  24. #if defined(HAVE_CURVE25519) || defined(HAVE_ED25519)
  25. #include <wolfssl/wolfcrypt/types.h>
  26. #if defined(USE_INTEL_SPEEDUP) && !defined(NO_CURVED25519_X64)
  27. #define CURVED25519_X64
  28. #elif defined(HAVE___UINT128_T) && !defined(NO_CURVED25519_128BIT)
  29. #define CURVED25519_128BIT
  30. #endif
  31. #if defined(CURVED25519_X64)
  32. #define CURVED25519_ASM_64BIT
  33. #define CURVED25519_ASM
  34. #endif
  35. #if defined(WOLFSSL_ARMASM)
  36. #ifdef __aarch64__
  37. #define CURVED25519_ASM_64BIT
  38. #else
  39. #define CURVED25519_ASM_32BIT
  40. #endif
  41. #define CURVED25519_ASM
  42. #endif
  43. /*
  44. fe means field element.
  45. Here the field is \Z/(2^255-19).
  46. An element t, entries t[0]...t[9], represents the integer
  47. t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9].
  48. Bounds on each t[i] vary depending on context.
  49. */
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #if defined(CURVE25519_SMALL) || defined(ED25519_SMALL)
  54. #define F25519_SIZE 32
  55. WOLFSSL_LOCAL void lm_copy(byte*, const byte*);
  56. WOLFSSL_LOCAL void lm_add(byte*, const byte*, const byte*);
  57. WOLFSSL_LOCAL void lm_sub(byte*, const byte*, const byte*);
  58. WOLFSSL_LOCAL void lm_neg(byte*,const byte*);
  59. WOLFSSL_LOCAL void lm_invert(byte*, const byte*);
  60. WOLFSSL_LOCAL void lm_mul(byte*,const byte*,const byte*);
  61. #endif
  62. #if !defined(FREESCALE_LTC_ECC)
  63. WOLFSSL_LOCAL void fe_init(void);
  64. WOLFSSL_LOCAL int curve25519(byte * q, const byte * n, const byte * p);
  65. #endif
  66. /* default to be faster but take more memory */
  67. #if !defined(CURVE25519_SMALL) || !defined(ED25519_SMALL)
  68. #ifdef CURVED25519_ASM_64BIT
  69. typedef sword64 fe[4];
  70. #elif defined(CURVED25519_ASM_32BIT)
  71. typedef sword32 fe[8];
  72. #elif defined(CURVED25519_128BIT)
  73. typedef sword64 fe[5];
  74. #else
  75. typedef sword32 fe[10];
  76. #endif
  77. WOLFSSL_LOCAL void fe_copy(fe h,const fe f);
  78. WOLFSSL_LOCAL void fe_add(fe h,const fe f,const fe g);
  79. WOLFSSL_LOCAL void fe_neg(fe h,const fe f);
  80. WOLFSSL_LOCAL void fe_sub(fe h,const fe f,const fe g);
  81. WOLFSSL_LOCAL void fe_invert(fe out,const fe z);
  82. WOLFSSL_LOCAL void fe_mul(fe h,const fe f,const fe g);
  83. /* Based On Daniel J Bernstein's curve25519 and ed25519 Public Domain ref10
  84. work. */
  85. WOLFSSL_LOCAL void fe_0(fe h);
  86. WOLFSSL_LOCAL void fe_1(fe h);
  87. WOLFSSL_LOCAL int fe_isnonzero(const fe f);
  88. WOLFSSL_LOCAL int fe_isnegative(const fe f);
  89. WOLFSSL_LOCAL void fe_tobytes(unsigned char *s,const fe h);
  90. WOLFSSL_LOCAL void fe_sq(fe h,const fe f);
  91. WOLFSSL_LOCAL void fe_sq2(fe h,const fe f);
  92. WOLFSSL_LOCAL void fe_frombytes(fe h,const unsigned char *s);
  93. WOLFSSL_LOCAL void fe_cswap(fe f, fe g, int b);
  94. WOLFSSL_LOCAL void fe_mul121666(fe h,fe f);
  95. WOLFSSL_LOCAL void fe_cmov(fe f, const fe g, int b);
  96. WOLFSSL_LOCAL void fe_pow22523(fe out,const fe z);
  97. /* 64 type needed for SHA512 */
  98. WOLFSSL_LOCAL word64 load_3(const unsigned char *in);
  99. WOLFSSL_LOCAL word64 load_4(const unsigned char *in);
  100. #ifdef CURVED25519_ASM
  101. WOLFSSL_LOCAL void fe_ge_to_p2(fe rx, fe ry, fe rz, const fe px, const fe py,
  102. const fe pz, const fe pt);
  103. WOLFSSL_LOCAL void fe_ge_to_p3(fe rx, fe ry, fe rz, fe rt, const fe px,
  104. const fe py, const fe pz, const fe pt);
  105. WOLFSSL_LOCAL void fe_ge_dbl(fe rx, fe ry, fe rz, fe rt, const fe px,
  106. const fe py, const fe pz);
  107. WOLFSSL_LOCAL void fe_ge_madd(fe rx, fe ry, fe rz, fe rt, const fe px,
  108. const fe py, const fe pz, const fe pt,
  109. const fe qxy2d, const fe qyplusx,
  110. const fe qyminusx);
  111. WOLFSSL_LOCAL void fe_ge_msub(fe rx, fe ry, fe rz, fe rt, const fe px,
  112. const fe py, const fe pz, const fe pt,
  113. const fe qxy2d, const fe qyplusx,
  114. const fe qyminusx);
  115. WOLFSSL_LOCAL void fe_ge_add(fe rx, fe ry, fe rz, fe rt, const fe px,
  116. const fe py, const fe pz, const fe pt, const fe qz,
  117. const fe qt2d, const fe qyplusx,
  118. const fe qyminusx);
  119. WOLFSSL_LOCAL void fe_ge_sub(fe rx, fe ry, fe rz, fe rt, const fe px,
  120. const fe py, const fe pz, const fe pt, const fe qz,
  121. const fe qt2d, const fe qyplusx,
  122. const fe qyminusx);
  123. WOLFSSL_LOCAL void fe_cmov_table(fe* r, fe* base, signed char b);
  124. #endif /* CURVED25519_ASM */
  125. #endif /* !CURVE25519_SMALL || !ED25519_SMALL */
  126. /* Use less memory and only 32bit types or less, but is slower
  127. Based on Daniel Beer's public domain work. */
  128. #if defined(CURVE25519_SMALL) || defined(ED25519_SMALL)
  129. static const byte c25519_base_x[F25519_SIZE] = {9};
  130. static const byte f25519_zero[F25519_SIZE] = {0};
  131. static const byte f25519_one[F25519_SIZE] = {1};
  132. static const byte fprime_zero[F25519_SIZE] = {0};
  133. static const byte fprime_one[F25519_SIZE] = {1};
  134. WOLFSSL_LOCAL void fe_load(byte *x, word32 c);
  135. WOLFSSL_LOCAL void fe_normalize(byte *x);
  136. WOLFSSL_LOCAL void fe_inv__distinct(byte *r, const byte *x);
  137. /* Conditional copy. If condition == 0, then zero is copied to dst. If
  138. * condition == 1, then one is copied to dst. Any other value results in
  139. * undefined behavior.
  140. */
  141. WOLFSSL_LOCAL void fe_select(byte *dst, const byte *zero, const byte *one,
  142. byte condition);
  143. /* Multiply a point by a small constant. The two pointers are not
  144. * required to be distinct.
  145. *
  146. * The constant must be less than 2^24.
  147. */
  148. WOLFSSL_LOCAL void fe_mul_c(byte *r, const byte *a, word32 b);
  149. WOLFSSL_LOCAL void fe_mul__distinct(byte *r, const byte *a, const byte *b);
  150. /* Compute one of the square roots of the field element, if the element
  151. * is square. The other square is -r.
  152. *
  153. * If the input is not square, the returned value is a valid field
  154. * element, but not the correct answer. If you don't already know that
  155. * your element is square, you should square the return value and test.
  156. */
  157. WOLFSSL_LOCAL void fe_sqrt(byte *r, const byte *x);
  158. /* Conditional copy. If condition == 0, then zero is copied to dst. If
  159. * condition == 1, then one is copied to dst. Any other value results in
  160. * undefined behavior.
  161. */
  162. WOLFSSL_LOCAL void fprime_select(byte *dst, const byte *zero, const byte *one,
  163. byte condition);
  164. WOLFSSL_LOCAL void fprime_add(byte *r, const byte *a, const byte *modulus);
  165. WOLFSSL_LOCAL void fprime_sub(byte *r, const byte *a, const byte *modulus);
  166. WOLFSSL_LOCAL void fprime_mul(byte *r, const byte *a, const byte *b,
  167. const byte *modulus);
  168. WOLFSSL_LOCAL void fprime_copy(byte *x, const byte *a);
  169. #endif /* CURVE25519_SMALL || ED25519_SMALL */
  170. #ifdef __cplusplus
  171. } /* extern "C" */
  172. #endif
  173. #endif /* HAVE_CURVE25519 || HAVE_ED25519 */
  174. #endif /* WOLF_CRYPT_FE_OPERATIONS_H */