caffeine-hal 0.6.6
A Header-Defined Interface c library, it provides the hal layer for the Caffeine framework
Loading...
Searching...
No Matches
cfn_hal_crypto.h
Go to the documentation of this file.
1
26#ifndef CAFFEINE_HAL_HAL_CRYPTO_H
27#define CAFFEINE_HAL_HAL_CRYPTO_H
28
29#ifdef __cplusplus
30extern "C"
31{
32#endif
33
34/* Includes ---------------------------------------------------------*/
35#include "cfn_hal.h"
36#include "cfn_hal_base.h"
37#include "cfn_hal_types.h"
38
39/* Defines ----------------------------------------------------------*/
40
41/* Types Enums ------------------------------------------------------*/
42
51
61
75
76/* Types Structs ----------------------------------------------------*/
77
86
90typedef struct
91{
92 void *instance;
93 void *user_arg;
95
96typedef struct cfn_hal_crypto_s cfn_hal_crypto_t;
98
107 uint32_t event_mask,
108 uint32_t error_mask,
109 void *user_arg);
110
115{
117
118 /* Crypto Specific Extensions */
120 cfn_hal_crypto_t *driver, const uint8_t *in, uint8_t *out, size_t size, uint32_t timeout);
122 cfn_hal_crypto_t *driver, const uint8_t *in, uint8_t *out, size_t size, uint32_t timeout);
123 cfn_hal_error_code_t (*hash_update)(cfn_hal_crypto_t *driver, const uint8_t *data, size_t size);
124 cfn_hal_error_code_t (*hash_finish)(cfn_hal_crypto_t *driver, uint8_t *hash, uint32_t timeout);
125 cfn_hal_error_code_t (*generate_random)(cfn_hal_crypto_t *driver, uint8_t *buffer, size_t size);
126 cfn_hal_error_code_t (*set_key)(cfn_hal_crypto_t *driver, const uint8_t *key, size_t key_size);
127};
128
130
133/* Functions inline ------------------------------------------------- */
135 uint32_t peripheral_id,
136 struct cfn_hal_clock_s *clock,
137 void *dependency,
138 const cfn_hal_crypto_api_t *api,
139 const cfn_hal_crypto_phy_t *phy,
140 const cfn_hal_crypto_config_t *config,
142 void *user_arg)
143{
145 driver, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, peripheral_id, clock, dependency, api, phy, config, callback, user_arg);
146}
147
155 const cfn_hal_crypto_config_t *config)
156{
157 if (driver == NULL || config == NULL)
158 {
160 }
161
162 if (config->algo >= CFN_HAL_CRYPTO_ALGO_MAX)
163 {
165 }
166
168}
169
176{
177 if (!driver)
178 {
180 }
181 driver->base.vmt = (const struct cfn_hal_api_base_s *) driver->api;
182 cfn_hal_error_code_t error = cfn_hal_crypto_config_validate(driver, driver->config);
183 if (error != CFN_HAL_ERROR_OK)
184 {
185 return error;
186 }
188}
189
203
211 const cfn_hal_crypto_config_t *config)
212{
213 if (!driver)
214 {
216 }
217 {
218 driver->config = config;
219 }
220 return cfn_hal_base_config_set(&driver->base, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, (const void *) config);
221}
222
230{
231 if (!driver || !config || !driver->config)
232 {
234 }
235 *config = *(driver->config);
236 return CFN_HAL_ERROR_OK;
237}
238
247 const cfn_hal_crypto_callback_t callback,
248 void *user_arg)
249{
250 if (!driver)
251 {
253 }
254 {
255 driver->cb = callback;
256 driver->cb_user_arg = user_arg;
257 }
259 &driver->base, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, (cfn_hal_callback_t) callback, user_arg);
260}
261
277
285{
286 if (!driver)
287 {
289 }
290 return cfn_hal_base_event_enable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, event_mask);
291}
292
300{
301 if (!driver)
302 {
304 }
305 return cfn_hal_base_event_disable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, event_mask);
306}
307
315{
316 if (!driver)
317 {
319 }
320 return cfn_hal_base_event_get(&driver->base, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, event_mask);
321}
322
330{
331 if (!driver)
332 {
334 }
335 return cfn_hal_base_error_enable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, error_mask);
336}
337
345{
346 if (!driver)
347 {
349 }
350 return cfn_hal_base_error_disable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, error_mask);
351}
352
360{
361 if (!driver)
362 {
364 }
365 return cfn_hal_base_error_get(&driver->base, CFN_HAL_PERIPHERAL_TYPE_CRYPTO, error_mask);
366}
367
368/* CRYPTO Specific Functions ---------------------------------------- */
369
380cfn_hal_crypto_encrypt(cfn_hal_crypto_t *driver, const uint8_t *in, uint8_t *out, size_t size, uint32_t timeout)
381{
383 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_CRYPTO, encrypt, driver, error, in, out, size, timeout);
384 return error;
385}
386
397cfn_hal_crypto_decrypt(cfn_hal_crypto_t *driver, const uint8_t *in, uint8_t *out, size_t size, uint32_t timeout)
398{
400 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_CRYPTO, decrypt, driver, error, in, out, size, timeout);
401 return error;
402}
403
412 const uint8_t *data,
413 size_t size)
414{
416 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_CRYPTO, hash_update, driver, error, data, size);
417 return error;
418}
419
428 uint8_t *hash,
429 uint32_t timeout)
430{
432 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_CRYPTO, hash_finish, driver, error, hash, timeout);
433 return error;
434}
435
444 uint8_t *buffer,
445 size_t size)
446{
448 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_CRYPTO, generate_random, driver, error, buffer, size);
449 return error;
450}
451
460 const uint8_t *key,
461 size_t key_size)
462{
464 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_CRYPTO, set_key, driver, error, key, key_size);
465 return error;
466}
468 const cfn_hal_crypto_config_t *config,
469 const cfn_hal_crypto_phy_t *phy,
470 struct cfn_hal_clock_s *clock,
471 void *dependency,
473 void *user_arg);
475#ifdef __cplusplus
476}
477#endif
478
479#endif // CAFFEINE_HAL_HAL_CRYPTO_H
Core Hardware Abstraction Layer definitions and macros.
#define CFN_HAL_CHECK_AND_CALL_FUNC_VARG(expected_peripheral_type, func, driver, result,...)
Definition cfn_hal.h:134
#define CFN_HAL_INLINE
Macro for inlining HAL wrapper functions. Can be overridden with attribute((always_inline)) for perfo...
Definition cfn_hal.h:61
#define CFN_HAL_BIT(x)
Definition cfn_hal.h:79
#define CFN_HAL_CREATE_DRIVER_TYPE(prefix, config_type, api_type, phy_type, cb_type)
Definition cfn_hal.h:102
Base driver Hardware Abstraction Layer declarations.
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_error_disable(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t error_mask)
Generic error disable for any driver. Deactivates exception-flow hardware triggers based on the provi...
Definition cfn_hal_base_impl.h:436
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_callback_register(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, cfn_hal_callback_t callback, void *user_arg)
Generic callback registration for any driver.
Definition cfn_hal_base_impl.h:254
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_error_enable(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t error_mask)
Generic error enable for any driver. Activates exception-flow hardware triggers based on the provided...
Definition cfn_hal_base_impl.h:411
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_event_enable(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t event_mask)
Generic event enable for any driver. Activates nominal hardware triggers based on the provided mask.
Definition cfn_hal_base_impl.h:331
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_event_get(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t *event_mask)
Generic event status getter for any driver. Retrieves the current nominal hardware triggers/flags.
Definition cfn_hal_base_impl.h:386
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_error_get(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t *error_mask)
Generic error status getter for any driver. Retrieves current exception-flow hardware flags/errors.
Definition cfn_hal_base_impl.h:461
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_config_set(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, const void *config)
Generic configuration setter for any driver.
Definition cfn_hal_base_impl.h:196
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_power_state_set(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, cfn_hal_power_state_t state)
Generic power state transition for any driver.
Definition cfn_hal_base_impl.h:279
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_deinit(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type)
Generic deinitialization for any driver. Returns the driver to the CONSTRUCTED state and releases boa...
Definition cfn_hal_base_impl.h:144
#define CFN_HAL_VMT_CHECK(api_struct_type)
Compile-time check to ensure a peripheral API struct is compatible with the base layer....
Definition cfn_hal_base.h:91
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_init(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type)
Generic initialization for any driver. Handles board-level hooks, type validation,...
Definition cfn_hal_base_impl.h:85
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_config_validate(const cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, const void *config)
Generic validation for a configuration.
Definition cfn_hal_base_impl.h:234
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_event_disable(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t event_mask)
Generic event disable for any driver. Deactivates nominal hardware triggers based on the provided mas...
Definition cfn_hal_base_impl.h:361
#define CFN_HAL_POPULATE_DRIVER( driver_ptr, periph_type, periph_id, clock_ptr, dep_ptr, api_ptr, phy_ptr, cfg_ptr, cb_func, cb_arg)
Standardizes the population of a peripheral driver structure. This macro ensures that the base driver...
Definition cfn_hal_base_impl.h:45
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_error_get(cfn_hal_crypto_t *driver, uint32_t *error_mask)
Retrieves the current Crypto exception error status.
Definition cfn_hal_crypto.h:359
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_config_validate(const cfn_hal_crypto_t *driver, const cfn_hal_crypto_config_t *config)
Validates the Crypto configuration.
Definition cfn_hal_crypto.h:154
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_event_disable(cfn_hal_crypto_t *driver, uint32_t event_mask)
Disables one or more Crypto nominal events.
Definition cfn_hal_crypto.h:299
cfn_hal_crypto_algo_t
Supported cryptographic algorithms.
Definition cfn_hal_crypto.h:66
@ CFN_HAL_CRYPTO_ALGO_AES_ECB
Definition cfn_hal_crypto.h:67
@ CFN_HAL_CRYPTO_ALGO_AES_GCM
Definition cfn_hal_crypto.h:69
@ CFN_HAL_CRYPTO_ALGO_SHA512
Definition cfn_hal_crypto.h:71
@ CFN_HAL_CRYPTO_ALGO_AES_CBC
Definition cfn_hal_crypto.h:68
@ CFN_HAL_CRYPTO_ALGO_MAX
Definition cfn_hal_crypto.h:73
@ CFN_HAL_CRYPTO_ALGO_SHA256
Definition cfn_hal_crypto.h:70
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_event_enable(cfn_hal_crypto_t *driver, uint32_t event_mask)
Enables one or more Crypto nominal events.
Definition cfn_hal_crypto.h:284
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_config_get(cfn_hal_crypto_t *driver, cfn_hal_crypto_config_t *config)
Gets the current Crypto configuration.
Definition cfn_hal_crypto.h:229
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_deinit(cfn_hal_crypto_t *driver)
Deinitializes the Crypto driver.
Definition cfn_hal_crypto.h:195
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_init(cfn_hal_crypto_t *driver)
Initializes the Crypto driver.
Definition cfn_hal_crypto.h:175
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_set_key(cfn_hal_crypto_t *driver, const uint8_t *key, size_t key_size)
Sets the cryptographic key for symmetric algorithms.
Definition cfn_hal_crypto.h:459
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_power_state_set(cfn_hal_crypto_t *driver, cfn_hal_power_state_t state)
Sets the Crypto power state.
Definition cfn_hal_crypto.h:268
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_event_get(cfn_hal_crypto_t *driver, uint32_t *event_mask)
Retrieves the current Crypto nominal event status.
Definition cfn_hal_crypto.h:314
void(* cfn_hal_crypto_callback_t)(cfn_hal_crypto_t *driver, uint32_t event_mask, uint32_t error_mask, void *user_arg)
Crypto callback signature.
Definition cfn_hal_crypto.h:106
cfn_hal_crypto_event_t
Crypto nominal event flags.
Definition cfn_hal_crypto.h:47
@ CFN_HAL_CRYPTO_EVENT_OP_COMPLETE
Definition cfn_hal_crypto.h:49
@ CFN_HAL_CRYPTO_EVENT_NONE
Definition cfn_hal_crypto.h:48
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_callback_register(cfn_hal_crypto_t *driver, const cfn_hal_crypto_callback_t callback, void *user_arg)
Registers a callback for Crypto events and errors.
Definition cfn_hal_crypto.h:246
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_decrypt(cfn_hal_crypto_t *driver, const uint8_t *in, uint8_t *out, size_t size, uint32_t timeout)
Decrypts a block of data.
Definition cfn_hal_crypto.h:397
cfn_hal_error_code_t cfn_hal_crypto_destruct(cfn_hal_crypto_t *driver)
Definition cfn_hal_crypto_port.c:151
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_error_disable(cfn_hal_crypto_t *driver, uint32_t error_mask)
Disables one or more Crypto exception errors.
Definition cfn_hal_crypto.h:344
cfn_hal_error_code_t cfn_hal_crypto_construct(cfn_hal_crypto_t *driver, const cfn_hal_crypto_config_t *config, const cfn_hal_crypto_phy_t *phy, struct cfn_hal_clock_s *clock, void *dependency, cfn_hal_crypto_callback_t callback, void *user_arg)
Definition cfn_hal_crypto_port.c:135
CFN_HAL_INLINE void cfn_hal_crypto_populate(cfn_hal_crypto_t *driver, uint32_t peripheral_id, struct cfn_hal_clock_s *clock, void *dependency, const cfn_hal_crypto_api_t *api, const cfn_hal_crypto_phy_t *phy, const cfn_hal_crypto_config_t *config, cfn_hal_crypto_callback_t callback, void *user_arg)
Definition cfn_hal_crypto.h:134
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_config_set(cfn_hal_crypto_t *driver, const cfn_hal_crypto_config_t *config)
Sets the Crypto configuration.
Definition cfn_hal_crypto.h:210
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_generate_random(cfn_hal_crypto_t *driver, uint8_t *buffer, size_t size)
Generates cryptographically secure random numbers.
Definition cfn_hal_crypto.h:443
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_error_enable(cfn_hal_crypto_t *driver, uint32_t error_mask)
Enables one or more Crypto exception errors.
Definition cfn_hal_crypto.h:329
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_hash_finish(cfn_hal_crypto_t *driver, uint8_t *hash, uint32_t timeout)
Finalizes the hash calculation and retrieves the digest.
Definition cfn_hal_crypto.h:427
struct cfn_hal_crypto_s cfn_hal_crypto_t
Definition cfn_hal_crypto.h:96
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_hash_update(cfn_hal_crypto_t *driver, const uint8_t *data, size_t size)
Feeds data into the hashing engine.
Definition cfn_hal_crypto.h:411
cfn_hal_crypto_error_t
Crypto exception error flags.
Definition cfn_hal_crypto.h:56
@ CFN_HAL_CRYPTO_ERROR_GENERAL
Definition cfn_hal_crypto.h:59
@ CFN_HAL_CRYPTO_ERROR_NONE
Definition cfn_hal_crypto.h:57
@ CFN_HAL_CRYPTO_ERROR_AUTH
Definition cfn_hal_crypto.h:58
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_crypto_encrypt(cfn_hal_crypto_t *driver, const uint8_t *in, uint8_t *out, size_t size, uint32_t timeout)
Encrypts a block of data.
Definition cfn_hal_crypto.h:380
Common type definitions and enums for the HAL.
void(* cfn_hal_callback_t)(void)
Generic function pointer for HAL callbacks. Used as a standard-compliant carrier in the base layer.
Definition cfn_hal_types.h:98
#define CFN_HAL_PERIPHERAL_TYPE_CRYPTO
Definition cfn_hal_types.h:137
enum cfn_hal_error_codes cfn_hal_error_code_t
cfn_hal_power_state_t
Definition cfn_hal_types.h:156
@ CFN_HAL_ERROR_OK
Definition cfn_hal_types.h:50
@ CFN_HAL_ERROR_BAD_PARAM
Definition cfn_hal_types.h:53
@ CFN_HAL_ERROR_BAD_CONFIG
Definition cfn_hal_types.h:60
Base API structure for all peripheral drivers. Every peripheral-specific API struct MUST have this as...
Definition cfn_hal_base.h:63
Crypto Virtual Method Table (VMT).
Definition cfn_hal_crypto.h:115
cfn_hal_error_code_t(* decrypt)(cfn_hal_crypto_t *driver, const uint8_t *in, uint8_t *out, size_t size, uint32_t timeout)
Definition cfn_hal_crypto.h:121
cfn_hal_error_code_t(* generate_random)(cfn_hal_crypto_t *driver, uint8_t *buffer, size_t size)
Definition cfn_hal_crypto.h:125
cfn_hal_error_code_t(* hash_finish)(cfn_hal_crypto_t *driver, uint8_t *hash, uint32_t timeout)
Definition cfn_hal_crypto.h:124
cfn_hal_error_code_t(* hash_update)(cfn_hal_crypto_t *driver, const uint8_t *data, size_t size)
Definition cfn_hal_crypto.h:123
cfn_hal_error_code_t(* set_key)(cfn_hal_crypto_t *driver, const uint8_t *key, size_t key_size)
Definition cfn_hal_crypto.h:126
cfn_hal_error_code_t(* encrypt)(cfn_hal_crypto_t *driver, const uint8_t *in, uint8_t *out, size_t size, uint32_t timeout)
Definition cfn_hal_crypto.h:119
cfn_hal_api_base_t base
Definition cfn_hal_crypto.h:116
Crypto configuration structure.
Definition cfn_hal_crypto.h:82
cfn_hal_crypto_algo_t algo
Definition cfn_hal_crypto.h:83
void * user_config
Definition cfn_hal_crypto.h:84
Crypto hardware physical mapping.
Definition cfn_hal_crypto.h:91
void * instance
Definition cfn_hal_crypto.h:92
void * user_arg
Definition cfn_hal_crypto.h:93