ecc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifndef __ELL_ECC_H
  23. #define __ELL_ECC_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <sys/types.h> // for ssize_t
  28. #define L_ECC_MAX_DIGITS 6
  29. #define L_ECC_SCALAR_MAX_BYTES L_ECC_MAX_DIGITS * 8
  30. #define L_ECC_POINT_MAX_BYTES L_ECC_SCALAR_MAX_BYTES * 2
  31. struct l_ecc_curve;
  32. struct l_ecc_point;
  33. struct l_ecc_scalar;
  34. enum l_ecc_point_type {
  35. L_ECC_POINT_TYPE_COMPLIANT = 0x01,
  36. L_ECC_POINT_TYPE_COMPRESSED_BIT0 = 0x02,
  37. L_ECC_POINT_TYPE_COMPRESSED_BIT1 = 0x03,
  38. L_ECC_POINT_TYPE_FULL = 0x04,
  39. };
  40. const unsigned int *l_ecc_supported_ike_groups(void);
  41. const unsigned int *l_ecc_supported_tls_groups(void);
  42. const struct l_ecc_curve *l_ecc_curve_from_name(const char *name);
  43. const struct l_ecc_curve *l_ecc_curve_from_ike_group(unsigned int group);
  44. const struct l_ecc_curve *l_ecc_curve_from_tls_group(unsigned int group);
  45. const char *l_ecc_curve_get_name(const struct l_ecc_curve *curve);
  46. unsigned int l_ecc_curve_get_ike_group(const struct l_ecc_curve *curve);
  47. unsigned int l_ecc_curve_get_tls_group(const struct l_ecc_curve *curve);
  48. struct l_ecc_scalar *l_ecc_curve_get_order(const struct l_ecc_curve *curve);
  49. struct l_ecc_scalar *l_ecc_curve_get_prime(const struct l_ecc_curve *curve);
  50. size_t l_ecc_curve_get_scalar_bytes(const struct l_ecc_curve *curve);
  51. struct l_ecc_point *l_ecc_point_new(const struct l_ecc_curve *curve);
  52. struct l_ecc_point *l_ecc_point_from_data(const struct l_ecc_curve *curve,
  53. enum l_ecc_point_type type,
  54. const void *data, size_t len);
  55. struct l_ecc_point *l_ecc_point_from_sswu(const struct l_ecc_scalar *u);
  56. struct l_ecc_point *l_ecc_point_clone(const struct l_ecc_point *p);
  57. const struct l_ecc_curve *l_ecc_point_get_curve(const struct l_ecc_point *p);
  58. ssize_t l_ecc_point_get_x(const struct l_ecc_point *p, void *x, size_t xlen);
  59. ssize_t l_ecc_point_get_y(const struct l_ecc_point *p, void *y, size_t ylen);
  60. ssize_t l_ecc_point_get_data(const struct l_ecc_point *p, void *buf, size_t len);
  61. void l_ecc_point_free(struct l_ecc_point *p);
  62. struct l_ecc_scalar *l_ecc_scalar_new(const struct l_ecc_curve *curve,
  63. const void *buf, size_t len);
  64. struct l_ecc_scalar *l_ecc_scalar_new_random(
  65. const struct l_ecc_curve *curve);
  66. struct l_ecc_scalar *l_ecc_scalar_new_modp(const struct l_ecc_curve *curve,
  67. const void *buf, size_t len);
  68. struct l_ecc_scalar *l_ecc_scalar_new_reduced_1_to_n(
  69. const struct l_ecc_curve *curve,
  70. const void *buf, size_t len);
  71. ssize_t l_ecc_scalar_get_data(const struct l_ecc_scalar *c, void *buf,
  72. size_t len);
  73. void l_ecc_scalar_free(struct l_ecc_scalar *c);
  74. /* Constant operations */
  75. bool l_ecc_scalar_add(struct l_ecc_scalar *ret, const struct l_ecc_scalar *a,
  76. const struct l_ecc_scalar *b,
  77. const struct l_ecc_scalar *mod);
  78. /* Point operations */
  79. bool l_ecc_point_multiply(struct l_ecc_point *ret,
  80. const struct l_ecc_scalar *scalar,
  81. const struct l_ecc_point *point);
  82. bool l_ecc_point_add(struct l_ecc_point *ret, const struct l_ecc_point *a,
  83. const struct l_ecc_point *b);
  84. bool l_ecc_point_inverse(struct l_ecc_point *p);
  85. /* extra operations needed for SAE */
  86. bool l_ecc_scalar_multiply(struct l_ecc_scalar *ret,
  87. const struct l_ecc_scalar *a,
  88. const struct l_ecc_scalar *b);
  89. int l_ecc_scalar_legendre(struct l_ecc_scalar *value);
  90. bool l_ecc_scalar_sum_x(struct l_ecc_scalar *ret, const struct l_ecc_scalar *x);
  91. bool l_ecc_scalars_are_equal(const struct l_ecc_scalar *a,
  92. const struct l_ecc_scalar *b);
  93. bool l_ecc_points_are_equal(const struct l_ecc_point *a,
  94. const struct l_ecc_point *b);
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. #endif /* __ELL_ECC_H */