esp_ds.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_timer.h"
  12. #include "esp_ds.h"
  13. #include "esp_crypto_lock.h"
  14. #include "esp_hmac.h"
  15. #include "esp_memory_utils.h"
  16. #if CONFIG_IDF_TARGET_ESP32S2
  17. #include "esp32s2/rom/aes.h"
  18. #include "esp32s2/rom/sha.h"
  19. #include "esp32s2/rom/hmac.h"
  20. #include "soc/soc_memory_layout.h"
  21. #else /* CONFIG_IDF_TARGET_ESP32S2 */
  22. #include "esp_private/periph_ctrl.h"
  23. #include "hal/ds_hal.h"
  24. #include "hal/ds_ll.h"
  25. #include "hal/hmac_hal.h"
  26. #endif /* !CONFIG_IDF_TARGET_ESP32S2 */
  27. #if CONFIG_IDF_TARGET_ESP32S2
  28. #include "esp32s2/rom/digital_signature.h"
  29. #endif
  30. #if CONFIG_IDF_TARGET_ESP32S3
  31. #include "esp32s3/rom/digital_signature.h"
  32. #endif
  33. #if CONFIG_IDF_TARGET_ESP32C3
  34. #include "esp32c3/rom/digital_signature.h"
  35. #endif
  36. #if CONFIG_IDF_TARGET_ESP32C6
  37. #include "esp32c6/rom/digital_signature.h"
  38. #endif
  39. #if CONFIG_IDF_TARGET_ESP32H2
  40. #include "esp32h2/rom/digital_signature.h"
  41. #endif
  42. struct esp_ds_context {
  43. const ets_ds_data_t *data;
  44. };
  45. /**
  46. * The vtask delay \c esp_ds_sign() is using while waiting for completion of the signing operation.
  47. */
  48. #define ESP_DS_SIGN_TASK_DELAY_MS 10
  49. #define RSA_LEN_MAX ((SOC_RSA_MAX_BIT_LEN/8) - 1)
  50. /*
  51. * Check that the size of esp_ds_data_t and ets_ds_data_t is the same because both structs are converted using
  52. * raw casts.
  53. */
  54. _Static_assert(sizeof(esp_ds_data_t) == sizeof(ets_ds_data_t),
  55. "The size and structure of esp_ds_data_t and ets_ds_data_t must match exactly, they're used in raw casts");
  56. /*
  57. * esp_digital_signature_length_t is used in esp_ds_data_t in contrast to ets_ds_data_t, where unsigned is used.
  58. * Check esp_digital_signature_length_t's width here because it's converted to unsigned using raw casts.
  59. */
  60. _Static_assert(sizeof(esp_digital_signature_length_t) == sizeof(unsigned),
  61. "The size of esp_digital_signature_length_t and unsigned has to be the same");
  62. #ifdef CONFIG_IDF_TARGET_ESP32S2
  63. static void ds_acquire_enable(void)
  64. {
  65. /* Lock AES, SHA and RSA peripheral */
  66. esp_crypto_dma_lock_acquire();
  67. esp_crypto_mpi_lock_acquire();
  68. ets_hmac_enable();
  69. ets_ds_enable();
  70. }
  71. static void ds_disable_release(void)
  72. {
  73. ets_ds_disable();
  74. ets_hmac_disable();
  75. esp_crypto_mpi_lock_release();
  76. esp_crypto_dma_lock_release();
  77. }
  78. esp_err_t esp_ds_sign(const void *message,
  79. const esp_ds_data_t *data,
  80. hmac_key_id_t key_id,
  81. void *signature)
  82. {
  83. // Need to check signature here, otherwise the signature is only checked when the signing has finished and fails
  84. // but the signing isn't uninitialized and the mutex is still locked.
  85. if (!signature) {
  86. return ESP_ERR_INVALID_ARG;
  87. }
  88. esp_ds_context_t *context;
  89. esp_err_t result = esp_ds_start_sign(message, data, key_id, &context);
  90. if (result != ESP_OK) {
  91. return result;
  92. }
  93. while (esp_ds_is_busy()) {
  94. vTaskDelay(ESP_DS_SIGN_TASK_DELAY_MS / portTICK_PERIOD_MS);
  95. }
  96. return esp_ds_finish_sign(signature, context);
  97. }
  98. esp_err_t esp_ds_start_sign(const void *message,
  99. const esp_ds_data_t *data,
  100. hmac_key_id_t key_id,
  101. esp_ds_context_t **esp_ds_ctx)
  102. {
  103. if (!message || !data || !esp_ds_ctx) {
  104. return ESP_ERR_INVALID_ARG;
  105. }
  106. if (key_id >= HMAC_KEY_MAX) {
  107. return ESP_ERR_INVALID_ARG;
  108. }
  109. if (!(data->rsa_length == ESP_DS_RSA_1024
  110. || data->rsa_length == ESP_DS_RSA_2048
  111. || data->rsa_length == ESP_DS_RSA_3072
  112. #if SOC_RSA_MAX_BIT_LEN == 4096
  113. || data->rsa_length == ESP_DS_RSA_4096
  114. #endif
  115. )) {
  116. return ESP_ERR_INVALID_ARG;
  117. }
  118. ds_acquire_enable();
  119. // initiate hmac
  120. int r = ets_hmac_calculate_downstream(ETS_EFUSE_BLOCK_KEY0 + (ets_efuse_block_t) key_id,
  121. ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE);
  122. if (r != ETS_OK) {
  123. ds_disable_release();
  124. return ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL;
  125. }
  126. esp_ds_context_t *context = malloc(sizeof(esp_ds_context_t));
  127. if (!context) {
  128. ds_disable_release();
  129. return ESP_ERR_NO_MEM;
  130. }
  131. ets_ds_data_t *ds_data = (ets_ds_data_t *) data;
  132. // initiate signing
  133. ets_ds_result_t result = ets_ds_start_sign(message, ds_data);
  134. // ETS_DS_INVALID_PARAM only happens if a parameter is NULL or data->rsa_length is wrong
  135. // We checked all of that already
  136. assert(result != ETS_DS_INVALID_PARAM);
  137. if (result == ETS_DS_INVALID_KEY) {
  138. ds_disable_release();
  139. free(context);
  140. return ESP_ERR_HW_CRYPTO_DS_INVALID_KEY;
  141. }
  142. context->data = (const ets_ds_data_t *)ds_data;
  143. *esp_ds_ctx = context;
  144. return ESP_OK;
  145. }
  146. bool esp_ds_is_busy(void)
  147. {
  148. return ets_ds_is_busy();
  149. }
  150. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx)
  151. {
  152. if (!signature || !esp_ds_ctx) {
  153. return ESP_ERR_INVALID_ARG;
  154. }
  155. const ets_ds_data_t *ds_data = esp_ds_ctx->data;
  156. ets_ds_result_t result = ets_ds_finish_sign(signature, ds_data);
  157. esp_err_t return_value = ESP_OK;
  158. // we checked all the parameters
  159. assert(result != ETS_DS_INVALID_PARAM);
  160. if (result == ETS_DS_INVALID_DIGEST) {
  161. return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST;
  162. }
  163. if (result == ETS_DS_INVALID_PADDING) {
  164. return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING;
  165. }
  166. free(esp_ds_ctx);
  167. int res = ets_hmac_invalidate_downstream(ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE);
  168. assert(res == ETS_OK); // should not fail if called with correct purpose
  169. (void)res;
  170. ds_disable_release();
  171. return return_value;
  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. {
  178. // p_data has to be valid, in internal memory and word aligned
  179. if (!p_data) {
  180. return ESP_ERR_INVALID_ARG;
  181. }
  182. assert(esp_ptr_internal(p_data) && esp_ptr_word_aligned(p_data));
  183. esp_err_t result = ESP_OK;
  184. esp_crypto_dma_lock_acquire();
  185. ets_aes_enable();
  186. ets_sha_enable();
  187. ets_ds_data_t *ds_data = (ets_ds_data_t *) data;
  188. const ets_ds_p_data_t *ds_plain_data = (const ets_ds_p_data_t *) p_data;
  189. ets_ds_result_t ets_result = ets_ds_encrypt_params(ds_data, iv, ds_plain_data, key, ETS_DS_KEY_HMAC);
  190. if (ets_result == ETS_DS_INVALID_PARAM) {
  191. result = ESP_ERR_INVALID_ARG;
  192. }
  193. ets_sha_disable();
  194. ets_aes_disable();
  195. esp_crypto_dma_lock_release();
  196. return result;
  197. }
  198. #else /* !CONFIG_IDF_TARGET_ESP32S2 (targets other than esp32s2) */
  199. static void ds_acquire_enable(void)
  200. {
  201. esp_crypto_ds_lock_acquire();
  202. #if CONFIG_IDF_TARGET_ESP32S3
  203. esp_crypto_mpi_lock_acquire();
  204. #endif
  205. // We also enable SHA and HMAC here. SHA is used by HMAC, HMAC is used by DS.
  206. periph_module_enable(PERIPH_HMAC_MODULE);
  207. periph_module_enable(PERIPH_SHA_MODULE);
  208. periph_module_enable(PERIPH_DS_MODULE);
  209. hmac_hal_start();
  210. }
  211. static void ds_disable_release(void)
  212. {
  213. ds_hal_finish();
  214. periph_module_disable(PERIPH_DS_MODULE);
  215. periph_module_disable(PERIPH_SHA_MODULE);
  216. periph_module_disable(PERIPH_HMAC_MODULE);
  217. #if CONFIG_IDF_TARGET_ESP32S3
  218. esp_crypto_mpi_lock_release();
  219. #endif
  220. esp_crypto_ds_lock_release();
  221. }
  222. esp_err_t esp_ds_sign(const void *message,
  223. const esp_ds_data_t *data,
  224. hmac_key_id_t key_id,
  225. void *signature)
  226. {
  227. // Need to check signature here, otherwise the signature is only checked when the signing has finished and fails
  228. // but the signing isn't uninitialized and the mutex is still locked.
  229. if (!signature) {
  230. return ESP_ERR_INVALID_ARG;
  231. }
  232. esp_ds_context_t *context;
  233. esp_err_t result = esp_ds_start_sign(message, data, key_id, &context);
  234. if (result != ESP_OK) {
  235. return result;
  236. }
  237. while (esp_ds_is_busy()) {
  238. vTaskDelay(ESP_DS_SIGN_TASK_DELAY_MS / portTICK_PERIOD_MS);
  239. }
  240. return esp_ds_finish_sign(signature, context);
  241. }
  242. esp_err_t esp_ds_start_sign(const void *message,
  243. const esp_ds_data_t *data,
  244. hmac_key_id_t key_id,
  245. esp_ds_context_t **esp_ds_ctx)
  246. {
  247. if (!message || !data || !esp_ds_ctx) {
  248. return ESP_ERR_INVALID_ARG;
  249. }
  250. if (key_id >= HMAC_KEY_MAX) {
  251. return ESP_ERR_INVALID_ARG;
  252. }
  253. if (!(data->rsa_length == ESP_DS_RSA_1024
  254. || data->rsa_length == ESP_DS_RSA_2048
  255. || data->rsa_length == ESP_DS_RSA_3072
  256. #if SOC_RSA_MAX_BIT_LEN == 4096
  257. || data->rsa_length == ESP_DS_RSA_4096
  258. #endif
  259. )) {
  260. return ESP_ERR_INVALID_ARG;
  261. }
  262. ds_acquire_enable();
  263. // initiate hmac
  264. uint32_t conf_error = hmac_hal_configure(HMAC_OUTPUT_DS, key_id);
  265. if (conf_error) {
  266. ds_disable_release();
  267. return ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL;
  268. }
  269. ds_hal_start();
  270. // check encryption key from HMAC
  271. int64_t start_time = esp_timer_get_time();
  272. while (ds_ll_busy() != 0) {
  273. if ((esp_timer_get_time() - start_time) > SOC_DS_KEY_CHECK_MAX_WAIT_US) {
  274. ds_disable_release();
  275. return ESP_ERR_HW_CRYPTO_DS_INVALID_KEY;
  276. }
  277. }
  278. esp_ds_context_t *context = malloc(sizeof(esp_ds_context_t));
  279. if (!context) {
  280. ds_disable_release();
  281. return ESP_ERR_NO_MEM;
  282. }
  283. size_t rsa_len = (data->rsa_length + 1) * 4;
  284. ds_hal_write_private_key_params(data->c);
  285. ds_hal_configure_iv((uint32_t *)data->iv);
  286. ds_hal_write_message(message, rsa_len);
  287. // initiate signing
  288. ds_hal_start_sign();
  289. context->data = (const ets_ds_data_t *)data;
  290. *esp_ds_ctx = context;
  291. return ESP_OK;
  292. }
  293. bool esp_ds_is_busy(void)
  294. {
  295. return ds_hal_busy();
  296. }
  297. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx)
  298. {
  299. if (!signature || !esp_ds_ctx) {
  300. return ESP_ERR_INVALID_ARG;
  301. }
  302. const esp_ds_data_t *data = (const esp_ds_data_t *)esp_ds_ctx->data;
  303. unsigned rsa_len = (data->rsa_length + 1) * 4;
  304. while (ds_hal_busy()) { }
  305. ds_signature_check_t sig_check_result = ds_hal_read_result((uint8_t *) signature, (size_t) rsa_len);
  306. esp_err_t return_value = ESP_OK;
  307. if (sig_check_result == DS_SIGNATURE_MD_FAIL || sig_check_result == DS_SIGNATURE_PADDING_AND_MD_FAIL) {
  308. return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST;
  309. }
  310. if (sig_check_result == DS_SIGNATURE_PADDING_FAIL) {
  311. return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING;
  312. }
  313. free(esp_ds_ctx);
  314. hmac_hal_clean();
  315. ds_disable_release();
  316. return return_value;
  317. }
  318. esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
  319. const void *iv,
  320. const esp_ds_p_data_t *p_data,
  321. const void *key)
  322. {
  323. if (!p_data) {
  324. return ESP_ERR_INVALID_ARG;
  325. }
  326. esp_err_t result = ESP_OK;
  327. esp_crypto_ds_lock_acquire();
  328. periph_module_enable(PERIPH_AES_MODULE);
  329. periph_module_enable(PERIPH_DS_MODULE);
  330. periph_module_enable(PERIPH_SHA_MODULE);
  331. periph_module_enable(PERIPH_HMAC_MODULE);
  332. periph_module_enable(PERIPH_RSA_MODULE);
  333. ets_ds_data_t *ds_data = (ets_ds_data_t *) data;
  334. const ets_ds_p_data_t *ds_plain_data = (const ets_ds_p_data_t *) p_data;
  335. ets_ds_result_t ets_result = ets_ds_encrypt_params(ds_data, iv, ds_plain_data, key, ETS_DS_KEY_HMAC);
  336. if (ets_result == ETS_DS_INVALID_PARAM) {
  337. result = ESP_ERR_INVALID_ARG;
  338. }
  339. periph_module_disable(PERIPH_RSA_MODULE);
  340. periph_module_disable(PERIPH_HMAC_MODULE);
  341. periph_module_disable(PERIPH_SHA_MODULE);
  342. periph_module_disable(PERIPH_DS_MODULE);
  343. periph_module_disable(PERIPH_AES_MODULE);
  344. esp_crypto_ds_lock_release();
  345. return result;
  346. }
  347. #endif