summaryrefslogtreecommitdiff
path: root/bsp/ck802/libraries/include/drv_aes.h
blob: 7fa5e7c817b2b43f8527c4dbb2c921d332e5047c (plain)
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
/*
 * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/******************************************************************************
 * @file     drv_aes.h
 * @brief    Header File for AES Driver
 * @version  V1.0
 * @date     02. June 2017
 ******************************************************************************/
#ifndef _CSI_AES_H_
#define _CSI_AES_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <drv_common.h>
#include <drv_errno.h>


/// definition for aes handle.
typedef void *aes_handle_t;

/****** AES specific error codes *****/
typedef enum {
    AES_ERROR_MODE  = (EDRV_SPECIFIC + 1)   ,     ///< Specified Mode not supported
    AES_ERROR_DATA_BITS                     ,     ///< Specified number of Data bits not supported
    AES_ERROR_ENDIAN                              ///< Specified endian not supported
} drv_aes_error_e;

/*----- AES Control Codes: Mode -----*/
typedef enum {
    AES_MODE_ECB                  = 0,   ///< ECB Mode
    AES_MODE_CBC                     ,   ///< CBC Mode
    AES_MODE_CFB                     ,   ///< CFB Mode
    AES_MODE_OFB                     ,   ///< OFB Mode
    AES_MODE_CTR                         ///< CTR Mode
} aes_mode_e;

/*----- AES Control Codes: Crypto Mode -----*/
typedef enum {
    AES_CRYPTO_MODE_ENCRYPT                  = 0,   ///< encrypt Mode
    AES_CRYPTO_MODE_DECRYPT                     ,   ///< decrypt Mode
} aes_crypto_mode_e;

/*----- AES Control Codes: Padding Mode -----*/
typedef enum {
    AES_PADDING_MODE_NO     = 0,        ///< NO-PADDING
    AES_PADDING_MODE_ZERO      ,        ///< ZERO-PADDING
    AES_PADDING_MODE_PKCS5              ///< PKCS5-PADDING
} aes_padding_mode_e;

/*----- AES Control Codes: Mode Parameters: Key length -----*/
typedef enum {
    AES_KEY_LEN_BITS_128        = 0,      ///< 128 Data bits
    AES_KEY_LEN_BITS_192           ,      ///< 192 Data bits
    AES_KEY_LEN_BITS_256                  ///< 256 Data bits
} aes_key_len_bits_e;

/*----- AES Control Codes: Mode Parameters: Endian -----*/
typedef enum {
    AES_ENDIAN_LITTLE          = 0,       ///< Little Endian
    AES_ENDIAN_BIG                        ///< Big Endian
} aes_endian_mode_e;

/**
\brief AES Status
*/
typedef struct {
    uint32_t busy             : 1;        ///< busy flag
} aes_status_t;

/****** AES Event *****/
typedef enum {
    AES_EVENT_CRYPTO_COMPLETE    = 0   ///< Encrypt completed
} aes_event_e;
typedef void (*aes_event_cb_t)(aes_event_e event);   ///< Pointer to \ref aes_event_cb_t : AES Event call back.


/**
\brief AES Device Driver Capabilities.
*/
typedef struct {
    uint32_t ecb_mode           : 1;      ///< supports ECB mode
    uint32_t cbc_mode           : 1;      ///< supports CBC mode
    uint32_t cfb_mode           : 1;      ///< supports CFB mode
    uint32_t ofb_mode           : 1;      ///< supports OFB mode
    uint32_t ctr_mode           : 1;      ///< supports CTR mode
    uint32_t bits_128           : 1;      ///< supports 128bits key length
    uint32_t bits_192           : 1;      ///< supports 192bits key length
    uint32_t bits_256           : 1;      ///< supports 256bits key length
} aes_capabilities_t;


// Function documentation

/**
  \brief       get aes instance count.
  \return      aes handle count
*/
int32_t csi_aes_get_instance_count(void);

/**
  \brief       Initialize AES Interface. 1. Initializes the resources needed for the AES interface 2.registers event callback function
  \param[in]   idx must not exceed return value of csi_aes_get_instance_count().
  \param[in]   cb_event  Pointer to \ref aes_event_cb_t
  \return      return aes handle if success
*/
aes_handle_t csi_aes_initialize(int32_t idx, aes_event_cb_t cb_event);

/**
  \brief       De-initialize AES Interface. stops operation and releases the software resources used by the interface
  \param[in]   handle  aes handle to operate.
  \return      error code
*/
int32_t csi_aes_uninitialize(aes_handle_t handle);

/**
  \brief       Get driver capabilities.
  \param[in]   handle  aes handle to operate.
  \return      \ref aes_capabilities_t
*/
aes_capabilities_t csi_aes_get_capabilities(aes_handle_t handle);

/**
  \brief       config aes mode.
  \param[in]   handle  aes handle to operate.
  \param[in]   mode      \ref aes_mode_e
  \param[in]   keylen_bits \ref aes_key_len_bits_e
  \param[in]   endian    \ref aes_endian_mode_e
  \param[in]   arg       Pointer to the iv address when mode is cbc_mode
  \return      error code
*/
int32_t csi_aes_config(aes_handle_t handle,
                       aes_mode_e mode,
                       aes_key_len_bits_e keylen_bits,
                       aes_endian_mode_e endian,
                       uint32_t arg
                      );

/**
  \brief       set crypto key.
  \param[in]   handle    aes handle to operate.
  \param[in]   context   aes information context(NULL when hardware implementation)
  \param[in]   key       Pointer to the key buf
  \param[in]   key_len    the key len
  \param[in]   enc       \ref aes_crypto_mode_e
  \return      error code
*/
int32_t csi_aes_set_key(aes_handle_t handle, void *context, void *key, uint32_t key_len, aes_crypto_mode_e enc);

/**
  \brief       encrypt or decrypt
  \param[in]   handle  aes handle to operate.
  \param[in]   context aes information context(NULL when hardware implementation)
  \param[in]   in   Pointer to the Source data
  \param[out]  out  Pointer to the Result data.
  \param[in]   len  the Source data len.
  \param[in]   padding \ref aes_padding_mode_e.
  \return      error code
*/
int32_t csi_aes_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len, aes_padding_mode_e padding);

/**
  \brief       Get AES status.
  \param[in]   handle  aes handle to operate.
  \return      AES status \ref aes_status_t
*/
aes_status_t csi_aes_get_status(aes_handle_t handle);

#ifdef __cplusplus
}
#endif

#endif /* _CSI_AES_H_ */