ecc_impl.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #define P256_LEN (256/8)
  13. #define P192_LEN (192/8)
  14. /* Note: x & y are stored in little endian order (same as CPU byte order, and order used internally by most libraries).
  15. This is the same order used in hardware
  16. Note this is opposite to most byte string formats used to represent keys, which are often big endian
  17. */
  18. typedef struct {
  19. uint8_t x[P256_LEN]; /* Little endian order */
  20. uint8_t y[P256_LEN]; /* Little endian order */
  21. unsigned len; /* P192_LEN or P256_LEN */
  22. } ecc_point_t;
  23. /**
  24. * @brief Perform ECC point multiplication (R = K * (Px, Py))
  25. *
  26. * @param point ECC point (multiplicand)
  27. * @param scalar Integer represented in byte array format (multiplier)
  28. * @param result Result of the multiplication
  29. * @param verify_first Verify that the point is on the curve before performing multiplication
  30. *
  31. * @return - 0 if the multiplication was successful
  32. * - -1 otherwise
  33. *
  34. * @note 'scalar' is expected as a byte array in little endian order.
  35. * Most byte string formats used to represent keys are in big endian order.
  36. */
  37. int esp_ecc_point_multiply(const ecc_point_t *point, const uint8_t *scalar, ecc_point_t *result, bool verify_first);
  38. /**
  39. * @brief Perform ECC point verification,
  40. * i.e check whether the point (Px, Py) lies on the curve
  41. *
  42. * @param point ECC point that needs to be verified
  43. *
  44. * @return - 1, if point lies on the curve
  45. * - 0, otherwise
  46. *
  47. */
  48. int esp_ecc_point_verify(const ecc_point_t *point);
  49. #ifdef __cplusplus
  50. }
  51. #endif