esp_ds.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "esp_hmac.h"
  8. #include "esp_err.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #define ESP32C3_ERR_HW_CRYPTO_DS_HMAC_FAIL ESP_ERR_HW_CRYPTO_BASE + 0x1 /*!< HMAC peripheral problem */
  13. #define ESP32C3_ERR_HW_CRYPTO_DS_INVALID_KEY ESP_ERR_HW_CRYPTO_BASE + 0x2 /*!< given HMAC key isn't correct,
  14. HMAC peripheral problem */
  15. #define ESP32C3_ERR_HW_CRYPTO_DS_INVALID_DIGEST ESP_ERR_HW_CRYPTO_BASE + 0x4 /*!< message digest check failed,
  16. result is invalid */
  17. #define ESP32C3_ERR_HW_CRYPTO_DS_INVALID_PADDING ESP_ERR_HW_CRYPTO_BASE + 0x5 /*!< padding check failed, but result
  18. is produced anyway and can be read*/
  19. #define ESP_DS_IV_BIT_LEN 128
  20. #define ESP_DS_IV_LEN (ESP_DS_IV_BIT_LEN / 8)
  21. #define ESP_DS_SIGNATURE_MAX_BIT_LEN 3072
  22. #define ESP_DS_SIGNATURE_MD_BIT_LEN 256
  23. #define ESP_DS_SIGNATURE_M_PRIME_BIT_LEN 32
  24. #define ESP_DS_SIGNATURE_L_BIT_LEN 32
  25. #define ESP_DS_SIGNATURE_PADDING_BIT_LEN 64
  26. /* Length of parameter 'C' stored in flash, in bytes
  27. - Operands Y, M and r_bar; each 3072 bits
  28. - Operand MD (message digest); 256 bits
  29. - Operands M' and L; each 32 bits
  30. - Operand beta (padding value; 64 bits
  31. */
  32. #define ESP_DS_C_LEN (((ESP_DS_SIGNATURE_MAX_BIT_LEN * 3 \
  33. + ESP_DS_SIGNATURE_MD_BIT_LEN \
  34. + ESP_DS_SIGNATURE_M_PRIME_BIT_LEN \
  35. + ESP_DS_SIGNATURE_L_BIT_LEN \
  36. + ESP_DS_SIGNATURE_PADDING_BIT_LEN) / 8))
  37. typedef struct esp_ds_context esp_ds_context_t;
  38. typedef enum {
  39. ESP_DS_RSA_1024 = (1024 / 32) - 1,
  40. ESP_DS_RSA_2048 = (2048 / 32) - 1,
  41. ESP_DS_RSA_3072 = (3072 / 32) - 1
  42. } esp_digital_signature_length_t;
  43. /**
  44. * Encrypted private key data. Recommended to store in flash in this format.
  45. *
  46. * @note This struct has to match to one from the ROM code! This documentation is mostly taken from there.
  47. */
  48. typedef struct esp_digital_signature_data {
  49. /**
  50. * RSA LENGTH register parameters
  51. * (number of words in RSA key & operands, minus one).
  52. *
  53. * Max value 127 (for RSA 3072).
  54. *
  55. * This value must match the length field encrypted and stored in 'c',
  56. * or invalid results will be returned. (The DS peripheral will
  57. * always use the value in 'c', not this value, so an attacker can't
  58. * alter the DS peripheral results this way, it will just truncate or
  59. * extend the message and the resulting signature in software.)
  60. *
  61. * @note In IDF, the enum type length is the same as of type unsigned, so they can be used interchangably.
  62. * See the ROM code for the original declaration of struct \c ets_ds_data_t.
  63. */
  64. esp_digital_signature_length_t rsa_length;
  65. /**
  66. * IV value used to encrypt 'c'
  67. */
  68. uint32_t iv[ESP_DS_IV_BIT_LEN / 32];
  69. /**
  70. * Encrypted Digital Signature parameters. Result of AES-CBC encryption
  71. * of plaintext values. Includes an encrypted message digest.
  72. */
  73. uint8_t c[ESP_DS_C_LEN];
  74. } esp_ds_data_t;
  75. /**
  76. * Plaintext parameters used by Digital Signature.
  77. *
  78. * This is only used for encrypting the RSA parameters by calling esp_ds_encrypt_params().
  79. * Afterwards, the result can be stored in flash or in other persistent memory.
  80. * The encryption is a prerequisite step before any signature operation can be done.
  81. */
  82. typedef struct {
  83. uint32_t Y[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA exponent
  84. uint32_t M[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA modulus
  85. uint32_t Rb[ESP_DS_SIGNATURE_MAX_BIT_LEN / 32]; //!< RSA r inverse operand
  86. uint32_t M_prime; //!< RSA M prime operand
  87. uint32_t length; //!< RSA length in words (32 bit)
  88. } esp_ds_p_data_t;
  89. /**
  90. * @brief Sign the message with a hardware key from specific key slot.
  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 This function locks the HMAC, SHA, AES and RSA components during its entire execution time.
  97. *
  98. * @param message the message to be signed; its length is determined by data->rsa_length
  99. * @param data the encrypted signing key data (AES encrypted RSA key + IV)
  100. * @param key_id the HMAC key ID determining the HMAC key of the HMAC which will be used to decrypt the
  101. * signing key data
  102. * @param signature the destination of the signature, should be (data->rsa_length + 1)*4 bytes long
  103. *
  104. * @return
  105. * - ESP_OK if successful, the signature was written to the parameter \c signature.
  106. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or data->rsa_length is too long or 0
  107. * - ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL if there was an HMAC failure during retrieval of the decryption key
  108. * - ESP_ERR_NO_MEM if there hasn't been enough memory to allocate the context object
  109. * - ESP_ERR_HW_CRYPTO_DS_INVALID_KEY if there's a problem with passing the HMAC key to the DS component
  110. * - ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST if the message digest didn't match; the signature is invalid.
  111. * - ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING if the message padding is incorrect, the signature can be read though
  112. * since the message digest matches.
  113. */
  114. esp_err_t esp_ds_sign(const void *message,
  115. const esp_ds_data_t *data,
  116. hmac_key_id_t key_id,
  117. void *signature);
  118. /**
  119. * @brief Start the signing process.
  120. *
  121. * This function yields a context object which needs to be passed to \c esp_ds_finish_sign() to finish the signing
  122. * process.
  123. *
  124. * @note This function locks the HMAC, SHA, AES and RSA components, so the user has to ensure to call
  125. * \c esp_ds_finish_sign() in a timely manner.
  126. *
  127. * @param message the message to be signed; its length is determined by data->rsa_length
  128. * @param data the encrypted signing key data (AES encrypted RSA key + IV)
  129. * @param key_id the HMAC key ID determining the HMAC key of the HMAC which will be used to decrypt the
  130. * signing key data
  131. * @param esp_ds_ctx the context object which is needed for finishing the signing process later
  132. *
  133. * @return
  134. * - ESP_OK if successful, the ds operation was started now and has to be finished with \c esp_ds_finish_sign()
  135. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or data->rsa_length is too long or 0
  136. * - ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL if there was an HMAC failure during retrieval of the decryption key
  137. * - ESP_ERR_NO_MEM if there hasn't been enough memory to allocate the context object
  138. * - ESP_ERR_HW_CRYPTO_DS_INVALID_KEY if there's a problem with passing the HMAC key to the DS component
  139. */
  140. esp_err_t esp_ds_start_sign(const void *message,
  141. const esp_ds_data_t *data,
  142. hmac_key_id_t key_id,
  143. esp_ds_context_t **esp_ds_ctx);
  144. /**
  145. * Return true if the DS peripheral is busy, otherwise false.
  146. *
  147. * @note Only valid if \c esp_ds_start_sign() was called before.
  148. */
  149. bool esp_ds_is_busy(void);
  150. /**
  151. * @brief Finish the signing process.
  152. *
  153. * @param signature the destination of the signature, should be (data->rsa_length + 1)*4 bytes long
  154. * @param esp_ds_ctx the context object retreived by \c esp_ds_start_sign()
  155. *
  156. * @return
  157. * - ESP_OK if successful, the ds operation has been finished and the result is written to signature.
  158. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL
  159. * - ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST if the message digest didn't match; the signature is invalid.
  160. * This means that the encrypted RSA key parameters are invalid, indicating that they may have been tampered
  161. * with or indicating a flash error, etc.
  162. * - ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING if the message padding is incorrect, the signature can be read though
  163. * since the message digest matches (see TRM for more details).
  164. */
  165. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx);
  166. /**
  167. * @brief Encrypt the private key parameters.
  168. *
  169. * The encryption is a prerequisite step before any signature operation can be done.
  170. * It is not strictly necessary to use this encryption function, the encryption could also happen on an external
  171. * device.
  172. *
  173. * @param data Output buffer to store encrypted data, suitable for later use generating signatures.
  174. * The allocated memory must be in internal memory and word aligned since it's filled by DMA. Both is asserted
  175. * at run time.
  176. * @param iv Pointer to 16 byte IV buffer, will be copied into 'data'. Should be randomly generated bytes each time.
  177. * @param p_data Pointer to input plaintext key data. The expectation is this data will be deleted after this process
  178. * is done and 'data' is stored.
  179. * @param key Pointer to 32 bytes of key data. Type determined by key_type parameter. The expectation is the
  180. * corresponding HMAC key will be stored to efuse and then permanently erased.
  181. *
  182. * @return
  183. * - ESP_OK if successful, the ds operation has been finished and the result is written to signature.
  184. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or p_data->rsa_length is too long
  185. */
  186. esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
  187. const void *iv,
  188. const esp_ds_p_data_t *p_data,
  189. const void *key);
  190. #ifdef __cplusplus
  191. }
  192. #endif