sec_crypto_mac.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file seccrypt_mac.h
  6. * @brief Header File for MAC
  7. * @version V1.0
  8. * @date 20. Jul 2020
  9. * @model mac
  10. ******************************************************************************/
  11. #ifndef _SC_MAC_H_
  12. #define _SC_MAC_H_
  13. #include "sec_include_config.h"
  14. #include <stdint.h>
  15. #include "sec_crypto_errcode.h"
  16. #include "sec_crypto_sha.h"
  17. #define SC_MAC_KEY_LEN_MAX (64)
  18. #define HMAC_SHA1_BLOCK_SIZE (64)
  19. #define HMAC_SHA224_BLOCK_SIZE (64)
  20. #define HMAC_SM3_BLOCK_SIZE (64)
  21. #define HMAC_SHA256_BLOCK_SIZE (64)
  22. #define HMAC_MD5_BLOCK_SIZE (64)
  23. #define HMAC_SHA384_BLOCK_SIZE (128)
  24. #define HMAC_SHA512_BLOCK_SIZE (128)
  25. #define HMAC_MAX_BLOCK_SIZE (128)
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. typedef struct sc_mac {
  30. sc_sha_t sha;
  31. uint8_t key[HMAC_MAX_BLOCK_SIZE];
  32. sc_sha_mode_t mode;
  33. } sc_mac_t;
  34. #define MAC_CONTEXT_SIZE sizeof(sc_sha_context_t)
  35. typedef struct {
  36. uint8_t ctx[MAC_CONTEXT_SIZE];
  37. } sc_mac_context_t;
  38. /**
  39. \brief Initialize MAC Interface. Initializes the resources needed for the MAC interface
  40. \param[in] mac operate handle.
  41. \param[in] idx index of mac
  42. \return error code \ref uint32_t
  43. */
  44. uint32_t sc_mac_init(sc_mac_t *mac, uint32_t idx);
  45. /**
  46. \brief De-initialize MAC Interface. stops operation and releases the software resources used by the interface
  47. \param[in] mac mac handle to operate.
  48. \return none
  49. */
  50. void sc_mac_uninit(sc_mac_t *mac);
  51. /**
  52. \brief MAC set key function.
  53. \param[in] mac mac handle to operate.
  54. \param[in] key Pointer to the mac key.
  55. \param[in] key_len Length of key.
  56. \return error code
  57. */
  58. uint32_t sc_mac_set_key(sc_mac_t *mac, uint8_t *key, uint32_t key_len);
  59. /**
  60. \brief MAC operation function.
  61. \param[in] mac mac handle to operate.
  62. \param[in] mode sc_sha_mode_t.
  63. \param[in] msg Pointer to the mac input message.
  64. \param[in] msg_len Length of msg.
  65. \param[out] out mac buffer, malloc by caller.
  66. \param[out] out_len out mac length, should 32 bytes if HMAC_SHA256 mode.
  67. \return error code
  68. */
  69. uint32_t sc_mac_calc(sc_mac_t *mac, sc_sha_mode_t mode, uint8_t *msg,
  70. uint32_t msg_len, uint8_t *out, uint32_t *out_len);
  71. /**
  72. \brief MAC start operation function.
  73. \param[in] mac mac handle to operate.
  74. \param[in] context mac context pointer.
  75. \param[in] mode sc_sha_mode_t.
  76. \return error code
  77. */
  78. uint32_t sc_mac_start(sc_mac_t *mac, sc_mac_context_t *context, sc_sha_mode_t mode);
  79. /**
  80. \brief MAC start operation function.
  81. \param[in] mac mac handle to operate.
  82. \param[in] context mac context pointer.
  83. \param[in] msg Pointer to the mac input message.
  84. \param[in] msg_len Length of msg.
  85. \return error code
  86. */
  87. uint32_t sc_mac_update(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *msg, uint32_t msg_len);
  88. /**
  89. \brief MAC start operation function.
  90. \param[in] mac mac handle to operate.
  91. \param[in] context mac context.
  92. \param[out] out mac buffer, malloc by caller.
  93. \param[out] out_len out mac length,
  94. \return error code
  95. */
  96. uint32_t sc_mac_finish(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *out, uint32_t *out_len);
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif /* _SC_MAC_H_ */