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 HAL_AES_SYNC_H_INCLUDED
35 #define HAL_AES_SYNC_H_INCLUDED
36 
37 #include <hpl_aes_sync.h>
38 #include <utils_assert.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * \addtogroup doc_driver_hal_crypto_aes_sync
46  *
47  * @{
48  */
49 
50 struct aes_sync_descriptor {
51 	struct _aes_sync_device dev; /*!< AES HPL device descriptor */
52 };
53 
54 /**
55  * \brief              Initialize AES Descriptor
56  *
57  * \param[in] desc     The AES descriptor to be initialized
58  * \param[in] hw       The pointer to hardware instance
59  */
60 int32_t aes_sync_init(struct aes_sync_descriptor *descr, void *const hw);
61 
62 /**
63  * \brief              Deinitialize AES Descriptor
64  *
65  * \param[in] desc     The AES descriptor to be deinitialized
66  */
67 int32_t aes_sync_deinit(struct aes_sync_descriptor *desc);
68 
69 /**
70  * \brief              Enable AES
71  *
72  * \param[in] desc     The AES descriptor
73  */
74 int32_t aes_sync_enable(struct aes_sync_descriptor *desc);
75 
76 /**
77  * \brief              Disable AES
78  *
79  * \param[in] desc     The AES descriptor
80  */
81 int32_t aes_sync_disable(struct aes_sync_descriptor *desc);
82 
83 /**
84  * \brief              Set AES Key (encryption).
85  *
86  * \param[in] desc     The AES descriptor
87  * \param[in] key      Encryption key
88  * \param[in] size     Bit length of key
89  */
90 int32_t aes_sync_set_encrypt_key(struct aes_sync_descriptor *descr, const uint8_t *key, const enum aes_keysize size);
91 
92 /**
93  * \brief              Set AES Key (decryption).
94  *
95  * \param[in] desc     The AES descriptor
96  * \param[in] key      Decryption key
97  * \param[in] size     Bit length of key
98  */
99 int32_t aes_sync_set_decrypt_key(struct aes_sync_descriptor *descr, const uint8_t *key, const enum aes_keysize size);
100 
101 /**
102  * \brief              AES-ECB block encryption/decryption
103  *
104  * \param[in]  descr   The AES descriptor
105  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
106  * \param[in]  input   16-byte input data
107  * \param[out] output  16-byte output data
108  *
109  * \return             ERR_NONE if successful
110  */
111 int32_t aes_sync_ecb_crypt(struct aes_sync_descriptor *descr, const enum aes_action enc, const uint8_t *input,
112                            uint8_t *output);
113 
114 /**
115  * \brief              The AES-CBC block encryption/decryption
116  *                     length should be a multiple of 16 bytes
117  *
118  * \param[in]  descr   The AES descriptor
119  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
120  * \param[in]  input   16-byte input data
121  * \param[out] output  16-byte output data
122  * \param[in]  length  Byte length of the input data
123  * \param[in, out] iv  Initialization vector (updated after use)
124  *
125  * \return             ERR_NONE if successful
126  */
127 int32_t aes_sync_cbc_crypt(struct aes_sync_descriptor *descr, const enum aes_action enc, const uint8_t *input,
128                            uint8_t *output, uint32_t length, uint8_t iv[16]);
129 
130 /**
131  * \brief              AES-CFB128 block encryption/decryption
132  *
133  * \param[in]  descr   The AES descriptor
134  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
135  * \param[in]  input   Buffer holding the input data
136  * \param[out] output  Buffer holding the output data
137  * \param[out] length  Byte length of the input data
138  * \param[in, out] iv  Initialization Vector (updated after use)
139  * \param[in, out] iv_ofst  Offset in IV (updated after use)
140 
141  * \return             ERR_NONE if successful
142  */
143 int32_t aes_sync_cfb128_crypt(struct aes_sync_descriptor *descr, const enum aes_action enc, const uint8_t *input,
144                               uint8_t *output, uint32_t length, uint8_t *iv, uint32_t *iv_ofst);
145 
146 /**
147  * \brief              AES-CFB64 block encryption/decryption
148  *
149  * \param[in]  descr   The AES descriptor
150  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
151  * \param[in]  input   Buffer holding the input data
152  * \param[out] output  Buffer holding the output data
153  * \param[out] length  Byte length of the input data
154  * \param[in, out] iv      Initialization Vector (updated after use)
155  * \param[in, out] iv_ofst  Offset in IV (updated after use)
156  *
157  * \return             ERR_NONE if successful
158  */
159 int32_t aes_sync_cfb64_crypt(struct aes_sync_descriptor *descr, const enum aes_action enc, const uint8_t *input,
160                              uint8_t *output, uint32_t length, uint8_t *iv, uint32_t *iv_ofst);
161 
162 /**
163  * \brief              AES-CFB32 block encryption/decryption
164  *
165  * \param[in]  descr   The AES descriptor
166  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
167  * \param[in]  input   Buffer holding the input data
168  * \param[out] output  Buffer holding the output data
169  * \param[out] length  Byte length of the input data
170  * \param[in, out] iv      Initialization Vector (updated after use)
171  * \param[in, out] iv_ofst  Offset in IV (updated after use)
172  *
173  * \return             ERR_NONE if successful
174  */
175 int32_t aes_sync_cfb32_crypt(struct aes_sync_descriptor *descr, const enum aes_action enc, const uint8_t *input,
176                              uint8_t *output, uint32_t length, uint8_t *iv, uint32_t *iv_ofst);
177 
178 /**
179  * \brief              AES-CFB16 block encryption/decryption
180  *
181  * \param[in]  descr   The AES descriptor
182  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
183  * \param[in]  input   Buffer holding the input data
184  * \param[out] output  Buffer holding the output data
185  * \param[out] length  Byte length of the input data
186  * \param[in, out] iv      Initialization Vector (updated after use)
187  * \param[in, out] iv_ofst  Offset in IV (updated after use)
188  *
189  * \return             ERR_NONE if successful
190  */
191 int32_t aes_sync_cfb16_crypt(struct aes_sync_descriptor *descr, const enum aes_action enc, const uint8_t *input,
192                              uint8_t *output, uint32_t length, uint8_t *iv, uint32_t *iv_ofst);
193 
194 /**
195  * \brief              AES-CFB8 block encryption/decryption
196  *
197  * \param[in]  descr   The AES descriptor
198  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
199  * \param[in]  input   Buffer holding the input data
200  * \param[out] output  Buffer holding the output data
201  * \param[in, out] iv  Initialization Vector (updated after use)
202  *
203  * \return             ERR_NONE if successful
204  */
205 int32_t aes_sync_cfb8_crypt(struct aes_sync_descriptor *descr, const enum aes_action enc, const uint8_t *input,
206                             uint8_t *output, uint32_t length, uint8_t *iv);
207 
208 /**
209  * \brief              AES-OFB block encryption/decryption
210  *
211  * \param[in]  descr   The AES descriptor
212  * \param[in]  input   Buffer holding the input data
213  * \param[out] output  Buffer holding the output data
214  * \param[out] length  Byte length of the input data
215  * \param[in, out] iv      Initialization Vector (updated after use)
216  * \param[in, out] iv_ofst  Offset in IV (updated after use)
217 
218  * \return             ERR_NONE if successful
219  */
220 int32_t aes_sync_ofb_crypt(struct aes_sync_descriptor *descr, const uint8_t *input, uint8_t *output, uint32_t length,
221                            uint8_t *iv, uint32_t *iv_ofst);
222 
223 /**
224  * \brief              AES-CTR block encryption/decryption
225  *
226  * \param[in]  descr   The AES descriptor
227  * \param[in]  input   Buffer holding the input data
228  * \param[out] output  Buffer holding the output data
229  * \param[in]  length  Byte length of the input data
230  * \param[in]  buffer  Stream block for resuming
231  * \param[in]  nc      The 128-bit nonce and counter
232  * \param[in]  nc_ofst  The offset in the current stream_block (for resuming
233  *                     within current cipher stream). The offset pointer
234  *                     should be 0 at the start of a stream.
235  *
236  * \return             ERR_NONE if successful
237  */
238 int32_t aes_sync_ctr_crypt(struct aes_sync_descriptor *descr, const uint8_t *input, uint8_t *output, uint32_t length,
239                            uint8_t buffer[16], uint8_t nc[16], uint32_t *nc_ofst);
240 
241 /**
242  * \brief              AES-GCM block encryption/decryption
243  *
244  * \param[in]  descr   The AES descriptor
245  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
246  * \param[in]  input   Buffer holding the input data
247  * \param[out] output  Buffer holding the output data
248  * \param[in]  length  Byte length of the input data
249  * \param[in]  iv      Initialization Vector
250  * \param[in]  iv_len  Length of IV
251  * \param[in]  aad     Additional data
252  * \param[in]  aad_len Length of additional data
253  * \param[out] tag     Buffer holding the input data
254  * \param[in]  tag_len Length of tag
255  *
256  * \return             ERR_NONE if successful
257  */
258 int32_t aes_sync_gcm_crypt_and_tag(struct aes_sync_descriptor *const descr, const enum aes_action enc,
259                                    const uint8_t *input, uint8_t *output, uint32_t length, const uint8_t *iv,
260                                    uint32_t iv_len, const uint8_t *aad, uint32_t aad_len, uint8_t *tag,
261                                    uint32_t tag_len);
262 /**
263  * \brief              AES-GCM block encryption
264  *
265  * \param[in] desc     The AES descriptor
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  * \param[in] iv       Initialization Vector
270  * \param[in] iv_len   Length of IV
271  * \param[in] aad      Additional data
272  * \param[in] aad_len  Length of additional data
273  * \param[in] tag      Buffer holding the input data
274  * \param[in] tag_len  Length of tag
275  *
276  * \return             ERR_NONE if successful
277  */
278 int32_t aes_sync_gcm_auth_decrypt(struct aes_sync_descriptor *const descr, const uint8_t *input, uint8_t *output,
279                                   uint32_t length, const uint8_t *iv, uint32_t iv_len, const uint8_t *aad,
280                                   uint32_t aad_len, const uint8_t *tag, uint32_t tag_len);
281 
282 /**
283  * \brief              AES-GCM block start
284  *
285  * \param[in]  descr   The AES descriptor
286  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
287  * \param[in]  iv      Initialization Vector
288  * \param[in]  iv_len  Length of the IV
289  * \param[in]  aad     Additional data
290  * \param[in]  aad_len Length of additional data
291  *
292  * \return             ERR_NONE if successful
293  */
294 int32_t aes_sync_gcm_start(struct aes_sync_descriptor *const descr, const enum aes_action enc, const uint8_t *iv,
295                            uint32_t iv_len, const uint8_t *aad, uint32_t aad_len);
296 
297 /**
298  * \brief              AES-GCM block update
299  *
300  * \param[in]  descr   The AES descriptor
301  * \param[in]  input   Buffer holding the input data
302  * \param[out] output  Buffer holding the output data
303  * \param[in]  length  Byte length of the input data
304  *
305  * \return             ERR_NONE if successful
306  */
307 int32_t aes_sync_gcm_update(struct aes_sync_descriptor *const descr, const uint8_t *input, uint8_t *output,
308                             uint32_t length);
309 
310 /**
311  * \brief              AES-GCM block finish
312  *
313  * \param[in]  descr   The AES descriptor
314  * \param[out] tag     Buffer holding the input data
315  * \param[in]  tag_len Length of tag
316  *
317  * \return             ERR_NONE if successful
318  */
319 int32_t aes_sync_gcm_finish(struct aes_sync_descriptor *const descr, uint8_t *tag, uint32_t tag_len);
320 
321 /**
322  * \brief              AES-CCM block encryption/decryption
323  *
324  * \param[in]  descr   The AES descriptor
325  * \param[in]  enc     AES_SYNC_ENCRYPT or AES_SYNC_DECRYPT
326  * \param[in]  input   Buffer holding the input data
327  * \param[out] output  Buffer holding the output data
328  * \param[in]  length  Byte length of the input data
329  * \param[in]  iv      Initialization Vector
330  * \param[in]  iv_len  Length of IV
331  * \param[in]  aad     Additional data
332  * \param[in]  aad_len Length of additional data
333  * \param[in]  tag     Buffer holding the input data
334  * \param[in]  tag_len Length of tag
335  *
336  * \return             ERR_NONE if successful
337  */
338 int32_t aes_sync_ccm_crypt_and_tag(struct aes_sync_descriptor *const descr, const enum aes_action enc,
339                                    const uint8_t *input, uint8_t *output, uint32_t length, const uint8_t *iv,
340                                    uint32_t iv_len, const uint8_t *aad, uint32_t aad_len, uint8_t *tag,
341                                    uint32_t tag_len);
342 
343 /**
344  * \brief              AES-CCM block authenticated decryption
345  *
346  * \param[in]  descr   The AES descriptor
347  * \param[in]  input   Buffer holding the input data
348  * \param[out] output  Buffer holding the output data
349  * \param[in]  length  Byte length of the input data
350  * \param[in]  iv      Initialization Vector
351  * \param[in]  iv_len  Length of IV
352  * \param[in]  aad     Additional data
353  * \param[in]  aad_len Length of additional data
354  * \param[in]  tag     Buffer holding the input data
355  * \param[in]  tag_len Length of tag
356  *
357  * \return             ERR_NONE if successful
358  */
359 int32_t aes_sync_ccm_auth_decrypt(struct aes_sync_descriptor *const descr, const uint8_t *input, uint8_t *output,
360                                   uint32_t length, const uint8_t *iv, uint32_t iv_len, const uint8_t *aad,
361                                   uint32_t aad_len, const uint8_t *tag, uint32_t tag_len);
362 
363 /**
364  * \brief Retrieve the current driver version
365  *
366  * \return Current driver version.
367  */
368 uint32_t aes_sync_get_version(void);
369 /**@}*/
370 
371 #ifdef __cplusplus
372 }
373 #endif
374 
375 #endif /* HAL_AES_SYNC_H_INCLUDED */
376