esp_hmac.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2015-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 <string.h>
  14. #include "esp32s2/rom/hmac.h"
  15. #include "esp_hmac.h"
  16. #include "esp_crypto_lock.h"
  17. #include "esp_efuse.h"
  18. #include "esp_efuse_table.h"
  19. #include "soc/hwcrypto_reg.h"
  20. #include "soc/system_reg.h"
  21. #include "esp_log.h"
  22. static const char *TAG = "esp_hmac";
  23. static ets_efuse_block_t convert_key_type(hmac_key_id_t key_id) {
  24. return ETS_EFUSE_BLOCK_KEY0 + (ets_efuse_block_t) key_id;
  25. }
  26. esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
  27. const void *message,
  28. size_t message_len,
  29. uint8_t *hmac)
  30. {
  31. int hmac_ret;
  32. if (!message || !hmac) return ESP_ERR_INVALID_ARG;
  33. if (key_id >= HMAC_KEY_MAX) return ESP_ERR_INVALID_ARG;
  34. esp_crypto_dma_lock_acquire();
  35. ets_hmac_enable();
  36. hmac_ret = ets_hmac_calculate_message(convert_key_type(key_id), message, message_len, hmac);
  37. ets_hmac_disable();
  38. esp_crypto_dma_lock_release();
  39. if (hmac_ret != 0) {
  40. return ESP_FAIL;
  41. } else {
  42. return ESP_OK;
  43. }
  44. }
  45. esp_err_t esp_hmac_jtag_enable(hmac_key_id_t key_id, const uint8_t *token)
  46. {
  47. int ets_status;
  48. esp_err_t err = ESP_OK;
  49. if ((!token) || (key_id >= HMAC_KEY_MAX))
  50. return ESP_ERR_INVALID_ARG;
  51. /* Check if JTAG is permanently disabled by HW Disable eFuse */
  52. if (esp_efuse_read_field_bit(ESP_EFUSE_HARD_DIS_JTAG)) {
  53. ESP_LOGE(TAG, "JTAG disabled permanently.");
  54. return ESP_FAIL;
  55. }
  56. esp_crypto_dma_lock_acquire();
  57. ets_hmac_enable();
  58. /* Token updating into HMAC module. */
  59. for (int i = 0; i < 32; i += 4) {
  60. uint32_t key_word;
  61. memcpy(&key_word, &token[i], 4);
  62. REG_WRITE(DPORT_JTAG_CTRL_0_REG + i, __builtin_bswap32(key_word));
  63. }
  64. ets_status = ets_hmac_calculate_downstream(convert_key_type(key_id), ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_JTAG);
  65. if (ets_status != ETS_OK) {
  66. err = ESP_FAIL;
  67. ESP_LOGE(TAG, "HMAC downstream JTAG enable mode setting failed. (%d)", err);
  68. }
  69. ESP_LOGD(TAG, "HMAC computation in downstream mode is completed.");
  70. ets_hmac_disable();
  71. esp_crypto_dma_lock_release();
  72. return err;
  73. }
  74. esp_err_t esp_hmac_jtag_disable()
  75. {
  76. esp_crypto_dma_lock_acquire();
  77. REG_WRITE(HMAC_SET_INVALIDATE_JTAG_REG, 1);
  78. esp_crypto_dma_lock_release();
  79. ESP_LOGD(TAG, "Invalidate JTAG result register. JTAG disabled.");
  80. return ESP_OK;
  81. }