chen
2024-07-29 13ee763a011697633a072a74a25c3eee9f40bb4f
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
 * its subsidiaries and affiliates (collectly called MKSEMI).
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into an MKSEMI
 *    integrated circuit in a product or a software update for such product,
 *    must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of MKSEMI nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    MKSEMI integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be
 *    reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#ifndef MK_AES_H_
#define MK_AES_H_
#include "mk_common.h"
#include "mk_dma.h"
 
#ifndef AES_INT_MODE_EN
#define AES_INT_MODE_EN (1)
#endif
 
#ifndef AES_DMA_MODE_EN
#define AES_DMA_MODE_EN (1)
#endif
 
#ifndef AES_POLL_MODE_EN
#define AES_POLL_MODE_EN (1)
#endif
 
/**
 * @addtogroup MK8000_AES
 * @{
 */
 
/**
 * @brief AES device IDs enumeration
 */
enum AES_DEV_T
{
    AES_ID0 = 0,
    AES_MAX_NUM
};
 
/**
 * @brief AES algorithm mode enumeration
 */
enum AES_MODE_T
{
    AES_MODE_ECB = 0, /*!< Electronic codebook chaining algorithm */
    AES_MODE_CBC,     /*!< Cipher block chaining algorithm        */
    AES_MODE_CTR,     /*!< Counter mode chaining algorithm        */
    AES_MODE_CCM,     /*!< Counter with Cipher Mode               */
    AES_MODE_DKEY,    /*!< Calculate decipher key for ECB and CBC */
};
 
/**
 * @brief AES work mode enumeration
 */
enum AES_DIR_T
{
    AES_DIR_ENCRYPT = 0,
    AES_DIR_DECRYPT,
};
 
/**
 * @brief AES CCM mode enumeration
 */
enum AES_CCM_MODE
{
    AES_CCM_ENCRYPT_AND_AUTH = 0,  /*!< CCM encryption and authentication  */
    AES_CCM_DECRYPT_AND_AUTH,      /*!< CCM decryption and authentication  */
    AES_CCM_STAR_ENCRYPT_AND_AUTH, /*!< CCM* encryption and authentication */
    AES_CCM_STAR_DECRYPT_AND_AUTH, /*!< CCM* decryption and authentication */
};
 
/**
 * @brief AES state enumeration
 */
enum AES_STATE_T
{
    AES_STATE_RESET = 0x00U, /*!< AES not yet initialized or disabled */
    AES_STATE_READY = 0x01U, /*!< AES initialized and ready for use   */
    AES_STATE_BREAK = 0x02U,
    AES_STATE_CONFLICT = 0x04U,
    AES_STATE_BUSY = 0x10U, /*!< AES internal processing is ongoing  */
    AES_STATE_TIMEOUT = 0x40U,
    AES_STATE_ERROR = 0x80U, /*!< AES error state                     */
};
 
/**
 * @brief AES CCM authentication checking result enumeration
 */
enum AES_MIN_CHE_T
{
    AES_MIN_VALID = 1,   /*!< MACTag verification is valid */
    AES_MIN_INVALID = 0, /*!< MACTag verification is invalid */
};
 
/**
 * @brief AES configure Structure
 */
struct AES_CFG_T
{
    uint8_t key[16];         /*!< The key used for encryption/decryption */
    uint8_t int_en;          /*!< Specifies whether the interrupt is enabled or disabled
                                  This parameter will be one of the following values:
                                      @arg true is enable
                                      @arg false is disable
                             */
    uint8_t dma_en;          /*!< Specifies whether the dma is enabled or disabled.
                                  This parameter shoule be one of the following values:
                                      @arg true is enable
                                      @arg false is disable
                             */
    uint8_t din_burst_size;  /*!< Specifies the Burst transfer configuration for the input memory.
                                      This parameter can be a value of @ref DMA_SRC_BURST_SIZE_T
                             */
    uint8_t dout_burst_size; /*!< Specifies the Burst transfer configuration for the output memory.
                                      This parameter can be a value of @ref DMA_SRC_BURST_SIZE_T
                             */
};
 
#define MK_AES_CCM_ERROR_MAC_SIZE 1              /*!< The MAC (Message Authentication Code) size is invalid.  */
#define MK_AES_CCM_ERROR_NONCE_SIZE 2            /*!< The nonce size is invalid.                              */
#define MK_AES_CCM_ERROR_INPUT_BUF_NOT_ENOUGH 3  /*!< Input buffer space is too small.                        */
#define MK_AES_CCM_ERROR_OUTPUT_BUF_NOT_ENOUGH 4 /*!< Output buffer space is too small.                       */
#define MK_AES_CCM_ERROR_ADATA_LEN_NOT_SUPPORT 5 /*!< Additional authentication data is too long.             */
 
/**
 * @brief AES CCM context Structure
 */
struct AES_CCM_CONTEXT_T
{
    uint8_t mac_size;         /*!< MAC (Message Authentication Code) byte size.
                                   Valid values for supported follow:
                                   CCM    [4, 6, 8, 10, 12, 14, 16]
                                   CCM*   [0, 4, 8, 16]
                              */
    uint8_t nonce_size;       /*!< Nonce byte size. Valid values for supported follow:
                                    CCM    [7 ... 13]
                                    CCM*   [13]
                              */
    const uint8_t *nonce;     /*!< Pointer to nonce. */
    const uint8_t *adata;     /*!< Pointer to additional authenticated data */
    const uint8_t *data_in;   /*!< Pointer to the input data for encryption or decryption */
    uint8_t *input_buf;       /*!< Pointer to the input data buffer, This buffer is used to temporarily
                                   store verification data and plaintext data or to store verification
                                   data and ciphertext data. The buffer size must be greater or equal than
                                   the sum of adata_len + data_in_len + 18
                              */
    uint8_t *output_buf;      /*!< Pointer to the output buffer where encrypted or decrypted data will be stored
                                   If encrypted, the length of the buffer is at least the sum of data_in_len + mac_size
                                   If decrypted, the length of the buffer is at least data_in_len - mac_size
                              */
    uint32_t input_buf_size;  /*!< Length of Input data buffer in bytes
                                   (input_buf_size >= adata_len + data_in_len + 18) */
    uint32_t output_buf_size; /*!< Length of output data buffer in bytes:
                                   Encrypt mode: output_buf_size >= data_in_len + mac_size
                                   Decrypt mode: output_buf_size >= data_in_len
                              */
    uint16_t adata_len;       /*!< Length of additional authenticated data in bytes
                                   For MK8000, adata_len shall be limited to 65517 bytes */
    uint16_t data_in_len;     /*!< Length of encryption or decryption data in bytes
                                   For MK8000, adata_len shall be limited to 65535 bytes */
};
 
/**
 * @brief AES handle Structure
 */
struct AES_HANDLE_T
{
    AES_TypeDef *const base;           /*!< AES registers base address  */
    const IRQn_Type irq;               /*!< AES interrupt number        */
    enum DMA_CH_T dma_in_ch;           /*!< AES data input DMA  channel */
    enum DMA_CH_T dma_out_ch;          /*!< AES data output DMA channel */
    enum AES_STATE_T state;            /*!< AES state                   */
    __IOM enum AES_MIN_CHE_T is_valid; /*!< AES CCM authentication checking result */
    struct AES_CFG_T config;           /*!< AES configure Structure     */
    uint8_t *input;                    /*!< Pointer to AES encryption or decryption input buffer  */
    uint8_t *output;                   /*!< Pointer to AES encryption or decryption output buffer */
    uint16_t input_len;                /*!< Length of input data        */
    uint16_t output_len;               /*!< Length of output data       */
    __IOM uint16_t input_count;        /*!< Counter of input data       */
    __IOM uint16_t output_count;       /*!< Counter of output data      */
    drv_callback_t callback;           /*!< Callback function provided by the user */
};
 
#ifdef __cplusplus
extern "C" {
#endif
 
/**
 * @brief Function for initializing the AES.
 *
 * @param[in] id            AES device instance ID.
 * @param[in] config        Pointer to a AES_CFG_T structure that contains the configuration information for AES.
 *                          If this cfg is NULL, this function will use the default config.
 * @return
 *         @arg DEV_ERROR   error id
 *         @arg DEV_OK      succeed
 */
int aes_open(enum AES_DEV_T id, struct AES_CFG_T *config);
 
/**
 * @brief Function for uninitializing the AES.
 *
 * @param[in] id            AES device instance ID.
 * @return
 *         @arg DEV_ERROR   error id
 *         @arg DEV_OK      succeed
 */
int aes_close(enum AES_DEV_T id);
 
/**
 * @brief Function for restoring the AES busy state to the ready state.
 *
 * @param[in] id            AES device instance ID.
 * @return
 *         @arg DEV_OK      succeed
 */
int aes_clear(enum AES_DEV_T id);
 
/**
 * @brief Function for updating the key.
 *
 * @param[in] id            AES device instance ID.
 * @param[in] key           Array to store keys.
 * @return
 *         @arg DEV_OK      succeed
 */
int aes_update_key(enum AES_DEV_T id, uint8_t key[16]);
 
/**
 * @brief Function for setting AES state
 *
 * @param[in] id            AES device instance ID.
 * @param[in] state         AES state value to be set.
 */
void aes_set_state(enum AES_DEV_T id, enum AES_STATE_T state);
 
/**
 * @brief Function for clearing AES state
 *
 * @param[in] id            AES device instance ID.
 * @param[in] state         AES state value to clear.
 */
void aes_clr_state(enum AES_DEV_T id, enum AES_STATE_T state);
 
/**
 * @brief Function for getting the current state of AES.
 *
 * @param[in] id            AES device instance ID.
 * @return
 *         This result can be a value of @ref AES_STATE_T
 */
uint8_t aes_get_state(enum AES_DEV_T id);
 
/**
 * @brief AES encrypts or decrypts data in AES ECB mode.
 *
 * @param[in] id            AES device instance ID.
 * @param[in] mode          AES encryption and decryption mode settings.
 *                          This parameter can be a value of @ref AES_DIR_T
 * @param[in] input         Pointer to the input (plaintext or cyphertext) buffer.
 * @param[out] output       Pointer to the output (plaintext or cyphertext) buffer.
 * @param[in] len           Length of input and output data (in bytes), must be a multiple of 16.
 * @param[in] callback      Callback function provided by the user.
 * @return
 *         @arg DEV_ERROR   error id
 *         @arg DRV_BUSY    AES internal processing is ongoing
 *         @arg DEV_OK      AES operation is successful
 */
int aes_crypt_ecb(enum AES_DEV_T id, enum AES_DIR_T mode, uint8_t *input, uint8_t *output, uint16_t len, drv_callback_t callback);
 
/**
 * @brief AES encrypts or decrypts data in AES CBC mode.
 *
 * @param[in] id            AES device instance ID.
 * @param[in] mode          AES encryption and decryption mode settings.
 *                          This parameter can be a value of @ref AES_DIR_T
 * @param[in] iv            Array to store IV.
 * @param[in] input         Pointer to the input (plaintext or cyphertext) buffer.
 * @param[out] output       Pointer to the output (plaintext or cyphertext) buffer.
 * @param[in] len           Length of input and output data (in bytes), must be a multiple of 16.
 * @param[in] callback      Callback function provided by the user.
 * @return
 *         @arg DEV_ERROR   error id
 *         @arg DRV_BUSY    AES internal processing is ongoing
 *         @arg DEV_OK      AES operation is successful
 */
int aes_crypt_cbc(enum AES_DEV_T id, enum AES_DIR_T mode, uint8_t iv[16], uint8_t *input, uint8_t *output, uint16_t len, drv_callback_t callback);
 
/**
 * @brief AES encrypts or decrypts data in AES CTR mode.
 *
 * @param[in] id                AES device instance ID.
 * @param[in] initial_counter   Array to store initial counter.
 * @param[in] input             Pointer to the input (plaintext or cyphertext) buffer.
 * @param[out] output           Pointer to the output (plaintext or cyphertext) buffer.
 * @param[in] len               Length of input and output data (in bytes), must be a multiple of 16.
 * @param[in] callback          Callback function provided by the user.
 * @return
 *         @arg DEV_ERROR   error id
 *         @arg DRV_BUSY    AES internal processing is ongoing
 *         @arg DEV_OK      AES operation is successful
 */
int aes_crypt_ctr(enum AES_DEV_T id, uint8_t initial_counter[16], uint8_t *input, uint8_t *output, uint16_t len, drv_callback_t callback);
 
/**
 * @brief AES encrypts or decrypts data in AES CCM mode.
 *
 * @param[in] id            AES device instance ID.
 * @param[in] opt           AES CCM mode enumeration.
 * @param[in] cxt           Pointer to struct AES_CCM_CONTEXT_T @ref AES_CCM_CONTEXT_T.
 * @param[in] callback      Callback function provided by the user.
 * @return
 *         @arg DEV_ERROR   error id
 *         @arg DRV_BUSY    AES internal processing is ongoing
 *         @arg DEV_OK      AES operation is successful
 */
int aes_crypt_ccm(enum AES_DEV_T id, enum AES_CCM_MODE opt, struct AES_CCM_CONTEXT_T *cxt, drv_callback_t callback);
 
/**
 * @brief Get AES CCM authentication checking result
 *
 * @param[in] id        AES device instance ID.
 * @return
 *         @ref AES_MIN_CHE_T
 *         @arg AES_MIN_VALID is valid
 *         @arg AES_MIN_INVALID is invalid
 */
enum AES_MIN_CHE_T aes_ccm_dec_mic_isvalid(enum AES_DEV_T id);
 
/**
 * @brief Function for Interrupt handler for AES.
 */
void AES_IRQHandler(void);
 
#ifdef __cplusplus
}
#endif
 
/**
 * @}
 */
 
#endif /* MK_AES_H_ */