esp_ds.h 8.8 KB

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