ChibiOS/HAL  6.1.0
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_TRNG TRUE
51 /** @{ */
52 
53 /*===========================================================================*/
54 /* Driver pre-compile time settings. */
55 /*===========================================================================*/
56 
57 /**
58  * @name PLATFORM configuration options
59  * @{
60  */
61 /**
62  * @brief CRY1 driver enable switch.
63  * @details If set to @p TRUE the support for CRY1 is included.
64  * @note The default is @p FALSE.
65  */
66 #if !defined(PLATFORM_CRY_USE_CRY1) || defined(__DOXYGEN__)
67 #define PLATFORM_CRY_USE_CRY1 FALSE
68 #endif
69 /** @} */
70 
71 /*===========================================================================*/
72 /* Derived constants and error checks. */
73 /*===========================================================================*/
74 
75 /*===========================================================================*/
76 /* Driver data structures and types. */
77 /*===========================================================================*/
78 
79 /**
80  * @brief CRY key identifier type.
81  */
82 typedef uint32_t crykey_t;
83 
84 /**
85  * @brief Type of a structure representing an CRY driver.
86  */
87 typedef struct CRYDriver CRYDriver;
88 
89 /**
90  * @brief Driver configuration structure.
91  * @note It could be empty on some architectures.
92  */
93 typedef struct {
94  uint32_t dummy;
95 } CRYConfig;
96 
97 /**
98  * @brief Structure representing an CRY driver.
99  */
100 struct CRYDriver {
101  /**
102  * @brief Driver state.
103  */
105  /**
106  * @brief Current configuration data.
107  */
109  /**
110  * @brief Algorithm type of transient key.
111  */
113  /**
114  * @brief Size of transient key.
115  */
116  size_t key0_size;
117 #if (HAL_CRY_USE_FALLBACK == TRUE) || defined(__DOXYGEN__)
118  /**
119  * @brief Key buffer for the fall-back implementation.
120  */
122 #endif
123 #if defined(CRY_DRIVER_EXT_FIELDS)
124  CRY_DRIVER_EXT_FIELDS
125 #endif
126  /* End of the mandatory fields.*/
127 };
128 
129 #if (CRY_LLD_SUPPORTS_SHA1 == TRUE) || defined(__DOXYGEN__)
130 /**
131  * @brief Type of a SHA1 context.
132  */
133 typedef struct {
134  uint32_t dummy;
135 } SHA1Context;
136 #endif
137 
138 #if (CRY_LLD_SUPPORTS_SHA256 == TRUE) || defined(__DOXYGEN__)
139 /**
140  * @brief Type of a SHA256 context.
141  */
142 typedef struct {
143  uint32_t dummy;
144 } SHA256Context;
145 #endif
146 
147 #if (CRY_LLD_SUPPORTS_SHA512 == TRUE) || defined(__DOXYGEN__)
148 /**
149  * @brief Type of a SHA512 context.
150  */
151 typedef struct {
152  uint32_t dummy;
153 } SHA512Context;
154 #endif
155 
156 /*===========================================================================*/
157 /* Driver macros. */
158 /*===========================================================================*/
159 
160 /*===========================================================================*/
161 /* External declarations. */
162 /*===========================================================================*/
163 
164 #if (PLATFORM_CRY_USE_CRY1 == TRUE) && !defined(__DOXYGEN__)
165 extern CRYDriver CRYD1;
166 #endif
167 
168 #ifdef __cplusplus
169 extern "C" {
170 #endif
171  void cry_lld_init(void);
172  void cry_lld_start(CRYDriver *cryp);
173  void cry_lld_stop(CRYDriver *cryp);
175  cryalgorithm_t algorithm,
176  size_t size,
177  const uint8_t *keyp);
179  crykey_t key_id,
180  const uint8_t *in,
181  uint8_t *out);
183  crykey_t key_id,
184  const uint8_t *in,
185  uint8_t *out);
187  crykey_t key_id,
188  size_t size,
189  const uint8_t *in,
190  uint8_t *out);
192  crykey_t key_id,
193  size_t size,
194  const uint8_t *in,
195  uint8_t *out);
197  crykey_t key_id,
198  size_t size,
199  const uint8_t *in,
200  uint8_t *out,
201  const uint8_t *iv);
203  crykey_t key_id,
204  size_t size,
205  const uint8_t *in,
206  uint8_t *out,
207  const uint8_t *iv);
209  crykey_t key_id,
210  size_t size,
211  const uint8_t *in,
212  uint8_t *out,
213  const uint8_t *iv);
215  crykey_t key_id,
216  size_t size,
217  const uint8_t *in,
218  uint8_t *out,
219  const uint8_t *iv);
221  crykey_t key_id,
222  size_t size,
223  const uint8_t *in,
224  uint8_t *out,
225  const uint8_t *iv);
227  crykey_t key_id,
228  size_t size,
229  const uint8_t *in,
230  uint8_t *out,
231  const uint8_t *iv);
233  crykey_t key_id,
234  size_t size,
235  const uint8_t *in,
236  uint8_t *out,
237  const uint8_t *iv,
238  size_t aadsize,
239  const uint8_t *aad,
240  uint8_t *authtag);
242  crykey_t key_id,
243  size_t size,
244  const uint8_t *in,
245  uint8_t *out,
246  const uint8_t *iv,
247  size_t aadsize,
248  const uint8_t *aad,
249  uint8_t *authtag);
251  crykey_t key_id,
252  const uint8_t *in,
253  uint8_t *out);
255  crykey_t key_id,
256  const uint8_t *in,
257  uint8_t *out);
259  crykey_t key_id,
260  size_t size,
261  const uint8_t *in,
262  uint8_t *out);
264  crykey_t key_id,
265  size_t size,
266  const uint8_t *in,
267  uint8_t *out);
269  crykey_t key_id,
270  size_t size,
271  const uint8_t *in,
272  uint8_t *out,
273  const uint8_t *iv);
275  crykey_t key_id,
276  size_t size,
277  const uint8_t *in,
278  uint8_t *out,
279  const uint8_t *iv);
282  size_t size, const uint8_t *in);
284  uint8_t *out);
287  size_t size, const uint8_t *in);
289  uint8_t *out);
292  size_t size, const uint8_t *in);
294  uint8_t *out);
295  cryerror_t cry_lld_TRNG(CRYDriver *cryp, uint8_t *out);
296 #ifdef __cplusplus
297 }
298 #endif
299 
300 #endif /* HAL_USE_CRY == TRUE */
301 
302 #endif /* HAL_CRYPTO_LLD_H */
303 
304 /** @} */
cryerror_t cry_lld_SHA1_init(CRYDriver *cryp, SHA1Context *sha1ctxp)
Hash initialization using SHA1.
cryerror_t cry_lld_decrypt_AES_GCM(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv, size_t aadsize, const uint8_t *aad, uint8_t *authtag)
Decryption 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:190
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_TRNG(CRYDriver *cryp, uint8_t *out)
True random numbers generator.
Structure representing an CRY driver.
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.
#define HAL_CRY_MAX_KEY_SIZE
Maximum size of a key for all supported algorithms.
Definition: hal_crypto.h:37
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:94
cryerror_t cry_lld_encrypt_AES_GCM(CRYDriver *cryp, crykey_t key_id, size_t size, const uint8_t *in, uint8_t *out, const uint8_t *iv, size_t aadsize, const uint8_t *aad, uint8_t *authtag)
Encryption operation using AES-GCM.
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.
Driver configuration structure.
cryalgorithm_t key0_type
Algorithm type of transient key.
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.
cryalgorithm_t
Type of an algorithm identifier.
Definition: hal_crypto.h:106
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.
size_t key0_size
Size of transient key.
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:85
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:174
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_loadkey(CRYDriver *cryp, cryalgorithm_t algorithm, size_t size, const uint8_t *keyp)
Initializes the transient key for a specific algorithm.
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:182
cryerror_t cry_lld_SHA256_final(CRYDriver *cryp, SHA256Context *sha256ctxp, uint8_t *out)
Hash finalization using SHA256.
const CRYConfig * config
Current configuration data.
uint8_t key0_buffer[HAL_CRY_MAX_KEY_SIZE]
Key buffer for the fall-back implementation.
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.