summaryrefslogtreecommitdiff
path: root/bsp/n32g452xx/Libraries/rt_drivers/drv_crypto.c
blob: debf00c747191d4c184f9a0e91823db049186c79 (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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes
 * 2022-04-21     wolfJane          first version
 */

#include <board.h>
#include "drv_crypto.h"

#ifdef RT_USING_HWCRYPTO

#define DRV_DEBUG
#define LOG_TAG              "drv.crypto"
#include <drv_log.h>

#ifdef RT_HWCRYPTO_USING_CRC
    #define BSP_USING_CRC
#endif

static uint16_t _reverse_16bit(uint16_t x)
{
    x = (((x & 0xaaaa) >> 1) | ((x & 0x5555) << 1));
    x = (((x & 0xcccc) >> 2) | ((x & 0x3333) << 2));
    x = (((x & 0xf0f0) >> 4) | ((x & 0x0f0f) << 4));
    return ((x >> 8) | (x << 8));
}


static uint16_t _get_crc16(struct hwcrypto_crc *ctx, const rt_uint8_t *in, rt_size_t length)
{
    uint16_t temp = 0;

    if (ctx ->crc_cfg.poly != CRC16_PLOY)
    {
        LOG_E("n32g45x crc16 is available only use poly 0x%04X", CRC16_PLOY);
        return 0;
    }

    if (ctx ->crc_cfg.flags & CRC_FLAG_REFIN)
    {
        CRC->CRC16CTRL |= CRC16_CTRL_LITTLE;
    }
    else
    {
        CRC->CRC16CTRL &= (~CRC16_CTRL_LITTLE);
    }

    CRC->CRC16CTRL |= CRC16_CTRL_RESET;
    CRC->CRC16D = ctx ->crc_cfg.last_val;
    CRC->LRC = 0;

    while (length--)
    {
        CRC->CRC16DAT = *in++;
    }
    temp = CRC->CRC16D;

    if (ctx ->crc_cfg.flags & CRC_FLAG_REFOUT)
    {
        temp = _reverse_16bit(temp);
    }

    return temp;
}

static uint32_t _get_crc32(struct hwcrypto_crc *ctx, const rt_uint8_t *in, rt_size_t length)
{
    uint32_t result = 0;
    int length_4 = length >> 2;
    uint8_t *xdat = (uint8_t *)in;

    if (ctx ->crc_cfg.poly != CRC32_PLOY)
    {
        LOG_E("n32g45x crc32 is available only use poly 0x%08X", CRC32_PLOY);
        return 0;
    }

    switch (ctx ->crc_cfg.flags & 0x03)
    {
    case 0:
    {
        while (length_4--)
        {
            result = ((uint32_t)xdat[0] << 24) | ((uint32_t)xdat[1] << 16)
                     | ((uint32_t)xdat[2] << 8) | xdat[3];
            CRC->CRC32DAT = result;
            xdat += 4;
        }

        result = CRC->CRC32DAT;
        length_4 = length & 0x03;

        if (length_4 < 1)
        {
            break;
        }

        uint8_t i = 0;
        while (length_4--)
        {
            result ^= (*xdat++) << 24;

            for (i = 0; i < 8; i++)
            {
                if (result & 0x80000000)
                {
                    result = (result << 1) ^ CRC32_PLOY;
                }
                else
                {
                    result <<= 1;
                }
            }
        }
    }
    break;
    case CRC_FLAG_REFIN:
    /* TODO: add control command handle */
    case CRC_FLAG_REFOUT:
    /* TODO: add control command handle */
    case (CRC_FLAG_REFIN|CRC_FLAG_REFOUT):
    /* TODO: add control command handle */
    default:
        break;
    }

    return result;
}

static rt_uint32_t _crc_update(struct hwcrypto_crc *ctx, const rt_uint8_t *in, rt_size_t length)
{
    rt_uint32_t result = 0;
    struct n32_hwcrypto_device *n32_hw_dev = (struct n32_hwcrypto_device *)ctx->parent.device->user_data;

    rt_mutex_take(&n32_hw_dev->mutex, RT_WAITING_FOREVER);

    if (ctx ->crc_cfg.flags >> 2)
    {
        LOG_E("flags out range 0x%08X", ctx ->crc_cfg.flags);
        goto _exit;
    }

    if (ctx ->crc_cfg.width == 16)
    {
        result = _get_crc16(ctx, in, length);
    }
    else if (ctx ->crc_cfg.width == 32)
    {
        result = _get_crc32(ctx, in, length);
    }

_exit:
    rt_mutex_release(&n32_hw_dev->mutex);

    return result;
}

static const struct hwcrypto_crc_ops crc_ops =
{
    .update = _crc_update,
};


static rt_err_t _crypto_create(struct rt_hwcrypto_ctx *ctx)
{
    rt_err_t res = RT_EOK;

    switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
    {
#if defined(BSP_USING_RNG)
    case HWCRYPTO_TYPE_RNG:
    {
        break;
    }
#endif /* BSP_USING_RNG */

#if defined(BSP_USING_CRC)
    case HWCRYPTO_TYPE_CRC:
    {
        RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_CRC, ENABLE);
        ((struct hwcrypto_crc *)ctx)->ops = &crc_ops;
        break;
    }
#endif /* BSP_USING_CRC */

#if defined(BSP_USING_HASH)
    case HWCRYPTO_TYPE_MD5:
    case HWCRYPTO_TYPE_SHA1:
    case HWCRYPTO_TYPE_SHA2:
    {
        break;
    }
#endif /* BSP_USING_HASH */

#if defined(BSP_USING_CRYP)
    case HWCRYPTO_TYPE_AES:
    case HWCRYPTO_TYPE_DES:
    case HWCRYPTO_TYPE_3DES:
    case HWCRYPTO_TYPE_RC4:
    case HWCRYPTO_TYPE_GCM:
    {
        break;
    }
#endif  /* BSP_USING_CRYP */

    default:
        res = -RT_ERROR;
        break;
    }
    return res;
}

static void _crypto_destroy(struct rt_hwcrypto_ctx *ctx)
{
    /* TODO: add control command handle */
    switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
    {
#if defined(BSP_USING_RNG)
    case HWCRYPTO_TYPE_RNG:
        break;
#endif /* BSP_USING_RNG */

#if defined(BSP_USING_CRC)
    case HWCRYPTO_TYPE_CRC:
        break;
#endif /* BSP_USING_CRC */

#if defined(BSP_USING_HASH)
    case HWCRYPTO_TYPE_MD5:
    case HWCRYPTO_TYPE_SHA1:
    case HWCRYPTO_TYPE_SHA2:
        break;
#endif /* BSP_USING_HASH */

#if defined(BSP_USING_CRYP)
    case HWCRYPTO_TYPE_AES:
    case HWCRYPTO_TYPE_DES:
    case HWCRYPTO_TYPE_3DES:
    case HWCRYPTO_TYPE_RC4:
    case HWCRYPTO_TYPE_GCM:
        break;
#endif /* BSP_USING_CRYP */

    default:
        break;
    }

    if (ctx->contex)
        rt_free(ctx->contex);
}

static rt_err_t _crypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
{
    rt_err_t res = RT_EOK;
    /* TODO: add control command handle */
    switch (src->type & HWCRYPTO_MAIN_TYPE_MASK)
    {
#if defined(BSP_USING_RNG)
    case HWCRYPTO_TYPE_RNG:
        break;
#endif /* BSP_USING_RNG */

#if defined(BSP_USING_CRC)
    case HWCRYPTO_TYPE_CRC:
        break;
#endif /* BSP_USING_CRC */

#if defined(BSP_USING_HASH)
    case HWCRYPTO_TYPE_MD5:
    case HWCRYPTO_TYPE_SHA1:
    case HWCRYPTO_TYPE_SHA2:
        break;
#endif /* BSP_USING_HASH */

#if defined(BSP_USING_CRYP)
    case HWCRYPTO_TYPE_AES:
    case HWCRYPTO_TYPE_DES:
    case HWCRYPTO_TYPE_3DES:
    case HWCRYPTO_TYPE_RC4:
    case HWCRYPTO_TYPE_GCM:
        break;
#endif /* BSP_USING_CRYP */

    default:
        res = -RT_ERROR;
        break;
    }
    return res;
}

static void _crypto_reset(struct rt_hwcrypto_ctx *ctx)
{
    /* TODO: add control command handle */
    switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
    {
#if defined(BSP_USING_RNG)
    case HWCRYPTO_TYPE_RNG:
        break;
#endif /* BSP_USING_RNG */

#if defined(BSP_USING_CRC)
    case HWCRYPTO_TYPE_CRC:
        break;
#endif /* BSP_USING_CRC */

#if defined(BSP_USING_HASH)
    case HWCRYPTO_TYPE_MD5:
    case HWCRYPTO_TYPE_SHA1:
    case HWCRYPTO_TYPE_SHA2:
        break;
#endif /* BSP_USING_HASH*/

#if defined(BSP_USING_CRYP)
    case HWCRYPTO_TYPE_AES:
    case HWCRYPTO_TYPE_DES:
    case HWCRYPTO_TYPE_3DES:
    case HWCRYPTO_TYPE_RC4:
    case HWCRYPTO_TYPE_GCM:
        break;
#endif /* BSP_USING_CRYP */
    default:
        break;
    }
}

static const struct rt_hwcrypto_ops _ops =
{
    .create = _crypto_create,
    .destroy = _crypto_destroy,
    .copy = _crypto_clone,
    .reset = _crypto_reset,
};

int n32_hw_crypto_device_init(void)
{
    static struct n32_hwcrypto_device _crypto_dev;
    rt_uint32_t cpuid[3] = {0};

    _crypto_dev.dev.ops = &_ops;
#if defined(BSP_USING_UDID)
    /* TODO: add control command handle */

#endif /* BSP_USING_UDID */

    _crypto_dev.dev.id = 0;
    rt_memcpy(&_crypto_dev.dev.id, cpuid, 8);

    _crypto_dev.dev.user_data = &_crypto_dev;

    if (rt_hwcrypto_register(&_crypto_dev.dev, RT_HWCRYPTO_DEFAULT_NAME) != RT_EOK)
    {
        return -1;
    }
    rt_mutex_init(&_crypto_dev.mutex, RT_HWCRYPTO_DEFAULT_NAME, RT_IPC_FLAG_PRIO);
    return 0;
}
INIT_DEVICE_EXPORT(n32_hw_crypto_device_init);

static void crc_demo(uint8_t argc, char **argv)
{
    struct hwcrypto_crc_cfg modbus_cfg =
    {
        .last_val = 0xFFFF,
        .poly = 0x8005,
        .width = 16,
        .xorout = 0x0000,
        .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT,
    };
    struct hwcrypto_crc_cfg crc32mpeg2_cfg =
    {
        .last_val = 0xFFFFFFFF,
        .poly = 0x04C11DB7,
        .width = 32,
        .xorout = 0x00000000,
        .flags = 0,
    };

    struct rt_hwcrypto_ctx *ctx;
    rt_uint32_t result = 0;
    rt_uint8_t temp[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
    rt_uint8_t i = 0;
        rt_uint8_t len = sizeof(temp);

    ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC32);

    rt_kprintf("crc dat:\n");
    for (i = 0; i < len; i++)
    {
        rt_kprintf("%02X ", temp[i]);
    }
    rt_kprintf("\n");

    rt_hwcrypto_crc_cfg(ctx, &crc32mpeg2_cfg);
    result = rt_hwcrypto_crc_update(ctx, temp, len);
    rt_kprintf("crc32mpeg2: %x \n", result);

    rt_hwcrypto_crc_cfg(ctx, &modbus_cfg);
    result = rt_hwcrypto_crc_update(ctx, temp, len);
    rt_kprintf("crc16modbus: %x \n", result);

    /*chang to msb*/
    modbus_cfg.flags = 0;
    rt_hwcrypto_crc_cfg(ctx, &modbus_cfg);
    result = rt_hwcrypto_crc_update(ctx, temp, len);
    rt_kprintf("crc16 msb: %x \n", result);

    rt_hwcrypto_crc_destroy(ctx);
}

MSH_CMD_EXPORT(crc_demo, demo for hardwave crc.);

#endif