ds_hal.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. //
  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. * NOTICE
  16. * The hal is not public api, don't use it in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. #pragma once
  20. #if CONFIG_IDF_TARGET_ESP32
  21. #error "ESP32 doesn't have a DS peripheral"
  22. #endif
  23. #include <stdint.h>
  24. #include <stddef.h>
  25. #include <stdbool.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. * The result when checking whether the key to decrypt the RSA parameters is ready.
  31. */
  32. typedef enum {
  33. DS_KEY_INPUT_OK = 0, /**< The decryption key is ready. */
  34. DS_NO_KEY_INPUT, /**< Dependent peripheral providing key hasn't been activated. */
  35. DS_OTHER_WRONG, /**< Dependent peripheral running but problem receiving the key. */
  36. } ds_key_check_t;
  37. typedef enum {
  38. DS_SIGNATURE_OK = 0, /**< Signature is valid and can be read. */
  39. DS_SIGNATURE_PADDING_FAIL = 1, /**< Padding invalid, signature can be read if user wants it. */
  40. DS_SIGNATURE_MD_FAIL = 2, /**< Message digest check failed, signature invalid. */
  41. DS_SIGNATURE_PADDING_AND_MD_FAIL = 3, /**< Both padding and MD check failed. */
  42. } ds_signature_check_t;
  43. /**
  44. * @brief Start the whole signing process after the input key is ready.
  45. *
  46. * Call this before using any of the functions below. The input key is ready must be ready at this point.
  47. */
  48. void ds_hal_start(void);
  49. /**
  50. * @brief Finish the whole signing process. Call this after the signature is read or in case of an error.
  51. */
  52. void ds_hal_finish(void);
  53. /**
  54. * @brief Check whether the key input (HMAC on ESP32-C3) is correct.
  55. */
  56. ds_key_check_t ds_hal_check_decryption_key(void);
  57. /**
  58. * @brief Write the initialization vector.
  59. */
  60. void ds_hal_configure_iv(const uint32_t *iv);
  61. /**
  62. * @brief Write the message which should be signed.
  63. *
  64. * @param msg Pointer to the message.
  65. * @param size Length of signature result in bytes. It is the RSA signature length in bytes.
  66. */
  67. void ds_hal_write_message(const uint8_t *msg, size_t size);
  68. /**
  69. * @brief Write the encrypted private key parameters.
  70. */
  71. void ds_hal_write_private_key_params(const uint8_t *block);
  72. /**
  73. * @brief Begin signing procedure.
  74. */
  75. void ds_hal_start_sign(void);
  76. /**
  77. * @brief Check whether the hardware is busy with an operation.
  78. *
  79. * @return True if the hardware has finished the signing procedure, otherwise false.
  80. */
  81. bool ds_hal_busy(void);
  82. /**
  83. * @brief Check and read the signature from the hardware.
  84. *
  85. * @return
  86. * - DS_SIGNATURE_OK if no issue is detected with the signature.
  87. * - DS_SIGNATURE_PADDING_FAIL if the padding of the private key parameters is wrong.
  88. * - DS_SIGNATURE_MD_FAIL if the message digest check failed. This means that the message digest calculated using
  89. * the private key parameters fails, i.e., the integrity of the private key parameters is not protected.
  90. * - DS_SIGNATURE_PADDING_AND_MD_FAIL if both padding and message digest check fail.
  91. */
  92. ds_signature_check_t ds_hal_read_result(uint8_t *result, size_t size);
  93. #ifdef __cplusplus
  94. }
  95. #endif