esp_ds.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "driver/periph_ctrl.h"
  19. #include "esp_crypto_lock.h"
  20. #include "hal/ds_hal.h"
  21. #include "hal/hmac_hal.h"
  22. #include "esp32c3/rom/digital_signature.h"
  23. #include "esp_ds.h"
  24. struct esp_ds_context {
  25. const esp_ds_data_t *data;
  26. };
  27. /**
  28. * The vtask delay \c esp_ds_sign() is using while waiting for completion of the signing operation.
  29. */
  30. #define ESP_DS_SIGN_TASK_DELAY_MS 10
  31. #define RSA_LEN_MAX 127
  32. /*
  33. * esp_digital_signature_length_t is used in esp_ds_data_t in contrast to ets_ds_data_t, where unsigned is used.
  34. * Check esp_digital_signature_length_t's width here because it's converted to unsigned using raw casts.
  35. */
  36. _Static_assert(sizeof(esp_digital_signature_length_t) == sizeof(unsigned),
  37. "The size of esp_digital_signature_length_t and unsigned has to be the same");
  38. /*
  39. * esp_ds_data_t is used in the encryption function but casted to ets_ds_data_t.
  40. * Check esp_ds_data_t's width here because it's converted using raw casts.
  41. */
  42. _Static_assert(sizeof(esp_ds_data_t) == sizeof(ets_ds_data_t),
  43. "The size of esp_ds_data_t and ets_ds_data_t has to be the same");
  44. static void ds_acquire_enable(void)
  45. {
  46. esp_crypto_ds_lock_acquire();
  47. // We also enable SHA and HMAC here. SHA is used by HMAC, HMAC is used by DS.
  48. periph_module_enable(PERIPH_HMAC_MODULE);
  49. periph_module_enable(PERIPH_SHA_MODULE);
  50. periph_module_enable(PERIPH_DS_MODULE);
  51. hmac_hal_start();
  52. }
  53. static void ds_disable_release(void)
  54. {
  55. ds_hal_finish();
  56. periph_module_disable(PERIPH_DS_MODULE);
  57. periph_module_disable(PERIPH_SHA_MODULE);
  58. periph_module_disable(PERIPH_HMAC_MODULE);
  59. esp_crypto_ds_lock_release();
  60. }
  61. esp_err_t esp_ds_sign(const void *message,
  62. const esp_ds_data_t *data,
  63. hmac_key_id_t key_id,
  64. void *signature)
  65. {
  66. // Need to check signature here, otherwise the signature is only checked when the signing has finished and fails
  67. // but the signing isn't uninitialized and the mutex is still locked.
  68. if (!signature) {
  69. return ESP_ERR_INVALID_ARG;
  70. }
  71. esp_ds_context_t *context;
  72. esp_err_t result = esp_ds_start_sign(message, data, key_id, &context);
  73. if (result != ESP_OK) {
  74. return result;
  75. }
  76. while (esp_ds_is_busy())
  77. vTaskDelay(ESP_DS_SIGN_TASK_DELAY_MS / portTICK_PERIOD_MS);
  78. return esp_ds_finish_sign(signature, context);
  79. }
  80. esp_err_t esp_ds_start_sign(const void *message,
  81. const esp_ds_data_t *data,
  82. hmac_key_id_t key_id,
  83. esp_ds_context_t **esp_ds_ctx)
  84. {
  85. if (!message || !data || !esp_ds_ctx) {
  86. return ESP_ERR_INVALID_ARG;
  87. }
  88. if (key_id >= HMAC_KEY_MAX) {
  89. return ESP_ERR_INVALID_ARG;
  90. }
  91. if (!(data->rsa_length == ESP_DS_RSA_1024
  92. || data->rsa_length == ESP_DS_RSA_2048
  93. || data->rsa_length == ESP_DS_RSA_3072)) {
  94. return ESP_ERR_INVALID_ARG;
  95. }
  96. ds_acquire_enable();
  97. // initiate hmac
  98. uint32_t conf_error = hmac_hal_configure(HMAC_OUTPUT_DS, key_id);
  99. if (conf_error) {
  100. ds_disable_release();
  101. return ESP32C3_ERR_HW_CRYPTO_DS_HMAC_FAIL;
  102. }
  103. ds_hal_start();
  104. // check encryption key from HMAC
  105. ds_key_check_t key_check_result = ds_hal_check_decryption_key();
  106. if (key_check_result != DS_KEY_INPUT_OK) {
  107. ds_disable_release();
  108. return ESP32C3_ERR_HW_CRYPTO_DS_INVALID_KEY;
  109. }
  110. esp_ds_context_t *context = malloc(sizeof(esp_ds_context_t));
  111. if (!context) {
  112. ds_disable_release();
  113. return ESP_ERR_NO_MEM;
  114. }
  115. size_t rsa_len = (data->rsa_length + 1) * 4;
  116. ds_hal_write_private_key_params(data->c);
  117. ds_hal_configure_iv(data->iv);
  118. ds_hal_write_message(message, rsa_len);
  119. // initiate signing
  120. ds_hal_start_sign();
  121. context->data = data;
  122. *esp_ds_ctx = context;
  123. return ESP_OK;
  124. }
  125. bool esp_ds_is_busy(void)
  126. {
  127. return ds_hal_busy();
  128. }
  129. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx)
  130. {
  131. if (!signature || !esp_ds_ctx) {
  132. return ESP_ERR_INVALID_ARG;
  133. }
  134. const esp_ds_data_t *data = esp_ds_ctx->data;
  135. unsigned rsa_len = (data->rsa_length + 1) * 4;
  136. while (ds_hal_busy()) { }
  137. ds_signature_check_t sig_check_result = ds_hal_read_result((uint8_t*) signature, (size_t) rsa_len);
  138. esp_err_t return_value = ESP_OK;
  139. if (sig_check_result == DS_SIGNATURE_MD_FAIL || sig_check_result == DS_SIGNATURE_PADDING_AND_MD_FAIL) {
  140. return_value = ESP32C3_ERR_HW_CRYPTO_DS_INVALID_DIGEST;
  141. }
  142. if (sig_check_result == DS_SIGNATURE_PADDING_FAIL) {
  143. return_value = ESP32C3_ERR_HW_CRYPTO_DS_INVALID_PADDING;
  144. }
  145. free(esp_ds_ctx);
  146. hmac_hal_clean();
  147. ds_disable_release();
  148. return return_value;
  149. }
  150. esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
  151. const void *iv,
  152. const esp_ds_p_data_t *p_data,
  153. const void *key)
  154. {
  155. if (!p_data) {
  156. return ESP_ERR_INVALID_ARG;
  157. }
  158. esp_err_t result = ESP_OK;
  159. esp_crypto_ds_lock_acquire();
  160. periph_module_enable(PERIPH_AES_MODULE);
  161. periph_module_enable(PERIPH_DS_MODULE);
  162. periph_module_enable(PERIPH_SHA_MODULE);
  163. periph_module_enable(PERIPH_HMAC_MODULE);
  164. periph_module_enable(PERIPH_RSA_MODULE);
  165. ets_ds_data_t *ds_data = (ets_ds_data_t*) data;
  166. const ets_ds_p_data_t *ds_plain_data = (const ets_ds_p_data_t*) p_data;
  167. ets_ds_result_t ets_result = ets_ds_encrypt_params(ds_data, iv, ds_plain_data, key, ETS_DS_KEY_HMAC);
  168. if (ets_result == ETS_DS_INVALID_PARAM) {
  169. result = ESP_ERR_INVALID_ARG;
  170. }
  171. periph_module_disable(PERIPH_RSA_MODULE);
  172. periph_module_disable(PERIPH_HMAC_MODULE);
  173. periph_module_disable(PERIPH_SHA_MODULE);
  174. periph_module_disable(PERIPH_DS_MODULE);
  175. periph_module_disable(PERIPH_AES_MODULE);
  176. esp_crypto_ds_lock_release();
  177. return result;
  178. }