gfp_p256.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright 2016 Brian Smith.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include "ecp_nistz256.h"
  15. #include "../../limbs/limbs.h"
  16. #include "../../internal.h"
  17. #include "../bn/internal.h"
  18. #include "../../limbs/limbs.inl"
  19. typedef Limb Elem[P256_LIMBS];
  20. typedef Limb ScalarMont[P256_LIMBS];
  21. typedef Limb Scalar[P256_LIMBS];
  22. void GFp_p256_scalar_sqr_rep_mont(ScalarMont r, const ScalarMont a, Limb rep);
  23. #if defined(OPENSSL_ARM) || defined(OPENSSL_X86)
  24. void GFp_nistz256_sqr_mont(Elem r, const Elem a) {
  25. /* XXX: Inefficient. TODO: optimize with dedicated squaring routine. */
  26. GFp_nistz256_mul_mont(r, a, a);
  27. }
  28. #endif
  29. #if !defined(OPENSSL_X86_64)
  30. void GFp_p256_scalar_mul_mont(ScalarMont r, const ScalarMont a,
  31. const ScalarMont b) {
  32. static const BN_ULONG N[] = {
  33. TOBN(0xf3b9cac2, 0xfc632551),
  34. TOBN(0xbce6faad, 0xa7179e84),
  35. TOBN(0xffffffff, 0xffffffff),
  36. TOBN(0xffffffff, 0x00000000),
  37. };
  38. static const BN_ULONG N_N0[] = {
  39. BN_MONT_CTX_N0(0xccd1c8aa, 0xee00bc4f)
  40. };
  41. /* XXX: Inefficient. TODO: optimize with dedicated multiplication routine. */
  42. GFp_bn_mul_mont(r, a, b, N, N_N0, P256_LIMBS);
  43. }
  44. #endif
  45. #if defined(OPENSSL_X86_64)
  46. void GFp_p256_scalar_sqr_mont(ScalarMont r, const ScalarMont a) {
  47. GFp_p256_scalar_sqr_rep_mont(r, a, 1);
  48. }
  49. #else
  50. void GFp_p256_scalar_sqr_mont(ScalarMont r, const ScalarMont a) {
  51. GFp_p256_scalar_mul_mont(r, a, a);
  52. }
  53. void GFp_p256_scalar_sqr_rep_mont(ScalarMont r, const ScalarMont a, Limb rep) {
  54. dev_assert_secret(rep >= 1);
  55. GFp_p256_scalar_sqr_mont(r, a);
  56. for (Limb i = 1; i < rep; ++i) {
  57. GFp_p256_scalar_sqr_mont(r, r);
  58. }
  59. }
  60. #endif
  61. #if !defined(OPENSSL_X86_64)
  62. /* TODO(perf): Optimize these. */
  63. void GFp_nistz256_select_w5(P256_POINT *out, const P256_POINT table[16],
  64. crypto_word index) {
  65. dev_assert_secret(index >= 0);
  66. alignas(32) Elem x; limbs_zero(x, P256_LIMBS);
  67. alignas(32) Elem y; limbs_zero(y, P256_LIMBS);
  68. alignas(32) Elem z; limbs_zero(z, P256_LIMBS);
  69. // TODO: Rewrite in terms of |limbs_select|.
  70. for (size_t i = 0; i < 16; ++i) {
  71. crypto_word equal = constant_time_eq_w(index, (crypto_word)i + 1);
  72. for (size_t j = 0; j < P256_LIMBS; ++j) {
  73. x[j] = constant_time_select_w(equal, table[i].X[j], x[j]);
  74. y[j] = constant_time_select_w(equal, table[i].Y[j], y[j]);
  75. z[j] = constant_time_select_w(equal, table[i].Z[j], z[j]);
  76. }
  77. }
  78. limbs_copy(out->X, x, P256_LIMBS);
  79. limbs_copy(out->Y, y, P256_LIMBS);
  80. limbs_copy(out->Z, z, P256_LIMBS);
  81. }
  82. #if defined GFp_USE_LARGE_TABLE
  83. void GFp_nistz256_select_w7(P256_POINT_AFFINE *out,
  84. const PRECOMP256_ROW table, crypto_word index) {
  85. alignas(32) Limb xy[P256_LIMBS * 2];
  86. limbs_select(xy, table, P256_LIMBS * 2, 64, index - 1);
  87. limbs_copy(out->X, &xy[0], P256_LIMBS);
  88. limbs_copy(out->Y, &xy[P256_LIMBS], P256_LIMBS);
  89. }
  90. #endif
  91. #endif