fe_operations.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* fe_operations.h
  2. *
  3. * Copyright (C) 2006-2023 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_cmov_table(fe* r, fe* base, signed char b);
  102. #endif /* CURVED25519_ASM */
  103. #endif /* !CURVE25519_SMALL || !ED25519_SMALL */
  104. /* Use less memory and only 32bit types or less, but is slower
  105. Based on Daniel Beer's public domain work. */
  106. #if defined(CURVE25519_SMALL) || defined(ED25519_SMALL)
  107. static const byte c25519_base_x[F25519_SIZE] = {9};
  108. static const byte f25519_zero[F25519_SIZE] = {0};
  109. static const byte f25519_one[F25519_SIZE] = {1};
  110. static const byte fprime_zero[F25519_SIZE] = {0};
  111. static const byte fprime_one[F25519_SIZE] = {1};
  112. WOLFSSL_LOCAL void fe_load(byte *x, word32 c);
  113. WOLFSSL_LOCAL void fe_normalize(byte *x);
  114. WOLFSSL_LOCAL void fe_inv__distinct(byte *r, const byte *x);
  115. /* Conditional copy. If condition == 0, then zero is copied to dst. If
  116. * condition == 1, then one is copied to dst. Any other value results in
  117. * undefined behavior.
  118. */
  119. WOLFSSL_LOCAL void fe_select(byte *dst, const byte *zero, const byte *one,
  120. byte condition);
  121. /* Multiply a point by a small constant. The two pointers are not
  122. * required to be distinct.
  123. *
  124. * The constant must be less than 2^24.
  125. */
  126. WOLFSSL_LOCAL void fe_mul_c(byte *r, const byte *a, word32 b);
  127. WOLFSSL_LOCAL void fe_mul__distinct(byte *r, const byte *a, const byte *b);
  128. /* Compute one of the square roots of the field element, if the element
  129. * is square. The other square is -r.
  130. *
  131. * If the input is not square, the returned value is a valid field
  132. * element, but not the correct answer. If you don't already know that
  133. * your element is square, you should square the return value and test.
  134. */
  135. WOLFSSL_LOCAL void fe_sqrt(byte *r, const byte *x);
  136. /* Conditional copy. If condition == 0, then zero is copied to dst. If
  137. * condition == 1, then one is copied to dst. Any other value results in
  138. * undefined behavior.
  139. */
  140. WOLFSSL_LOCAL void fprime_select(byte *dst, const byte *zero, const byte *one,
  141. byte condition);
  142. WOLFSSL_LOCAL void fprime_add(byte *r, const byte *a, const byte *modulus);
  143. WOLFSSL_LOCAL void fprime_sub(byte *r, const byte *a, const byte *modulus);
  144. WOLFSSL_LOCAL void fprime_mul(byte *r, const byte *a, const byte *b,
  145. const byte *modulus);
  146. WOLFSSL_LOCAL void fprime_copy(byte *x, const byte *a);
  147. #endif /* CURVE25519_SMALL || ED25519_SMALL */
  148. #ifdef __cplusplus
  149. } /* extern "C" */
  150. #endif
  151. #endif /* HAVE_CURVE25519 || HAVE_ED25519 */
  152. #endif /* WOLF_CRYPT_FE_OPERATIONS_H */