ChibiOS/HAL  7.0.3
hal_crypto_lld.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_crypto_lld.h
19  * @brief PLATFORM cryptographic subsystem low level driver header.
20  *
21  * @addtogroup CRYPTO
22  * @{
23  */
24 
25 #ifndef HAL_CRYPTO_LLD_H
26 #define HAL_CRYPTO_LLD_H
27 
28 #if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__)
29 
30 /*===========================================================================*/
31 /* Driver constants. */
32 /*===========================================================================*/
33 
34 /**
35  * @name Driver capability switches
36  * @{
37  */
38 #define CRY_LLD_SUPPORTS_AES TRUE
39 #define CRY_LLD_SUPPORTS_AES_ECB TRUE
40 #define CRY_LLD_SUPPORTS_AES_CBC TRUE
41 #define CRY_LLD_SUPPORTS_AES_CFB TRUE
42 #define CRY_LLD_SUPPORTS_AES_CTR TRUE
43 #define CRY_LLD_SUPPORTS_AES_GCM TRUE
44 #define CRY_LLD_SUPPORTS_DES TRUE
45 #define CRY_LLD_SUPPORTS_DES_ECB TRUE
46 #define CRY_LLD_SUPPORTS_DES_CBC TRUE
47 #define CRY_LLD_SUPPORTS_SHA1 TRUE
48 #define CRY_LLD_SUPPORTS_SHA256 TRUE
49 #define CRY_LLD_SUPPORTS_SHA512 TRUE
50 #define CRY_LLD_SUPPORTS_HMAC_SHA256 TRUE
51 #define CRY_LLD_SUPPORTS_HMAC_SHA512 TRUE
52 /** @} */
53 
54 /*===========================================================================*/
55 /* Driver pre-compile time settings. */
56 /*===========================================================================*/
57 
58 /**
59  * @name PLATFORM configuration options
60  * @{
61  */
62 /**
63  * @brief CRY1 driver enable switch.
64  * @details If set to @p TRUE the support for CRY1 is included.
65  * @note The default is @p FALSE.
66  */
67 #if !defined(PLATFORM_CRY_USE_CRY1) || defined(__DOXYGEN__)
68 #define PLATFORM_CRY_USE_CRY1 FALSE
69 #endif
70 /** @} */
71 
72 /*===========================================================================*/
73 /* Derived constants and error checks. */
74 /*===========================================================================*/
75 
76 /*===========================================================================*/
77 /* Driver data structures and types. */
78 /*===========================================================================*/
79 
80 /**
81  * @brief CRY key identifier type.
82  */
83 typedef uint32_t crykey_t;
84 
85 /**
86  * @brief Type of a structure representing an CRY driver.
87  */
88 typedef struct CRYDriver CRYDriver;
89 
90 /**
91  * @brief Driver configuration structure.
92  * @note It could be empty on some architectures.
93  */
94 typedef struct {
95  uint32_t dummy;
96 } CRYConfig;
97 
98 /**
99  * @brief Structure representing an CRY driver.
100  */
101 struct CRYDriver {
102  /**
103  * @brief Driver state.
104  */
106  /**
107  * @brief Current configuration data.
108  */
110 #if defined(CRY_DRIVER_EXT_FIELDS)
111  CRY_DRIVER_EXT_FIELDS
112 #endif
113  /* End of the mandatory fields.*/
114 };
115 
116 #if (CRY_LLD_SUPPORTS_SHA1 == TRUE) || defined(__DOXYGEN__)
117 /**
118  * @brief Type of a SHA1 context.
119  */
120 typedef struct {
121  uint32_t dummy;
122 } SHA1Context;
123 #endif
124 
125 #if (CRY_LLD_SUPPORTS_SHA256 == TRUE) || defined(__DOXYGEN__)
126 /**
127  * @brief Type of a SHA256 context.
128  */
129 typedef struct {
130  uint32_t dummy;
131 } SHA256Context;
132 #endif
133 
134 #if (CRY_LLD_SUPPORTS_SHA512 == TRUE) || defined(__DOXYGEN__)
135 /**
136  * @brief Type of a SHA512 context.
137  */
138 typedef struct {
139  uint32_t dummy;
140 } SHA512Context;
141 #endif
142 
143 #if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || defined(__DOXYGEN__)
144 /**
145  * @brief Type of a HMAC_SHA256 context.
146  */
147 typedef struct {
148  uint32_t dummy;
150 #endif
151 
152 #if (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE) || defined(__DOXYGEN__)
153 /**
154  * @brief Type of a HMAC_SHA512 context.
155  */
156 typedef struct {
157  uint32_t dummy;
159 #endif
160 
161 /*===========================================================================*/
162 /* Driver macros. */
163 /*===========================================================================*/
164 
165 /*===========================================================================*/
166 /* External declarations. */
167 /*===========================================================================*/
168 
169 #if (PLATFORM_CRY_USE_CRY1 == TRUE) && !defined(__DOXYGEN__)
170 extern CRYDriver CRYD1;
171 #endif
172 
173 #ifdef __cplusplus
174 extern "C" {
175 #endif
176  void cry_lld_init(void);
177  void cry_lld_start(CRYDriver *cryp);
178  void cry_lld_stop(CRYDriver *cryp);
179 #if (CRY_LLD_SUPPORTS_AES == TRUE) || \
180  (CRY_LLD_SUPPORTS_AES_ECB == TRUE) || \
181  (CRY_LLD_SUPPORTS_AES_CBC == TRUE) || \
182  (CRY_LLD_SUPPORTS_AES_CFB == TRUE) || \
183  (CRY_LLD_SUPPORTS_AES_CTR == TRUE) || \
184  (CRY_LLD_SUPPORTS_AES_GCM == TRUE) || \
185  defined(__DOXYGEN__)
187  size_t size,
188  const uint8_t *keyp);
189 #endif
190 #if (CRY_LLD_SUPPORTS_AES == TRUE) || defined(__DOXYGEN__)
192  crykey_t key_id,
193  const uint8_t *in,
194  uint8_t *out);
196  crykey_t key_id,
197  const uint8_t *in,
198  uint8_t *out);
199 #endif
200 #if (CRY_LLD_SUPPORTS_AES_ECB == TRUE) || defined(__DOXYGEN__)
202  crykey_t key_id,
203  size_t size,
204  const uint8_t *in,
205  uint8_t *out);
207  crykey_t key_id,
208  size_t size,
209  const uint8_t *in,
210  uint8_t *out);
211 #endif
212 #if (CRY_LLD_SUPPORTS_AES_CBC == TRUE) || defined(__DOXYGEN__)
214  crykey_t key_id,
215  size_t size,
216  const uint8_t *in,
217  uint8_t *out,
218  const uint8_t *iv);
220  crykey_t key_id,
221  size_t size,
222  const uint8_t *in,
223  uint8_t *out,
224  const uint8_t *iv);
225 #endif
226 #if (CRY_LLD_SUPPORTS_AES_CFB == TRUE) || defined(__DOXYGEN__)
228  crykey_t key_id,
229  size_t size,
230  const uint8_t *in,
231  uint8_t *out,
232  const uint8_t *iv);
234  crykey_t key_id,
235  size_t size,
236  const uint8_t *in,
237  uint8_t *out,
238  const uint8_t *iv);
239 #endif
240 #if (CRY_LLD_SUPPORTS_AES_CTR == TRUE) || defined(__DOXYGEN__)
242  crykey_t key_id,
243  size_t size,
244  const uint8_t *in,
245  uint8_t *out,
246  const uint8_t *iv);
248  crykey_t key_id,
249  size_t size,
250  const uint8_t *in,
251  uint8_t *out,
252  const uint8_t *iv);
253 #endif
254 #if (CRY_LLD_SUPPORTS_AES_GCM == TRUE) || defined(__DOXYGEN__)
256  crykey_t key_id,
257  size_t auth_size,
258  const uint8_t *auth_in,
259  size_t text_size,
260  const uint8_t *text_in,
261  uint8_t *text_out,
262  const uint8_t *iv,
263  size_t tag_size,
264  uint8_t *tag_out);
266  crykey_t key_id,
267  size_t auth_size,
268  const uint8_t *auth_in,
269  size_t text_size,
270  const uint8_t *text_in,
271  uint8_t *text_out,
272  const uint8_t *iv,
273  size_t tag_size,
274  const uint8_t *tag_in);
275 #endif
276 #if (CRY_LLD_SUPPORTS_DES == TRUE) || \
277  (CRY_LLD_SUPPORTS_DES_ECB == TRUE) || \
278  (CRY_LLD_SUPPORTS_DES_CBC == TRUE) || \
279  defined(__DOXYGEN__)
281  size_t size,
282  const uint8_t *keyp);
283 #endif
284 #if (CRY_LLD_SUPPORTS_DES == TRUE) || defined(__DOXYGEN__)
286  crykey_t key_id,
287  const uint8_t *in,
288  uint8_t *out);
290  crykey_t key_id,
291  const uint8_t *in,
292  uint8_t *out);
293 #endif
294 #if (CRY_LLD_SUPPORTS_DES_ECB == TRUE) || defined(__DOXYGEN__)
296  crykey_t key_id,
297  size_t size,
298  const uint8_t *in,
299  uint8_t *out);
301  crykey_t key_id,
302  size_t size,
303  const uint8_t *in,
304  uint8_t *out);
305 #endif
306 #if (CRY_LLD_SUPPORTS_DES_CBC == TRUE) || defined(__DOXYGEN__)
308  crykey_t key_id,
309  size_t size,
310  const uint8_t *in,
311  uint8_t *out,
312  const uint8_t *iv);
314  crykey_t key_id,
315  size_t size,
316  const uint8_t *in,
317  uint8_t *out,
318  const uint8_t *iv);
319 #endif
320 #if (CRY_LLD_SUPPORTS_SHA1 == TRUE) || defined(__DOXYGEN__)
323  size_t size, const uint8_t *in);
325  uint8_t *out);
326 #endif
327 #if (CRY_LLD_SUPPORTS_SHA256 == TRUE) || defined(__DOXYGEN__)
330  size_t size, const uint8_t *in);
332  uint8_t *out);
333 #endif
334 #if (CRY_LLD_SUPPORTS_SHA512 == TRUE) || defined(__DOXYGEN__)
337  size_t size, const uint8_t *in);
339  uint8_t *out);
340 #endif
341 #if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || \
342  (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE) || \
343  defined(__DOXYGEN__)
345  size_t size,
346  const uint8_t *keyp);
347 #endif
348 #if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || defined(__DOXYGEN__)
350  HMACSHA256Context *hmacsha256ctxp);
352  HMACSHA256Context *hmacsha256ctxp,
353  size_t size, const uint8_t *in);
355  HMACSHA256Context *hmacsha256ctxp,
356  uint8_t *out);
357 #endif
358 #if (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE) || defined(__DOXYGEN__)
360  HMACSHA512Context *hmacsha512ctxp);
362  HMACSHA512Context *hmacsha512ctxp,
363  size_t size, const uint8_t *in);
365  HMACSHA512Context *hmacsha512ctxp,
366  uint8_t *out);
367 #endif
368 #ifdef __cplusplus
369 }
370 #endif
371 
372 #endif /* HAL_USE_CRY == TRUE */
373 
374 #endif /* HAL_CRYPTO_LLD_H */
375 
376 /** @} */
cryerror_t cry_lld_SHA1_init(CRYDriver *cryp, SHA1Context *sha1ctxp)
Hash initialization using SHA1.
cryerror_t cry_lld_encrypt_AES_GCM(CRYDriver *cryp, crykey_t key_id, size_t auth_size, const uint8_t *auth_in, size_t text_size, const uint8_t *text_in, uint8_t *text_out, const uint8_t *iv, size_t tag_size, uint8_t *tag_out)
Encryption operation using AES-GCM.
cryerror_t cry_lld_decrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv)
Decryption operation using AES-CFB.
cryerror_t cry_lld_encrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv)
Encryption operation using AES-CBC.
cryerror_t cry_lld_SHA1_final(CRYDriver *cryp, SHA1Context *sha1ctxp, uint8_t *out)
Hash finalization using SHA1.
cryerror_t cry_lld_SHA1_update(CRYDriver *cryp, SHA1Context *sha1ctxp, size_t size, const uint8_t *in)
Hash update using SHA1.
Type of a SHA512 context.
Definition: hal_crypto.h:187
cryerror_t cry_lld_decrypt_DES_ECB(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out)
Decryption operation using (T)DES-ECB.
cryerror_t cry_lld_HMACSHA256_init(CRYDriver *cryp, HMACSHA256Context *hmacsha256ctxp)
Hash initialization using HMAC_SHA256.
Structure representing an CRY driver.
cryerror_t cry_lld_des_loadkey(CRYDriver *cryp, size_t size, const uint8_t *keyp)
Initializes the DES transient key.
cryerror_t cry_lld_encrypt_DES_CBC(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv)
Encryption operation using (T)DES-CBC.
void cry_lld_stop(CRYDriver *cryp)
Deactivates the crypto peripheral.
cryerror_t cry_lld_encrypt_AES_CFB(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv)
Encryption operation using AES-CFB.
cryerror_t
Driver error codes.
Definition: hal_crypto.h:89
cryerror_t cry_lld_decrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out)
Decryption operation using AES-ECB.
cryerror_t cry_lld_HMACSHA512_update(CRYDriver *cryp, HMACSHA512Context *hmacsha512ctxp, size_t size, const uint8_t *in)
Hash update using HMAC.
Driver configuration structure.
Type of a HMAC_SHA512 context.
Definition: hal_crypto.h:203
Type of a HMAC_SHA256 context.
Definition: hal_crypto.h:195
cryerror_t cry_lld_decrypt_AES_GCM(CRYDriver *cryp, crykey_t key_id, size_t auth_size, const uint8_t *auth_in, size_t text_size, const uint8_t *text_in, uint8_t *text_out, const uint8_t *iv, size_t tag_size, const uint8_t *tag_in)
Decryption operation using AES-GCM.
void cry_lld_start(CRYDriver *cryp)
Configures and activates the crypto peripheral.
uint32_t crykey_t
CRY key identifier type.
cryerror_t cry_lld_decrypt_AES(CRYDriver *cryp, crykey_t key_id, const uint8_t *in, uint8_t *out)
Decryption of a single block using AES.
cryerror_t cry_lld_SHA512_init(CRYDriver *cryp, SHA512Context *sha512ctxp)
Hash initialization using SHA512.
void cry_lld_init(void)
Low level crypto driver initialization.
cryerror_t cry_lld_SHA512_final(CRYDriver *cryp, SHA512Context *sha512ctxp, uint8_t *out)
Hash finalization using SHA512.
cryerror_t cry_lld_decrypt_AES_CTR(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv)
Decryption operation using AES-CTR.
cryerror_t cry_lld_HMACSHA256_final(CRYDriver *cryp, HMACSHA256Context *hmacsha256ctxp, uint8_t *out)
Hash finalization using HMAC.
cryerror_t cry_lld_HMACSHA256_update(CRYDriver *cryp, HMACSHA256Context *hmacsha256ctxp, size_t size, const uint8_t *in)
Hash update using HMAC.
cryerror_t cry_lld_hmac_loadkey(CRYDriver *cryp, size_t size, const uint8_t *keyp)
Initializes the HMAC transient key.
cryerror_t cry_lld_encrypt_AES_ECB(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out)
Encryption operation using AES-ECB.
CRYDriver CRYD1
CRY1 driver identifier.
cryerror_t cry_lld_decrypt_DES_CBC(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv)
Decryption operation using (T)DES-CBC.
cryerror_t cry_lld_encrypt_DES_ECB(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out)
Encryption operation using (T)DES-ECB.
cryerror_t cry_lld_decrypt_DES(CRYDriver *cryp, crykey_t key_id, const uint8_t *in, uint8_t *out)
Decryption of a single block using (T)DES.
cryerror_t cry_lld_HMACSHA512_final(CRYDriver *cryp, HMACSHA512Context *hmacsha512ctxp, uint8_t *out)
Hash finalization using HMAC.
cryerror_t cry_lld_encrypt_AES(CRYDriver *cryp, crykey_t key_id, const uint8_t *in, uint8_t *out)
Encryption of a single block using AES.
crystate_t
Driver state machine possible states.
Definition: hal_crypto.h:80
cryerror_t cry_lld_SHA512_update(CRYDriver *cryp, SHA512Context *sha512ctxp, size_t size, const uint8_t *in)
Hash update using SHA512.
USBInEndpointState in
IN EP0 state.
Definition: hal_usb_lld.c:57
cryerror_t cry_lld_SHA256_init(CRYDriver *cryp, SHA256Context *sha256ctxp)
Hash initialization using SHA256.
cryerror_t cry_lld_decrypt_AES_CBC(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv)
Decryption operation using AES-CBC.
crystate_t state
Driver state.
Type of a SHA1 context.
Definition: hal_crypto.h:171
USBOutEndpointState out
OUT EP0 state.
Definition: hal_usb_lld.c:61
cryerror_t cry_lld_encrypt_DES(CRYDriver *cryp, crykey_t key_id, const uint8_t *in, uint8_t *out)
Encryption of a single block using (T)DES.
cryerror_t cry_lld_HMACSHA512_init(CRYDriver *cryp, HMACSHA512Context *hmacsha512ctxp)
Hash initialization using HMAC_SHA512.
cryerror_t cry_lld_SHA256_update(CRYDriver *cryp, SHA256Context *sha256ctxp, size_t size, const uint8_t *in)
Hash update using SHA256.
Type of a SHA256 context.
Definition: hal_crypto.h:179
cryerror_t cry_lld_SHA256_final(CRYDriver *cryp, SHA256Context *sha256ctxp, uint8_t *out)
Hash finalization using SHA256.
const CRYConfig * config
Current configuration data.
cryerror_t cry_lld_encrypt_AES_CTR(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv)
Encryption operation using AES-CTR.
cryerror_t cry_lld_aes_loadkey(CRYDriver *cryp, size_t size, const uint8_t *keyp)
Initializes the AES transient key.