1 /**
2  * \file
3  *
4  * \brief AES Advanced Encryption Standard(Sync) functionality declaration.
5  *
6  * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Subject to your compliance with these terms, you may use Microchip
13  * software and any derivatives exclusively with Microchip products.
14  * It is your responsibility to comply with third party license terms applicable
15  * to your use of third party software (including open source software) that
16  * may accompany Microchip software.
17  *
18  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19  * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20  * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22  * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23  * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24  * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25  * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
26  * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27  * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28  * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29  *
30  * \asf_license_stop
31  *
32  */
33 
34 #ifndef HPL_AES_SYNC_H_INCLUDED
35 #define HPL_AES_SYNC_H_INCLUDED
36 #include "hpl_aes.h"
37 #include "utils.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 COMPILER_PACK_SET(4)
44 struct _aes_sync_device {
45 	void *           hw;      /*!< Hardware module instance handler */
46 	uint8_t          key[32]; /*!< Key value 128/192/256 bits */
47 	uint8_t          iv[16];  /*!< Initialization Vector */
48 	uint32_t         aad_len; /*!< length of additional data(GCM) */
49 	enum aes_keysize keysize; /*!< bit length of key */
50 };
51 COMPILER_PACK_RESET()
52 
53 /**
54  * \brief              Initialize AES
55  *
56  * \param[in]  dev     The pointer to device instance
57  * \param[in]  hw      The pointer to hardware instance
58  */
59 int32_t _aes_sync_init(struct _aes_sync_device *const dev, void *const hw);
60 
61 /**
62  * \brief              Deinitialize AES
63  *
64  * \param[in]  dev     The pointer to device instance
65  */
66 int32_t _aes_sync_deinit(struct _aes_sync_device *const dev);
67 
68 /**
69  * \brief              Enable AES
70  *
71  * \param[in]  dev     The pointer to device instance
72  */
73 int32_t _aes_sync_enable(struct _aes_sync_device *const dev);
74 
75 /**
76  * \brief              Disable AES
77  *
78  * \param[in]  dev     The pointer to device instance
79  */
80 int32_t _aes_sync_disable(struct _aes_sync_device *const dev);
81 
82 /**
83  * \brief              Set AES Key (encryption/decryption)
84  *
85  * \param[in] dev      The pointer to device instance
86  * \param[in] key      Encryption/decryption key
87  * \param[in] size     Bit length of key
88  */
89 int32_t _aes_sync_set_key(struct _aes_sync_device *const dev, const uint8_t *key, const enum aes_keysize size);
90 
91 /**
92  * \brief              AES-ECB block encryption/decryption
93  *
94  * \param[in]  dev     The pointer to device instance
95  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
96  * \param[in]  input   16-byte input data
97  * \param[out] output  16-byte output data
98  *
99  * \return             ERR_NONE if successful
100  */
101 int32_t _aes_sync_ecb_crypt(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
102                             uint8_t *output);
103 
104 /**
105  * \brief              AES-CBC block encryption/decryption
106  *
107  * \param[in]  dev     The pointer to device instance
108  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
109  * \param[in]  input   16-byte input data
110  * \param[out] output  16-byte output data
111  * \param[in]  length  Byte length of the input data
112  * \param[in, out] iv  Initialization vector (updated after use)
113  *
114  * \return             ERR_NONE if successful
115  */
116 int32_t _aes_sync_cbc_crypt(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
117                             uint8_t *output, uint32_t length, uint8_t iv[16]);
118 
119 /**
120  * \brief              AES-CFB128 block encryption/decryption
121  *
122  * \param[in]  dev     The pointer to device instance
123  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
124  * \param[in]  input   Buffer holding the input data
125  * \param[out] output  Buffer holding the output data
126  * \param[out] length  Byte length of the input data
127  * \param[in, out] iv  Initialization Vector (updated after use)
128  * \param[in, out] iv_ofst  Offset in IV (updated after use)
129 
130  * \return             ERR_NONE if successful
131  */
132 int32_t _aes_sync_cfb128_crypt(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
133                                uint8_t *output, uint32_t length, uint8_t *iv);
134 
135 /**
136  * \brief              AES-CFB64 block encryption/decryption
137  *
138  * \param[in]  dev     The pointer to device instance
139  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
140  * \param[in]  input   Buffer holding the input data
141  * \param[out] output  Buffer holding the output data
142  * \param[out] length  Byte length of the input data
143  * \param[in, out] iv      Initialization Vector (updated after use)
144  *
145  * \return             ERR_NONE if successful
146  */
147 int32_t _aes_sync_cfb64_crypt(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
148                               uint8_t *output, uint32_t length, uint8_t *iv);
149 
150 /**
151  * \brief              AES-CFB32 block encryption/decryption
152  *
153  * \param[in]  dev     The pointer to device instance
154  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
155  * \param[in]  input   Buffer holding the input data
156  * \param[out] output  Buffer holding the output data
157  * \param[out] length  Byte length of the input data
158  * \param[in, out] iv      Initialization Vector (updated after use)
159  *
160  * \return             ERR_NONE if successful
161  */
162 int32_t _aes_sync_cfb32_crypt(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
163                               uint8_t *output, uint32_t length, uint8_t *iv);
164 
165 /**
166  * \brief              AES-CFB16 block encryption/decryption
167  *
168  * \param[in]  dev     The pointer to device instance
169  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
170  * \param[in]  input   Buffer holding the input data
171  * \param[out] output  Buffer holding the output data
172  * \param[out] length  Byte length of the input data
173  * \param[in, out] iv      Initialization Vector (updated after use)
174  *
175  * \return             ERR_NONE if successful
176  */
177 int32_t _aes_sync_cfb16_crypt(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
178                               uint8_t *output, uint32_t length, uint8_t *iv);
179 
180 /**
181  * \brief              AES-CFB8 block encryption/decryption
182  *
183  * \param[in]  dev     The pointer to device instance
184  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
185  * \param[in]  input   Buffer holding the input data
186  * \param[out] output  Buffer holding the output data
187  * \param[in, out] iv  Initialization Vector (updated after use)
188  *
189  * \return             ERR_NONE if successful
190  */
191 int32_t _aes_sync_cfb8_crypt(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
192                              uint8_t *output, uint32_t length, uint8_t *iv);
193 
194 /**
195  * \brief              AES-OFB block encryption/decryption
196  *
197  * \param[in]  dev     The pointer to device instance
198  * \param[in]  input   Buffer holding the input data
199  * \param[out] output  Buffer holding the output data
200  * \param[out] length  Byte length of the input data
201  * \param[in, out] iv      Initialization Vector (updated after use)
202  * \param[in, out] iv_ofst  Offset in IV (updated after use)
203 
204  * \return             ERR_NONE if successful
205  */
206 int32_t _aes_sync_ofb_crypt(struct _aes_sync_device *const dev, const uint8_t *input, uint8_t *output, uint32_t length,
207                             uint8_t *iv);
208 
209 /**
210  * \brief              AES-CTR block encryption/decryption
211  *
212  * \param[in]  dev     The pointer to device instance
213  * \param[in]  input   Buffer holding the input data
214  * \param[out] output  Buffer holding the output data
215  * \param[in]  length  Byte length of the input data
216  * \param[in]  nc      The 128-bit nonce and counter
217  * \param[in]  nc_ofst  The offset in the current stream_block (for resuming
218  *                     within current cipher stream). The offset pointer to
219  *                     should be 0 at the start of a stream.
220  *
221  * \return             ERR_NONE if successful
222  */
223 int32_t _aes_sync_ctr_crypt(struct _aes_sync_device *const dev, const uint8_t *input, uint8_t *output, uint32_t length,
224                             uint8_t nc[16]);
225 
226 /**
227  * \brief              AES-GCM block encryption/decryption
228  *
229  * \param[in]  dev     The pointer to device instance
230  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
231  * \param[in]  input   Buffer holding the input data
232  * \param[out] output  Buffer holding the output data
233  * \param[in]  length  Byte length of the input data
234  * \param[in]  iv      Initialization Vector
235  * \param[in]  iv_len  Length of IV
236  * \param[in]  aad     Additional data
237  * \param[in]  aad_len Length of additional data
238  * \param[out] tag     Buffer holding the input data
239  * \param[out] tag_len Length of tag
240  *
241  * \return             ERR_NONE if successful
242  */
243 int32_t _aes_sync_gcm_crypt_and_tag(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
244                                     uint8_t *output, uint32_t length, const uint8_t *iv, uint32_t iv_len,
245                                     const uint8_t *aad, uint32_t aad_len, uint8_t *tag, uint32_t tag_len);
246 
247 /**
248  * \brief              AES-GCM block start
249  *
250  * \param[in]  dev     The pointer to device instance
251  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
252  * \param[in]  iv      Initialization Vector
253  * \param[in]  iv_len  Length of the IV
254  * \param[in]  aad     Additional data
255  * \param[in]  aad_len Length of additional data
256  *
257  * \return             ERR_NONE if successful
258  */
259 int32_t _aes_sync_gcm_start(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *iv,
260                             uint32_t iv_len, const uint8_t *aad, uint32_t aad_len);
261 
262 /**
263  * \brief              AES-GCM block update
264  *
265  * \param[in]  dev     The pointer to device instance
266  * \param[in]  input   Buffer holding the input data
267  * \param[out] output  Buffer holding the output data
268  * \param[in]  length  Byte length of the input data
269  *
270  * \return             ERR_NONE if successful
271  */
272 int32_t _aes_sync_gcm_update(struct _aes_sync_device *const dev, const uint8_t *input, uint8_t *output,
273                              uint32_t length);
274 
275 /**
276  * \brief              AES-GCM block finish
277  *
278  * \param[in]  dev     The pointer to device instance
279  * \param[out] tag     Buffer holding the input data
280  * \param[out] tag_len Length of tag
281  *
282  * \return             ERR_NONE if successful
283  */
284 int32_t _aes_sync_gcm_finish(struct _aes_sync_device *const dev, uint8_t *tag, uint32_t tag_len);
285 
286 /**
287  * \brief              AES-CCM block encryption/decryption
288  *
289  * \param[in]  dev     The pointer to device instance
290  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
291  * \param[in]  input   Buffer holding the input data
292  * \param[out] output  Buffer holding the output data
293  * \param[in]  length  Byte length of the input data
294  * \param[in]  iv      Initialization Vector
295  * \param[in]  iv_len  Length of IV
296  * \param[in]  aad     Additional data
297  * \param[in]  aad_len Length of additional data
298  * \param[in]  tag     Buffer holding the input data
299  * \param[in]  tag_len Length of tag
300  *
301  * \return             ERR_NONE if successful
302  */
303 int32_t _aes_sync_ccm_crypt_and_tag(struct _aes_sync_device *const dev, const enum aes_action enc, const uint8_t *input,
304                                     uint8_t *output, uint32_t length, const uint8_t *iv, uint32_t iv_len,
305                                     const uint8_t *aad, uint32_t aad_len, uint8_t *tag, uint32_t tag_len);
306 
307 /**
308  * \brief Retrieve the current driver version
309  *
310  * \return Current driver version.
311  */
312 uint32_t _aes_sync_get_version(void);
313 
314 #ifdef __cplusplus
315 }
316 #endif
317 
318 #endif /* HPL_AES_SYNC_H_INCLUDED */
319