esp_hmac.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "esp32s2/rom/hmac.h"
  14. #include "esp32s2/rom/ets_sys.h"
  15. #include "esp_hmac.h"
  16. #include "esp_crypto_lock.h"
  17. static ets_efuse_block_t convert_key_type(hmac_key_id_t key_id) {
  18. return ETS_EFUSE_BLOCK_KEY0 + (ets_efuse_block_t) key_id;
  19. }
  20. esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
  21. const void *message,
  22. size_t message_len,
  23. uint8_t *hmac)
  24. {
  25. int hmac_ret;
  26. if (!message || !hmac) return ESP_ERR_INVALID_ARG;
  27. if (key_id >= HMAC_KEY_MAX) return ESP_ERR_INVALID_ARG;
  28. esp_crypto_dma_lock_acquire();
  29. ets_hmac_enable();
  30. hmac_ret = ets_hmac_calculate_message(convert_key_type(key_id), message, message_len, hmac);
  31. ets_hmac_disable();
  32. esp_crypto_dma_lock_release();
  33. if (hmac_ret != ETS_OK) {
  34. return ESP_FAIL;
  35. } else {
  36. return ESP_OK;
  37. }
  38. }