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-05-14     tyx          the first version
9  */
10 
11 #ifndef __HW_GCM_H__
12 #define __HW_GCM_H__
13 
14 #include "hw_symmetric.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 struct hwcrypto_gcm;
21 
22 struct hwcrypto_gcm_ops
23 {
24     rt_err_t (*start)(struct hwcrypto_gcm *gcm_ctx,
25                       const unsigned char *add, rt_size_t add_len);    /**< Set additional data. start GCM operation */
26     rt_err_t (*finish)(struct hwcrypto_gcm *gcm_ctx,
27                        const unsigned char *tag, rt_size_t tag_len);   /**< finish GCM operation. get tag */
28 };
29 
30 /**
31  * @brief           GCM context. Hardware driver usage
32  */
33 struct hwcrypto_gcm
34 {
35     struct hwcrypto_symmetric parent;       /**< Inheritance from hardware symmetric crypto context */
36     hwcrypto_type crypt_type;               /**< symmetric crypto type. eg: AES/DES */
37     const struct hwcrypto_gcm_ops *ops;     /**< !! Hardware initializes this value when creating context !! */
38 };
39 
40 /**
41  * @brief           Creating GCM Context
42  *
43  * @param device    Hardware crypto device
44  * @param type      Type of symmetric crypto context
45  *
46  * @return          GCM context
47  */
48 struct rt_hwcrypto_ctx *rt_hwcrypto_gcm_create(struct rt_hwcrypto_device *device,
49                                                hwcrypto_type crypt_type);
50 
51 /**
52  * @brief           Destroy GCM Context
53  *
54  * @param ctx       GCM context
55  */
56 void rt_hwcrypto_gcm_destroy(struct rt_hwcrypto_ctx *ctx);
57 
58 /**
59  * @brief           This function starts a GCM encryption or decryption operation
60  *
61  * @param ctx       GCM context
62  * @param add       The buffer holding the additional data
63  * @param add_len   The length of the additional data
64  *
65  * @return          RT_EOK on success.
66  */
67 rt_err_t rt_hwcrypto_gcm_start(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *add,
68                                rt_size_t add_len);
69 
70 /**
71  * @brief           This function finishes the GCM operation and generates the authentication tag
72  *
73  * @param ctx       GCM context
74  * @param tag       The buffer for holding the tag
75  * @param tag_len   The length of the tag to generate
76  *
77  * @return          RT_EOK on success.
78  */
79 rt_err_t rt_hwcrypto_gcm_finish(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *tag,
80                                 rt_size_t tag_len);
81 
82 /**
83  * @brief           This function performs a symmetric encryption or decryption operation
84  *
85  * @param ctx       GCM context
86  * @param mode      Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT
87  * @param length    The length of the input data in Bytes. This must be a multiple of the block size
88  * @param in        The buffer holding the input data
89  * @param out       The buffer holding the output data
90  *
91  * @return          RT_EOK on success.
92  */
93 rt_err_t rt_hwcrypto_gcm_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode,
94                                rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out);
95 
96 /**
97  * @brief           Set Symmetric Encryption and Decryption Key
98  *
99  * @param ctx       GCM context
100  * @param key       The crypto key
101  * @param bitlen    The crypto key bit length
102  *
103  * @return          RT_EOK on success.
104  */
105 rt_err_t rt_hwcrypto_gcm_setkey(struct rt_hwcrypto_ctx *ctx,
106                                 const rt_uint8_t *key, rt_uint32_t bitlen);
107 
108 /**
109  * @brief           Get Symmetric Encryption and Decryption Key
110  *
111  * @param ctx       GCM context
112  * @param key       The crypto key buffer
113  * @param bitlen    The crypto key bit length
114  *
115  * @return          Key length of copy
116  */
117 rt_err_t rt_hwcrypto_gcm_getkey(struct rt_hwcrypto_ctx *ctx,
118                                 rt_uint8_t *key, rt_uint32_t bitlen);
119 
120 /**
121  * @brief           Set Symmetric Encryption and Decryption initialization vector
122  *
123  * @param ctx       GCM 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_gcm_setiv(struct rt_hwcrypto_ctx *ctx,
130                                const rt_uint8_t *iv, rt_size_t len);
131 
132 /**
133  * @brief           Get Symmetric Encryption and Decryption initialization vector
134  *
135  * @param ctx       GCM context
136  * @param iv        The crypto initialization vector buffer
137  * @param len       The crypto initialization vector buffer length
138  *
139  * @return          IV length of copy
140  */
141 rt_err_t rt_hwcrypto_gcm_getiv(struct rt_hwcrypto_ctx *ctx,
142                                rt_uint8_t *iv, rt_size_t len);
143 
144 /**
145  * @brief           Set offset in initialization vector
146  *
147  * @param ctx       GCM context
148  * @param iv_off    The offset in IV
149  */
150 void rt_hwcrypto_gcm_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off);
151 
152 /**
153  * @brief           Get offset in initialization vector
154  *
155  * @param ctx       GCM context
156  * @param iv_off    It must point to a valid memory
157  */
158 void rt_hwcrypto_gcm_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off);
159 
160 /**
161  * @brief           This function copy GCM context
162  *
163  * @param des       The destination GCM context
164  * @param src       The GCM context to be copy
165  *
166  * @return          RT_EOK on success.
167  */
168 rt_err_t rt_hwcrypto_gcm_cpy(struct rt_hwcrypto_ctx *des,
169                              const struct rt_hwcrypto_ctx *src);
170 
171 /**
172  * @brief           Reset GCM context
173  *
174  * @param ctx       GCM context
175  */
176 void rt_hwcrypto_gcm_reset(struct rt_hwcrypto_ctx *ctx);
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 #endif
183