summaryrefslogtreecommitdiff
path: root/bsp/xuantie/libraries/xuantie_libraries/chip_riscv_dummy/src/drivers/aes.c
blob: 2749501431eade3b6f80cafb5d8741d5ddf47ba6 (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
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
/*
 *  Copyright (C) 2017-2024 Alibaba Group Holding Limited
 */

/*******************************************************
 * @file    wj_aes.c
 * @brief   source file for aes csi driver
 * @version V1.0
 * @date    2. July 2020
 * ******************************************************/

#include <drv/aes.h>
#include <drv/irq.h>

#define AES_READ_CACHE_LITTLE(_addr_)   \
    (((uint32_t) * (_addr_ + 0U) << 0)  | ((uint32_t) * (_addr_ + 1U) << 8) | \
     ((uint32_t) * (_addr_ + 2U) << 16) | ((uint32_t) * (_addr_ + 3U) << 24))

#define AES_WRITE_CACHE_LITTLE(_addr_, _data_) \
    do {  \
        *(_addr_+0U) = (uint8_t)*(_data_+0U);  \
        *(_addr_+1U) = (uint8_t)*(_data_+1U);  \
        *(_addr_+2U) = (uint8_t)*(_data_+2U);  \
        *(_addr_+3U) = (uint8_t)*(_data_+3U);  \
    } while(0)

#define AES_IS_16BYTES_ALIGNMENT(_size_)     ((_size_ % 16U) ? -1 : 0)

/**
  \brief       Initialize AES Interface. Initializes the resources needed for the AES interface
  \param[in]   aes    operate handle
  \param[in]   idx    device id
  \return      error code \ref csi_error_t
*/
csi_error_t csi_aes_init(csi_aes_t *aes, uint32_t idx)
{
    CSI_PARAM_CHK(aes, CSI_ERROR);

    csi_error_t ret = CSI_OK;

    ///< 获取中断号、基地址等相关信息
    if (0 == target_get(DEV_WJ_AES_TAG, idx, &aes->dev)) {

        ///< TODO:对AES的所有寄存器进行清零操作

    } else {
        ret = CSI_ERROR;
    }

    return ret;
}
/**
  \brief       De-initialize AES Interface. stops operation and releases the software resources used by the interface
  \param[in]   aes    aes handle to operate
  \return      None
*/
void csi_aes_uninit(csi_aes_t *aes)
{
    CSI_PARAM_CHK_NORETVAL(aes);

    ///< TODO:对AES的所有寄存器进行清零操作

}
/**
  \brief       Set encrypt key
  \param[in]   aes        aes handle to operate
  \param[in]   key        Pointer to the key buf
  \param[in]   key_len    Pointer to \ref csi_aes_key_bits_t
  \return      error code \ref csi_error_t
*/
csi_error_t csi_aes_set_encrypt_key(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len)
{
    CSI_PARAM_CHK(aes, CSI_ERROR);
    csi_error_t ret = CSI_OK;

    switch (key_len) {
        case AES_KEY_LEN_BITS_128:
            ///< TODO:设置密钥长度为128 bits
            break;

        case AES_KEY_LEN_BITS_192:
            ///< TODO:设置密钥长度为128 bits
            break;

        case AES_KEY_LEN_BITS_256:
            ///< TODO:设置密钥长度为128 bits
            break;

        default:
            ret = CSI_ERROR;
            break;
    }

        ///< TODO:设置密钥
        ///< TODO:使能AES
        ///< TODO:停止AES

    return ret;
}
/**
  \brief       Set decrypt key
  \param[in]   aes        aes handle to operate
  \param[in]   key        Pointer to the key buf
  \param[in]   key_len    Pointer to \ref csi_aes_key_bits_t
  \return      error code \ref csi_error_t
*/
csi_error_t csi_aes_set_decrypt_key(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len)
{
    CSI_PARAM_CHK(aes, CSI_ERROR);
    csi_error_t ret = CSI_OK;

    switch (key_len) {
        case AES_KEY_LEN_BITS_128:
            ///< TODO:设置密钥长度为128 bits
            break;

        case AES_KEY_LEN_BITS_192:
            ///< TODO:设置密钥长度为128 bits
            break;

        case AES_KEY_LEN_BITS_256:
            ///< TODO:设置密钥长度为128 bits
            break;

        default:
            ret = CSI_ERROR;
            break;
    }

       ///< TODO:设置密钥
       ///< TODO:使能AES
       ///< TODO:停止AES

    return ret;
}

/**
  \brief       Aes ecb encrypt or decrypt
  \param[in]   aes     aes handle to operate
  \param[in]   in      Pointer to the Source data
  \param[out]  out     Pointer to the Result data
  \param[in]   size    the Source data size
  \return      error code \ref csi_error_t
*/
csi_error_t csi_aes_ecb_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size)
{
    CSI_PARAM_CHK(aes, CSI_ERROR);
    csi_error_t ret = CSI_OK;

    uint32_t i = 0U;
    uint32_t j = 0U;
    uint32_t n = 0U;
    uint32_t temp;
    ///< 加密数据长度判断
    if (0 == AES_IS_16BYTES_ALIGNMENT(size)) {

       ///< TODO:设置ECB模式
       ///< TODO:设置加密

        /* set the text before aes calculating */
        for (i = 0U; i < size; i = i + 16U) {
            for (j = 0U; j < 4U; j++) {
                n = (j << 2) + i;            ///< j:each time offset 4bytes, i:each time offset 16bytes
                temp = AES_READ_CACHE_LITTLE((uint8_t *)in + n);
                ///< TODO:写入待加密数据
            }

            ///< TODO:开始加密

            ///< TODO:等待加密完成

            for (j = 0U; j < 4U; j++) {
                n = (j << 2) + i;            ///< j:each time offset 4bytes, i:each time offset 16bytes
                ///< TODO:获取加密数据到 temp
                AES_WRITE_CACHE_LITTLE((uint8_t *)out + n, (uint8_t *)&temp);
            }
        }
    } else {
        ret = CSI_ERROR;
    }

    return ret;
}
/**
  \brief       Aes ecb decrypt
  \param[in]   aes     aes handle to operate
  \param[in]   in      Pointer to the Source data
  \param[out]  out     Pointer to the Result data
  \param[in]   size    the Source data size
  \return      error code \ref csi_error_t
*/
csi_error_t csi_aes_ecb_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size)
{
    CSI_PARAM_CHK(aes, CSI_ERROR);
    csi_error_t ret = CSI_OK;

    uint32_t i = 0U;
    uint32_t j = 0U;
    uint32_t n = 0U;
    uint32_t temp;

    if (0 == AES_IS_16BYTES_ALIGNMENT(size)) {

       ///< TODO:设置ECB模式
       ///< TODO:设置解密


        /* set the text before aes calculating */
        for (i = 0U; i < size; i = i + 16U) {
            for (j = 0U; j < 4U; j++) {
                n = (j << 2) + i;            ///< j:each time offset 4bytes, i:each time offset 16bytes
                temp = AES_READ_CACHE_LITTLE((uint8_t *)in + n);
                 ///< TODO:写入待解密数据
            }

            ///< TODO:开始解密


            ///< TODO:等待加密完成

            for (j = 0U; j < 4U; j++) {
                n = (j << 2) + i;            ///< j:each time offset 4bytes, i:each time offset 16bytes
                ///< TODO:获取解密数据到 temp
                AES_WRITE_CACHE_LITTLE((uint8_t *)out + n, (uint8_t *)&temp);
            }
        }
    } else {
        ret = CSI_ERROR;
    }

    return ret;
}
/**
  \brief       Aes cbc encrypt or decrypt
  \param[in]   aes     aes handle to operate
  \param[in]   in      Pointer to the Source data
  \param[out]  out     Pointer to the Result data
  \param[in]   size    the Source data size
  \return      error code \ref csi_error_t
*/
csi_error_t csi_aes_cbc_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv)
{
    CSI_PARAM_CHK(aes, CSI_ERROR);
    csi_error_t ret = CSI_OK;

    uint32_t i = 0U;
    uint32_t j = 0U;
    uint32_t n = 0U;
    uint32_t temp;

    if (0 == AES_IS_16BYTES_ALIGNMENT(size)) {
        ///< TODO:清除IV 值

       ///< TODO:设置CBC模式
       ///< TODO:设置加密密

        /* set iv if the mode is CBC */
        for (i = 0U; i < 4U; i++) {
            temp = AES_READ_CACHE_LITTLE((uint8_t *)iv + (i << 2));
            ///< TODO:设置CBC值
        }

        /* set the text before aes calculating */
        for (i = 0U; i < size; i = i + 16U) {
            for (j = 0U; j < 4U; j++) {
                n = (j << 2) + i;            ///< j:each time offset 4bytes, i:each time offset 16bytes
                temp = AES_READ_CACHE_LITTLE((uint8_t *)in + n);
                ///< TODO:写入待加密数据
            }

            ///< TODO:开始加密

            ///< TODO:等待加密

            for (j = 0U; j < 4U; j++) {
                n = (j << 2) + i;            ///< j:each time offset 4bytes, i:each time offset 16bytes
                ///< TODO:获取加密数据到 temp
                AES_WRITE_CACHE_LITTLE((uint8_t *)out + n, (uint8_t *)&temp);
            }
        }

        for (i = 0U; i < 4U; i++) {
            ///< TODO:获取IV 数据到 temp
            AES_WRITE_CACHE_LITTLE((uint8_t *)iv + (i << 2), (uint8_t *)&temp);
        }
    } else {
        ret = CSI_ERROR;
    }

    return ret;
}
/**
  \brief       Aes cbc decrypt
  \param[in]   aes     aes handle to operate
  \param[in]   in      Pointer to the Source data
  \param[out]  out     Pointer to the Result data
  \param[in]   size    the Source data size
  \param[in]   iv      init vector
  \return      error code \ref csi_error_t
*/
csi_error_t csi_aes_cbc_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv)
{
    CSI_PARAM_CHK(aes, CSI_ERROR);
    csi_error_t ret = CSI_OK;

    uint32_t i = 0U;
    uint32_t j = 0U;
    uint32_t n = 0U;
    uint32_t temp = 0U;

    if (0 == AES_IS_16BYTES_ALIGNMENT(size)) {
        ///< TODO:清除IV 值


       ///< TODO:设置CBC模式
       ///< TODO:设置解密

        ///< TODO:设置IV 值

        /* set the text before aes calculating */
        for (i = 0U; i < size; i = i + 16U) {
            for (j = 0U; j < 4U; j++) {
                n = (j << 2) + i;            ///< j:each time offset 4bytes, i:each time offset 16bytes
                temp = AES_READ_CACHE_LITTLE((uint8_t *)in + n);
                ///< TODO:写入待解密数据
            }

            ///< TODO:开始解密

            ///< TODO:等待解密


            for (j = 0U; j < 4U; j++) {
                n = (j << 2) + i;            ///< j:each time offset 4bytes, i:each time offset 16bytes
                ///< TODO:获取解密数据到 temp
                AES_WRITE_CACHE_LITTLE((uint8_t *)out + n, (uint8_t *)&temp);
            }
        }

        for (i = 0U; i < 4U; i++) {
            ///< TODO:获取IV数据到 temp
            AES_WRITE_CACHE_LITTLE((uint8_t *)iv + (i << 2), (uint8_t *)&temp);
        }
    } else {
        ret = CSI_ERROR;
    }

    return ret;
}