添加rtthread相关代码
This commit is contained in:
		
							
								
								
									
										34
									
								
								riscv/rtthread/components/drivers/hwcrypto/SConscript
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										34
									
								
								riscv/rtthread/components/drivers/hwcrypto/SConscript
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| Import('RTT_ROOT') | ||||
| Import('rtconfig') | ||||
| from building import * | ||||
|  | ||||
| cwd     = GetCurrentDir() | ||||
| CPPPATH = [cwd, str(Dir('#'))] | ||||
|  | ||||
| src  = ['hwcrypto.c'] | ||||
|  | ||||
| if (GetDepend(['RT_HWCRYPTO_USING_AES'])  or | ||||
|     GetDepend(['RT_HWCRYPTO_USING_DES'])  or | ||||
|     GetDepend(['RT_HWCRYPTO_USING_3DES']) or | ||||
|     GetDepend(['RT_HWCRYPTO_USING_RC4'])): | ||||
|     src += ['hw_symmetric.c'] | ||||
|     if GetDepend(['RT_HWCRYPTO_USING_GCM']): | ||||
|         src += ['hw_gcm.c'] | ||||
|  | ||||
| if (GetDepend(['RT_HWCRYPTO_USING_MD5'])  or | ||||
|     GetDepend(['RT_HWCRYPTO_USING_SHA1']) or | ||||
|     GetDepend(['RT_HWCRYPTO_USING_SHA2'])): | ||||
|     src += ['hw_hash.c'] | ||||
|  | ||||
| if GetDepend(['RT_HWCRYPTO_USING_RNG']): | ||||
|     src += ['hw_rng.c'] | ||||
|  | ||||
| if GetDepend(['RT_HWCRYPTO_USING_CRC']): | ||||
|     src += ['hw_crc.c'] | ||||
|  | ||||
| if GetDepend(['RT_HWCRYPTO_USING_BIGNUM']): | ||||
|     src += ['hw_bignum.c'] | ||||
|  | ||||
| group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_HWCRYPTO'], CPPPATH = CPPPATH) | ||||
|  | ||||
| Return('group') | ||||
							
								
								
									
										318
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_bignum.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										318
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_bignum.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,318 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-25     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
| #include <hw_bignum.h> | ||||
|  | ||||
| static struct rt_hwcrypto_ctx *bignum_default; | ||||
|  | ||||
| rt_inline rt_err_t hwcrypto_bignum_dev_is_init(void) | ||||
| { | ||||
|     struct rt_hwcrypto_device *dev; | ||||
|  | ||||
|     if (bignum_default) | ||||
|     { | ||||
|         return RT_EOK; | ||||
|     } | ||||
|     dev = rt_hwcrypto_dev_default(); | ||||
|     if (dev == RT_NULL) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     return rt_hwcrypto_bignum_default(dev); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting bignum default devices | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device) | ||||
| { | ||||
|     if (bignum_default) | ||||
|     { | ||||
|         rt_hwcrypto_ctx_destroy(bignum_default); | ||||
|         bignum_default = RT_NULL; | ||||
|     } | ||||
|     if (device == RT_NULL) | ||||
|     { | ||||
|         return RT_EOK; | ||||
|     } | ||||
|     bignum_default = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_BIGNUM, sizeof(struct hwcrypto_bignum)); | ||||
|     if (bignum_default == RT_NULL) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     return RT_EOK; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Init bignum obj | ||||
|  * | ||||
|  * @param n         bignum obj | ||||
|  */ | ||||
| void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n) | ||||
| { | ||||
|     if(n == RT_NULL) | ||||
|         return; | ||||
|  | ||||
|     n->sign = 1; | ||||
|     n->total = 0; | ||||
|     n->p = RT_NULL; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           free a bignum obj | ||||
|  * | ||||
|  * @param           Pointer to bignum obj | ||||
|  */ | ||||
| void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n) | ||||
| { | ||||
|     if (n) | ||||
|     { | ||||
|         rt_memset(n->p, 0xFF, n->total); | ||||
|         rt_free(n->p); | ||||
|         n->sign = 0; | ||||
|         n->total = 0; | ||||
|         n->p = RT_NULL; | ||||
|     } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get length of bignum as an unsigned binary buffer | ||||
|  * | ||||
|  * @param n         bignum obj | ||||
|  * | ||||
|  * @return          binary buffer length | ||||
|  */ | ||||
| int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n) | ||||
| { | ||||
|     int tmp_len, total; | ||||
|  | ||||
|     if (n == RT_NULL || n->p == RT_NULL) | ||||
|     { | ||||
|         return 0; | ||||
|     } | ||||
|     tmp_len = 0; | ||||
|     total = n->total; | ||||
|     while ((total > 0) && (n->p[total - 1] == 0)) | ||||
|     { | ||||
|         tmp_len++; | ||||
|         total--; | ||||
|     } | ||||
|     return n->total - tmp_len; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Export n into unsigned binary data, big endian | ||||
|  * | ||||
|  * @param n         bignum obj | ||||
|  * @param buf       Buffer for the binary number | ||||
|  * @param len       Length of the buffer | ||||
|  * | ||||
|  * @return          export bin length | ||||
|  */ | ||||
| int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len) | ||||
| { | ||||
|     int cp_len, i, j; | ||||
|  | ||||
|     if (n == RT_NULL || buf == RT_NULL) | ||||
|     { | ||||
|         return 0; | ||||
|     } | ||||
|     rt_memset(buf, 0, len); | ||||
|     cp_len = (int)n->total > len ? len : (int)n->total; | ||||
|     for(i = cp_len, j = 0; i > 0; i--, j++) | ||||
|     { | ||||
|         buf[i - 1] = n->p[j]; | ||||
|     } | ||||
|  | ||||
|     return cp_len; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Import n from unsigned binary data, big endian | ||||
|  * | ||||
|  * @param n         bignum obj | ||||
|  * @param buf       Buffer for the binary number | ||||
|  * @param len       Length of the buffer | ||||
|  * | ||||
|  * @return          import length. | ||||
|  */ | ||||
| int rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len) | ||||
| { | ||||
|     int cp_len, i, j; | ||||
|     void *temp_p; | ||||
|  | ||||
|     if (n == RT_NULL || buf == RT_NULL) | ||||
|     { | ||||
|         return 0; | ||||
|     } | ||||
|     if ((int)n->total < len) | ||||
|     { | ||||
|         temp_p = rt_malloc(len); | ||||
|         if (temp_p == RT_NULL) | ||||
|         { | ||||
|             return 0; | ||||
|         } | ||||
|         rt_free(n->p); | ||||
|         n->p = temp_p; | ||||
|         n->total = len; | ||||
|     } | ||||
|  | ||||
|     n->sign = 1; | ||||
|     rt_memset(n->p, 0, n->total); | ||||
|     cp_len = (int)n->total > len ? len : n->total; | ||||
|  | ||||
|     for(i = cp_len, j = 0; i > 0; i--, j++) | ||||
|     { | ||||
|         n->p[j] = buf[i - 1]; | ||||
|     } | ||||
|  | ||||
|     return cp_len; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a + b | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x, | ||||
|                                 const struct hw_bignum_mpi *a, | ||||
|                                 const struct hw_bignum_mpi *b) | ||||
| { | ||||
|     struct hwcrypto_bignum *bignum_ctx; | ||||
|  | ||||
|     if (hwcrypto_bignum_dev_is_init() != RT_EOK) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     bignum_ctx = (struct hwcrypto_bignum *)bignum_default; | ||||
|     if (bignum_ctx->ops->add) | ||||
|     { | ||||
|         return bignum_ctx->ops->add(bignum_ctx, x, a, b); | ||||
|     } | ||||
|     return -RT_ERROR; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a - b | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x, | ||||
|                                 const struct hw_bignum_mpi *a, | ||||
|                                 const struct hw_bignum_mpi *b) | ||||
| { | ||||
|     struct hwcrypto_bignum *bignum_ctx; | ||||
|  | ||||
|     if (hwcrypto_bignum_dev_is_init() != RT_EOK) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     bignum_ctx = (struct hwcrypto_bignum *)bignum_default; | ||||
|     if (bignum_ctx->ops->sub) | ||||
|     { | ||||
|         return bignum_ctx->ops->sub(bignum_ctx, x, a, b); | ||||
|     } | ||||
|     return -RT_ERROR; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a * b | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x, | ||||
|                                 const struct hw_bignum_mpi *a, | ||||
|                                 const struct hw_bignum_mpi *b) | ||||
| { | ||||
|     struct hwcrypto_bignum *bignum_ctx; | ||||
|  | ||||
|     if (hwcrypto_bignum_dev_is_init() != RT_EOK) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     bignum_ctx = (struct hwcrypto_bignum *)bignum_default; | ||||
|     if (bignum_ctx->ops->mul) | ||||
|     { | ||||
|         return bignum_ctx->ops->mul(bignum_ctx, x, a, b); | ||||
|     } | ||||
|     return -RT_ERROR; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a * b (mod c) | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x, | ||||
|                                    const struct hw_bignum_mpi *a, | ||||
|                                    const struct hw_bignum_mpi *b, | ||||
|                                    const struct hw_bignum_mpi *c) | ||||
| { | ||||
|     struct hwcrypto_bignum *bignum_ctx; | ||||
|  | ||||
|     if (hwcrypto_bignum_dev_is_init() != RT_EOK) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     bignum_ctx = (struct hwcrypto_bignum *)bignum_default; | ||||
|     if (bignum_ctx->ops->mulmod) | ||||
|     { | ||||
|         return bignum_ctx->ops->mulmod(bignum_ctx, x, a, b, c); | ||||
|     } | ||||
|     return -RT_ERROR; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a ^ b (mod c) | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x, | ||||
|                                     const struct hw_bignum_mpi *a, | ||||
|                                     const struct hw_bignum_mpi *b, | ||||
|                                     const struct hw_bignum_mpi *c) | ||||
| { | ||||
|     struct hwcrypto_bignum *bignum_ctx; | ||||
|  | ||||
|     if (hwcrypto_bignum_dev_is_init() != RT_EOK) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     bignum_ctx = (struct hwcrypto_bignum *)bignum_default; | ||||
|     if (bignum_ctx->ops->exptmod) | ||||
|     { | ||||
|         return bignum_ctx->ops->exptmod(bignum_ctx, x, a, b, c); | ||||
|     } | ||||
|     return -RT_ERROR; | ||||
| } | ||||
							
								
								
									
										186
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_bignum.h
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										186
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_bignum.h
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,186 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-25     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #ifndef __HW_BIGNUM_H__ | ||||
| #define __HW_BIGNUM_H__ | ||||
|  | ||||
| #include <hwcrypto.h> | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| struct hwcrypto_bignum; | ||||
|  | ||||
| /* bignum obj */ | ||||
| struct hw_bignum_mpi | ||||
| { | ||||
|     int sign;                   /**< integer sign. -1 or 1 */ | ||||
|     rt_size_t total;            /**< total of limbs */ | ||||
|     rt_uint8_t *p;              /**< pointer to limbs */ | ||||
| }; | ||||
|  | ||||
| struct hwcrypto_bignum_ops | ||||
| { | ||||
|     rt_err_t (*add)(struct hwcrypto_bignum *bignum_ctx, | ||||
|                     struct hw_bignum_mpi *x, | ||||
|                     const struct hw_bignum_mpi *a, | ||||
|                     const struct hw_bignum_mpi *b);             /**< x = a + b */ | ||||
|     rt_err_t (*sub)(struct hwcrypto_bignum *bignum_ctx, | ||||
|                     struct hw_bignum_mpi *x, | ||||
|                     const struct hw_bignum_mpi *a, | ||||
|                     const struct hw_bignum_mpi *b);             /**< x = a - b */ | ||||
|     rt_err_t (*mul)(struct hwcrypto_bignum *bignum_ctx, | ||||
|                     struct hw_bignum_mpi *x, | ||||
|                     const struct hw_bignum_mpi *a, | ||||
|                     const struct hw_bignum_mpi *b);             /**< x = a * b */ | ||||
|     rt_err_t (*mulmod)(struct hwcrypto_bignum *bignum_ctx, | ||||
|                        struct hw_bignum_mpi *x, | ||||
|                        const struct hw_bignum_mpi *a, | ||||
|                        const struct hw_bignum_mpi *b, | ||||
|                        const struct hw_bignum_mpi *c);          /**< x = a * b (mod c) */ | ||||
|     rt_err_t (*exptmod)(struct hwcrypto_bignum *bignum_ctx, | ||||
|                         struct hw_bignum_mpi *x, | ||||
|                         const struct hw_bignum_mpi *a, | ||||
|                         const struct hw_bignum_mpi *b, | ||||
|                         const struct hw_bignum_mpi *c);         /**< x = a ^ b (mod c) */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           bignum context. Hardware driver usage | ||||
|  */ | ||||
| struct hwcrypto_bignum | ||||
| { | ||||
|     struct rt_hwcrypto_ctx parent;              /**< Inheritance from hardware crypto context */ | ||||
|     const struct hwcrypto_bignum_ops *ops;      /**< !! Hardware initializes this value when creating context !! */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting bignum default devices | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device); | ||||
|  | ||||
| /** | ||||
|  * @brief           Init bignum obj | ||||
|  */ | ||||
| void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n); | ||||
|  | ||||
| /** | ||||
|  * @brief           free a bignum obj | ||||
|  * | ||||
|  * @param           Pointer to bignum obj | ||||
|  */ | ||||
| void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get length of bignum as an unsigned binary buffer | ||||
|  * | ||||
|  * @param n         bignum obj | ||||
|  * | ||||
|  * @return          binary buffer Length | ||||
|  */ | ||||
| int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n); | ||||
|  | ||||
| /** | ||||
|  * @brief           Export n into unsigned binary data, big endian | ||||
|  * | ||||
|  * @param n         bignum obj | ||||
|  * @param buf       Buffer for the binary number | ||||
|  * @param len       Length of the buffer | ||||
|  * | ||||
|  * @return          export bin length | ||||
|  */ | ||||
| int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); | ||||
|  | ||||
| /** | ||||
|  * @brief           Import n from unsigned binary data, big endian | ||||
|  * | ||||
|  * @param n         bignum obj | ||||
|  * @param buf       Buffer for the binary number | ||||
|  * @param len       Length of the buffer | ||||
|  * | ||||
|  * @return          import length. | ||||
|  */ | ||||
| int rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a + b | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x, | ||||
|                                 const struct hw_bignum_mpi *a, | ||||
|                                 const struct hw_bignum_mpi *b); | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a - b | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x, | ||||
|                                 const struct hw_bignum_mpi *a, | ||||
|                                 const struct hw_bignum_mpi *b); | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a * b | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x, | ||||
|                                 const struct hw_bignum_mpi *a, | ||||
|                                 const struct hw_bignum_mpi *b); | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a * b (mod c) | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x, | ||||
|                                    const struct hw_bignum_mpi *a, | ||||
|                                    const struct hw_bignum_mpi *b, | ||||
|                                    const struct hw_bignum_mpi *c); | ||||
|  | ||||
| /** | ||||
|  * @brief           x = a ^ b (mod c) | ||||
|  * | ||||
|  * @param a         bignum obj | ||||
|  * @param b         bignum obj | ||||
|  * @param c         bignum obj | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x, | ||||
|                                     const struct hw_bignum_mpi *a, | ||||
|                                     const struct hw_bignum_mpi *b, | ||||
|                                     const struct hw_bignum_mpi *c); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif | ||||
							
								
								
									
										117
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_crc.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										117
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_crc.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,117 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-25     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
| #include <hw_crc.h> | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating CRC Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param mode      Setting default mode or custom mode | ||||
|  * | ||||
|  * @return          CRC context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_crc_create(struct rt_hwcrypto_device *device, | ||||
|                                                hwcrypto_crc_mode mode) | ||||
| { | ||||
|     struct hwcrypto_crc *crc_ctx; | ||||
|  | ||||
|     crc_ctx = (struct hwcrypto_crc *)rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_CRC, sizeof(struct hwcrypto_crc)); | ||||
|     if (crc_ctx == RT_NULL) | ||||
|     { | ||||
|         return RT_NULL; | ||||
|     } | ||||
|  | ||||
|     switch (mode) | ||||
|     { | ||||
|     case HWCRYPTO_CRC_CRC8: | ||||
|     { | ||||
|         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC8_CFG; | ||||
|         crc_ctx->crc_cfg = temp; | ||||
|         break; | ||||
|     } | ||||
|     case HWCRYPTO_CRC_CRC16: | ||||
|     { | ||||
|         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC16_CFG; | ||||
|         crc_ctx->crc_cfg = temp; | ||||
|         break; | ||||
|     } | ||||
|     case HWCRYPTO_CRC_CRC32: | ||||
|     { | ||||
|         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC32_CFG; | ||||
|         crc_ctx->crc_cfg = temp; | ||||
|         break; | ||||
|     } | ||||
|     case HWCRYPTO_CRC_CCITT: | ||||
|     { | ||||
|         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC_CCITT_CFG; | ||||
|         crc_ctx->crc_cfg = temp; | ||||
|         break; | ||||
|     } | ||||
|     case HWCRYPTO_CRC_DNP: | ||||
|     { | ||||
|         struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC_DNP_CFG; | ||||
|         crc_ctx->crc_cfg = temp; | ||||
|         break; | ||||
|     } | ||||
|     default: | ||||
|         break; | ||||
|     } | ||||
|  | ||||
|     return &crc_ctx->parent; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy CRC Context | ||||
|  * | ||||
|  * @param ctx       CRC context | ||||
|  */ | ||||
| void rt_hwcrypto_crc_destroy(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     rt_hwcrypto_ctx_destroy(ctx); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Processing a packet of data | ||||
|  * | ||||
|  * @param ctx       CRC context | ||||
|  * @param input     Data buffer to be Processed | ||||
|  * @param length    Data Buffer length | ||||
|  * | ||||
|  * @return          CRC value | ||||
|  */ | ||||
| rt_uint32_t rt_hwcrypto_crc_update(struct rt_hwcrypto_ctx *ctx, | ||||
|                                              const rt_uint8_t *input, | ||||
|                                              rt_size_t length) | ||||
| { | ||||
|     struct hwcrypto_crc *crc_ctx = (struct hwcrypto_crc *)ctx; | ||||
|     if (ctx && crc_ctx->ops->update) | ||||
|     { | ||||
|         return crc_ctx->ops->update(crc_ctx, input, length); | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           CRC context configuration | ||||
|  * | ||||
|  * @param ctx       CRC context | ||||
|  * @param cfg       CRC config | ||||
|  */ | ||||
| void rt_hwcrypto_crc_cfg(struct rt_hwcrypto_ctx *ctx, | ||||
|                                    struct hwcrypto_crc_cfg *cfg) | ||||
| { | ||||
|     if (cfg) | ||||
|     { | ||||
|         ((struct hwcrypto_crc *)ctx)->crc_cfg = *cfg; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										148
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_crc.h
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										148
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_crc.h
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,148 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-25     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #ifndef __HW_CRC_H__ | ||||
| #define __HW_CRC_H__ | ||||
|  | ||||
| #include <hwcrypto.h> | ||||
|  | ||||
| #define CRC_FLAG_REFIN    (0x1 << 0) | ||||
| #define CRC_FLAG_REFOUT   (0x1 << 1) | ||||
|  | ||||
| #define HWCRYPTO_CRC8_CFG       \ | ||||
| {                               \ | ||||
|     .last_val = 0x00,           \ | ||||
|     .poly = 0x07,               \ | ||||
|     .width = 8,                 \ | ||||
|     .xorout = 0x00,             \ | ||||
|     .flags = 0,                 \ | ||||
| } | ||||
|  | ||||
| #define HWCRYPTO_CRC16_CFG      \ | ||||
| {                               \ | ||||
|     .last_val = 0x0000,           \ | ||||
|     .poly = 0x8005,               \ | ||||
|     .width = 16,                 \ | ||||
|     .xorout = 0x0000,             \ | ||||
|     .flags = 0,                 \ | ||||
| } | ||||
|  | ||||
| #define HWCRYPTO_CRC32_CFG   \ | ||||
| {                           \ | ||||
|     .last_val = 0x00000000, \ | ||||
|     .poly = 0x04C11DB7,              \ | ||||
|     .width = 32,             \ | ||||
|     .xorout = 0x00000000,            \ | ||||
|     .flags = 0,             \ | ||||
| } | ||||
|  | ||||
| #define HWCRYPTO_CRC_CCITT_CFG   \ | ||||
| {                           \ | ||||
|     .last_val = 0x0000,          \ | ||||
|     .poly = 0x1021,              \ | ||||
|     .width = 16,             \ | ||||
|     .xorout = 0x0000,            \ | ||||
|     .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT, \ | ||||
| } | ||||
|  | ||||
| #define HWCRYPTO_CRC_DNP_CFG   \ | ||||
| {                           \ | ||||
|     .last_val = 0x0000,          \ | ||||
|     .poly = 0x3D65,              \ | ||||
|     .width = 16,             \ | ||||
|     .xorout = 0xffff,            \ | ||||
|     .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT, \ | ||||
| } | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| struct hwcrypto_crc; | ||||
|  | ||||
| typedef enum | ||||
| { | ||||
|     HWCRYPTO_CRC_CUSTOM,        /**< Custom CRC mode */ | ||||
|     HWCRYPTO_CRC_CRC8,          /**< poly : 0x07 */ | ||||
|     HWCRYPTO_CRC_CRC16,         /**< poly : 0x8005 */ | ||||
|     HWCRYPTO_CRC_CRC32,         /**< poly : 0x04C11DB7 */ | ||||
|     HWCRYPTO_CRC_CCITT,         /**< poly : 0x1021 */ | ||||
|     HWCRYPTO_CRC_DNP,           /**< poly : 0x3D65 */ | ||||
| } hwcrypto_crc_mode; | ||||
|  | ||||
| struct hwcrypto_crc_cfg | ||||
| { | ||||
|     rt_uint32_t last_val;       /**< Last CRC value cache */ | ||||
|     rt_uint32_t poly;           /**< CRC polynomial */ | ||||
|     rt_uint16_t width;          /**< CRC value width */ | ||||
|     rt_uint32_t xorout;         /**< Result XOR Value */ | ||||
|     rt_uint16_t flags;          /**< Input or output data reverse. CRC_FLAG_REFIN or CRC_FLAG_REFOUT */ | ||||
| }; | ||||
|  | ||||
| struct hwcrypto_crc_ops | ||||
| { | ||||
|     rt_uint32_t (*update)(struct hwcrypto_crc *ctx, | ||||
|                           const rt_uint8_t *in, rt_size_t length);  /**< Perform a CRC calculation. return CRC value */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           CRC context. Hardware driver usage | ||||
|  */ | ||||
| struct hwcrypto_crc | ||||
| { | ||||
|     struct rt_hwcrypto_ctx parent;          /**< Inherited from the standard device */ | ||||
|     struct hwcrypto_crc_cfg crc_cfg;        /**< CRC configure */ | ||||
|     const struct hwcrypto_crc_ops *ops;     /**< !! Hardware initializes this value when creating context !! */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating CRC Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param mode      Setting default mode or custom mode | ||||
|  * | ||||
|  * @return          CRC context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_crc_create(struct rt_hwcrypto_device *device, | ||||
|                                                hwcrypto_crc_mode mode); | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy CRC Context | ||||
|  * | ||||
|  * @param ctx       CRC context | ||||
|  */ | ||||
| void rt_hwcrypto_crc_destroy(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           Processing a packet of data | ||||
|  * | ||||
|  * @param ctx       CRC context | ||||
|  * @param input     Data buffer to be Processed | ||||
|  * @param length    Data Buffer length | ||||
|  * | ||||
|  * @return          CRC value | ||||
|  */ | ||||
| rt_uint32_t rt_hwcrypto_crc_update(struct rt_hwcrypto_ctx *ctx, | ||||
|                                    const rt_uint8_t *input, rt_size_t length); | ||||
|  | ||||
| /** | ||||
|  * @brief           CRC context configuration | ||||
|  * | ||||
|  * @param ctx       CRC context | ||||
|  * @param cfg       CRC config | ||||
|  */ | ||||
| void rt_hwcrypto_crc_cfg(struct rt_hwcrypto_ctx *ctx, | ||||
|                          struct hwcrypto_crc_cfg *cfg); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif | ||||
							
								
								
									
										218
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_gcm.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										218
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_gcm.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,218 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-05-14     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
| #include <hw_gcm.h> | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating GCM Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of symmetric crypto context | ||||
|  * | ||||
|  * @return          GCM context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_gcm_create(struct rt_hwcrypto_device *device, | ||||
|                                                hwcrypto_type crypt_type) | ||||
| { | ||||
|     struct rt_hwcrypto_ctx *ctx; | ||||
|  | ||||
|     ctx = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_GCM, sizeof(struct hwcrypto_gcm)); | ||||
|     if (ctx) | ||||
|     { | ||||
|         ((struct hwcrypto_gcm *)ctx)->crypt_type = crypt_type; | ||||
|     } | ||||
|     return ctx; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy GCM Context | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  */ | ||||
| void rt_hwcrypto_gcm_destroy(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     rt_hwcrypto_ctx_destroy(ctx); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           This function starts a GCM encryption or decryption operation | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param add       The buffer holding the additional data | ||||
|  * @param add_len   The length of the additional data | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_start(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *add, | ||||
|                                rt_size_t add_len) | ||||
| { | ||||
|     struct hwcrypto_gcm *gcm_ctx = (struct hwcrypto_gcm *)ctx; | ||||
|  | ||||
|     if (gcm_ctx && gcm_ctx->ops->start) | ||||
|     { | ||||
|         return gcm_ctx->ops->start(gcm_ctx, add, add_len); | ||||
|     } | ||||
|     return -RT_EINVAL; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           This function finishes the GCM operation and generates the authentication tag | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param tag       The buffer for holding the tag | ||||
|  * @param tag_len   The length of the tag to generate | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_finish(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *tag, | ||||
|                                 rt_size_t tag_len) | ||||
| { | ||||
|     struct hwcrypto_gcm *gcm_ctx = (struct hwcrypto_gcm *)ctx; | ||||
|  | ||||
|     if (gcm_ctx && gcm_ctx->ops->finish) | ||||
|     { | ||||
|         return gcm_ctx->ops->finish(gcm_ctx, tag, tag_len); | ||||
|     } | ||||
|     return -RT_EINVAL; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           This function performs a symmetric encryption or decryption operation | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param mode      Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT | ||||
|  * @param length    The length of the input data in Bytes. This must be a multiple of the block size | ||||
|  * @param in        The buffer holding the input data | ||||
|  * @param out       The buffer holding the output data | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, | ||||
|                                rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out) | ||||
| { | ||||
|     return rt_hwcrypto_symmetric_crypt(ctx, mode, length, in, out); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Set Symmetric Encryption and Decryption Key | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param key       The crypto key | ||||
|  * @param bitlen    The crypto key bit length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_setkey(struct rt_hwcrypto_ctx *ctx, | ||||
|                                 const rt_uint8_t *key, rt_uint32_t bitlen) | ||||
| { | ||||
|     return rt_hwcrypto_symmetric_setkey(ctx, key, bitlen); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get Symmetric Encryption and Decryption Key | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param key       The crypto key buffer | ||||
|  * @param bitlen    The crypto key bit length | ||||
|  * | ||||
|  * @return          Key length of copy | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_getkey(struct rt_hwcrypto_ctx *ctx, | ||||
|                                 rt_uint8_t *key, rt_uint32_t bitlen) | ||||
| { | ||||
|     return rt_hwcrypto_symmetric_getkey(ctx, key, bitlen); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Set Symmetric Encryption and Decryption initialization vector | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param iv        The crypto initialization vector | ||||
|  * @param len       The crypto initialization vector length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_setiv(struct rt_hwcrypto_ctx *ctx, | ||||
|                                const rt_uint8_t *iv, rt_size_t len) | ||||
| { | ||||
|     return rt_hwcrypto_symmetric_setiv(ctx, iv, len); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get Symmetric Encryption and Decryption initialization vector | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param iv        The crypto initialization vector buffer | ||||
|  * @param len       The crypto initialization vector buffer length | ||||
|  * | ||||
|  * @return          IV length of copy | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_getiv(struct rt_hwcrypto_ctx *ctx, | ||||
|                                rt_uint8_t *iv, rt_size_t len) | ||||
| { | ||||
|     return rt_hwcrypto_symmetric_getiv(ctx, iv, len); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Set offset in initialization vector | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param iv_off    The offset in IV | ||||
|  */ | ||||
| void rt_hwcrypto_gcm_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off) | ||||
| { | ||||
|     rt_hwcrypto_symmetric_set_ivoff(ctx, iv_off); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get offset in initialization vector | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param iv_off    It must point to a valid memory | ||||
|  */ | ||||
| void rt_hwcrypto_gcm_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off) | ||||
| { | ||||
|     rt_hwcrypto_symmetric_get_ivoff(ctx, iv_off); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           This function copy GCM context | ||||
|  * | ||||
|  * @param des       The destination GCM context | ||||
|  * @param src       The GCM context to be copy | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_cpy(struct rt_hwcrypto_ctx *des, | ||||
|                              const struct rt_hwcrypto_ctx *src) | ||||
| { | ||||
|     struct hwcrypto_gcm *gcm_des = (struct hwcrypto_gcm *)des; | ||||
|     struct hwcrypto_gcm *gcm_src = (struct hwcrypto_gcm *)src; | ||||
|  | ||||
|     if (des != RT_NULL && src != RT_NULL) | ||||
|     { | ||||
|         gcm_des->crypt_type = gcm_src->crypt_type; | ||||
|         /* symmetric crypto context copy */ | ||||
|         return rt_hwcrypto_symmetric_cpy(des, src); | ||||
|     } | ||||
|     return -RT_EINVAL; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Reset GCM context | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  */ | ||||
| void rt_hwcrypto_gcm_reset(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     rt_hwcrypto_symmetric_reset(ctx); | ||||
| } | ||||
							
								
								
									
										182
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_gcm.h
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										182
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_gcm.h
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,182 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-05-14     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #ifndef __HW_GCM_H__ | ||||
| #define __HW_GCM_H__ | ||||
|  | ||||
| #include "hw_symmetric.h" | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| struct hwcrypto_gcm; | ||||
|  | ||||
| struct hwcrypto_gcm_ops | ||||
| { | ||||
|     rt_err_t (*start)(struct hwcrypto_gcm *gcm_ctx, | ||||
|                       const unsigned char *add, rt_size_t add_len);    /**< Set additional data. start GCM operation */ | ||||
|     rt_err_t (*finish)(struct hwcrypto_gcm *gcm_ctx, | ||||
|                        const unsigned char *tag, rt_size_t tag_len);   /**< finish GCM operation. get tag */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           GCM context. Hardware driver usage | ||||
|  */ | ||||
| struct hwcrypto_gcm | ||||
| { | ||||
|     struct hwcrypto_symmetric parent;       /**< Inheritance from hardware symmetric crypto context */ | ||||
|     hwcrypto_type crypt_type;               /**< symmetric crypto type. eg: AES/DES */ | ||||
|     const struct hwcrypto_gcm_ops *ops;     /**< !! Hardware initializes this value when creating context !! */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating GCM Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of symmetric crypto context | ||||
|  * | ||||
|  * @return          GCM context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_gcm_create(struct rt_hwcrypto_device *device, | ||||
|                                                hwcrypto_type crypt_type); | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy GCM Context | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  */ | ||||
| void rt_hwcrypto_gcm_destroy(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           This function starts a GCM encryption or decryption operation | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param add       The buffer holding the additional data | ||||
|  * @param add_len   The length of the additional data | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_start(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *add, | ||||
|                                rt_size_t add_len); | ||||
|  | ||||
| /** | ||||
|  * @brief           This function finishes the GCM operation and generates the authentication tag | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param tag       The buffer for holding the tag | ||||
|  * @param tag_len   The length of the tag to generate | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_finish(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *tag, | ||||
|                                 rt_size_t tag_len); | ||||
|  | ||||
| /** | ||||
|  * @brief           This function performs a symmetric encryption or decryption operation | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param mode      Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT | ||||
|  * @param length    The length of the input data in Bytes. This must be a multiple of the block size | ||||
|  * @param in        The buffer holding the input data | ||||
|  * @param out       The buffer holding the output data | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, | ||||
|                                rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out); | ||||
|  | ||||
| /** | ||||
|  * @brief           Set Symmetric Encryption and Decryption Key | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param key       The crypto key | ||||
|  * @param bitlen    The crypto key bit length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_setkey(struct rt_hwcrypto_ctx *ctx, | ||||
|                                 const rt_uint8_t *key, rt_uint32_t bitlen); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get Symmetric Encryption and Decryption Key | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param key       The crypto key buffer | ||||
|  * @param bitlen    The crypto key bit length | ||||
|  * | ||||
|  * @return          Key length of copy | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_getkey(struct rt_hwcrypto_ctx *ctx, | ||||
|                                 rt_uint8_t *key, rt_uint32_t bitlen); | ||||
|  | ||||
| /** | ||||
|  * @brief           Set Symmetric Encryption and Decryption initialization vector | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param iv        The crypto initialization vector | ||||
|  * @param len       The crypto initialization vector length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_setiv(struct rt_hwcrypto_ctx *ctx, | ||||
|                                const rt_uint8_t *iv, rt_size_t len); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get Symmetric Encryption and Decryption initialization vector | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param iv        The crypto initialization vector buffer | ||||
|  * @param len       The crypto initialization vector buffer length | ||||
|  * | ||||
|  * @return          IV length of copy | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_getiv(struct rt_hwcrypto_ctx *ctx, | ||||
|                                rt_uint8_t *iv, rt_size_t len); | ||||
|  | ||||
| /** | ||||
|  * @brief           Set offset in initialization vector | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param iv_off    The offset in IV | ||||
|  */ | ||||
| void rt_hwcrypto_gcm_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get offset in initialization vector | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  * @param iv_off    It must point to a valid memory | ||||
|  */ | ||||
| void rt_hwcrypto_gcm_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off); | ||||
|  | ||||
| /** | ||||
|  * @brief           This function copy GCM context | ||||
|  * | ||||
|  * @param des       The destination GCM context | ||||
|  * @param src       The GCM context to be copy | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_gcm_cpy(struct rt_hwcrypto_ctx *des, | ||||
|                              const struct rt_hwcrypto_ctx *src); | ||||
|  | ||||
| /** | ||||
|  * @brief           Reset GCM context | ||||
|  * | ||||
|  * @param ctx       GCM context | ||||
|  */ | ||||
| void rt_hwcrypto_gcm_reset(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif | ||||
							
								
								
									
										111
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_hash.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										111
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_hash.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,111 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-23     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
| #include <hw_hash.h> | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating hash Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of hash context | ||||
|  * | ||||
|  * @return          Hash context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_hash_create(struct rt_hwcrypto_device *device, hwcrypto_type type) | ||||
| { | ||||
|     struct rt_hwcrypto_ctx *ctx; | ||||
|  | ||||
|     ctx = rt_hwcrypto_ctx_create(device, type, sizeof(struct hwcrypto_hash)); | ||||
|     return ctx; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy hash Context | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  */ | ||||
| void rt_hwcrypto_hash_destroy(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     rt_hwcrypto_ctx_destroy(ctx); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get the final hash value | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  * @param output    Hash value buffer | ||||
|  * @param length    Hash value buffer length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_hash_finish(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *output, rt_size_t length) | ||||
| { | ||||
|     if (ctx && ((struct hwcrypto_hash *)ctx)->ops->finish) | ||||
|     { | ||||
|         return ((struct hwcrypto_hash *)ctx)->ops->finish((struct hwcrypto_hash *)ctx, output, length); | ||||
|     } | ||||
|     return -RT_ERROR; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Processing a packet of data | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  * @param input     Data buffer to be Processed | ||||
|  * @param length    Data Buffer length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_hash_update(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *input, rt_size_t length) | ||||
| { | ||||
|     if (ctx && ((struct hwcrypto_hash *)ctx)->ops->update) | ||||
|     { | ||||
|         return ((struct hwcrypto_hash *)ctx)->ops->update((struct hwcrypto_hash *)ctx, input, length); | ||||
|     } | ||||
|     return -RT_ERROR; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           This function copy hash context | ||||
|  * | ||||
|  * @param des       The destination hash context | ||||
|  * @param src       The hash context to be copy | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_hash_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src) | ||||
| { | ||||
|     return rt_hwcrypto_ctx_cpy(des, src); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Reset hash context | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  */ | ||||
| void rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     rt_hwcrypto_ctx_reset(ctx); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting hash context type | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  * @param type      Types of settings | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_hash_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type) | ||||
| { | ||||
|     return rt_hwcrypto_set_type(ctx, type); | ||||
| } | ||||
							
								
								
									
										110
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_hash.h
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										110
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_hash.h
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,110 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-23     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #ifndef __HW_HASH_H__ | ||||
| #define __HW_HASH_H__ | ||||
|  | ||||
| #include <hwcrypto.h> | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| struct hwcrypto_hash; | ||||
|  | ||||
| struct hwcrypto_hash_ops | ||||
| { | ||||
|     rt_err_t (*update)(struct hwcrypto_hash *hash_ctx, | ||||
|                        const rt_uint8_t *in, rt_size_t length);     /**< Processing a packet of data */ | ||||
|     rt_err_t (*finish)(struct hwcrypto_hash *hash_ctx, | ||||
|                        rt_uint8_t *out, rt_size_t length);          /**< Get the final hash value */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           hash context. Hardware driver usage | ||||
|  */ | ||||
| struct hwcrypto_hash | ||||
| { | ||||
|     struct rt_hwcrypto_ctx parent;              /**< Inheritance from hardware crypto context */ | ||||
|     const struct hwcrypto_hash_ops *ops;        /**< !! Hardware initializes this value when creating context !! */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating hash Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of hash context | ||||
|  * | ||||
|  * @return          Hash context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_hash_create(struct rt_hwcrypto_device *device, | ||||
|                                                 hwcrypto_type type); | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy hash Context | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  */ | ||||
| void rt_hwcrypto_hash_destroy(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get the final hash value | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  * @param output    Hash value buffer | ||||
|  * @param length    Hash value buffer length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_hash_finish(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *output, rt_size_t length); | ||||
|  | ||||
| /** | ||||
|  * @brief           Processing a packet of data | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  * @param input     Data buffer to be Processed | ||||
|  * @param length    Data Buffer length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_hash_update(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *input, rt_size_t length); | ||||
|  | ||||
| /** | ||||
|  * @brief           This function copy hash context | ||||
|  * | ||||
|  * @param des       The destination hash context | ||||
|  * @param src       The hash context to be copy | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_hash_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src); | ||||
|  | ||||
| /** | ||||
|  * @brief           Reset hash context | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  */ | ||||
| void rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting hash context type | ||||
|  * | ||||
|  * @param ctx       Hash context | ||||
|  * @param type      Types of settings | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_hash_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif | ||||
							
								
								
									
										110
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_rng.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										110
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_rng.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,110 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-25     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
| #include <hw_rng.h> | ||||
|  | ||||
| /* Used to save default RNG Context */ | ||||
| static struct rt_hwcrypto_ctx *ctx_default; | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating RNG Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * | ||||
|  * @return          RNG context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_rng_create(struct rt_hwcrypto_device *device) | ||||
| { | ||||
|     struct rt_hwcrypto_ctx *ctx; | ||||
|  | ||||
|     ctx = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_RNG, sizeof(struct hwcrypto_rng)); | ||||
|     return ctx; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy RNG Context | ||||
|  * | ||||
|  * @param ctx       RNG context | ||||
|  */ | ||||
| void rt_hwcrypto_rng_destroy(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     /* Destroy the defaule RNG Context ? */ | ||||
|     if (ctx == ctx_default) | ||||
|     { | ||||
|         ctx_default = RT_NULL; | ||||
|     } | ||||
|     rt_hwcrypto_ctx_destroy(ctx); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting RNG default devices | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_rng_default(struct rt_hwcrypto_device *device) | ||||
| { | ||||
|     struct rt_hwcrypto_ctx *tmp_ctx; | ||||
|  | ||||
|     /* if device is null, destroy default RNG Context */ | ||||
|     if (device == RT_NULL) | ||||
|     { | ||||
|         if (ctx_default) | ||||
|         { | ||||
|             rt_hwcrypto_rng_destroy(ctx_default); | ||||
|             ctx_default = RT_NULL; | ||||
|         } | ||||
|         return RT_EOK; | ||||
|     } | ||||
|     /* Try create RNG Context */ | ||||
|     tmp_ctx = rt_hwcrypto_rng_create(device); | ||||
|     if (tmp_ctx == RT_NULL) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     /* create RNG Context success, update default RNG Context */ | ||||
|     rt_hwcrypto_rng_destroy(ctx_default); | ||||
|     ctx_default = tmp_ctx; | ||||
|  | ||||
|     return RT_EOK; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Getting Random Numbers from RNG Context | ||||
|  * | ||||
|  * @param ctx       RNG context | ||||
|  * | ||||
|  * @return          Random number | ||||
|  */ | ||||
| rt_uint32_t rt_hwcrypto_rng_update_ctx(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     if (ctx) | ||||
|     { | ||||
|         return ((struct hwcrypto_rng *)ctx)->ops->update((struct hwcrypto_rng *)ctx); | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Return a random number | ||||
|  * | ||||
|  * @return          Random number | ||||
|  */ | ||||
| rt_uint32_t rt_hwcrypto_rng_update(void) | ||||
| { | ||||
|     /* Default device does not exist ? */ | ||||
|     if (ctx_default == RT_NULL) | ||||
|     { | ||||
|         /* try create Context from default device */ | ||||
|         rt_hwcrypto_rng_default(rt_hwcrypto_dev_default()); | ||||
|     } | ||||
|     return rt_hwcrypto_rng_update_ctx(ctx_default); | ||||
| } | ||||
							
								
								
									
										79
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_rng.h
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										79
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_rng.h
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,79 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-25     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #ifndef __HW_RNG_H__ | ||||
| #define __HW_RNG_H__ | ||||
|  | ||||
| #include <hwcrypto.h> | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| struct hwcrypto_rng; | ||||
|  | ||||
| struct hwcrypto_rng_ops | ||||
| { | ||||
|     rt_uint32_t (*update)(struct hwcrypto_rng *ctx);    /**< Return a random number */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           random context. Hardware driver usage | ||||
|  */ | ||||
| struct hwcrypto_rng | ||||
| { | ||||
|     struct rt_hwcrypto_ctx parent;          /**< Inheritance from hardware crypto context */ | ||||
|     const struct hwcrypto_rng_ops *ops;     /**< !! Hardware initializes this value when creating context !! */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating RNG Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * | ||||
|  * @return          RNG context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_rng_create(struct rt_hwcrypto_device *device); | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy RNG Context | ||||
|  * | ||||
|  * @param ctx       RNG context | ||||
|  */ | ||||
| void rt_hwcrypto_rng_destroy(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting RNG default devices | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_rng_default(struct rt_hwcrypto_device *device); | ||||
|  | ||||
| /** | ||||
|  * @brief           Getting Random Numbers from RNG Context | ||||
|  * | ||||
|  * @param ctx       RNG context | ||||
|  * | ||||
|  * @return          Random number | ||||
|  */ | ||||
| rt_uint32_t rt_hwcrypto_rng_update_ctx(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           Return a random number | ||||
|  * | ||||
|  * @return          Random number | ||||
|  */ | ||||
| rt_uint32_t rt_hwcrypto_rng_update(void); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif | ||||
							
								
								
									
										276
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_symmetric.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										276
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_symmetric.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,276 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-25     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
| #include <hw_symmetric.h> | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating Symmetric Encryption and Decryption Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of symmetric crypto context | ||||
|  * | ||||
|  * @return          Symmetric crypto context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_symmetric_create(struct rt_hwcrypto_device *device, hwcrypto_type type) | ||||
| { | ||||
|     struct rt_hwcrypto_ctx *ctx; | ||||
|  | ||||
|     ctx = rt_hwcrypto_ctx_create(device, type, sizeof(struct hwcrypto_symmetric)); | ||||
|     return ctx; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy Symmetric Encryption and Decryption Context | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  */ | ||||
| void rt_hwcrypto_symmetric_destroy(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     rt_hwcrypto_ctx_destroy(ctx); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           This function performs a symmetric encryption or decryption operation | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param mode      Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT | ||||
|  * @param length    The length of the input data in Bytes. This must be a multiple of the block size | ||||
|  * @param in        The buffer holding the input data | ||||
|  * @param out       The buffer holding the output data | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out) | ||||
| { | ||||
|     struct hwcrypto_symmetric *symmetric_ctx; | ||||
|     struct hwcrypto_symmetric_info symmetric_info; | ||||
|     rt_err_t err; | ||||
|  | ||||
|     if (ctx == RT_NULL) | ||||
|     { | ||||
|         return -RT_EINVAL; | ||||
|     } | ||||
|     symmetric_ctx = (struct hwcrypto_symmetric *)ctx; | ||||
|     if (symmetric_ctx->ops->crypt == RT_NULL) | ||||
|     { | ||||
|         return -RT_ERROR; | ||||
|     } | ||||
|     if (mode != HWCRYPTO_MODE_ENCRYPT && mode != HWCRYPTO_MODE_DECRYPT) | ||||
|     { | ||||
|         return -RT_EINVAL; | ||||
|     } | ||||
|  | ||||
|     /* Input information packaging */ | ||||
|     symmetric_info.mode = mode; | ||||
|     symmetric_info.in = in; | ||||
|     symmetric_info.out = out; | ||||
|     symmetric_info.length = length; | ||||
|  | ||||
|     /* Calling Hardware Encryption and Decryption Function */ | ||||
|     err = symmetric_ctx->ops->crypt(symmetric_ctx, &symmetric_info); | ||||
|  | ||||
|     /* clean up flags */ | ||||
|     symmetric_ctx->flags &= ~(SYMMTRIC_MODIFY_KEY | SYMMTRIC_MODIFY_IV | SYMMTRIC_MODIFY_IVOFF); | ||||
|     return err; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Set Symmetric Encryption and Decryption Key | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param key       The crypto key | ||||
|  * @param bitlen    The crypto key bit length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_setkey(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *key, rt_uint32_t bitlen) | ||||
| { | ||||
|     struct hwcrypto_symmetric *symmetric_ctx; | ||||
|  | ||||
|     if (ctx && bitlen <= RT_HWCRYPTO_KEYBIT_MAX_SIZE) | ||||
|     { | ||||
|         symmetric_ctx = (struct hwcrypto_symmetric *)ctx; | ||||
|         rt_memcpy(symmetric_ctx->key, key, bitlen >> 3); | ||||
|         /* Record key length */ | ||||
|         symmetric_ctx->key_bitlen = bitlen; | ||||
|         /* Key change flag set up */ | ||||
|         symmetric_ctx->flags |= SYMMTRIC_MODIFY_KEY; | ||||
|         return RT_EOK; | ||||
|     } | ||||
|  | ||||
|     return -RT_EINVAL; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get Symmetric Encryption and Decryption Key | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param key       The crypto key buffer | ||||
|  * @param bitlen    The crypto key bit length | ||||
|  * | ||||
|  * @return          Key length of copy | ||||
|  */ | ||||
| int rt_hwcrypto_symmetric_getkey(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *key, rt_uint32_t bitlen) | ||||
| { | ||||
|     struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx; | ||||
|  | ||||
|     if (ctx && bitlen >= symmetric_ctx->key_bitlen) | ||||
|     { | ||||
|         rt_memcpy(key, symmetric_ctx->key, symmetric_ctx->key_bitlen >> 3); | ||||
|         return symmetric_ctx->key_bitlen; | ||||
|     } | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Set Symmetric Encryption and Decryption initialization vector | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param iv        The crypto initialization vector | ||||
|  * @param len       The crypto initialization vector length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_setiv(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *iv, rt_size_t len) | ||||
| { | ||||
|     struct hwcrypto_symmetric *symmetric_ctx; | ||||
|  | ||||
|     if (ctx && len <= RT_HWCRYPTO_IV_MAX_SIZE) | ||||
|     { | ||||
|         symmetric_ctx = (struct hwcrypto_symmetric *)ctx; | ||||
|         rt_memcpy(symmetric_ctx->iv, iv, len); | ||||
|         symmetric_ctx->iv_len = len; | ||||
|         /* IV change flag set up */ | ||||
|         symmetric_ctx->flags |= SYMMTRIC_MODIFY_IV; | ||||
|         return RT_EOK; | ||||
|     } | ||||
|  | ||||
|     return -RT_EINVAL; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get Symmetric Encryption and Decryption initialization vector | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param iv        The crypto initialization vector buffer | ||||
|  * @param len       The crypto initialization vector buffer length | ||||
|  * | ||||
|  * @return          IV length of copy | ||||
|  */ | ||||
| int rt_hwcrypto_symmetric_getiv(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *iv, rt_size_t len) | ||||
| { | ||||
|     struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx;; | ||||
|  | ||||
|     if (ctx && len >= symmetric_ctx->iv_len) | ||||
|     { | ||||
|         rt_memcpy(iv, symmetric_ctx->iv, symmetric_ctx->iv_len); | ||||
|         return symmetric_ctx->iv_len; | ||||
|     } | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Set offset in initialization vector | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param iv_off    The offset in IV | ||||
|  */ | ||||
| void rt_hwcrypto_symmetric_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off) | ||||
| { | ||||
|     if (ctx) | ||||
|     { | ||||
|         ((struct hwcrypto_symmetric *)ctx)->iv_off = iv_off; | ||||
|         /* iv_off change flag set up */ | ||||
|         ((struct hwcrypto_symmetric *)ctx)->flags |= SYMMTRIC_MODIFY_IVOFF; | ||||
|     } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get offset in initialization vector | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param iv_off    It must point to a valid memory | ||||
|  */ | ||||
| void rt_hwcrypto_symmetric_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off) | ||||
| { | ||||
|     if (ctx && iv_off) | ||||
|     { | ||||
|         *iv_off = ((struct hwcrypto_symmetric *)ctx)->iv_off; | ||||
|     } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           This function copy symmetric crypto context | ||||
|  * | ||||
|  * @param des       The destination symmetric crypto context | ||||
|  * @param src       The symmetric crypto context to be copy | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src) | ||||
| { | ||||
|     struct hwcrypto_symmetric *symmetric_des = (struct hwcrypto_symmetric *)des; | ||||
|     struct hwcrypto_symmetric *symmetric_src = (struct hwcrypto_symmetric *)src; | ||||
|  | ||||
|     if (des != RT_NULL && src != RT_NULL) | ||||
|     { | ||||
|         /* Copy Symmetric Encryption and Decryption Context Information */ | ||||
|         symmetric_des->flags      = symmetric_src->flags     ; | ||||
|         symmetric_des->iv_len     = symmetric_src->iv_len    ; | ||||
|         symmetric_des->iv_off     = symmetric_src->iv_off    ; | ||||
|         symmetric_des->key_bitlen = symmetric_src->key_bitlen; | ||||
|         rt_memcpy(symmetric_des->iv, symmetric_src->iv, symmetric_src->iv_len); | ||||
|         rt_memcpy(symmetric_des->key, symmetric_src->key, symmetric_src->key_bitlen >> 3); | ||||
|  | ||||
|         /* Hardware context copy */ | ||||
|         return rt_hwcrypto_ctx_cpy(des, src); | ||||
|     } | ||||
|     return -RT_EINVAL; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Reset symmetric crypto context | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  */ | ||||
| void rt_hwcrypto_symmetric_reset(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx; | ||||
|     if (ctx != RT_NULL) | ||||
|     { | ||||
|         /* Copy Symmetric Encryption and Decryption Context Information */ | ||||
|         symmetric_ctx->flags      = 0x00; | ||||
|         symmetric_ctx->iv_len     = 0x00; | ||||
|         symmetric_ctx->iv_off     = 0x00; | ||||
|         symmetric_ctx->key_bitlen = 0x00; | ||||
|         rt_memset(symmetric_ctx->iv, 0, RT_HWCRYPTO_IV_MAX_SIZE); | ||||
|         rt_memset(symmetric_ctx->key, 0, RT_HWCRYPTO_KEYBIT_MAX_SIZE >> 3); | ||||
|  | ||||
|         /* Hardware context reset */ | ||||
|         rt_hwcrypto_ctx_reset(ctx); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting symmetric crypto context type | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param type      Types of settings | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type) | ||||
| { | ||||
|     return rt_hwcrypto_set_type(ctx, type); | ||||
| } | ||||
							
								
								
									
										189
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_symmetric.h
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										189
									
								
								riscv/rtthread/components/drivers/hwcrypto/hw_symmetric.h
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,189 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-25     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #ifndef __HW_SYMMETRIC_H__ | ||||
| #define __HW_SYMMETRIC_H__ | ||||
|  | ||||
| #include <hwcrypto.h> | ||||
|  | ||||
| #ifndef RT_HWCRYPTO_IV_MAX_SIZE | ||||
| #define RT_HWCRYPTO_IV_MAX_SIZE  (16) | ||||
| #endif | ||||
| #ifndef RT_HWCRYPTO_KEYBIT_MAX_SIZE | ||||
| #define RT_HWCRYPTO_KEYBIT_MAX_SIZE  (256) | ||||
| #endif | ||||
|  | ||||
| #define SYMMTRIC_MODIFY_KEY    (0x1 << 0) | ||||
| #define SYMMTRIC_MODIFY_IV     (0x1 << 1) | ||||
| #define SYMMTRIC_MODIFY_IVOFF  (0x1 << 2) | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| struct hwcrypto_symmetric; | ||||
| struct hwcrypto_symmetric_info; | ||||
|  | ||||
| struct hwcrypto_symmetric_ops | ||||
| { | ||||
|     rt_err_t (*crypt)(struct hwcrypto_symmetric *symmetric_ctx, | ||||
|                       struct hwcrypto_symmetric_info *symmetric_info);  /**< Hardware Symmetric Encryption and Decryption Callback */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Hardware driver usage, including input and output information | ||||
|  */ | ||||
| struct hwcrypto_symmetric_info | ||||
| { | ||||
|     hwcrypto_mode mode;             /**< crypto mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT */ | ||||
|     const rt_uint8_t *in;           /**< Input data */ | ||||
|     rt_uint8_t *out;                /**< Output data will be written */ | ||||
|     rt_size_t length;               /**< The length of the input data in Bytes. It's a multiple of block size. */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Symmetric crypto context. Hardware driver usage | ||||
|  */ | ||||
| struct hwcrypto_symmetric | ||||
| { | ||||
|     struct rt_hwcrypto_ctx parent;                      /**< Inheritance from hardware crypto context */ | ||||
|     rt_uint16_t flags;                                  /**< key or iv or ivoff has been changed. The flag will be set up */ | ||||
|     rt_uint16_t iv_len;                                 /**< initialization vector effective length */ | ||||
|     rt_uint16_t iv_off;                                 /**< The offset in IV */ | ||||
|     rt_uint16_t key_bitlen;                             /**< The crypto key bit length */ | ||||
|     rt_uint8_t iv[RT_HWCRYPTO_IV_MAX_SIZE];             /**< The initialization vector */ | ||||
|     rt_uint8_t key[RT_HWCRYPTO_KEYBIT_MAX_SIZE >> 3];   /**< The crypto key */ | ||||
|     const struct hwcrypto_symmetric_ops *ops;           /**< !! Hardware initializes this value when creating context !! */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Creating Symmetric Encryption and Decryption Context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of symmetric crypto context | ||||
|  * | ||||
|  * @return          Symmetric crypto context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_symmetric_create(struct rt_hwcrypto_device *device, | ||||
|         hwcrypto_type type); | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy Symmetric Encryption and Decryption Context | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  */ | ||||
| void rt_hwcrypto_symmetric_destroy(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           This function performs a symmetric encryption or decryption operation | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param mode      Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT | ||||
|  * @param length    The length of the input data in Bytes. This must be a multiple of the block size | ||||
|  * @param in        The buffer holding the input data | ||||
|  * @param out       The buffer holding the output data | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, | ||||
|                                      rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out); | ||||
|  | ||||
| /** | ||||
|  * @brief           Set Symmetric Encryption and Decryption Key | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param key       The crypto key | ||||
|  * @param bitlen    The crypto key bit length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_setkey(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *key, rt_uint32_t bitlen); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get Symmetric Encryption and Decryption Key | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param key       The crypto key buffer | ||||
|  * @param bitlen    The crypto key bit length | ||||
|  * | ||||
|  * @return          Key length of copy | ||||
|  */ | ||||
| int rt_hwcrypto_symmetric_getkey(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *key, rt_uint32_t bitlen); | ||||
|  | ||||
| /** | ||||
|  * @brief           Set Symmetric Encryption and Decryption initialization vector | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param iv        The crypto initialization vector | ||||
|  * @param len       The crypto initialization vector length | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_setiv(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *iv, rt_size_t len); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get Symmetric Encryption and Decryption initialization vector | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param iv        The crypto initialization vector buffer | ||||
|  * @param len       The crypto initialization vector buffer length | ||||
|  * | ||||
|  * @return          IV length of copy | ||||
|  */ | ||||
| int rt_hwcrypto_symmetric_getiv(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *iv, rt_size_t len); | ||||
|  | ||||
| /** | ||||
|  * @brief           Set offset in initialization vector | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param iv_off    The offset in IV | ||||
|  */ | ||||
| void rt_hwcrypto_symmetric_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get offset in initialization vector | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param iv_off    It must point to a valid memory | ||||
|  */ | ||||
| void rt_hwcrypto_symmetric_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off); | ||||
|  | ||||
| /** | ||||
|  * @brief           This function copy symmetric crypto context | ||||
|  * | ||||
|  * @param des       The destination symmetric crypto context | ||||
|  * @param src       The symmetric crypto context to be copy | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src); | ||||
|  | ||||
| /** | ||||
|  * @brief           Reset symmetric crypto context | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  */ | ||||
| void rt_hwcrypto_symmetric_reset(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting symmetric crypto context type | ||||
|  * | ||||
|  * @param ctx       Symmetric crypto context | ||||
|  * @param type      Types of settings | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_symmetric_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif | ||||
							
								
								
									
										255
									
								
								riscv/rtthread/components/drivers/hwcrypto/hwcrypto.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										255
									
								
								riscv/rtthread/components/drivers/hwcrypto/hwcrypto.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,255 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-23     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
| #include <hwcrypto.h> | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting context type (Direct calls are not recommended) | ||||
|  * | ||||
|  * @param ctx       Crypto context | ||||
|  * @param type      Types of settings | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type) | ||||
| { | ||||
|     if (ctx) | ||||
|     { | ||||
|         /* Is it the same category? */ | ||||
|         if ((ctx->type & HWCRYPTO_MAIN_TYPE_MASK) == (type & HWCRYPTO_MAIN_TYPE_MASK)) | ||||
|         { | ||||
|             ctx->type = type; | ||||
|             return RT_EOK; | ||||
|         } | ||||
|         /* Context is empty type */ | ||||
|         else if (ctx->type == HWCRYPTO_TYPE_NULL) | ||||
|         { | ||||
|             ctx->type = type; | ||||
|             return RT_EOK; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             return -RT_ERROR; | ||||
|         } | ||||
|     } | ||||
|     return -RT_EINVAL; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Reset context type (Direct calls are not recommended) | ||||
|  * | ||||
|  * @param ctx       Crypto context | ||||
|  * | ||||
|  */ | ||||
| void rt_hwcrypto_ctx_reset(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     if (ctx && ctx->device->ops->reset) | ||||
|     { | ||||
|         ctx->device->ops->reset(ctx); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Init crypto context | ||||
|  * | ||||
|  * @param ctx       The context to initialize | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of context | ||||
|  * @param obj_size  Size of context object | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_ctx_init(struct rt_hwcrypto_ctx *ctx, struct rt_hwcrypto_device *device, hwcrypto_type type) | ||||
| { | ||||
|     rt_err_t err; | ||||
|  | ||||
|     /* Setting context type */ | ||||
|     rt_hwcrypto_set_type(ctx, type); | ||||
|     ctx->device = device; | ||||
|     /* Create hardware context */ | ||||
|     err = ctx->device->ops->create(ctx); | ||||
|     if (err != RT_EOK) | ||||
|     { | ||||
|         return err; | ||||
|     } | ||||
|     return RT_EOK; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Create crypto context | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of context | ||||
|  * @param obj_size  Size of context object | ||||
|  * | ||||
|  * @return          Crypto context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_ctx_create(struct rt_hwcrypto_device *device, hwcrypto_type type, rt_uint32_t obj_size) | ||||
| { | ||||
|     struct rt_hwcrypto_ctx *ctx; | ||||
|     rt_err_t err; | ||||
|  | ||||
|     /* Parameter checking */ | ||||
|     if (device == RT_NULL || obj_size < sizeof(struct rt_hwcrypto_ctx)) | ||||
|     { | ||||
|         return RT_NULL; | ||||
|     } | ||||
|     ctx = rt_malloc(obj_size); | ||||
|     if (ctx == RT_NULL) | ||||
|     { | ||||
|         return ctx; | ||||
|     } | ||||
|     rt_memset(ctx, 0, obj_size); | ||||
|     /* Init context */ | ||||
|     err = rt_hwcrypto_ctx_init(ctx, device, type); | ||||
|     if (err != RT_EOK) | ||||
|     { | ||||
|         rt_free(ctx); | ||||
|         ctx = RT_NULL; | ||||
|     } | ||||
|     return ctx; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy crypto context | ||||
|  * | ||||
|  * @param device    Crypto context | ||||
|  */ | ||||
| void rt_hwcrypto_ctx_destroy(struct rt_hwcrypto_ctx *ctx) | ||||
| { | ||||
|     if (ctx == RT_NULL) | ||||
|     { | ||||
|         return; | ||||
|     } | ||||
|     /* Destroy hardware context */ | ||||
|     if (ctx->device->ops->destroy) | ||||
|     { | ||||
|         ctx->device->ops->destroy(ctx); | ||||
|     } | ||||
|     /* Free the resources */ | ||||
|     rt_free(ctx); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Copy crypto context | ||||
|  * | ||||
|  * @param des       The destination context | ||||
|  * @param src       The context to be copy | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_ctx_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src) | ||||
| { | ||||
|     if (des == RT_NULL || src == RT_NULL) | ||||
|     { | ||||
|         return -RT_EINVAL; | ||||
|     } | ||||
|  | ||||
|     /* The equipment is different or of different types and cannot be copied */ | ||||
|     if (des->device != src->device || | ||||
|         (des->type & HWCRYPTO_MAIN_TYPE_MASK) != (src->type & HWCRYPTO_MAIN_TYPE_MASK)) | ||||
|     { | ||||
|         return -RT_EINVAL; | ||||
|     } | ||||
|     des->type = src->type; | ||||
|     /* Calling Hardware Context Copy Function */ | ||||
|     return src->device->ops->copy(des, src); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get the default hardware crypto device | ||||
|  * | ||||
|  * @return          Hardware crypto device | ||||
|  * | ||||
|  */ | ||||
| struct rt_hwcrypto_device *rt_hwcrypto_dev_default(void) | ||||
| { | ||||
|     static struct rt_hwcrypto_device *hwcrypto_dev; | ||||
|  | ||||
|     /* If there is a default device, return the device */ | ||||
|     if (hwcrypto_dev) | ||||
|     { | ||||
|         return hwcrypto_dev; | ||||
|     } | ||||
|     /* Find by default device name */ | ||||
|     hwcrypto_dev = (struct rt_hwcrypto_device *)rt_device_find(RT_HWCRYPTO_DEFAULT_NAME); | ||||
|     return hwcrypto_dev; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @brief           Get the unique ID of the device | ||||
|  * | ||||
|  * @param device    Device object | ||||
|  * | ||||
|  * @return          Device unique ID | ||||
|  */ | ||||
| rt_uint64_t rt_hwcrypto_id(struct rt_hwcrypto_device *device) | ||||
| { | ||||
|     if (device) | ||||
|     { | ||||
|         return device->id; | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| #ifdef RT_USING_DEVICE_OPS | ||||
| const static struct rt_device_ops hwcrypto_ops = | ||||
| { | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     RT_NULL | ||||
| }; | ||||
| #endif | ||||
|  | ||||
| /** | ||||
|  * @brief           Register hardware crypto device | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param name      Name of device | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_register(struct rt_hwcrypto_device *device, const char *name) | ||||
| { | ||||
|     rt_err_t err; | ||||
|  | ||||
|     RT_ASSERT(device != RT_NULL); | ||||
|     RT_ASSERT(name != RT_NULL); | ||||
|     RT_ASSERT(device->ops != RT_NULL); | ||||
|     RT_ASSERT(device->ops->create != RT_NULL); | ||||
|     RT_ASSERT(device->ops->destroy != RT_NULL); | ||||
|     RT_ASSERT(device->ops->copy != RT_NULL); | ||||
|     RT_ASSERT(device->ops->reset != RT_NULL); | ||||
|  | ||||
|     rt_memset(&device->parent, 0, sizeof(struct rt_device)); | ||||
| #ifdef RT_USING_DEVICE_OPS | ||||
|     device->parent.ops = &hwcrypto_ops; | ||||
| #else | ||||
|     device->parent.init       = RT_NULL; | ||||
|     device->parent.open       = RT_NULL; | ||||
|     device->parent.close      = RT_NULL; | ||||
|     device->parent.read       = RT_NULL; | ||||
|     device->parent.write      = RT_NULL; | ||||
|     device->parent.control    = RT_NULL; | ||||
| #endif | ||||
|  | ||||
|     device->parent.user_data  = RT_NULL; | ||||
|     device->parent.type = RT_Device_Class_Security; | ||||
|  | ||||
|     /* Register device */ | ||||
|     err = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR); | ||||
|  | ||||
|     return err; | ||||
| } | ||||
							
								
								
									
										193
									
								
								riscv/rtthread/components/drivers/hwcrypto/hwcrypto.h
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										193
									
								
								riscv/rtthread/components/drivers/hwcrypto/hwcrypto.h
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,193 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2019-04-23     tyx          the first version | ||||
|  */ | ||||
|  | ||||
| #ifndef __HWCRYPTO_H__ | ||||
| #define __HWCRYPTO_H__ | ||||
|  | ||||
| #include <rtthread.h> | ||||
|  | ||||
| #ifndef RT_HWCRYPTO_DEFAULT_NAME | ||||
| #define RT_HWCRYPTO_DEFAULT_NAME    ("hwcryto") | ||||
| #endif | ||||
|  | ||||
| #define HWCRYPTO_MAIN_TYPE_MASK     (0xffffUL << 16) | ||||
| #define HWCRYPTO_SUB_TYPE_MASK      (0xffUL << 8) | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| typedef enum | ||||
| { | ||||
|     HWCRYPTO_TYPE_NULL = 0x00000000, | ||||
|  | ||||
|     /* Main Type */ | ||||
|     /* symmetric Type */ | ||||
|     HWCRYPTO_TYPE_HEAD    = __LINE__, | ||||
|     HWCRYPTO_TYPE_AES     = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< AES */ | ||||
|     HWCRYPTO_TYPE_DES     = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< DES */ | ||||
|     HWCRYPTO_TYPE_3DES    = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< 3DES */ | ||||
|     HWCRYPTO_TYPE_RC4     = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< RC4 */ | ||||
|     HWCRYPTO_TYPE_GCM     = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< GCM */ | ||||
|     /* HASH Type */ | ||||
|     HWCRYPTO_TYPE_MD5     = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< MD5 */ | ||||
|     HWCRYPTO_TYPE_SHA1    = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< SHA1 */ | ||||
|     HWCRYPTO_TYPE_SHA2    = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< SHA2 */ | ||||
|     /* Other Type */ | ||||
|     HWCRYPTO_TYPE_RNG     = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< RNG */ | ||||
|     HWCRYPTO_TYPE_CRC     = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< CRC */ | ||||
|     HWCRYPTO_TYPE_BIGNUM  = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16,  /**< BIGNUM */ | ||||
|  | ||||
|     /* AES Subtype */ | ||||
|     HWCRYPTO_TYPE_AES_ECB = HWCRYPTO_TYPE_AES | (0x01 << 8), | ||||
|     HWCRYPTO_TYPE_AES_CBC = HWCRYPTO_TYPE_AES | (0x02 << 8), | ||||
|     HWCRYPTO_TYPE_AES_CFB = HWCRYPTO_TYPE_AES | (0x03 << 8), | ||||
|     HWCRYPTO_TYPE_AES_CTR = HWCRYPTO_TYPE_AES | (0x04 << 8), | ||||
|     HWCRYPTO_TYPE_AES_OFB = HWCRYPTO_TYPE_AES | (0x05 << 8), | ||||
|  | ||||
|     /* DES Subtype */ | ||||
|     HWCRYPTO_TYPE_DES_ECB = HWCRYPTO_TYPE_DES | (0x01 << 8), | ||||
|     HWCRYPTO_TYPE_DES_CBC = HWCRYPTO_TYPE_DES | (0x02 << 8), | ||||
|  | ||||
|     /* 3DES Subtype */ | ||||
|     HWCRYPTO_TYPE_3DES_ECB = HWCRYPTO_TYPE_3DES | (0x01 << 8), | ||||
|     HWCRYPTO_TYPE_3DES_CBC = HWCRYPTO_TYPE_3DES | (0x02 << 8), | ||||
|  | ||||
|     /* SHA2 Subtype */ | ||||
|     HWCRYPTO_TYPE_SHA224 = HWCRYPTO_TYPE_SHA2 | (0x01 << 8), | ||||
|     HWCRYPTO_TYPE_SHA256 = HWCRYPTO_TYPE_SHA2 | (0x02 << 8), | ||||
|     HWCRYPTO_TYPE_SHA384 = HWCRYPTO_TYPE_SHA2 | (0x03 << 8), | ||||
|     HWCRYPTO_TYPE_SHA512 = HWCRYPTO_TYPE_SHA2 | (0x04 << 8), | ||||
| } hwcrypto_type; | ||||
|  | ||||
| typedef enum | ||||
| { | ||||
|     HWCRYPTO_MODE_ENCRYPT = 0x1,        /**< Encryption operations */ | ||||
|     HWCRYPTO_MODE_DECRYPT = 0x2,        /**< Decryption operations */ | ||||
|     HWCRYPTO_MODE_UNKNOWN = 0x7fffffff, /**< Unknown */ | ||||
| } hwcrypto_mode; | ||||
|  | ||||
| struct rt_hwcrypto_ctx; | ||||
|  | ||||
| struct rt_hwcrypto_ops | ||||
| { | ||||
|     rt_err_t (*create)(struct rt_hwcrypto_ctx *ctx);        /**< Creating hardware context */ | ||||
|     void (*destroy)(struct rt_hwcrypto_ctx *ctx);           /**< Delete hardware context */ | ||||
|     rt_err_t (*copy)(struct rt_hwcrypto_ctx *des, | ||||
|                      const struct rt_hwcrypto_ctx *src);    /**< Cpoy hardware context */ | ||||
|     void (*reset)(struct rt_hwcrypto_ctx *ctx);             /**< Reset hardware context */ | ||||
| }; | ||||
|  | ||||
| struct rt_hwcrypto_device | ||||
| { | ||||
|     struct rt_device parent;                            /**< Inherited from the standard device */ | ||||
|     const struct rt_hwcrypto_ops *ops;                  /**< Hardware crypto ops */ | ||||
|     rt_uint64_t id;                                     /**< Unique id */ | ||||
|     void *user_data;                                    /**< Device user data */ | ||||
| }; | ||||
|  | ||||
| struct rt_hwcrypto_ctx | ||||
| { | ||||
|     struct rt_hwcrypto_device *device;  /**< Binding device */ | ||||
|     hwcrypto_type type;                 /**< Encryption and decryption types */ | ||||
|     void *contex;                       /**< Hardware context */ | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @brief           Setting context type (Direct calls are not recommended) | ||||
|  * | ||||
|  * @param ctx       Crypto context | ||||
|  * @param type      Types of settings | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); | ||||
|  | ||||
| /** | ||||
|  * @brief           Reset context type (Direct calls are not recommended) | ||||
|  * | ||||
|  * @param ctx       Crypto context | ||||
|  */ | ||||
| void rt_hwcrypto_ctx_reset(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           Init crypto context (Direct calls are not recommended) | ||||
|  * | ||||
|  * @param ctx       The context to initialize | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of context | ||||
|  * @param obj_size  Size of context object | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_ctx_init(struct rt_hwcrypto_ctx *ctx, | ||||
|                               struct rt_hwcrypto_device *device, hwcrypto_type type); | ||||
|  | ||||
| /** | ||||
|  * @brief           Create crypto context (Direct calls are not recommended) | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param type      Type of context | ||||
|  * @param obj_size  Size of context object | ||||
|  * | ||||
|  * @return          Crypto context | ||||
|  */ | ||||
| struct rt_hwcrypto_ctx *rt_hwcrypto_ctx_create(struct rt_hwcrypto_device *device, | ||||
|                                                hwcrypto_type type, rt_uint32_t obj_size); | ||||
|  | ||||
| /** | ||||
|  * @brief           Destroy crypto context (Direct calls are not recommended) | ||||
|  * | ||||
|  * @param device    Crypto context | ||||
|  */ | ||||
| void rt_hwcrypto_ctx_destroy(struct rt_hwcrypto_ctx *ctx); | ||||
|  | ||||
| /** | ||||
|  * @brief           Copy crypto context (Direct calls are not recommended) | ||||
|  * | ||||
|  * @param des       The destination context | ||||
|  * @param src       The context to be copy | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_ctx_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src); | ||||
|  | ||||
| /** | ||||
|  * @brief           Register hardware crypto device | ||||
|  * | ||||
|  * @param device    Hardware crypto device | ||||
|  * @param name      Name of device | ||||
|  * | ||||
|  * @return          RT_EOK on success. | ||||
|  */ | ||||
| rt_err_t rt_hwcrypto_register(struct rt_hwcrypto_device *device, const char *name); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get the default hardware crypto device | ||||
|  * | ||||
|  * @return          Hardware crypto device | ||||
|  * | ||||
|  */ | ||||
| struct rt_hwcrypto_device *rt_hwcrypto_dev_default(void); | ||||
|  | ||||
| /** | ||||
|  * @brief           Get the unique ID of the device | ||||
|  * | ||||
|  * @param device    Device object | ||||
|  * | ||||
|  * @return          Device unique ID | ||||
|  */ | ||||
| rt_uint64_t rt_hwcrypto_id(struct rt_hwcrypto_device *device); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif | ||||
		Reference in New Issue
	
	Block a user