nrf_sec.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_SEC_H__
  13. #define NRF_SEC_H__
  14. #include "nrf_svc.h"
  15. #include <stdint.h>
  16. #define NRF_SEC_SVC_BASE 0x8 /**< The lowest SVC number reserved for the nRF Security library. */
  17. /**@brief The SVC numbers used by the SVC functions in the security library. */
  18. enum NRF_SEC_SVCS
  19. {
  20. NRF_SEC_SVC_HASH = NRF_SEC_SVC_BASE, /**< SVC number for generating a digest using a hash function over a dataset. */
  21. NRF_SEC_SVC_VERIFY, /**< SVC number for veryfying a signature for a given dataset using provided keys and signature. */
  22. NRF_SEC_SVC_LAST
  23. };
  24. /**@brief Enumeration containing list of supported ECC curve and hash functions in the nRF Security library.
  25. */
  26. typedef enum
  27. {
  28. NRF_SEC_NIST256_SHA256 = 0x00, /**< Selection of NIST P-256 curve with SHA-256 digest for verification of signature. */
  29. NRF_SEC_ALGO_LAST /**< Last element in enumeration closing valid algorithm list. */
  30. }nrf_sec_algo_t;
  31. /**@brief Enumeration containing list of supported hash functions in the nRF Security library.
  32. */
  33. typedef enum
  34. {
  35. NRF_SEC_SHA256 = 0x00, /**< Selection of SHA-256 hash function. */
  36. NRF_SEC_HASH_FUNC_LAST /**< Last element in enumeration closing valid hash function list. */
  37. }nrf_sec_hash_func_t;
  38. /**@brief Structure with length of data and pointer to data for signing or verification.
  39. */
  40. typedef struct
  41. {
  42. uint32_t length; /**< Length of data pointed to by @ref p_data. */
  43. uint8_t * p_data; /**< Pointer to data to be used. */
  44. } nrf_sec_data_t;
  45. /**@brief Structure with pointers to X and Y coordinates of a point on the curve.
  46. */
  47. typedef struct
  48. {
  49. uint8_t * p_x; /**< Pointer to X coordinate of point. */
  50. uint32_t x_len; /**< Length of data pointed to by p_x. */
  51. uint8_t * p_y; /**< Pointer to X coordinate of point. */
  52. uint32_t y_len; /**< Length of data pointed to by p_x. */
  53. } nrf_sec_ecc_point_t;
  54. /**@brief Structure with pointers to r and s parts of the ecc signature.
  55. */
  56. typedef struct
  57. {
  58. uint8_t * p_r; /**< Pointer to R part of signature */
  59. uint32_t r_len; /**< Length of data pointed to by p_r */
  60. uint8_t * p_s; /**< Pointer to S part of signature */
  61. uint32_t s_len; /**< Length to data pointed to by p_s */
  62. } nrf_sec_ecc_signature_t;
  63. /**@brief SVC Function for verifying a signature over a given dataset.
  64. *
  65. * @param[in] p_data Pointer to the data which must be verified.
  66. * @param[in] p_Q Pointer to the public key, Q.
  67. * @param[in] p_signature Pointer to the signature (r and s).
  68. * @param[in] algorithm Curve and hash algorithm to be used for verifying the signature, for
  69. example NIST P-256 with SHA-256.
  70. *
  71. * @retval NRF_ERROR_INVALID_DATA If the signature does not match the dataset provided.
  72. * @retval NRF_ERROR_NOT_SUPPORTED If the selected algorithm is not supported in this version of
  73. the library.
  74. * @retval NRF_SUCCESS If the signature matches the dataset provided.
  75. */
  76. SVCALL(NRF_SEC_SVC_VERIFY, uint32_t, nrf_sec_svc_verify (nrf_sec_data_t * p_data,
  77. nrf_sec_ecc_point_t * p_Q,
  78. nrf_sec_ecc_signature_t * p_signature,
  79. nrf_sec_algo_t algorithm));
  80. /**@brief SVC Function for generating a signature over a given dataset.
  81. *
  82. * @param[in] p_data Pointer to the data which must be verified.
  83. * @param[out] p_digest Pointer to memory where generated digest shall be stored. Ensure
  84. * sufficient memory is available, that is for a SHA-256 there must
  85. * be at least 256 bits (8 words) available.
  86. * @param[in] hash_func Type of fash function to use when generating the digest
  87. *
  88. * @retval NRF_ERROR_NULL If a null pointer is provided as data or hash.
  89. * @retval NRF_ERROR_NOT_SUPPORTED If the selected hash functions is not supported in this version
  90. * of the library.
  91. * @retval NRF_SUCCESS If generation of a digest was successful.
  92. */
  93. SVCALL(NRF_SEC_SVC_HASH, uint32_t, nrf_sec_svc_hash(nrf_sec_data_t * p_data,
  94. uint8_t * p_digest,
  95. nrf_sec_hash_func_t hash_func));
  96. #endif // NRF_SEC_H__