ecc-private.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2018 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include "ecc.h"
  25. #include "util.h"
  26. struct l_ecc_curve;
  27. struct l_ecc_point {
  28. uint64_t x[L_ECC_MAX_DIGITS];
  29. uint64_t y[L_ECC_MAX_DIGITS];
  30. const struct l_ecc_curve *curve;
  31. };
  32. struct l_ecc_curve {
  33. unsigned int ndigits;
  34. unsigned int ike_group;
  35. unsigned int tls_group;
  36. const char *name;
  37. struct l_ecc_point g;
  38. uint64_t p[L_ECC_MAX_DIGITS];
  39. uint64_t n[L_ECC_MAX_DIGITS];
  40. uint64_t b[L_ECC_MAX_DIGITS];
  41. int z;
  42. };
  43. struct l_ecc_scalar {
  44. uint64_t c[L_ECC_MAX_DIGITS];
  45. const struct l_ecc_curve *curve;
  46. };
  47. /*
  48. * Performs a secure memory comparison of two uint64_t buffers of size bytes
  49. * representing an integer. Blobs are ordered in little endian. It returns
  50. * a negative, zero or positif value if a < b, a == b or a > b respectively.
  51. */
  52. static inline int secure_memcmp_64(const uint64_t *a, const uint64_t *b,
  53. size_t size)
  54. {
  55. uint64_t aa_64, bb_64;
  56. int res = 0, mask;
  57. size_t i = 0;
  58. if (size) {
  59. /*
  60. * Arrays store blobs in LE, we will process each blob as a
  61. * byte array of size 8 using l_secure_memcmp. We need to make
  62. * sure to feed a BE byte array to avoid unexpected behavior
  63. * on different architectures.
  64. */
  65. do {
  66. aa_64 = L_CPU_TO_BE64(a[i]);
  67. bb_64 = L_CPU_TO_BE64(b[i]);
  68. mask = l_secure_memcmp(&aa_64, &bb_64, 8);
  69. res = (mask & res) | mask;
  70. i++;
  71. } while (i != size);
  72. }
  73. return res;
  74. }
  75. void _ecc_be2native(uint64_t *dest, const uint64_t *bytes,
  76. unsigned int ndigits);
  77. void _ecc_native2be(uint64_t *dest, const uint64_t *native,
  78. unsigned int ndigits);
  79. void _vli_mod_inv(uint64_t *result, const uint64_t *input, const uint64_t *mod,
  80. unsigned int ndigits);
  81. void _vli_mod_sub(uint64_t *result, const uint64_t *left, const uint64_t *right,
  82. const uint64_t *mod, unsigned int ndigits);
  83. void _vli_mod_add(uint64_t *result, const uint64_t *left, const uint64_t *right,
  84. const uint64_t *mod, unsigned int ndigits);
  85. void _vli_rshift1(uint64_t *vli, unsigned int ndigits);
  86. bool _vli_mmod_fast(uint64_t *result, uint64_t *product,
  87. const uint64_t *curve_prime, unsigned int ndigits);
  88. void _vli_mod_mult_fast(uint64_t *result, const uint64_t *left,
  89. const uint64_t *right, const uint64_t *curve_prime,
  90. unsigned int ndigits);
  91. void _vli_mod_square_fast(uint64_t *result, const uint64_t *left,
  92. const uint64_t *curve_prime,
  93. unsigned int ndigits);
  94. void _vli_mod_exp(uint64_t *result, const uint64_t *base, const uint64_t *exp,
  95. const uint64_t *mod, unsigned int ndigits);
  96. int _vli_cmp(const uint64_t *left, const uint64_t *right, unsigned int ndigits);
  97. bool _vli_is_zero_or_one(const uint64_t *vli, unsigned int ndigits);
  98. uint64_t _vli_add(uint64_t *result, const uint64_t *left,
  99. const uint64_t *right, unsigned int ndigits);
  100. uint64_t _vli_sub(uint64_t *result, const uint64_t *left,
  101. const uint64_t *right, unsigned int ndigits);
  102. int _vli_legendre(uint64_t *val, const uint64_t *p, unsigned int ndigits);
  103. bool _ecc_point_is_zero(const struct l_ecc_point *point);
  104. void _ecc_calculate_p2(const struct l_ecc_curve *curve, uint64_t *p2);
  105. bool _ecc_compute_y(const struct l_ecc_curve *curve, uint64_t *y,
  106. const uint64_t *x);
  107. void _ecc_point_mult(struct l_ecc_point *result,
  108. const struct l_ecc_point *point, const uint64_t *scalar,
  109. uint64_t *initial_z, const uint64_t *curve_prime);
  110. void _ecc_point_add(struct l_ecc_point *ret, const struct l_ecc_point *p,
  111. const struct l_ecc_point *q,
  112. const uint64_t *curve_prime);
  113. struct l_ecc_scalar *_ecc_constant_new(const struct l_ecc_curve *curve,
  114. const void *buf, size_t len);