sec_crypto_mac.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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,
  67. should 32 bytes if HMAC_SHA256 mode.
  68. \return error code
  69. */
  70. uint32_t sc_mac_calc(sc_mac_t *mac, sc_sha_mode_t mode, uint8_t *msg,
  71. uint32_t msg_len, uint8_t *out, uint32_t *out_len);
  72. /**
  73. \brief MAC start operation function.
  74. \param[in] mac mac handle to operate.
  75. \param[in] context mac context pointer.
  76. \param[in] mode sc_sha_mode_t.
  77. \return error code
  78. */
  79. uint32_t sc_mac_start(sc_mac_t *mac, sc_mac_context_t *context,
  80. sc_sha_mode_t mode);
  81. /**
  82. \brief MAC start operation function.
  83. \param[in] mac mac handle to operate.
  84. \param[in] context mac context pointer.
  85. \param[in] msg Pointer to the mac input message.
  86. \param[in] msg_len Length of msg.
  87. \return error code
  88. */
  89. uint32_t sc_mac_update(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *msg,
  90. uint32_t msg_len);
  91. /**
  92. \brief MAC start operation function.
  93. \param[in] mac mac handle to operate.
  94. \param[in] context mac context pointer.
  95. \param[out] out mac buffer, malloc by caller.
  96. \param[out] out_len, out mac length,
  97. \return error code
  98. */
  99. uint32_t sc_mac_finish(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *out,
  100. uint32_t *out_len);
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif /* _SC_MAC_H_ */