esp_ds.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include "esp_hmac.h"
  9. #include "esp_err.h"
  10. #include "esp_ds_err.h"
  11. #include "soc/soc_caps.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #define ESP_DS_IV_BIT_LEN 128
  16. #define ESP_DS_IV_LEN (ESP_DS_IV_BIT_LEN / 8)
  17. #define ESP_DS_SIGNATURE_MAX_BIT_LEN SOC_RSA_MAX_BIT_LEN
  18. #define ESP_DS_SIGNATURE_MD_BIT_LEN 256
  19. #define ESP_DS_SIGNATURE_M_PRIME_BIT_LEN 32
  20. #define ESP_DS_SIGNATURE_L_BIT_LEN 32
  21. #define ESP_DS_SIGNATURE_PADDING_BIT_LEN 64
  22. /* Length of parameter 'C' stored in flash, in bytes
  23. - Operands Y, M and r_bar; each equal to maximum RSA bit length
  24. - Operand MD (message digest); 256 bits
  25. - Operands M' and L; each 32 bits
  26. - Operand beta (padding value; 64 bits
  27. */
  28. #define ESP_DS_C_LEN (((ESP_DS_SIGNATURE_MAX_BIT_LEN * 3 \
  29. + ESP_DS_SIGNATURE_MD_BIT_LEN \
  30. + ESP_DS_SIGNATURE_M_PRIME_BIT_LEN \
  31. + ESP_DS_SIGNATURE_L_BIT_LEN \
  32. + ESP_DS_SIGNATURE_PADDING_BIT_LEN) / 8))
  33. typedef struct esp_ds_context esp_ds_context_t;
  34. typedef enum {
  35. ESP_DS_RSA_1024 = (1024 / 32) - 1,
  36. ESP_DS_RSA_2048 = (2048 / 32) - 1,
  37. ESP_DS_RSA_3072 = (3072 / 32) - 1,
  38. ESP_DS_RSA_4096 = (4096 / 32) - 1
  39. } esp_digital_signature_length_t;
  40. /**
  41. * Encrypted private key data. Recommended to store in flash in this format.
  42. *
  43. * @note This struct has to match to one from the ROM code! This documentation is mostly taken from there.
  44. */
  45. typedef struct esp_digital_signature_data {
  46. /**
  47. * RSA LENGTH register parameters
  48. * (number of words in RSA key & operands, minus one).
  49. *
  50. * This value must match the length field encrypted and stored in 'c',
  51. * or invalid results will be returned. (The DS peripheral will
  52. * always use the value in 'c', not this value, so an attacker can't
  53. * alter the DS peripheral results this way, it will just truncate or
  54. * extend the message and the resulting signature in software.)
  55. *
  56. * @note In IDF, the enum type length is the same as of type unsigned, so they can be used interchangably.
  57. * See the ROM code for the original declaration of struct \c ets_ds_data_t.
  58. */
  59. esp_digital_signature_length_t rsa_length;
  60. /**
  61. * IV value used to encrypt 'c'
  62. */
  63. uint32_t iv[ESP_DS_IV_BIT_LEN / 32];
  64. /**
  65. * Encrypted Digital Signature parameters. Result of AES-CBC encryption
  66. * of plaintext values. Includes an encrypted message digest.
  67. */
  68. uint8_t c[ESP_DS_C_LEN];
  69. } esp_ds_data_t;
  70. /**
  71. * Plaintext parameters used by Digital Signature.
  72. *
  73. * This is only used for encrypting the RSA parameters by calling esp_ds_encrypt_params().
  74. * Afterwards, the result can be stored in flash or in other persistent memory.
  75. * The encryption is a prerequisite step before any signature operation can be done.
  76. *
  77. * @note
  78. * Y, M, Rb, & M_Prime must all be in little endian format.
  79. */
  80. typedef struct {
  81. uint32_t Y[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA exponent
  82. uint32_t M[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA modulus
  83. uint32_t Rb[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA r inverse operand
  84. uint32_t M_prime; //!< RSA M prime operand
  85. uint32_t length; //!< RSA length in words (32 bit)
  86. } esp_ds_p_data_t;
  87. /**
  88. * @brief Sign the message with a hardware key from specific key slot.
  89. * The function calculates a plain RSA signature with help of the DS peripheral.
  90. * The RSA encryption operation is as follows:
  91. * Z = XY mod M where,
  92. * Z is the signature, X is the input message,
  93. * Y and M are the RSA private key parameters.
  94. *
  95. * This function is a wrapper around \c esp_ds_finish_sign() and \c esp_ds_start_sign(), so do not use them
  96. * in parallel.
  97. * It blocks until the signing is finished and then returns the signature.
  98. *
  99. * @note
  100. * Please see note section of \c esp_ds_start_sign() for more details about the input parameters.
  101. *
  102. * @param message the message to be signed; its length should be (data->rsa_length + 1)*4 bytes, and those
  103. bytes must be in little endian format. It is your responsibility to apply your hash function
  104. and padding before calling this function, if required. (e.g. message = padding(hash(inputMsg)))
  105. * @param data the encrypted signing key data (AES encrypted RSA key + IV)
  106. * @param key_id the HMAC key ID determining the HMAC key of the HMAC which will be used to decrypt the
  107. * signing key data
  108. * @param signature the destination of the signature, should be (data->rsa_length + 1)*4 bytes long
  109. *
  110. * @return
  111. * - ESP_OK if successful, the signature was written to the parameter \c signature.
  112. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or data->rsa_length is too long or 0
  113. * - ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL if there was an HMAC failure during retrieval of the decryption key
  114. * - ESP_ERR_NO_MEM if there hasn't been enough memory to allocate the context object
  115. * - ESP_ERR_HW_CRYPTO_DS_INVALID_KEY if there's a problem with passing the HMAC key to the DS component
  116. * - ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST if the message digest didn't match; the signature is invalid.
  117. * - ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING if the message padding is incorrect, the signature can be read though
  118. * since the message digest matches.
  119. */
  120. esp_err_t esp_ds_sign(const void *message,
  121. const esp_ds_data_t *data,
  122. hmac_key_id_t key_id,
  123. void *signature);
  124. /**
  125. * @brief Start the signing process.
  126. *
  127. * This function yields a context object which needs to be passed to \c esp_ds_finish_sign() to finish the signing
  128. * process.
  129. * The function calculates a plain RSA signature with help of the DS peripheral.
  130. * The RSA encryption operation is as follows:
  131. * Z = XY mod M where,
  132. * Z is the signature, X is the input message,
  133. * Y and M are the RSA private key parameters.
  134. *
  135. * @note
  136. * This function locks the HMAC, SHA, AES and RSA components, so the user has to ensure to call
  137. * \c esp_ds_finish_sign() in a timely manner.
  138. * The numbers Y, M, Rb which are a part of esp_ds_data_t should be provided in little endian format
  139. * and should be of length equal to the RSA private key bit length
  140. * The message length in bits should also be equal to the RSA private key bit length.
  141. * No padding is applied to the message automatically, Please ensure the message is appropriate padded before
  142. * calling the API.
  143. *
  144. * @param message the message to be signed; its length should be (data->rsa_length + 1)*4 bytes, and those
  145. bytes must be in little endian format. It is your responsibility to apply your hash function
  146. and padding before calling this function, if required. (e.g. message = padding(hash(inputMsg)))
  147. * @param data the encrypted signing key data (AES encrypted RSA key + IV)
  148. * @param key_id the HMAC key ID determining the HMAC key of the HMAC which will be used to decrypt the
  149. * signing key data
  150. * @param esp_ds_ctx the context object which is needed for finishing the signing process later
  151. *
  152. * @return
  153. * - ESP_OK if successful, the ds operation was started now and has to be finished with \c esp_ds_finish_sign()
  154. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or data->rsa_length is too long or 0
  155. * - ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL if there was an HMAC failure during retrieval of the decryption key
  156. * - ESP_ERR_NO_MEM if there hasn't been enough memory to allocate the context object
  157. * - ESP_ERR_HW_CRYPTO_DS_INVALID_KEY if there's a problem with passing the HMAC key to the DS component
  158. */
  159. esp_err_t esp_ds_start_sign(const void *message,
  160. const esp_ds_data_t *data,
  161. hmac_key_id_t key_id,
  162. esp_ds_context_t **esp_ds_ctx);
  163. /**
  164. * Return true if the DS peripheral is busy, otherwise false.
  165. *
  166. * @note Only valid if \c esp_ds_start_sign() was called before.
  167. */
  168. bool esp_ds_is_busy(void);
  169. /**
  170. * @brief Finish the signing process.
  171. *
  172. * @param signature the destination of the signature, should be (data->rsa_length + 1)*4 bytes long,
  173. the resultant signature bytes shall be written in little endian format.
  174. * @param esp_ds_ctx the context object retreived by \c esp_ds_start_sign()
  175. *
  176. * @return
  177. * - ESP_OK if successful, the ds operation has been finished and the result is written to signature.
  178. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL
  179. * - ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST if the message digest didn't match; the signature is invalid.
  180. * This means that the encrypted RSA key parameters are invalid, indicating that they may have been tampered
  181. * with or indicating a flash error, etc.
  182. * - ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING if the message padding is incorrect, the signature can be read though
  183. * since the message digest matches (see TRM for more details).
  184. */
  185. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx);
  186. /**
  187. * @brief Encrypt the private key parameters.
  188. *
  189. * The encryption is a prerequisite step before any signature operation can be done.
  190. * It is not strictly necessary to use this encryption function, the encryption could also happen on an external
  191. * device.
  192. *
  193. * @param data Output buffer to store encrypted data, suitable for later use generating signatures.
  194. * @param iv Pointer to 16 byte IV buffer, will be copied into 'data'. Should be randomly generated bytes each time.
  195. * @param p_data Pointer to input plaintext key data. The expectation is this data will be deleted after this process
  196. * is done and 'data' is stored.
  197. * @param key Pointer to 32 bytes of key data. Type determined by key_type parameter. The expectation is the
  198. * corresponding HMAC key will be stored to efuse and then permanently erased.
  199. *
  200. * @note
  201. * The numbers Y, M, Rb which are a part of esp_ds_data_t should be provided in little endian format
  202. * and should be of length equal to the RSA private key bit length
  203. * The message length in bits should also be equal to the RSA private key bit length.
  204. * No padding is applied to the message automatically, Please ensure the message is appropriate padded before
  205. * calling the API.
  206. *
  207. * @return
  208. * - ESP_OK if successful, the ds operation has been finished and the result is written to signature.
  209. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or p_data->rsa_length is too long
  210. */
  211. esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
  212. const void *iv,
  213. const esp_ds_p_data_t *p_data,
  214. const void *key);
  215. #ifdef __cplusplus
  216. }
  217. #endif