esp_ds.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. esp_crypto_lock_acquire();
  48. ets_hmac_enable();
  49. ets_ds_enable();
  50. }
  51. static void ds_disable_release(void) {
  52. ets_ds_disable();
  53. ets_hmac_disable();
  54. esp_crypto_lock_release();
  55. }
  56. esp_err_t esp_ds_sign(const void *message,
  57. const esp_ds_data_t *data,
  58. hmac_key_id_t key_id,
  59. void *signature)
  60. {
  61. // Need to check signature here, otherwise the signature is only checked when the signing has finished and fails
  62. // but the signing isn't uninitialized and the mutex is still locked.
  63. if (!signature) return ESP_ERR_INVALID_ARG;
  64. esp_ds_context_t *context;
  65. esp_err_t result = esp_ds_start_sign(message, data, key_id, &context);
  66. if (result != ESP_OK) return result;
  67. while (esp_ds_is_busy())
  68. vTaskDelay(ESP_DS_SIGN_TASK_DELAY_MS / portTICK_PERIOD_MS);
  69. return esp_ds_finish_sign(signature, context);
  70. }
  71. esp_err_t esp_ds_start_sign(const void *message,
  72. const esp_ds_data_t *data,
  73. hmac_key_id_t key_id,
  74. esp_ds_context_t **esp_ds_ctx)
  75. {
  76. if (!message || !data || !esp_ds_ctx) return ESP_ERR_INVALID_ARG;
  77. if (key_id >= HMAC_KEY_MAX) return ESP_ERR_INVALID_ARG;
  78. if (!(data->rsa_length == ESP_DS_RSA_1024
  79. || data->rsa_length == ESP_DS_RSA_2048
  80. || data->rsa_length == ESP_DS_RSA_3072
  81. || data->rsa_length == ESP_DS_RSA_4096)) {
  82. return ESP_ERR_INVALID_ARG;
  83. }
  84. ds_acquire_enable();
  85. // initiate hmac
  86. int r = ets_hmac_calculate_downstream(ETS_EFUSE_BLOCK_KEY0 + (ets_efuse_block_t) key_id,
  87. ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE);
  88. if (r != ETS_OK) {
  89. ds_disable_release();
  90. return ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL;
  91. }
  92. esp_ds_context_t *context = malloc(sizeof(esp_ds_context_t));
  93. if (!context) {
  94. ds_disable_release();
  95. return ESP_ERR_NO_MEM;
  96. }
  97. ets_ds_data_t *ds_data = (ets_ds_data_t*) data;
  98. // initiate signing
  99. ets_ds_result_t result = ets_ds_start_sign(message, ds_data);
  100. // ETS_DS_INVALID_PARAM only happens if a parameter is NULL or data->rsa_length is wrong
  101. // We checked all of that already
  102. assert(result != ETS_DS_INVALID_PARAM);
  103. if (result == ETS_DS_INVALID_KEY) {
  104. ds_disable_release();
  105. return ESP_ERR_HW_CRYPTO_DS_INVALID_KEY;
  106. }
  107. context->data = ds_data;
  108. *esp_ds_ctx = context;
  109. return ESP_OK;
  110. }
  111. bool esp_ds_is_busy(void)
  112. {
  113. return ets_ds_is_busy();
  114. }
  115. esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx)
  116. {
  117. if (!signature || !esp_ds_ctx) return ESP_ERR_INVALID_ARG;
  118. const ets_ds_data_t *ds_data = esp_ds_ctx->data;
  119. ets_ds_result_t result = ets_ds_finish_sign(signature, ds_data);
  120. esp_err_t return_value = ESP_OK;
  121. // we checked all the parameters
  122. assert(result != ETS_DS_INVALID_PARAM);
  123. if (result == ETS_DS_INVALID_DIGEST) return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST;
  124. if (result == ETS_DS_INVALID_PADDING) return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING;
  125. free(esp_ds_ctx);
  126. // should not fail if called with correct purpose
  127. assert(ets_hmac_invalidate_downstream(ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE) == ETS_OK);
  128. ds_disable_release();
  129. return return_value;
  130. }
  131. esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
  132. const void *iv,
  133. const esp_ds_p_data_t *p_data,
  134. const void *key)
  135. {
  136. // p_data has to be valid, in internal memory and word aligned
  137. if (!p_data) return ESP_ERR_INVALID_ARG;
  138. assert(esp_ptr_internal(p_data) && esp_ptr_word_aligned(p_data));
  139. esp_err_t result = ESP_OK;
  140. esp_crypto_lock_acquire();
  141. ets_aes_enable();
  142. ets_sha_enable();
  143. ets_ds_data_t *ds_data = (ets_ds_data_t*) data;
  144. const ets_ds_p_data_t *ds_plain_data = (const ets_ds_p_data_t*) p_data;
  145. ets_ds_result_t ets_result = ets_ds_encrypt_params(ds_data, iv, ds_plain_data, key, ETS_DS_KEY_HMAC);
  146. if (ets_result == ETS_DS_INVALID_PARAM) result = ESP_ERR_INVALID_ARG;
  147. ets_sha_disable();
  148. ets_aes_disable();
  149. esp_crypto_lock_release();
  150. return result;
  151. }