1 /* 2 * Copyright (c) 2006-2023, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2019-04-23 tyx the first version 9 */ 10 11 #ifndef __HWCRYPTO_H__ 12 #define __HWCRYPTO_H__ 13 14 #include <rtthread.h> 15 16 #ifndef RT_HWCRYPTO_DEFAULT_NAME 17 #define RT_HWCRYPTO_DEFAULT_NAME ("hwcryto") 18 #endif 19 20 #define HWCRYPTO_MAIN_TYPE_MASK (0xffffUL << 16) 21 #define HWCRYPTO_SUB_TYPE_MASK (0xffUL << 8) 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 typedef enum 28 { 29 HWCRYPTO_TYPE_NULL = 0x00000000, 30 31 /* Main Type */ 32 /* symmetric Type */ 33 HWCRYPTO_TYPE_HEAD = __LINE__, 34 HWCRYPTO_TYPE_AES = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< AES */ 35 HWCRYPTO_TYPE_DES = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< DES */ 36 HWCRYPTO_TYPE_3DES = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< 3DES */ 37 HWCRYPTO_TYPE_RC4 = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< RC4 */ 38 HWCRYPTO_TYPE_GCM = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< GCM */ 39 /* HASH Type */ 40 HWCRYPTO_TYPE_MD5 = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< MD5 */ 41 HWCRYPTO_TYPE_SHA1 = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< SHA1 */ 42 HWCRYPTO_TYPE_SHA2 = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< SHA2 */ 43 /* Other Type */ 44 HWCRYPTO_TYPE_RNG = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< RNG */ 45 HWCRYPTO_TYPE_CRC = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< CRC */ 46 HWCRYPTO_TYPE_BIGNUM = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< BIGNUM */ 47 48 /* AES Subtype */ 49 HWCRYPTO_TYPE_AES_ECB = HWCRYPTO_TYPE_AES | (0x01 << 8), 50 HWCRYPTO_TYPE_AES_CBC = HWCRYPTO_TYPE_AES | (0x02 << 8), 51 HWCRYPTO_TYPE_AES_CFB = HWCRYPTO_TYPE_AES | (0x03 << 8), 52 HWCRYPTO_TYPE_AES_CTR = HWCRYPTO_TYPE_AES | (0x04 << 8), 53 HWCRYPTO_TYPE_AES_OFB = HWCRYPTO_TYPE_AES | (0x05 << 8), 54 55 /* DES Subtype */ 56 HWCRYPTO_TYPE_DES_ECB = HWCRYPTO_TYPE_DES | (0x01 << 8), 57 HWCRYPTO_TYPE_DES_CBC = HWCRYPTO_TYPE_DES | (0x02 << 8), 58 59 /* 3DES Subtype */ 60 HWCRYPTO_TYPE_3DES_ECB = HWCRYPTO_TYPE_3DES | (0x01 << 8), 61 HWCRYPTO_TYPE_3DES_CBC = HWCRYPTO_TYPE_3DES | (0x02 << 8), 62 63 /* SHA2 Subtype */ 64 HWCRYPTO_TYPE_SHA224 = HWCRYPTO_TYPE_SHA2 | (0x01 << 8), 65 HWCRYPTO_TYPE_SHA256 = HWCRYPTO_TYPE_SHA2 | (0x02 << 8), 66 HWCRYPTO_TYPE_SHA384 = HWCRYPTO_TYPE_SHA2 | (0x03 << 8), 67 HWCRYPTO_TYPE_SHA512 = HWCRYPTO_TYPE_SHA2 | (0x04 << 8), 68 } hwcrypto_type; 69 70 typedef enum 71 { 72 HWCRYPTO_MODE_ENCRYPT = 0x1, /**< Encryption operations */ 73 HWCRYPTO_MODE_DECRYPT = 0x2, /**< Decryption operations */ 74 HWCRYPTO_MODE_UNKNOWN = 0x7fffffff, /**< Unknown */ 75 } hwcrypto_mode; 76 77 struct rt_hwcrypto_ctx; 78 79 struct rt_hwcrypto_ops 80 { 81 rt_err_t (*create)(struct rt_hwcrypto_ctx *ctx); /**< Creating hardware context */ 82 void (*destroy)(struct rt_hwcrypto_ctx *ctx); /**< Delete hardware context */ 83 rt_err_t (*copy)(struct rt_hwcrypto_ctx *des, 84 const struct rt_hwcrypto_ctx *src); /**< Cpoy hardware context */ 85 void (*reset)(struct rt_hwcrypto_ctx *ctx); /**< Reset hardware context */ 86 }; 87 88 struct rt_hwcrypto_device 89 { 90 struct rt_device parent; /**< Inherited from the standard device */ 91 const struct rt_hwcrypto_ops *ops; /**< Hardware crypto ops */ 92 rt_uint64_t id; /**< Unique id */ 93 void *user_data; /**< Device user data */ 94 }; 95 96 struct rt_hwcrypto_ctx 97 { 98 struct rt_hwcrypto_device *device; /**< Binding device */ 99 hwcrypto_type type; /**< Encryption and decryption types */ 100 void *contex; /**< Hardware context */ 101 }; 102 103 /** 104 * @brief Setting context type (Direct calls are not recommended) 105 * 106 * @param ctx Crypto context 107 * @param type Types of settings 108 * 109 * @return RT_EOK on success. 110 */ 111 rt_err_t rt_hwcrypto_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); 112 113 /** 114 * @brief Reset context type (Direct calls are not recommended) 115 * 116 * @param ctx Crypto context 117 */ 118 void rt_hwcrypto_ctx_reset(struct rt_hwcrypto_ctx *ctx); 119 120 /** 121 * @brief Init crypto context (Direct calls are not recommended) 122 * 123 * @param ctx The context to initialize 124 * @param device Hardware crypto device 125 * @param type Type of context 126 * @param obj_size Size of context object 127 * 128 * @return RT_EOK on success. 129 */ 130 rt_err_t rt_hwcrypto_ctx_init(struct rt_hwcrypto_ctx *ctx, 131 struct rt_hwcrypto_device *device, hwcrypto_type type); 132 133 /** 134 * @brief Create crypto context (Direct calls are not recommended) 135 * 136 * @param device Hardware crypto device 137 * @param type Type of context 138 * @param obj_size Size of context object 139 * 140 * @return Crypto context 141 */ 142 struct rt_hwcrypto_ctx *rt_hwcrypto_ctx_create(struct rt_hwcrypto_device *device, 143 hwcrypto_type type, rt_uint32_t obj_size); 144 145 /** 146 * @brief Destroy crypto context (Direct calls are not recommended) 147 * 148 * @param device Crypto context 149 */ 150 void rt_hwcrypto_ctx_destroy(struct rt_hwcrypto_ctx *ctx); 151 152 /** 153 * @brief Copy crypto context (Direct calls are not recommended) 154 * 155 * @param des The destination context 156 * @param src The context to be copy 157 * 158 * @return RT_EOK on success. 159 */ 160 rt_err_t rt_hwcrypto_ctx_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src); 161 162 /** 163 * @brief Register hardware crypto device 164 * 165 * @param device Hardware crypto device 166 * @param name Name of device 167 * 168 * @return RT_EOK on success. 169 */ 170 rt_err_t rt_hwcrypto_register(struct rt_hwcrypto_device *device, const char *name); 171 172 /** 173 * @brief Get the default hardware crypto device 174 * 175 * @return Hardware crypto device 176 * 177 */ 178 struct rt_hwcrypto_device *rt_hwcrypto_dev_default(void); 179 180 /** 181 * @brief Get the unique ID of the device 182 * 183 * @param device Device object 184 * 185 * @return Device unique ID 186 */ 187 rt_uint64_t rt_hwcrypto_id(struct rt_hwcrypto_device *device); 188 189 #ifdef __cplusplus 190 } 191 #endif 192 193 #endif 194