esp_hmac.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "driver/periph_ctrl.h"
  15. #include "esp32c3/rom/hmac.h"
  16. #include "esp32c3/rom/ets_sys.h"
  17. #include "esp_hmac.h"
  18. #include "esp_crypto_lock.h"
  19. #include "hal/hmac_hal.h"
  20. #define SHA256_BLOCK_SZ 64
  21. #define SHA256_PAD_SZ 8
  22. /**
  23. * @brief Apply the HMAC padding without the embedded length.
  24. *
  25. * @note This function does not check the data length, it is the responsability of the other functions in this
  26. * module to make sure that \c data_len is at most SHA256_BLOCK_SZ - 1 so the padding fits in.
  27. * Otherwise, this function has undefined behavior.
  28. * Note however, that for the actual HMAC implementation on ESP32C3, the length also needs to be applied at the end
  29. * of the block. This function alone deosn't do that.
  30. */
  31. static void write_and_padd(uint8_t *block, const uint8_t *data, uint16_t data_len)
  32. {
  33. memcpy(block, data, data_len);
  34. // Apply a one bit, followed by zero bits (refer to the ESP32C3 TRM).
  35. block[data_len] = 0x80;
  36. bzero(block + data_len + 1, SHA256_BLOCK_SZ - data_len - 1);
  37. }
  38. esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
  39. const void *message,
  40. size_t message_len,
  41. uint8_t *hmac)
  42. {
  43. const uint8_t *message_bytes = (const uint8_t *)message;
  44. if (!message || !hmac) {
  45. return ESP_ERR_INVALID_ARG;
  46. }
  47. if (key_id >= HMAC_KEY_MAX) {
  48. return ESP_ERR_INVALID_ARG;
  49. }
  50. esp_crypto_hmac_lock_acquire();
  51. // We also enable SHA and DS here. SHA is used by HMAC, DS will otherwise hold SHA in reset state.
  52. periph_module_enable(PERIPH_HMAC_MODULE);
  53. periph_module_enable(PERIPH_SHA_MODULE);
  54. periph_module_enable(PERIPH_DS_MODULE);
  55. hmac_hal_start();
  56. uint32_t conf_error = hmac_hal_configure(HMAC_OUTPUT_USER, key_id);
  57. if (conf_error) {
  58. esp_crypto_hmac_lock_release();
  59. return ESP_FAIL;
  60. }
  61. if (message_len + 1 + SHA256_PAD_SZ <= SHA256_BLOCK_SZ) {
  62. // If message including padding is only one block...
  63. // Last message block, so apply SHA-256 padding rules in software
  64. uint8_t block[SHA256_BLOCK_SZ];
  65. uint64_t bit_len = __builtin_bswap64(message_len * 8 + 512);
  66. write_and_padd(block, message_bytes, message_len);
  67. // Final block: append the bit length in this block and signal padding to peripheral
  68. memcpy(block + SHA256_BLOCK_SZ - sizeof(bit_len),
  69. &bit_len, sizeof(bit_len));
  70. hmac_hal_write_one_block_512(block);
  71. } else {
  72. // If message including padding is needs more than one block
  73. // write all blocks without padding except the last one
  74. size_t remaining_blocks = message_len / SHA256_BLOCK_SZ;
  75. for (int i = 1; i < remaining_blocks; i++) {
  76. hmac_hal_write_block_512(message_bytes);
  77. message_bytes += SHA256_BLOCK_SZ;
  78. hmac_hal_next_block_normal();
  79. }
  80. // If message fits into one block but without padding, we must not write another block.
  81. if (remaining_blocks) {
  82. hmac_hal_write_block_512(message_bytes);
  83. message_bytes += SHA256_BLOCK_SZ;
  84. }
  85. size_t remaining = message_len % SHA256_BLOCK_SZ;
  86. // Last message block, so apply SHA-256 padding rules in software
  87. uint8_t block[SHA256_BLOCK_SZ];
  88. uint64_t bit_len = __builtin_bswap64(message_len * 8 + 512);
  89. // If the remaining message and appended padding doesn't fit into a single block, we have to write an
  90. // extra block with the rest of the message and potential padding first.
  91. if (remaining >= SHA256_BLOCK_SZ - SHA256_PAD_SZ) {
  92. write_and_padd(block, message_bytes, remaining);
  93. hmac_hal_next_block_normal();
  94. hmac_hal_write_block_512(block);
  95. bzero(block, SHA256_BLOCK_SZ);
  96. } else {
  97. write_and_padd(block, message_bytes, remaining);
  98. }
  99. memcpy(block + SHA256_BLOCK_SZ - sizeof(bit_len),
  100. &bit_len, sizeof(bit_len));
  101. hmac_hal_next_block_padding();
  102. hmac_hal_write_block_512(block);
  103. }
  104. // Read back result (bit swapped)
  105. hmac_hal_read_result_256(hmac);
  106. periph_module_disable(PERIPH_DS_MODULE);
  107. periph_module_disable(PERIPH_SHA_MODULE);
  108. periph_module_disable(PERIPH_HMAC_MODULE);
  109. esp_crypto_hmac_lock_release();
  110. return ESP_OK;
  111. }