esp_ds.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. typedef struct {
  78. uint32_t Y[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA exponent
  79. uint32_t M[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA modulus
  80. uint32_t Rb[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA r inverse operand
  81. uint32_t M_prime; //!< RSA M prime operand
  82. uint32_t length; //!< RSA length in words (32 bit)
  83. } esp_ds_p_data_t;
  84. /**
  85. * @brief Sign the message with a hardware key from specific key slot.
  86. * The function calculates a plain RSA signature with help of the DS peripheral.
  87. * The RSA encryption operation is as follows:
  88. * Z = XY mod M where,
  89. * Z is the signature, X is the input message,
  90. * Y and M are the RSA private key parameters.
  91. *
  92. * This function is a wrapper around \c esp_ds_finish_sign() and \c esp_ds_start_sign(), so do not use them
  93. * in parallel.
  94. * It blocks until the signing is finished and then returns the signature.
  95. *
  96. * @note
  97. * Please see note section of \c esp_ds_start_sign() for more details about the input parameters.
  98. *
  99. * @param message the message to be signed; its length should be (data->rsa_length + 1)*4 bytes
  100. * @param data the encrypted signing key data (AES encrypted RSA key + IV)
  101. * @param key_id the HMAC key ID determining the HMAC key of the HMAC which will be used to decrypt the
  102. * signing key data
  103. * @param signature the destination of the signature, should be (data->rsa_length + 1)*4 bytes long
  104. *
  105. * @return
  106. * - ESP_OK if successful, the signature was written to the parameter \c signature.
  107. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or data->rsa_length is too long or 0
  108. * - ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL if there was an HMAC failure during retrieval of the decryption key
  109. * - ESP_ERR_NO_MEM if there hasn't been enough memory to allocate the context object
  110. * - ESP_ERR_HW_CRYPTO_DS_INVALID_KEY if there's a problem with passing the HMAC key to the DS component
  111. * - ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST if the message digest didn't match; the signature is invalid.
  112. * - ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING if the message padding is incorrect, the signature can be read though
  113. * since the message digest matches.
  114. */
  115. esp_err_t esp_ds_sign(const void *message,
  116. const esp_ds_data_t *data,
  117. hmac_key_id_t key_id,
  118. void *signature);
  119. /**
  120. * @brief Start the signing process.
  121. *
  122. * This function yields a context object which needs to be passed to \c esp_ds_finish_sign() to finish the signing
  123. * process.
  124. * The function calculates a plain RSA signature with help of the DS peripheral.
  125. * The RSA encryption operation is as follows:
  126. * Z = XY mod M where,
  127. * Z is the signature, X is the input message,
  128. * Y and M are the RSA private key parameters.
  129. *
  130. * @note
  131. * This function locks the HMAC, SHA, AES and RSA components, so the user has to ensure to call
  132. * \c esp_ds_finish_sign() in a timely manner.
  133. * The numbers Y, M, Rb which are a part of esp_ds_data_t should be provided in little endian format
  134. * and should be of length equal to the RSA private key bit length
  135. * The message length in bits should also be equal to the RSA private key bit length.
  136. * No padding is applied to the message automatically, Please ensure the message is appropriate padded before
  137. * calling the API.
  138. *
  139. * @param message the message to be signed; its length should be (data->rsa_length + 1)*4 bytes
  140. * @param data the encrypted signing key data (AES encrypted RSA key + IV)
  141. * @param key_id the HMAC key ID determining the HMAC key of the HMAC which will be used to decrypt the
  142. * signing key data
  143. * @param esp_ds_ctx the context object which is needed for finishing the signing process later
  144. *
  145. * @return
  146. * - ESP_OK if successful, the ds operation was started now and has to be finished with \c esp_ds_finish_sign()
  147. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or data->rsa_length is too long or 0
  148. * - ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL if there was an HMAC failure during retrieval of the decryption key
  149. * - ESP_ERR_NO_MEM if there hasn't been enough memory to allocate the context object
  150. * - ESP_ERR_HW_CRYPTO_DS_INVALID_KEY if there's a problem with passing the HMAC key to the DS component
  151. */
  152. esp_err_t esp_ds_start_sign(const void *message,
  153. const esp_ds_data_t *data,
  154. hmac_key_id_t key_id,
  155. esp_ds_context_t **esp_ds_ctx);
  156. /**
  157. * Return true if the DS peripheral is busy, otherwise false.
  158. *
  159. * @note Only valid if \c esp_ds_start_sign() was called before.
  160. */
  161. bool esp_ds_is_busy(void);
  162. /**
  163. * @brief Finish the signing process.
  164. *
  165. * @param signature the destination of the signature, should be (data->rsa_length + 1)*4 bytes long
  166. * @param esp_ds_ctx the context object retreived by \c esp_ds_start_sign()
  167. *
  168. * @return
  169. * - ESP_OK if successful, the ds operation has been finished and the result is written to signature.
  170. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL
  171. * - ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST if the message digest didn't match; the signature is invalid.
  172. * This means that the encrypted RSA key parameters are invalid, indicating that they may have been tampered
  173. * with or indicating a flash error, etc.
  174. * - ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING if the message padding is incorrect, the signature can be read though
  175. * since the message digest matches (see TRM for more details).
  176. */
  177. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx);
  178. /**
  179. * @brief Encrypt the private key parameters.
  180. *
  181. * The encryption is a prerequisite step before any signature operation can be done.
  182. * It is not strictly necessary to use this encryption function, the encryption could also happen on an external
  183. * device.
  184. *
  185. * @param data Output buffer to store encrypted data, suitable for later use generating signatures.
  186. * @param iv Pointer to 16 byte IV buffer, will be copied into 'data'. Should be randomly generated bytes each time.
  187. * @param p_data Pointer to input plaintext key data. The expectation is this data will be deleted after this process
  188. * is done and 'data' is stored.
  189. * @param key Pointer to 32 bytes of key data. Type determined by key_type parameter. The expectation is the
  190. * corresponding HMAC key will be stored to efuse and then permanently erased.
  191. *
  192. * @note
  193. * The numbers Y, M, Rb which are a part of esp_ds_data_t should be provided in little endian format
  194. * and should be of length equal to the RSA private key bit length
  195. * The message length in bits should also be equal to the RSA private key bit length.
  196. * No padding is applied to the message automatically, Please ensure the message is appropriate padded before
  197. * calling the API.
  198. *
  199. * @return
  200. * - ESP_OK if successful, the ds operation has been finished and the result is written to signature.
  201. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or p_data->rsa_length is too long
  202. */
  203. esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
  204. const void *iv,
  205. const esp_ds_p_data_t *p_data,
  206. const void *key);
  207. #ifdef __cplusplus
  208. }
  209. #endif