1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2019-04-25     tyx          the first version
9  */
10 
11 #ifndef __HW_SYMMETRIC_H__
12 #define __HW_SYMMETRIC_H__
13 
14 #include <hwcrypto.h>
15 
16 #ifndef RT_HWCRYPTO_IV_MAX_SIZE
17 #define RT_HWCRYPTO_IV_MAX_SIZE  (16)
18 #endif
19 #ifndef RT_HWCRYPTO_KEYBIT_MAX_SIZE
20 #define RT_HWCRYPTO_KEYBIT_MAX_SIZE  (256)
21 #endif
22 
23 #define SYMMTRIC_MODIFY_KEY    (0x1 << 0)
24 #define SYMMTRIC_MODIFY_IV     (0x1 << 1)
25 #define SYMMTRIC_MODIFY_IVOFF  (0x1 << 2)
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 struct hwcrypto_symmetric;
32 struct hwcrypto_symmetric_info;
33 
34 struct hwcrypto_symmetric_ops
35 {
36     rt_err_t (*crypt)(struct hwcrypto_symmetric *symmetric_ctx,
37                       struct hwcrypto_symmetric_info *symmetric_info);  /**< Hardware Symmetric Encryption and Decryption Callback */
38 };
39 
40 /**
41  * @brief           Hardware driver usage, including input and output information
42  */
43 struct hwcrypto_symmetric_info
44 {
45     hwcrypto_mode mode;             /**< crypto mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT */
46     const rt_uint8_t *in;           /**< Input data */
47     rt_uint8_t *out;                /**< Output data will be written */
48     rt_size_t length;               /**< The length of the input data in Bytes. It's a multiple of block size. */
49 };
50 
51 /**
52  * @brief           Symmetric crypto context. Hardware driver usage
53  */
54 struct hwcrypto_symmetric
55 {
56     struct rt_hwcrypto_ctx parent;                      /**< Inheritance from hardware crypto context */
57     rt_uint16_t flags;                                  /**< key or iv or ivoff has been changed. The flag will be set up */
58     rt_uint16_t iv_len;                                 /**< initialization vector effective length */
59     rt_uint16_t iv_off;                                 /**< The offset in IV */
60     rt_uint16_t key_bitlen;                             /**< The crypto key bit length */
61     rt_uint8_t iv[RT_HWCRYPTO_IV_MAX_SIZE];             /**< The initialization vector */
62     rt_uint8_t key[RT_HWCRYPTO_KEYBIT_MAX_SIZE >> 3];   /**< The crypto key */
63     const struct hwcrypto_symmetric_ops *ops;           /**< !! Hardware initializes this value when creating context !! */
64 };
65 
66 /**
67  * @brief           Creating Symmetric Encryption and Decryption Context
68  *
69  * @param device    Hardware crypto device
70  * @param type      Type of symmetric crypto context
71  *
72  * @return          Symmetric crypto context
73  */
74 struct rt_hwcrypto_ctx *rt_hwcrypto_symmetric_create(struct rt_hwcrypto_device *device,
75         hwcrypto_type type);
76 
77 /**
78  * @brief           Destroy Symmetric Encryption and Decryption Context
79  *
80  * @param ctx       Symmetric crypto context
81  */
82 void rt_hwcrypto_symmetric_destroy(struct rt_hwcrypto_ctx *ctx);
83 
84 /**
85  * @brief           This function performs a symmetric encryption or decryption operation
86  *
87  * @param ctx       Symmetric crypto context
88  * @param mode      Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT
89  * @param length    The length of the input data in Bytes. This must be a multiple of the block size
90  * @param in        The buffer holding the input data
91  * @param out       The buffer holding the output data
92  *
93  * @return          RT_EOK on success.
94  */
95 rt_err_t rt_hwcrypto_symmetric_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode,
96                                      rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out);
97 
98 /**
99  * @brief           Set Symmetric Encryption and Decryption Key
100  *
101  * @param ctx       Symmetric crypto context
102  * @param key       The crypto key
103  * @param bitlen    The crypto key bit length
104  *
105  * @return          RT_EOK on success.
106  */
107 rt_err_t rt_hwcrypto_symmetric_setkey(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *key, rt_uint32_t bitlen);
108 
109 /**
110  * @brief           Get Symmetric Encryption and Decryption Key
111  *
112  * @param ctx       Symmetric crypto context
113  * @param key       The crypto key buffer
114  * @param bitlen    The crypto key bit length
115  *
116  * @return          Key length of copy
117  */
118 int rt_hwcrypto_symmetric_getkey(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *key, rt_uint32_t bitlen);
119 
120 /**
121  * @brief           Set Symmetric Encryption and Decryption initialization vector
122  *
123  * @param ctx       Symmetric crypto context
124  * @param iv        The crypto initialization vector
125  * @param len       The crypto initialization vector length
126  *
127  * @return          RT_EOK on success.
128  */
129 rt_err_t rt_hwcrypto_symmetric_setiv(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *iv, rt_size_t len);
130 
131 /**
132  * @brief           Get Symmetric Encryption and Decryption initialization vector
133  *
134  * @param ctx       Symmetric crypto context
135  * @param iv        The crypto initialization vector buffer
136  * @param len       The crypto initialization vector buffer length
137  *
138  * @return          IV length of copy
139  */
140 int rt_hwcrypto_symmetric_getiv(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *iv, rt_size_t len);
141 
142 /**
143  * @brief           Set offset in initialization vector
144  *
145  * @param ctx       Symmetric crypto context
146  * @param iv_off    The offset in IV
147  */
148 void rt_hwcrypto_symmetric_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off);
149 
150 /**
151  * @brief           Get offset in initialization vector
152  *
153  * @param ctx       Symmetric crypto context
154  * @param iv_off    It must point to a valid memory
155  */
156 void rt_hwcrypto_symmetric_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off);
157 
158 /**
159  * @brief           This function copy symmetric crypto context
160  *
161  * @param des       The destination symmetric crypto context
162  * @param src       The symmetric crypto context to be copy
163  *
164  * @return          RT_EOK on success.
165  */
166 rt_err_t rt_hwcrypto_symmetric_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src);
167 
168 /**
169  * @brief           Reset symmetric crypto context
170  *
171  * @param ctx       Symmetric crypto context
172  */
173 void rt_hwcrypto_symmetric_reset(struct rt_hwcrypto_ctx *ctx);
174 
175 /**
176  * @brief           Setting symmetric crypto context type
177  *
178  * @param ctx       Symmetric crypto context
179  * @param type      Types of settings
180  *
181  * @return          RT_EOK on success.
182  */
183 rt_err_t rt_hwcrypto_symmetric_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type);
184 
185 #ifdef __cplusplus
186 }
187 #endif
188 
189 #endif
190