esp_hmac.c
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 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <string.h> 16 #include "esp32s2/rom/hmac.h" 17 #include "esp_hmac.h" 18 #include "esp_crypto_lock.h" 19 #include "esp_efuse.h" 20 #include "esp_efuse_table.h" 21 #include "soc/hwcrypto_reg.h" 22 #include "soc/system_reg.h" 23 #include "esp_log.h" 24 25 static const char *TAG = "esp_hmac"; 26 27 static ets_efuse_block_t convert_key_type(hmac_key_id_t key_id) { 28 return ETS_EFUSE_BLOCK_KEY0 + (ets_efuse_block_t) key_id; 29 } 30 31 esp_err_t esp_hmac_calculate(hmac_key_id_t key_id, 32 const void *message, 33 size_t message_len, 34 uint8_t *hmac) 35 { 36 int hmac_ret; 37 if (!message || !hmac) return ESP_ERR_INVALID_ARG; 38 if (key_id >= HMAC_KEY_MAX) return ESP_ERR_INVALID_ARG; 39 40 esp_crypto_dma_lock_acquire(); 41 42 ets_hmac_enable(); 43 hmac_ret = ets_hmac_calculate_message(convert_key_type(key_id), message, message_len, hmac); 44 ets_hmac_disable(); 45 46 esp_crypto_dma_lock_release(); 47 48 if (hmac_ret != 0) { 49 return ESP_FAIL; 50 } else { 51 return ESP_OK; 52 } 53 54 } 55 56 esp_err_t esp_hmac_jtag_enable(hmac_key_id_t key_id, const uint8_t *token) 57 { 58 esp_err_t err; 59 60 if ((!token) || (key_id >= HMAC_KEY_MAX)) 61 return ESP_ERR_INVALID_ARG; 62 63 /* Check if JTAG is permanently disabled by HW Disable eFuse */ 64 if (esp_efuse_read_field_bit(ESP_EFUSE_HARD_DIS_JTAG)) { 65 ESP_LOGE(TAG, "JTAG disabled permanently."); 66 return ESP_FAIL; 67 } 68 69 esp_crypto_dma_lock_acquire(); 70 71 ets_hmac_enable(); 72 73 /* Token updating into HMAC module. */ 74 for (int i = 0; i < 32; i += 4) { 75 uint32_t key_word; 76 memcpy(&key_word, &token[i], 4); 77 REG_WRITE(DPORT_JTAG_CTRL_0_REG + i, __builtin_bswap32(key_word)); 78 } 79 80 err = ets_hmac_calculate_downstream(convert_key_type(key_id), ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_JTAG); 81 if (err != ETS_OK) { 82 ESP_LOGE(TAG, "HMAC downstream JTAG enable mode setting failed."); 83 return ESP_FAIL; 84 } 85 86 ESP_LOGD(TAG, "HMAC computation in downstream mode is completed."); 87 88 ets_hmac_disable(); 89 90 esp_crypto_dma_lock_release(); 91 92 return ESP_OK; 93 } 94 95 esp_err_t esp_hmac_jtag_disable() 96 { 97 esp_crypto_dma_lock_acquire(); 98 99 REG_WRITE(HMAC_SET_INVALIDATE_JTAG_REG, 1); 100 101 esp_crypto_dma_lock_release(); 102 103 ESP_LOGD(TAG, "Invalidate JTAG result register. JTAG disabled."); 104 105 return ESP_OK; 106 }