2
0

field.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2014 Cryptography Research, Inc.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Mike Hamburg
  11. */
  12. #ifndef OSSL_CRYPTO_EC_CURVE448_FIELD_H
  13. # define OSSL_CRYPTO_EC_CURVE448_FIELD_H
  14. # include "internal/constant_time.h"
  15. # include <string.h>
  16. # include <assert.h>
  17. # include "word.h"
  18. # define NLIMBS (64/sizeof(word_t))
  19. # define X_SER_BYTES 56
  20. # define SER_BYTES 56
  21. # if defined(__GNUC__) || defined(__clang__)
  22. # define INLINE_UNUSED __inline__ __attribute__((__unused__,__always_inline__))
  23. # define RESTRICT __restrict__
  24. # define ALIGNED __attribute__((__aligned__(16)))
  25. # else
  26. # define INLINE_UNUSED ossl_inline
  27. # define RESTRICT
  28. # define ALIGNED
  29. # endif
  30. typedef struct gf_s {
  31. word_t limb[NLIMBS];
  32. } ALIGNED gf_s, gf[1];
  33. /* RFC 7748 support */
  34. # define X_PUBLIC_BYTES X_SER_BYTES
  35. # define X_PRIVATE_BYTES X_PUBLIC_BYTES
  36. # define X_PRIVATE_BITS 448
  37. static INLINE_UNUSED void gf_copy(gf out, const gf a)
  38. {
  39. *out = *a;
  40. }
  41. static INLINE_UNUSED void gf_add_RAW(gf out, const gf a, const gf b);
  42. static INLINE_UNUSED void gf_sub_RAW(gf out, const gf a, const gf b);
  43. static INLINE_UNUSED void gf_bias(gf inout, int amount);
  44. static INLINE_UNUSED void gf_weak_reduce(gf inout);
  45. void gf_strong_reduce(gf inout);
  46. void gf_add(gf out, const gf a, const gf b);
  47. void gf_sub(gf out, const gf a, const gf b);
  48. void ossl_gf_mul(gf_s * RESTRICT out, const gf a, const gf b);
  49. void ossl_gf_mulw_unsigned(gf_s * RESTRICT out, const gf a, uint32_t b);
  50. void ossl_gf_sqr(gf_s * RESTRICT out, const gf a);
  51. mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0. Return true if successful */
  52. mask_t gf_eq(const gf x, const gf y);
  53. mask_t gf_lobit(const gf x);
  54. mask_t gf_hibit(const gf x);
  55. void gf_serialize(uint8_t serial[SER_BYTES], const gf x, int with_highbit);
  56. mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
  57. uint8_t hi_nmask);
  58. # define LIMBPERM(i) (i)
  59. # if (ARCH_WORD_BITS == 32)
  60. # include "arch_32/f_impl.h" /* Bring in the inline implementations */
  61. # define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
  62. # elif (ARCH_WORD_BITS == 64)
  63. # include "arch_64/f_impl.h" /* Bring in the inline implementations */
  64. # define LIMB_MASK(i) (((1ULL)<<LIMB_PLACE_VALUE(i))-1)
  65. # endif
  66. static const gf ZERO = {{{0}}}, ONE = {{{1}}};
  67. /* Square x, n times. */
  68. static ossl_inline void gf_sqrn(gf_s * RESTRICT y, const gf x, int n)
  69. {
  70. gf tmp;
  71. assert(n > 0);
  72. if (n & 1) {
  73. ossl_gf_sqr(y, x);
  74. n--;
  75. } else {
  76. ossl_gf_sqr(tmp, x);
  77. ossl_gf_sqr(y, tmp);
  78. n -= 2;
  79. }
  80. for (; n; n -= 2) {
  81. ossl_gf_sqr(tmp, y);
  82. ossl_gf_sqr(y, tmp);
  83. }
  84. }
  85. # define gf_add_nr gf_add_RAW
  86. /* Subtract mod p. Bias by 2 and don't reduce */
  87. static ossl_inline void gf_sub_nr(gf c, const gf a, const gf b)
  88. {
  89. gf_sub_RAW(c, a, b);
  90. gf_bias(c, 2);
  91. if (GF_HEADROOM < 3)
  92. gf_weak_reduce(c);
  93. }
  94. /* Subtract mod p. Bias by amt but don't reduce. */
  95. static ossl_inline void gf_subx_nr(gf c, const gf a, const gf b, int amt)
  96. {
  97. gf_sub_RAW(c, a, b);
  98. gf_bias(c, amt);
  99. if (GF_HEADROOM < amt + 1)
  100. gf_weak_reduce(c);
  101. }
  102. /* Mul by signed int. Not constant-time WRT the sign of that int. */
  103. static ossl_inline void gf_mulw(gf c, const gf a, int32_t w)
  104. {
  105. if (w > 0) {
  106. ossl_gf_mulw_unsigned(c, a, w);
  107. } else {
  108. ossl_gf_mulw_unsigned(c, a, -w);
  109. gf_sub(c, ZERO, c);
  110. }
  111. }
  112. /* Constant time, x = is_z ? z : y */
  113. static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z)
  114. {
  115. size_t i;
  116. for (i = 0; i < NLIMBS; i++) {
  117. #if ARCH_WORD_BITS == 32
  118. x[0].limb[i] = constant_time_select_32(is_z, z[0].limb[i],
  119. y[0].limb[i]);
  120. #else
  121. /* Must be 64 bit */
  122. x[0].limb[i] = constant_time_select_64(is_z, z[0].limb[i],
  123. y[0].limb[i]);
  124. #endif
  125. }
  126. }
  127. /* Constant time, if (neg) x=-x; */
  128. static ossl_inline void gf_cond_neg(gf x, mask_t neg)
  129. {
  130. gf y;
  131. gf_sub(y, ZERO, x);
  132. gf_cond_sel(x, x, y, neg);
  133. }
  134. /* Constant time, if (swap) (x,y) = (y,x); */
  135. static ossl_inline void gf_cond_swap(gf x, gf_s * RESTRICT y, mask_t swap)
  136. {
  137. size_t i;
  138. for (i = 0; i < NLIMBS; i++) {
  139. #if ARCH_WORD_BITS == 32
  140. constant_time_cond_swap_32(swap, &(x[0].limb[i]), &(y->limb[i]));
  141. #else
  142. /* Must be 64 bit */
  143. constant_time_cond_swap_64(swap, &(x[0].limb[i]), &(y->limb[i]));
  144. #endif
  145. }
  146. }
  147. #endif /* OSSL_CRYPTO_EC_CURVE448_FIELD_H */