field.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2014 Cryptography Research, Inc.
  4. *
  5. * Licensed under the OpenSSL license (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 gf_mul(gf_s * RESTRICT out, const gf a, const gf b);
  49. void gf_mulw_unsigned(gf_s * RESTRICT out, const gf a, uint32_t b);
  50. void 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. # include "f_impl.h" /* Bring in the inline implementations */
  59. # define LIMBPERM(i) (i)
  60. # define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
  61. static const gf ZERO = {{{0}}}, ONE = {{{1}}};
  62. /* Square x, n times. */
  63. static ossl_inline void gf_sqrn(gf_s * RESTRICT y, const gf x, int n)
  64. {
  65. gf tmp;
  66. assert(n > 0);
  67. if (n & 1) {
  68. gf_sqr(y, x);
  69. n--;
  70. } else {
  71. gf_sqr(tmp, x);
  72. gf_sqr(y, tmp);
  73. n -= 2;
  74. }
  75. for (; n; n -= 2) {
  76. gf_sqr(tmp, y);
  77. gf_sqr(y, tmp);
  78. }
  79. }
  80. # define gf_add_nr gf_add_RAW
  81. /* Subtract mod p. Bias by 2 and don't reduce */
  82. static ossl_inline void gf_sub_nr(gf c, const gf a, const gf b)
  83. {
  84. gf_sub_RAW(c, a, b);
  85. gf_bias(c, 2);
  86. if (GF_HEADROOM < 3)
  87. gf_weak_reduce(c);
  88. }
  89. /* Subtract mod p. Bias by amt but don't reduce. */
  90. static ossl_inline void gf_subx_nr(gf c, const gf a, const gf b, int amt)
  91. {
  92. gf_sub_RAW(c, a, b);
  93. gf_bias(c, amt);
  94. if (GF_HEADROOM < amt + 1)
  95. gf_weak_reduce(c);
  96. }
  97. /* Mul by signed int. Not constant-time WRT the sign of that int. */
  98. static ossl_inline void gf_mulw(gf c, const gf a, int32_t w)
  99. {
  100. if (w > 0) {
  101. gf_mulw_unsigned(c, a, w);
  102. } else {
  103. gf_mulw_unsigned(c, a, -w);
  104. gf_sub(c, ZERO, c);
  105. }
  106. }
  107. /* Constant time, x = is_z ? z : y */
  108. static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z)
  109. {
  110. size_t i;
  111. for (i = 0; i < NLIMBS; i++) {
  112. #if ARCH_WORD_BITS == 32
  113. x[0].limb[i] = constant_time_select_32(is_z, z[0].limb[i],
  114. y[0].limb[i]);
  115. #else
  116. /* Must be 64 bit */
  117. x[0].limb[i] = constant_time_select_64(is_z, z[0].limb[i],
  118. y[0].limb[i]);
  119. #endif
  120. }
  121. }
  122. /* Constant time, if (neg) x=-x; */
  123. static ossl_inline void gf_cond_neg(gf x, mask_t neg)
  124. {
  125. gf y;
  126. gf_sub(y, ZERO, x);
  127. gf_cond_sel(x, x, y, neg);
  128. }
  129. /* Constant time, if (swap) (x,y) = (y,x); */
  130. static ossl_inline void gf_cond_swap(gf x, gf_s * RESTRICT y, mask_t swap)
  131. {
  132. size_t i;
  133. for (i = 0; i < NLIMBS; i++) {
  134. #if ARCH_WORD_BITS == 32
  135. constant_time_cond_swap_32(swap, &(x[0].limb[i]), &(y->limb[i]));
  136. #else
  137. /* Must be 64 bit */
  138. constant_time_cond_swap_64(swap, &(x[0].limb[i]), &(y->limb[i]));
  139. #endif
  140. }
  141. }
  142. #endif /* OSSL_CRYPTO_EC_CURVE448_FIELD_H */