esp_ds.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include <stdlib.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include "esp32s2/rom/aes.h"
  17. #include "esp32s2/rom/sha.h"
  18. #include "esp32s2/rom/hmac.h"
  19. #include "esp32s2/rom/digital_signature.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "soc/soc_memory_layout.h"
  23. #include "esp_crypto_lock.h"
  24. #include "esp_hmac.h"
  25. #include "esp_ds.h"
  26. struct esp_ds_context {
  27. const ets_ds_data_t *data;
  28. };
  29. /**
  30. * The vtask delay \c esp_ds_sign() is using while waiting for completion of the signing operation.
  31. */
  32. #define ESP_DS_SIGN_TASK_DELAY_MS 10
  33. #define RSA_LEN_MAX 127
  34. /*
  35. * Check that the size of esp_ds_data_t and ets_ds_data_t is the same because both structs are converted using
  36. * raw casts.
  37. */
  38. _Static_assert(sizeof(esp_ds_data_t) == sizeof(ets_ds_data_t),
  39. "The size and structure of esp_ds_data_t and ets_ds_data_t must match exactly, they're used in raw casts");
  40. /*
  41. * esp_digital_signature_length_t is used in esp_ds_data_t in contrast to ets_ds_data_t, where unsigned is used.
  42. * Check esp_digital_signature_length_t's width here because it's converted to unsigned using raw casts.
  43. */
  44. _Static_assert(sizeof(esp_digital_signature_length_t) == sizeof(unsigned),
  45. "The size of esp_digital_signature_length_t and unsigned has to be the same");
  46. static void ds_acquire_enable(void) {
  47. /* Lock AES, SHA and RSA peripheral */
  48. esp_crypto_dma_lock_acquire();
  49. esp_crypto_mpi_lock_acquire();
  50. ets_hmac_enable();
  51. ets_ds_enable();
  52. }
  53. static void ds_disable_release(void) {
  54. ets_ds_disable();
  55. ets_hmac_disable();
  56. esp_crypto_mpi_lock_release();
  57. esp_crypto_dma_lock_release();
  58. }
  59. esp_err_t esp_ds_sign(const void *message,
  60. const esp_ds_data_t *data,
  61. hmac_key_id_t key_id,
  62. void *signature)
  63. {
  64. // Need to check signature here, otherwise the signature is only checked when the signing has finished and fails
  65. // but the signing isn't uninitialized and the mutex is still locked.
  66. if (!signature) return ESP_ERR_INVALID_ARG;
  67. esp_ds_context_t *context;
  68. esp_err_t result = esp_ds_start_sign(message, data, key_id, &context);
  69. if (result != ESP_OK) return result;
  70. while (esp_ds_is_busy())
  71. vTaskDelay(ESP_DS_SIGN_TASK_DELAY_MS / portTICK_PERIOD_MS);
  72. return esp_ds_finish_sign(signature, context);
  73. }
  74. esp_err_t esp_ds_start_sign(const void *message,
  75. const esp_ds_data_t *data,
  76. hmac_key_id_t key_id,
  77. esp_ds_context_t **esp_ds_ctx)
  78. {
  79. if (!message || !data || !esp_ds_ctx) return ESP_ERR_INVALID_ARG;
  80. if (key_id >= HMAC_KEY_MAX) return ESP_ERR_INVALID_ARG;
  81. if (!(data->rsa_length == ESP_DS_RSA_1024
  82. || data->rsa_length == ESP_DS_RSA_2048
  83. || data->rsa_length == ESP_DS_RSA_3072
  84. || data->rsa_length == ESP_DS_RSA_4096)) {
  85. return ESP_ERR_INVALID_ARG;
  86. }
  87. ds_acquire_enable();
  88. // initiate hmac
  89. int r = ets_hmac_calculate_downstream(ETS_EFUSE_BLOCK_KEY0 + (ets_efuse_block_t) key_id,
  90. ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE);
  91. if (r != ETS_OK) {
  92. ds_disable_release();
  93. return ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL;
  94. }
  95. esp_ds_context_t *context = malloc(sizeof(esp_ds_context_t));
  96. if (!context) {
  97. ds_disable_release();
  98. return ESP_ERR_NO_MEM;
  99. }
  100. ets_ds_data_t *ds_data = (ets_ds_data_t*) data;
  101. // initiate signing
  102. ets_ds_result_t result = ets_ds_start_sign(message, ds_data);
  103. // ETS_DS_INVALID_PARAM only happens if a parameter is NULL or data->rsa_length is wrong
  104. // We checked all of that already
  105. assert(result != ETS_DS_INVALID_PARAM);
  106. if (result == ETS_DS_INVALID_KEY) {
  107. ds_disable_release();
  108. free(context);
  109. return ESP_ERR_HW_CRYPTO_DS_INVALID_KEY;
  110. }
  111. context->data = ds_data;
  112. *esp_ds_ctx = context;
  113. return ESP_OK;
  114. }
  115. bool esp_ds_is_busy(void)
  116. {
  117. return ets_ds_is_busy();
  118. }
  119. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx)
  120. {
  121. if (!signature || !esp_ds_ctx) return ESP_ERR_INVALID_ARG;
  122. const ets_ds_data_t *ds_data = esp_ds_ctx->data;
  123. ets_ds_result_t result = ets_ds_finish_sign(signature, ds_data);
  124. esp_err_t return_value = ESP_OK;
  125. // we checked all the parameters
  126. assert(result != ETS_DS_INVALID_PARAM);
  127. if (result == ETS_DS_INVALID_DIGEST) return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST;
  128. if (result == ETS_DS_INVALID_PADDING) return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING;
  129. free(esp_ds_ctx);
  130. int res = ets_hmac_invalidate_downstream(ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE);
  131. assert(res == ETS_OK); // should not fail if called with correct purpose
  132. (void)res;
  133. ds_disable_release();
  134. return return_value;
  135. }
  136. esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
  137. const void *iv,
  138. const esp_ds_p_data_t *p_data,
  139. const void *key)
  140. {
  141. // p_data has to be valid, in internal memory and word aligned
  142. if (!p_data) return ESP_ERR_INVALID_ARG;
  143. assert(esp_ptr_internal(p_data) && esp_ptr_word_aligned(p_data));
  144. esp_err_t result = ESP_OK;
  145. esp_crypto_dma_lock_acquire();
  146. ets_aes_enable();
  147. ets_sha_enable();
  148. ets_ds_data_t *ds_data = (ets_ds_data_t*) data;
  149. const ets_ds_p_data_t *ds_plain_data = (const ets_ds_p_data_t*) p_data;
  150. ets_ds_result_t ets_result = ets_ds_encrypt_params(ds_data, iv, ds_plain_data, key, ETS_DS_KEY_HMAC);
  151. if (ets_result == ETS_DS_INVALID_PARAM) result = ESP_ERR_INVALID_ARG;
  152. ets_sha_disable();
  153. ets_aes_disable();
  154. esp_crypto_dma_lock_release();
  155. return result;
  156. }