scalar.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2015-2016 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. #include <openssl/crypto.h>
  13. #include "word.h"
  14. #include "point_448.h"
  15. static const c448_word_t MONTGOMERY_FACTOR = (c448_word_t) 0x3bd440fae918bc5ULL;
  16. static const curve448_scalar_t sc_p = {
  17. {
  18. {
  19. SC_LIMB(0x2378c292ab5844f3ULL), SC_LIMB(0x216cc2728dc58f55ULL),
  20. SC_LIMB(0xc44edb49aed63690ULL), SC_LIMB(0xffffffff7cca23e9ULL),
  21. SC_LIMB(0xffffffffffffffffULL), SC_LIMB(0xffffffffffffffffULL),
  22. SC_LIMB(0x3fffffffffffffffULL)
  23. }
  24. }
  25. }, sc_r2 = {
  26. {
  27. {
  28. SC_LIMB(0xe3539257049b9b60ULL), SC_LIMB(0x7af32c4bc1b195d9ULL),
  29. SC_LIMB(0x0d66de2388ea1859ULL), SC_LIMB(0xae17cf725ee4d838ULL),
  30. SC_LIMB(0x1a9cc14ba3c47c44ULL), SC_LIMB(0x2052bcb7e4d070afULL),
  31. SC_LIMB(0x3402a939f823b729ULL)
  32. }
  33. }
  34. };
  35. #define WBITS C448_WORD_BITS /* NB this may be different from ARCH_WORD_BITS */
  36. const curve448_scalar_t ossl_curve448_scalar_one = {{{1}}};
  37. const curve448_scalar_t ossl_curve448_scalar_zero = {{{0}}};
  38. /*
  39. * {extra,accum} - sub +? p
  40. * Must have extra <= 1
  41. */
  42. static void sc_subx(curve448_scalar_t out,
  43. const c448_word_t accum[C448_SCALAR_LIMBS],
  44. const curve448_scalar_t sub,
  45. const curve448_scalar_t p, c448_word_t extra)
  46. {
  47. c448_dsword_t chain = 0;
  48. unsigned int i;
  49. c448_word_t borrow;
  50. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  51. chain = (chain + accum[i]) - sub->limb[i];
  52. out->limb[i] = (c448_word_t)chain;
  53. chain >>= WBITS;
  54. }
  55. borrow = (c448_word_t)chain + extra; /* = 0 or -1 */
  56. chain = 0;
  57. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  58. chain = (chain + out->limb[i]) + (p->limb[i] & borrow);
  59. out->limb[i] = (c448_word_t)chain;
  60. chain >>= WBITS;
  61. }
  62. }
  63. static void sc_montmul(curve448_scalar_t out, const curve448_scalar_t a,
  64. const curve448_scalar_t b)
  65. {
  66. unsigned int i, j;
  67. c448_word_t accum[C448_SCALAR_LIMBS + 1] = { 0 };
  68. c448_word_t hi_carry = 0;
  69. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  70. c448_word_t mand = a->limb[i];
  71. const c448_word_t *mier = b->limb;
  72. c448_dword_t chain = 0;
  73. for (j = 0; j < C448_SCALAR_LIMBS; j++) {
  74. chain += ((c448_dword_t) mand) * mier[j] + accum[j];
  75. accum[j] = (c448_word_t)chain;
  76. chain >>= WBITS;
  77. }
  78. accum[j] = (c448_word_t)chain;
  79. mand = accum[0] * MONTGOMERY_FACTOR;
  80. chain = 0;
  81. mier = sc_p->limb;
  82. for (j = 0; j < C448_SCALAR_LIMBS; j++) {
  83. chain += (c448_dword_t) mand *mier[j] + accum[j];
  84. if (j)
  85. accum[j - 1] = (c448_word_t)chain;
  86. chain >>= WBITS;
  87. }
  88. chain += accum[j];
  89. chain += hi_carry;
  90. accum[j - 1] = (c448_word_t)chain;
  91. hi_carry = chain >> WBITS;
  92. }
  93. sc_subx(out, accum, sc_p, sc_p, hi_carry);
  94. }
  95. void ossl_curve448_scalar_mul(curve448_scalar_t out, const curve448_scalar_t a,
  96. const curve448_scalar_t b)
  97. {
  98. sc_montmul(out, a, b);
  99. sc_montmul(out, out, sc_r2);
  100. }
  101. void
  102. ossl_curve448_scalar_sub(curve448_scalar_t out, const curve448_scalar_t a,
  103. const curve448_scalar_t b)
  104. {
  105. sc_subx(out, a->limb, b, sc_p, 0);
  106. }
  107. void
  108. ossl_curve448_scalar_add(curve448_scalar_t out, const curve448_scalar_t a,
  109. const curve448_scalar_t b)
  110. {
  111. c448_dword_t chain = 0;
  112. unsigned int i;
  113. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  114. chain = (chain + a->limb[i]) + b->limb[i];
  115. out->limb[i] = (c448_word_t)chain;
  116. chain >>= WBITS;
  117. }
  118. sc_subx(out, out->limb, sc_p, sc_p, (c448_word_t)chain);
  119. }
  120. static ossl_inline void scalar_decode_short(curve448_scalar_t s,
  121. const unsigned char *ser,
  122. size_t nbytes)
  123. {
  124. size_t i, j, k = 0;
  125. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  126. c448_word_t out = 0;
  127. for (j = 0; j < sizeof(c448_word_t) && k < nbytes; j++, k++)
  128. out |= ((c448_word_t) ser[k]) << (8 * j);
  129. s->limb[i] = out;
  130. }
  131. }
  132. c448_error_t
  133. ossl_curve448_scalar_decode(curve448_scalar_t s,
  134. const unsigned char ser[C448_SCALAR_BYTES])
  135. {
  136. unsigned int i;
  137. c448_dsword_t accum = 0;
  138. scalar_decode_short(s, ser, C448_SCALAR_BYTES);
  139. for (i = 0; i < C448_SCALAR_LIMBS; i++)
  140. accum = (accum + s->limb[i] - sc_p->limb[i]) >> WBITS;
  141. /* Here accum == 0 or -1 */
  142. ossl_curve448_scalar_mul(s, s, ossl_curve448_scalar_one); /* ham-handed reduce */
  143. return c448_succeed_if(~word_is_zero((uint32_t)accum));
  144. }
  145. void ossl_curve448_scalar_destroy(curve448_scalar_t scalar)
  146. {
  147. OPENSSL_cleanse(scalar, sizeof(curve448_scalar_t));
  148. }
  149. void
  150. ossl_curve448_scalar_decode_long(curve448_scalar_t s,
  151. const unsigned char *ser, size_t ser_len)
  152. {
  153. size_t i;
  154. curve448_scalar_t t1, t2;
  155. if (ser_len == 0) {
  156. curve448_scalar_copy(s, ossl_curve448_scalar_zero);
  157. return;
  158. }
  159. i = ser_len - (ser_len % C448_SCALAR_BYTES);
  160. if (i == ser_len)
  161. i -= C448_SCALAR_BYTES;
  162. scalar_decode_short(t1, &ser[i], ser_len - i);
  163. if (ser_len == sizeof(curve448_scalar_t)) {
  164. assert(i == 0);
  165. /* ham-handed reduce */
  166. ossl_curve448_scalar_mul(s, t1, ossl_curve448_scalar_one);
  167. ossl_curve448_scalar_destroy(t1);
  168. return;
  169. }
  170. while (i) {
  171. i -= C448_SCALAR_BYTES;
  172. sc_montmul(t1, t1, sc_r2);
  173. (void)ossl_curve448_scalar_decode(t2, ser + i);
  174. ossl_curve448_scalar_add(t1, t1, t2);
  175. }
  176. curve448_scalar_copy(s, t1);
  177. ossl_curve448_scalar_destroy(t1);
  178. ossl_curve448_scalar_destroy(t2);
  179. }
  180. void
  181. ossl_curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
  182. const curve448_scalar_t s)
  183. {
  184. unsigned int i, j, k = 0;
  185. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  186. for (j = 0; j < sizeof(c448_word_t); j++, k++)
  187. ser[k] = s->limb[i] >> (8 * j);
  188. }
  189. }
  190. void
  191. ossl_curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a)
  192. {
  193. c448_word_t mask = 0 - (a->limb[0] & 1);
  194. c448_dword_t chain = 0;
  195. unsigned int i;
  196. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  197. chain = (chain + a->limb[i]) + (sc_p->limb[i] & mask);
  198. out->limb[i] = (c448_word_t)chain;
  199. chain >>= C448_WORD_BITS;
  200. }
  201. for (i = 0; i < C448_SCALAR_LIMBS - 1; i++)
  202. out->limb[i] = out->limb[i] >> 1 | out->limb[i + 1] << (WBITS - 1);
  203. out->limb[i] = out->limb[i] >> 1 | (c448_word_t)(chain << (WBITS - 1));
  204. }