1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2019-04-25     tyx          the first version
9  */
10 
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include <hw_crc.h>
14 
15 /**
16  * @brief           Creating CRC Context
17  *
18  * @param device    Hardware crypto device
19  * @param mode      Setting default mode or custom mode
20  *
21  * @return          CRC context
22  */
rt_hwcrypto_crc_create(struct rt_hwcrypto_device * device,hwcrypto_crc_mode mode)23 struct rt_hwcrypto_ctx *rt_hwcrypto_crc_create(struct rt_hwcrypto_device *device,
24                                                hwcrypto_crc_mode mode)
25 {
26     struct hwcrypto_crc *crc_ctx;
27 
28     crc_ctx = (struct hwcrypto_crc *)rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_CRC, sizeof(struct hwcrypto_crc));
29     if (crc_ctx == RT_NULL)
30     {
31         return RT_NULL;
32     }
33 
34     switch (mode)
35     {
36     case HWCRYPTO_CRC_CRC8:
37     {
38         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC8_CFG;
39         crc_ctx->crc_cfg = temp;
40         break;
41     }
42     case HWCRYPTO_CRC_CRC16:
43     {
44         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC16_CFG;
45         crc_ctx->crc_cfg = temp;
46         break;
47     }
48     case HWCRYPTO_CRC_CRC32:
49     {
50         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC32_CFG;
51         crc_ctx->crc_cfg = temp;
52         break;
53     }
54     case HWCRYPTO_CRC_CCITT:
55     {
56         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC_CCITT_CFG;
57         crc_ctx->crc_cfg = temp;
58         break;
59     }
60     case HWCRYPTO_CRC_DNP:
61     {
62         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC_DNP_CFG;
63         crc_ctx->crc_cfg = temp;
64         break;
65     }
66     default:
67         break;
68     }
69 
70     return &crc_ctx->parent;
71 }
72 
73 /**
74  * @brief           Destroy CRC Context
75  *
76  * @param ctx       CRC context
77  */
rt_hwcrypto_crc_destroy(struct rt_hwcrypto_ctx * ctx)78 void rt_hwcrypto_crc_destroy(struct rt_hwcrypto_ctx *ctx)
79 {
80     rt_hwcrypto_ctx_destroy(ctx);
81 }
82 
83 /**
84  * @brief           Processing a packet of data
85  *
86  * @param ctx       CRC context
87  * @param input     Data buffer to be Processed
88  * @param length    Data Buffer length
89  *
90  * @return          CRC value
91  */
rt_hwcrypto_crc_update(struct rt_hwcrypto_ctx * ctx,const rt_uint8_t * input,rt_size_t length)92 rt_uint32_t rt_hwcrypto_crc_update(struct rt_hwcrypto_ctx *ctx,
93                                              const rt_uint8_t *input,
94                                              rt_size_t length)
95 {
96     struct hwcrypto_crc *crc_ctx = (struct hwcrypto_crc *)ctx;
97     if (ctx && crc_ctx->ops->update)
98     {
99         return crc_ctx->ops->update(crc_ctx, input, length);
100     }
101     return 0;
102 }
103 
104 /**
105  * @brief           CRC context configuration
106  *
107  * @param ctx       CRC context
108  * @param cfg       CRC config
109  */
rt_hwcrypto_crc_cfg(struct rt_hwcrypto_ctx * ctx,struct hwcrypto_crc_cfg * cfg)110 void rt_hwcrypto_crc_cfg(struct rt_hwcrypto_ctx *ctx,
111                                    struct hwcrypto_crc_cfg *cfg)
112 {
113     if (cfg)
114     {
115         ((struct hwcrypto_crc *)ctx)->crc_cfg = *cfg;
116     }
117 }
118