esp_ds.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #include "esp_hmac.h"
  15. #include "esp_err.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL ESP_ERR_HW_CRYPTO_BASE + 0x1 /*!< HMAC peripheral problem */
  20. #define ESP_ERR_HW_CRYPTO_DS_INVALID_KEY ESP_ERR_HW_CRYPTO_BASE + 0x2 /*!< given HMAC key isn't correct,
  21. HMAC peripheral problem */
  22. #define ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST ESP_ERR_HW_CRYPTO_BASE + 0x4 /*!< message digest check failed,
  23. result is invalid */
  24. #define ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING ESP_ERR_HW_CRYPTO_BASE + 0x5 /*!< padding check failed, but result
  25. is produced anyway and can be read*/
  26. #define ESP_DS_IV_LEN 16
  27. /* Length of parameter 'C' stored in flash */
  28. #define ESP_DS_C_LEN (12672 / 8)
  29. typedef struct esp_ds_context esp_ds_context_t;
  30. typedef enum {
  31. ESP_DS_RSA_1024 = (1024 / 32) - 1,
  32. ESP_DS_RSA_2048 = (2048 / 32) - 1,
  33. ESP_DS_RSA_3072 = (3072 / 32) - 1,
  34. ESP_DS_RSA_4096 = (4096 / 32) - 1
  35. } esp_digital_signature_length_t;
  36. /**
  37. * Encrypted private key data. Recommended to store in flash in this format.
  38. *
  39. * @note This struct has to match to one from the ROM code! This documentation is mostly taken from there.
  40. */
  41. typedef struct esp_digital_signature_data {
  42. /**
  43. * RSA LENGTH register parameters
  44. * (number of words in RSA key & operands, minus one).
  45. *
  46. * Max value 127 (for RSA 4096).
  47. *
  48. * This value must match the length field encrypted and stored in 'c',
  49. * or invalid results will be returned. (The DS peripheral will
  50. * always use the value in 'c', not this value, so an attacker can't
  51. * alter the DS peripheral results this way, it will just truncate or
  52. * extend the message and the resulting signature in software.)
  53. *
  54. * @note In IDF, the enum type length is the same as of type unsigned, so they can be used interchangably.
  55. * See the ROM code for the original declaration of struct \c ets_ds_data_t.
  56. */
  57. esp_digital_signature_length_t rsa_length;
  58. /**
  59. * IV value used to encrypt 'c'
  60. */
  61. uint8_t iv[ESP_DS_IV_LEN];
  62. /**
  63. * Encrypted Digital Signature parameters. Result of AES-CBC encryption
  64. * of plaintext values. Includes an encrypted message digest.
  65. */
  66. uint8_t c[ESP_DS_C_LEN];
  67. } esp_ds_data_t;
  68. /** Plaintext parameters used by Digital Signature.
  69. *
  70. * Not used for signing with DS peripheral, but can be encrypted
  71. * in-device by calling esp_ds_encrypt_params()
  72. *
  73. * @note This documentation is mostly taken from the ROM code.
  74. */
  75. typedef struct {
  76. uint32_t Y[4096/32]; //!< RSA exponent
  77. uint32_t M[4096/32]; //!< RSA modulus
  78. uint32_t Rb[4096/32]; //!< RSA r inverse operand
  79. uint32_t M_prime; //!< RSA M prime operand
  80. esp_digital_signature_length_t length; //!< RSA length
  81. } esp_ds_p_data_t;
  82. /**
  83. * Sign the message.
  84. *
  85. * This function is a wrapper around \c esp_ds_finish_sign() and \c esp_ds_start_sign(), so do not use them
  86. * in parallel.
  87. * It blocks until the signing is finished and then returns the signature.
  88. *
  89. * @note This function locks the HMAC, SHA, AES and RSA components during its entire execution time.
  90. *
  91. * @param message the message to be signed; its length is determined by data->rsa_length
  92. * @param data the encrypted signing key data (AES encrypted RSA key + IV)
  93. * @param key_id the HMAC key ID determining the HMAC key of the HMAC which will be used to decrypt the
  94. * signing key data
  95. * @param signature the destination of the signature, should be (data->rsa_length + 1)*4 bytes long
  96. *
  97. * @return
  98. * - ESP_OK if successful, the signature was written to the parameter \c signature.
  99. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or data->rsa_length is too long or 0
  100. * - ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL if there was an HMAC failure during retrieval of the decryption key
  101. * - ESP_ERR_NO_MEM if there hasn't been enough memory to allocate the context object
  102. * - ESP_ERR_HW_CRYPTO_DS_INVALID_KEY if there's a problem with passing the HMAC key to the DS component
  103. * - ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST if the message digest didn't match; the signature is invalid.
  104. * - ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING if the message padding is incorrect, the signature can be read though
  105. * since the message digest matches.
  106. */
  107. esp_err_t esp_ds_sign(const void *message,
  108. const esp_ds_data_t *data,
  109. hmac_key_id_t key_id,
  110. void *signature);
  111. /**
  112. * Start the signing process.
  113. *
  114. * This function yields a context object which needs to be passed to \c esp_ds_finish_sign() to finish the signing
  115. * process.
  116. *
  117. * @note This function locks the HMAC, SHA, AES and RSA components, so the user has to ensure to call
  118. * \c esp_ds_finish_sign() in a timely manner.
  119. *
  120. * @param message the message to be signed; its length is determined by data->rsa_length
  121. * @param data the encrypted signing key data (AES encrypted RSA key + IV)
  122. * @param key_id the HMAC key ID determining the HMAC key of the HMAC which will be used to decrypt the
  123. * signing key data
  124. * @param esp_ds_ctx the context object which is needed for finishing the signing process later
  125. *
  126. * @return
  127. * - ESP_OK if successful, the ds operation was started now and has to be finished with \c esp_ds_finish_sign()
  128. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or data->rsa_length is too long or 0
  129. * - ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL if there was an HMAC failure during retrieval of the decryption key
  130. * - ESP_ERR_NO_MEM if there hasn't been enough memory to allocate the context object
  131. * - ESP_ERR_HW_CRYPTO_DS_INVALID_KEY if there's a problem with passing the HMAC key to the DS component
  132. */
  133. esp_err_t esp_ds_start_sign(const void *message,
  134. const esp_ds_data_t *data,
  135. hmac_key_id_t key_id,
  136. esp_ds_context_t **esp_ds_ctx);
  137. /**
  138. * Return true if the DS peripheral is busy, otherwise false.
  139. *
  140. * @note Only valid if \c esp_ds_start_sign() was called before.
  141. */
  142. bool esp_ds_is_busy(void);
  143. /**
  144. * Finish the signing process.
  145. *
  146. * @param signature the destination of the signature, should be (data->rsa_length + 1)*4 bytes long
  147. * @param esp_ds_ctx the context object retreived by \c esp_ds_start_sign()
  148. *
  149. * @return
  150. * - ESP_OK if successful, the ds operation has been finished and the result is written to signature.
  151. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL
  152. * - ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST if the message digest didn't match; the signature is invalid.
  153. * - ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING if the message padding is incorrect, the signature can be read though
  154. * since the message digest matches.
  155. */
  156. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx);
  157. /**
  158. * Encrypt the private key parameters.
  159. *
  160. * @param data Output buffer to store encrypted data, suitable for later use generating signatures.
  161. * The allocated memory must be in internal memory and word aligned since it's filled by DMA. Both is asserted
  162. * at run time.
  163. * @param iv Pointer to 16 byte IV buffer, will be copied into 'data'. Should be randomly generated bytes each time.
  164. * @param p_data Pointer to input plaintext key data. The expectation is this data will be deleted after this process
  165. * is done and 'data' is stored.
  166. * @param key Pointer to 32 bytes of key data. Type determined by key_type parameter. The expectation is the
  167. * corresponding HMAC key will be stored to efuse and then permanently erased.
  168. *
  169. * @return
  170. * - ESP_OK if successful, the ds operation has been finished and the result is written to signature.
  171. * - ESP_ERR_INVALID_ARG if one of the parameters is NULL or p_data->rsa_length is too long
  172. */
  173. esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
  174. const void *iv,
  175. const esp_ds_p_data_t *p_data,
  176. const void *key);
  177. #ifdef __cplusplus
  178. }
  179. #endif