summaryrefslogtreecommitdiff
path: root/bsp/hc32/libraries/hc32_drivers/drv_crypto.c
blob: e014ef755b5f0207dc4244c14729f06a712cb08f (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-10     CDT          first version
 */
#include "board.h"

#if defined(BSP_USING_HWCRYPTO)

// #define DRV_DEBUG
#define LOG_TAG             "drv_crypto"
#include <drv_log.h>

struct hc32_hwcrypto_device
{
    struct rt_hwcrypto_device dev;
    struct rt_mutex mutex;
};

#if defined(BSP_USING_CRC)

#define DEFAULT_CRC16_CCITT_POLY    (0x1021)       /*!<  X^16 + X^12 + X^5 + 1 */
#define DEFAULT_CRC32_POLY          (0x04C11DB7)   /*!<  X^32 + X^26 + X^23 + X^22 + X^16 + X^12 + X^11 + X^10 +X^8 + X^7 + X^5 + X^4 + X^2+ X + 1 */

static rt_uint32_t _crc_update(struct hwcrypto_crc *ctx, const rt_uint8_t *in, rt_size_t length)
{
    rt_uint32_t result = 0;
    stc_crc_init_t stcCrcInit;
    static struct hwcrypto_crc_cfg crc_cfgbk = {0};
    struct hc32_hwcrypto_device *hc32_hw_dev = (struct hc32_hwcrypto_device *)ctx->parent.device->user_data;

    rt_mutex_take(&hc32_hw_dev->mutex, RT_WAITING_FOREVER);

    if (ctx->crc_cfg.poly != DEFAULT_CRC16_CCITT_POLY && ctx->crc_cfg.poly != DEFAULT_CRC32_POLY)
    {
        LOG_E("CRC polynomial only support 0x1021/0x04C11DB7U.");
        goto _exit;
    }

    /* if crc_cfg change we need init crc again */
    if (rt_memcmp(&crc_cfgbk, &ctx->crc_cfg, sizeof(struct hwcrypto_crc_cfg)))
    {
#if defined(HC32F460)
        switch (ctx->crc_cfg.flags)
        {
        case 0:
            stcCrcInit.u32RefIn = CRC_REFIN_DISABLE;
            stcCrcInit.u32RefOut = CRC_REFOUT_DISABLE;
            break;
        case CRC_FLAG_REFIN:
            stcCrcInit.u32RefIn = CRC_REFIN_ENABLE;
            stcCrcInit.u32RefOut = CRC_REFOUT_DISABLE;
            break;
        case CRC_FLAG_REFOUT:
            stcCrcInit.u32RefIn = CRC_REFIN_DISABLE;
            stcCrcInit.u32RefOut = CRC_REFOUT_ENABLE;
            break;
        case CRC_FLAG_REFIN | CRC_FLAG_REFOUT:
            stcCrcInit.u32RefIn = CRC_REFIN_ENABLE;
            stcCrcInit.u32RefOut = CRC_REFOUT_ENABLE;
            break;
        default :
            LOG_E("crc flag parameter error.");
            goto _exit;
        }

        if (ctx->crc_cfg.xorout)
        {
            stcCrcInit.u32XorOut = CRC_XOROUT_ENABLE;
        }
        else
        {
            stcCrcInit.u32XorOut = CRC_XOROUT_DISABLE;
        }
#endif

        switch (ctx->crc_cfg.width)
        {
        case 16U:
            stcCrcInit.u32Protocol = CRC_CRC16;
            break;
        case 32U:
            stcCrcInit.u32Protocol = CRC_CRC32;
            break;
        default :
            LOG_E("crc width only support 16/32.");
            goto _exit;
        }

        stcCrcInit.u32InitValue = ctx->crc_cfg.last_val;

        if (CRC_Init(&stcCrcInit) != LL_OK)
        {
            LOG_E("crc init error.");
            goto _exit;
        }
        LOG_D("CRC_Init.");
        rt_memcpy(&crc_cfgbk, &ctx->crc_cfg, sizeof(struct hwcrypto_crc_cfg));
    }
    if (16U  == ctx->crc_cfg.width)
    {
        result = CRC_CRC16_Calculate(ctx->crc_cfg.last_val, CRC_DATA_WIDTH_8BIT, in, length);
    }
    else        /* CRC32 */
    {
        result = CRC_CRC32_Calculate(ctx->crc_cfg.last_val, CRC_DATA_WIDTH_8BIT, in, length);
    }

_exit:
    rt_mutex_release(&hc32_hw_dev->mutex);

    return result;
}

static const struct hwcrypto_crc_ops crc_ops =
{
    .update = _crc_update,
};
#endif /* BSP_USING_CRC */

#if defined(BSP_USING_RNG)
static rt_uint32_t _rng_rand(struct hwcrypto_rng *ctx)
{
    rt_uint32_t gen_random = 0;

    if (TRNG_GenerateRandom(&gen_random, 1U) != LL_OK)
    {
        return 0;
    }

    return gen_random;
}

static const struct hwcrypto_rng_ops rng_ops =
{
    .update = _rng_rand,
};
#endif /* BSP_USING_RNG */

#if defined(BSP_USING_HASH)

#define HASH_SHA256_MSG_DIGEST_SIZE        (32U)

static const rt_uint8_t *hash_in = RT_NULL;
static rt_size_t hash_length = 0;

static rt_err_t _hash_update(struct hwcrypto_hash *ctx, const rt_uint8_t *in, rt_size_t length)
{
    rt_uint32_t result = RT_EOK;

    struct hc32_hwcrypto_device *hc32_hw_dev = (struct hc32_hwcrypto_device *)ctx->parent.device->user_data;
    rt_mutex_take(&hc32_hw_dev->mutex, RT_WAITING_FOREVER);

    /* Start HASH computation transfer */
    switch (ctx->parent.type)
    {
    case HWCRYPTO_TYPE_SHA256:
        hash_in = in;
        hash_length = length;
        break;
    default :
        LOG_E("not support hash type: %x", ctx->parent.type);
        result = -RT_ERROR;
        break;
    }

    rt_mutex_release(&hc32_hw_dev->mutex);

    return  result;
}

static rt_err_t _hash_finish(struct hwcrypto_hash *ctx, rt_uint8_t *out, rt_size_t length)
{
    rt_uint32_t result = RT_EOK;

    struct hc32_hwcrypto_device *hc32_hw_dev = (struct hc32_hwcrypto_device *)ctx->parent.device->user_data;
    rt_mutex_take(&hc32_hw_dev->mutex, RT_WAITING_FOREVER);

    if (hash_in == RT_NULL || hash_length == 0)
    {
        LOG_E("no data input.");
        result = -RT_ERROR;
        goto _exit;
    }

    /* Get the hash Subtype */
    switch (ctx->parent.type)
    {
    case HWCRYPTO_TYPE_SHA256:
        /* SHA256 = 32*8 Bits */
        if (length == HASH_SHA256_MSG_DIGEST_SIZE)
        {
            result = HASH_Calculate(hash_in, hash_length, out);
        }
        else
        {
            LOG_E("The out size must be 32 bytes");
        }
        break;

    default :
        LOG_E("not support hash type: %x", ctx->parent.type);
        result = -RT_ERROR;
        break;
    }

_exit:
    rt_mutex_release(&hc32_hw_dev->mutex);

    return result;
}

static const struct hwcrypto_hash_ops hash_ops =
{
    .update = _hash_update,
    .finish  = _hash_finish
};

#endif /* BSP_USING_HASH */

#if defined(BSP_USING_AES)
static rt_err_t _cryp_crypt(struct hwcrypto_symmetric *ctx, struct hwcrypto_symmetric_info *info)
{
    rt_uint32_t result = RT_EOK;
    struct hc32_hwcrypto_device *hc32_hw_dev = (struct hc32_hwcrypto_device *)ctx->parent.device->user_data;
    rt_mutex_take(&hc32_hw_dev->mutex, RT_WAITING_FOREVER);

    switch (ctx->parent.type)
    {
    case HWCRYPTO_TYPE_AES_ECB:
        LOG_D("AES type is ECB.");
        break;
    case HWCRYPTO_TYPE_AES_CBC:
    case HWCRYPTO_TYPE_AES_CTR:
    case HWCRYPTO_TYPE_DES_ECB:
    case HWCRYPTO_TYPE_DES_CBC:
    default :
        LOG_E("not support cryp type: %x", ctx->parent.type);
        break;
    }

#if defined (HC32F460)
    if (ctx->key_bitlen != (AES_KEY_SIZE_16BYTE * 8U))
    {
        LOG_E("not support key bitlen: %d", ctx->key_bitlen);
        result = -RT_ERROR;
        goto _exit;
    }
#elif defined (HC32F4A0)
    if (ctx->key_bitlen != (AES_KEY_SIZE_16BYTE * 8U) && ctx->key_bitlen != (AES_KEY_SIZE_24BYTE * 8U) && \
            ctx->key_bitlen != (AES_KEY_SIZE_32BYTE * 8U))
    {
        LOG_E("not support key bitlen: %d", ctx->key_bitlen);
        result = -RT_ERROR;
        goto _exit;
    }
#endif

    if ((info->length % 16U) != 0U)
    {
        LOG_E("aes supports only an integer multiple of 16 in length");
        result = -RT_ERROR;
        goto _exit;
    }

    if (info->mode == HWCRYPTO_MODE_ENCRYPT)
    {
        /* AES encryption. */
        result = AES_Encrypt(info->in, info->length, ctx->key, (ctx->key_bitlen / 8U), info->out);
    }
    else if (info->mode == HWCRYPTO_MODE_DECRYPT)
    {
        /* AES decryption */
        result = AES_Decrypt(info->in, info->length, ctx->key, (ctx->key_bitlen / 8U), info->out);
    }
    else
    {
        rt_kprintf("error cryp mode : %02x!\n", info->mode);
        result = -RT_ERROR;
        goto _exit;
    }

_exit:
    rt_mutex_release(&hc32_hw_dev->mutex);

    return result;
}

static const struct hwcrypto_symmetric_ops cryp_ops =
{
    .crypt = _cryp_crypt
};
#endif

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:
    {
        /* Enable TRNG. */
        FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_TRNG, ENABLE);
        /* TRNG initialization configuration. */
        TRNG_Init(TRNG_SHIFT_CNT64, TRNG_RELOAD_INIT_VAL_ENABLE);

        ((struct hwcrypto_rng *)ctx)->ops = &rng_ops;

        break;
    }
#endif /* BSP_USING_RNG */

#if defined(BSP_USING_CRC)
    case HWCRYPTO_TYPE_CRC:
    {
        /* Enable CRC module clock. */
        FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_CRC, ENABLE);
        /* do not Initialize CRC because crc_update will do it */
        ((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:
    {
        if (ctx->type == HWCRYPTO_TYPE_SHA256)
        {
            /* Enable HASH. */
            FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_HASH, ENABLE);
            ((struct hwcrypto_hash *)ctx)->ops = &hash_ops;
        }
        else
        {
            LOG_E("not support hash type.");
            res = -RT_ERROR;
        }
        break;
    }
#endif /* BSP_USING_HASH */

#if defined(BSP_USING_AES)
    case HWCRYPTO_TYPE_AES:
    case HWCRYPTO_TYPE_DES:
    case HWCRYPTO_TYPE_3DES:
    case HWCRYPTO_TYPE_RC4:
    case HWCRYPTO_TYPE_GCM:
    {
        /* Enable AES peripheral clock. */
        FCG_Fcg0PeriphClockCmd(PWC_FCG0_AES, ENABLE);

        ((struct hwcrypto_symmetric *)ctx)->ops = &cryp_ops;

        break;
    }
#endif  /* BSP_USING_AES */

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

static void _crypto_destroy(struct rt_hwcrypto_ctx *ctx)
{
    switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
    {
#if defined(BSP_USING_RNG)
    case HWCRYPTO_TYPE_RNG:
        TRNG_DeInit();
        FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_TRNG, DISABLE);
        break;
#endif /* BSP_USING_RNG */

#if defined(BSP_USING_CRC)
    case HWCRYPTO_TYPE_CRC:
        CRC_DeInit();
        FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_CRC, DISABLE);
        break;
#endif /* BSP_USING_CRC */

#if defined(BSP_USING_HASH)
    case HWCRYPTO_TYPE_MD5:
    case HWCRYPTO_TYPE_SHA1:
    case HWCRYPTO_TYPE_SHA2:
        HASH_DeInit();
        FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_HASH, DISABLE);
        break;
#endif /* BSP_USING_HASH */

#if defined(BSP_USING_AES)
    case HWCRYPTO_TYPE_AES:
    case HWCRYPTO_TYPE_DES:
    case HWCRYPTO_TYPE_3DES:
    case HWCRYPTO_TYPE_RC4:
    case HWCRYPTO_TYPE_GCM:
        AES_DeInit();
        FCG_Fcg0PeriphClockCmd(PWC_FCG0_AES, DISABLE);
        break;
#endif /* BSP_USING_AES */

    default:
        break;
    }
}

static rt_err_t _crypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
{
    rt_err_t res = RT_EOK;

    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_AES)
    case HWCRYPTO_TYPE_AES:
    case HWCRYPTO_TYPE_DES:
    case HWCRYPTO_TYPE_3DES:
    case HWCRYPTO_TYPE_RC4:
    case HWCRYPTO_TYPE_GCM:
        break;
#endif /* BSP_USING_AES */

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

static void _crypto_reset(struct rt_hwcrypto_ctx *ctx)
{
    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_AES)
    case HWCRYPTO_TYPE_AES:
    case HWCRYPTO_TYPE_DES:
    case HWCRYPTO_TYPE_3DES:
    case HWCRYPTO_TYPE_RC4:
    case HWCRYPTO_TYPE_GCM:
        break;
#endif /* BSP_USING_AES */

    default:
        break;
    }
}

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

static int rt_hw_crypto_device_init(void)
{
    static struct hc32_hwcrypto_device _crypto_dev;

#if defined(BSP_USING_UQID)
    stc_efm_unique_id_t pstcUID;
    rt_uint32_t cpuid[3] = {0};

    EFM_GetUID(&pstcUID);
    cpuid[0] = pstcUID.u32UniqueID0;
    cpuid[1] = pstcUID.u32UniqueID1;
    cpuid[2] = pstcUID.u32UniqueID2;
    /* we only used 2 words to as the UQID */
    rt_memcpy(&_crypto_dev.dev.id, cpuid, 8);
    LOG_D("UQID = %x%x", cpuid[0], cpuid[1]);
#endif /* BSP_USING_UQID */

    _crypto_dev.dev.ops = &_ops;
    _crypto_dev.dev.user_data = &_crypto_dev;

    if (rt_hwcrypto_register(&_crypto_dev.dev, RT_HWCRYPTO_DEFAULT_NAME) != RT_EOK)
    {
        return -RT_ERROR;
    }

    rt_mutex_init(&_crypto_dev.mutex, RT_HWCRYPTO_DEFAULT_NAME, RT_IPC_FLAG_PRIO);
    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_crypto_device_init);

#endif