aboutsummaryrefslogtreecommitdiff
path: root/lib/sec_library/include/sec_crypto_mac.h
blob: dd82105fd54f91a712f0a91f954fa2494bc69397 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2017-2020 Alibaba Group Holding Limited
 */
/******************************************************************************
 * @file     seccrypt_mac.h
 * @brief    Header File for MAC
 * @version  V1.0
 * @date     20. Jul 2020
 * @model    mac
 ******************************************************************************/
#ifndef _SC_MAC_H_
#define _SC_MAC_H_
#include "sec_include_config.h"
#include <stdint.h>
#include "sec_crypto_errcode.h"
#include "sec_crypto_sha.h"

#define SC_MAC_KEY_LEN_MAX      (64)
#define HMAC_SHA1_BLOCK_SIZE    (64)
#define HMAC_SHA224_BLOCK_SIZE  (64)
#define HMAC_SM3_BLOCK_SIZE     (64)
#define HMAC_SHA256_BLOCK_SIZE  (64)
#define HMAC_MD5_BLOCK_SIZE     (64)
#define HMAC_SHA384_BLOCK_SIZE  (128)
#define HMAC_SHA512_BLOCK_SIZE  (128)
#define HMAC_MAX_BLOCK_SIZE     (128)

#ifdef __cplusplus
extern "C" {
#endif


typedef struct sc_mac {
	sc_sha_t sha;
	uint8_t key[HMAC_MAX_BLOCK_SIZE];
    sc_sha_mode_t mode;
} sc_mac_t;

#define MAC_CONTEXT_SIZE sizeof(sc_sha_context_t)
typedef struct {
	uint8_t ctx[MAC_CONTEXT_SIZE];
} sc_mac_context_t;

/**
  \brief       Initialize MAC Interface. Initializes the resources needed for the MAC interface
  \param[in]   mac  operate handle.
  \param[in]   idx  index of mac
  \return      error code \ref uint32_t
*/
uint32_t sc_mac_init(sc_mac_t *mac, uint32_t idx);

/**
  \brief       De-initialize MAC Interface. stops operation and releases the software resources used by the interface
  \param[in]   mac  mac handle to operate.
  \return      none
*/
void sc_mac_uninit(sc_mac_t *mac);

/**
  \brief       MAC      set key function.
  \param[in]   mac      mac handle to operate.
  \param[in]   key      Pointer to the mac key.
  \param[in]   key_len  Length of key.
  \return      error code
*/
uint32_t sc_mac_set_key(sc_mac_t *mac, uint8_t *key, uint32_t key_len);

/**
  \brief       MAC operation function.
  \param[in]   mac      mac handle to operate.
  \param[in]   mode     sc_sha_mode_t.
  \param[in]   msg      Pointer to the mac input message.
  \param[in]   msg_len  Length of msg.
  \param[out]  out      mac buffer, malloc by caller.
  \param[out]  out_len  out mac length, should 32 bytes if HMAC_SHA256 mode.
  \return      error code
*/
uint32_t sc_mac_calc(sc_mac_t *mac, sc_sha_mode_t mode, uint8_t *msg,
		     uint32_t msg_len, uint8_t *out, uint32_t *out_len);

/**
  \brief       MAC start operation function.
  \param[in]   mac 		  mac handle to operate.
  \param[in]   context 	mac context pointer.
  \param[in]   mode 	  sc_sha_mode_t.
  \return      error code
*/
uint32_t sc_mac_start(sc_mac_t *mac, sc_mac_context_t *context, sc_sha_mode_t mode);

/**
  \brief       MAC start operation function.
  \param[in]   mac      mac handle to operate.
  \param[in]   context  mac context pointer.
  \param[in]   msg      Pointer to the mac input message.
  \param[in]   msg_len  Length of msg.
  \return      error code
*/
uint32_t sc_mac_update(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *msg, uint32_t msg_len);

/**
  \brief       MAC start operation function.
  \param[in]   mac 		mac handle to operate.
  \param[in]   context 	mac context.
  \param[out]  out 		mac buffer, malloc by caller.
  \param[out]  out_len  out mac length,
  \return      error code
*/
uint32_t sc_mac_finish(sc_mac_t *mac, sc_mac_context_t *context, uint8_t *out, uint32_t *out_len);
#ifdef __cplusplus
}
#endif

#endif /* _SC_MAC_H_ */