scalar.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2015-2016 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. #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 curve448_scalar_one = {{{1}}};
  37. const curve448_scalar_t 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 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 curve448_scalar_sub(curve448_scalar_t out, const curve448_scalar_t a,
  102. const curve448_scalar_t b)
  103. {
  104. sc_subx(out, a->limb, b, sc_p, 0);
  105. }
  106. void curve448_scalar_add(curve448_scalar_t out, const curve448_scalar_t a,
  107. const curve448_scalar_t b)
  108. {
  109. c448_dword_t chain = 0;
  110. unsigned int i;
  111. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  112. chain = (chain + a->limb[i]) + b->limb[i];
  113. out->limb[i] = (c448_word_t)chain;
  114. chain >>= WBITS;
  115. }
  116. sc_subx(out, out->limb, sc_p, sc_p, (c448_word_t)chain);
  117. }
  118. static ossl_inline void scalar_decode_short(curve448_scalar_t s,
  119. const unsigned char *ser,
  120. size_t nbytes)
  121. {
  122. size_t i, j, k = 0;
  123. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  124. c448_word_t out = 0;
  125. for (j = 0; j < sizeof(c448_word_t) && k < nbytes; j++, k++)
  126. out |= ((c448_word_t) ser[k]) << (8 * j);
  127. s->limb[i] = out;
  128. }
  129. }
  130. c448_error_t curve448_scalar_decode(
  131. curve448_scalar_t s,
  132. const unsigned char ser[C448_SCALAR_BYTES])
  133. {
  134. unsigned int i;
  135. c448_dsword_t accum = 0;
  136. scalar_decode_short(s, ser, C448_SCALAR_BYTES);
  137. for (i = 0; i < C448_SCALAR_LIMBS; i++)
  138. accum = (accum + s->limb[i] - sc_p->limb[i]) >> WBITS;
  139. /* Here accum == 0 or -1 */
  140. curve448_scalar_mul(s, s, curve448_scalar_one); /* ham-handed reduce */
  141. return c448_succeed_if(~word_is_zero((uint32_t)accum));
  142. }
  143. void curve448_scalar_destroy(curve448_scalar_t scalar)
  144. {
  145. OPENSSL_cleanse(scalar, sizeof(curve448_scalar_t));
  146. }
  147. void curve448_scalar_decode_long(curve448_scalar_t s,
  148. const unsigned char *ser, size_t ser_len)
  149. {
  150. size_t i;
  151. curve448_scalar_t t1, t2;
  152. if (ser_len == 0) {
  153. curve448_scalar_copy(s, curve448_scalar_zero);
  154. return;
  155. }
  156. i = ser_len - (ser_len % C448_SCALAR_BYTES);
  157. if (i == ser_len)
  158. i -= C448_SCALAR_BYTES;
  159. scalar_decode_short(t1, &ser[i], ser_len - i);
  160. if (ser_len == sizeof(curve448_scalar_t)) {
  161. assert(i == 0);
  162. /* ham-handed reduce */
  163. curve448_scalar_mul(s, t1, curve448_scalar_one);
  164. curve448_scalar_destroy(t1);
  165. return;
  166. }
  167. while (i) {
  168. i -= C448_SCALAR_BYTES;
  169. sc_montmul(t1, t1, sc_r2);
  170. (void)curve448_scalar_decode(t2, ser + i);
  171. curve448_scalar_add(t1, t1, t2);
  172. }
  173. curve448_scalar_copy(s, t1);
  174. curve448_scalar_destroy(t1);
  175. curve448_scalar_destroy(t2);
  176. }
  177. void curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
  178. const curve448_scalar_t s)
  179. {
  180. unsigned int i, j, k = 0;
  181. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  182. for (j = 0; j < sizeof(c448_word_t); j++, k++)
  183. ser[k] = s->limb[i] >> (8 * j);
  184. }
  185. }
  186. void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a)
  187. {
  188. c448_word_t mask = 0 - (a->limb[0] & 1);
  189. c448_dword_t chain = 0;
  190. unsigned int i;
  191. for (i = 0; i < C448_SCALAR_LIMBS; i++) {
  192. chain = (chain + a->limb[i]) + (sc_p->limb[i] & mask);
  193. out->limb[i] = (c448_word_t)chain;
  194. chain >>= C448_WORD_BITS;
  195. }
  196. for (i = 0; i < C448_SCALAR_LIMBS - 1; i++)
  197. out->limb[i] = out->limb[i] >> 1 | out->limb[i + 1] << (WBITS - 1);
  198. out->limb[i] = out->limb[i] >> 1 | (c448_word_t)(chain << (WBITS - 1));
  199. }