From 39daa496e9b2a7c3825ca102fa3c7462dc6683fc Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sun, 22 Oct 2017 08:38:45 +0000 Subject: Crypto driver initial setup. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10869 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/hal.mk | 3 + os/hal/include/hal.h | 5 + os/hal/include/hal_crypto.h | 205 ++++++++++++ os/hal/ports/STM32/LLD/CRYPv1/driver.mk | 9 + os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.c | 97 ++++++ os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.h | 159 ++++++++++ os/hal/ports/STM32/STM32F7xx/platform.mk | 1 + os/hal/src/hal.c | 3 + os/hal/src/hal_crypto.c | 424 +++++++++++++++++++++++++ os/hal/templates/hal_crypto_lld.h | 149 +++++++++ 10 files changed, 1055 insertions(+) create mode 100644 os/hal/include/hal_crypto.h create mode 100644 os/hal/ports/STM32/LLD/CRYPv1/driver.mk create mode 100644 os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.c create mode 100644 os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.h create mode 100644 os/hal/src/hal_crypto.c create mode 100644 os/hal/templates/hal_crypto_lld.h diff --git a/os/hal/hal.mk b/os/hal/hal.mk index 9691e4b23..12f6b997b 100644 --- a/os/hal/hal.mk +++ b/os/hal/hal.mk @@ -20,6 +20,9 @@ endif ifneq ($(findstring HAL_USE_CAN TRUE,$(HALCONF)),) HALSRC += $(CHIBIOS)/os/hal/src/hal_can.c endif +ifneq ($(findstring HAL_USE_CRY TRUE,$(HALCONF)),) +HALSRC += $(CHIBIOS)/os/hal/src/hal_crypto.c +endif ifneq ($(findstring HAL_USE_DAC TRUE,$(HALCONF)),) HALSRC += $(CHIBIOS)/os/hal/src/hal_dac.c endif diff --git a/os/hal/include/hal.h b/os/hal/include/hal.h index f57bfa5b0..7b0fe8337 100644 --- a/os/hal/include/hal.h +++ b/os/hal/include/hal.h @@ -42,6 +42,10 @@ #define HAL_USE_CAN FALSE #endif +#if !defined(HAL_USE_CRY) +#define HAL_USE_CRY FALSE +#endif + #if !defined(HAL_USE_DAC) #define HAL_USE_DAC FALSE #endif @@ -124,6 +128,7 @@ #include "hal_pal.h" #include "hal_adc.h" #include "hal_can.h" +#include "hal_crypto.h" #include "hal_dac.h" #include "hal_ext.h" #include "hal_gpt.h" diff --git a/os/hal/include/hal_crypto.h b/os/hal/include/hal_crypto.h new file mode 100644 index 000000000..6e5309e37 --- /dev/null +++ b/os/hal/include/hal_crypto.h @@ -0,0 +1,205 @@ +/* + ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file hal_crypto.h + * @brief Cryptographic Driver macros and structures. + * + * @addtogroup CRYPTO + * @{ + */ + +#ifndef HAL_CRYPTO_H +#define HAL_CRYPTO_H + +#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name CRYPTO configuration options + * @{ + */ +/** + * @brief Enables asynchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(CRY_USE_CALLBACKS) || defined(__DOXYGEN__) +#define CRY_USE_CALLBACKS TRUE +#endif + +/** + * @brief Enables the @p cryAcquireBus() and @p cryReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(CRY_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define CRY_USE_MUTUAL_EXCLUSION TRUE +#endif +/** @} */ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Driver state machine possible states. + */ +typedef enum { + CRY_UNINIT = 0, /**< Not initialized. */ + CRY_STOP = 1, /**< Stopped. */ + CRY_READY = 2, /**< Ready. */ + CRY_ACTIVE = 3 /**< Operation running. */ +} crystate_t; + +/** + * @brief Driver error codes. + */ +typedef enum { + CRY_NOERROR = 0, /**< No error. */ + CRY_ERR_INV_ALGO = 1, /**< Invalid cypher/mode. */ + CRY_ERR_INV_KEY_SIZE = 2, /**< Invalid key size. */ + CRY_ERR_INV_KEY_TYPE = 3, /**< Invalid key type. */ + CRY_ERR_INV_KEY_ID = 4 /**< Invalid key type. */ +} cryerror_t; + +#include "hal_crypto_lld.h" + +#if !defined(CRY_LLD_SUPPORTS_AES_ECB) || \ + !defined(CRY_LLD_SUPPORTS_AES_CBC) || \ + !defined(CRY_LLD_SUPPORTS_AES_CFB) || \ + !defined(CRY_LLD_SUPPORTS_AES_CTR) +#error "CRYPTO LLD does not export required switches" +#endif + +/** + * @brief Type of an algorithm identifier. + */ +typedef enum { +#if (CRY_LLD_SUPPORTS_AES_ECB == TRUE) || defined(__DOXYGEN__) + cry_algo_aes_ecb, +#endif +#if (CRY_LLD_SUPPORTS_AES_CBC == TRUE) || defined(__DOXYGEN__) + cry_algo_aes_cbc, +#endif +#if (CRY_LLD_SUPPORTS_AES_CFB == TRUE) || defined(__DOXYGEN__) + cry_algo_aes_cfb, +#endif +#if (CRY_LLD_SUPPORTS_AES_CTR == TRUE) || defined(__DOXYGEN__) + cry_algo_aes_ctr, +#endif +} cryalgorithm_t; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/** + * @name Low level driver helper macros + * @{ + */ +/** @} */ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#ifdef __cplusplus +extern "C" { +#endif + void cryInit(void); + void cryObjectInit(CRYDriver *cryp); + void cryStart(CRYDriver *cryp, const CRYConfig *config); + void cryStop(CRYDriver *cryp); + cryerror_t cryLoadTransientKey(CRYDriver *cryp, + cryalgorithm_t algorithm, + size_t size, + const uint8_t *keyp); + +#if CRY_LLD_SUPPORTS_AES_ECB == TRUE + cryerror_t cryEncryptAES_ECB(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out); + cryerror_t cryDecryptAES_ECB(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out); +#endif /* CRY_LLD_SUPPORTS_AES_ECB == TRUE */ + +#if CRY_LLD_SUPPORTS_AES_CBC == TRUE + cryerror_t cryEncryptAES_CBC(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *iv); + cryerror_t cryDecryptAES_CBC(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *iv); +#endif /* CRY_LLD_SUPPORTS_AES_CBC == TRUE */ + +#if CRY_LLD_SUPPORTS_AES_CFB == TRUE + cryerror_t cryEncryptAES_CFB(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *iv); + cryerror_t cryDecryptAES_CFB(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *iv); +#endif /* CRY_LLD_SUPPORTS_AES_CFB == TRUE */ + +#if CRY_LLD_SUPPORTS_AES_CTR == TRUE + cryerror_t cryEncryptAES_CTR(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *nonce, + uint8_t *cnt); + cryerror_t cryDecryptAES_CTR(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *nonce, + uint8_t *cnt); +#endif /* CRY_LLD_SUPPORTS_AES_CTR == TRUE */ + +#if ADC_USE_MUTUAL_EXCLUSION == TRUE + void cryAcquireBus(CRYDriver *cryp); + void cryReleaseBus(CRYDriver *cryp); +#endif +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_CRYPTO == TRUE */ + +#endif /* HAL_CRYPTO_H */ + +/** @} */ diff --git a/os/hal/ports/STM32/LLD/CRYPv1/driver.mk b/os/hal/ports/STM32/LLD/CRYPv1/driver.mk new file mode 100644 index 000000000..43936d3e3 --- /dev/null +++ b/os/hal/ports/STM32/LLD/CRYPv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_CRY TRUE,$(HALCONF)),) +PLATFORMSRC += $(CHIBIOS)/os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.c +endif +else +PLATFORMSRC += $(CHIBIOS)/os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.c +endif + +PLATFORMINC += $(CHIBIOS)/os/hal/ports/STM32/LLD/CRYPv1 diff --git a/os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.c b/os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.c new file mode 100644 index 000000000..37cdb2a71 --- /dev/null +++ b/os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.c @@ -0,0 +1,97 @@ +/* + ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file CRYPv1/hal_crypto_lld.c + * @brief STM32 cryptographic subsystem low level driver source. + * + * @addtogroup CRYPTO + * @{ + */ + +#include "hal.h" + +#if HAL_USE_CRY || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/** @brief CRYP1 driver identifier.*/ +#if STM32_CRY_USE_CRYP1 || defined(__DOXYGEN__) +CRYDriver CRYD1; +#endif + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver interrupt handlers. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Low level crypto driver initialization. + * + * @notapi + */ +void cry_lld_init(void) { + +} + +/** + * @brief Configures and activates the crypto peripheral. + * + * @param[in] cryp pointer to the @p CRYDriver object + * + * @notapi + */ +void cry_lld_start(CRYDriver *cryp) { + + if (cryp->state == CRY_STOP) { + + } +} + +/** + * @brief Deactivates the crypto peripheral. + * + * @param[in] cryp pointer to the @p CRYDriver object + * + * @notapi + */ +void cry_lld_stop(CRYDriver *cryp) { + + if (cryp->state == CRY_READY) { + + } +} + +#endif /* HAL_USE_CRY */ + +/** @} */ diff --git a/os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.h b/os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.h new file mode 100644 index 000000000..e7279728b --- /dev/null +++ b/os/hal/ports/STM32/LLD/CRYPv1/hal_crypto_lld.h @@ -0,0 +1,159 @@ +/* + ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file CRYPv1/hal_cry_lld.h + * @brief STM32 cryptographic subsystem low level driver header. + * + * @addtogroup CRYPTO + * @{ + */ + +#ifndef HAL_CRYPTO_LLD_H +#define HAL_CRYPTO_LLD_H + +#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/** + * @name Driver capability switches + * @{ + */ +#define CRY_LLD_SUPPORTS_AES_ECB TRUE +#define CRY_LLD_SUPPORTS_AES_CBC TRUE +#define CRY_LLD_SUPPORTS_AES_CFB TRUE +#define CRY_LLD_SUPPORTS_AES_CTR TRUE +/** @{ */ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name PLATFORM configuration options + * @{ + */ +/** + * @brief CRY1 driver enable switch. + * @details If set to @p TRUE the support for CRY1 is included. + * @note The default is @p FALSE. + */ +#if !defined(PLATFORM_CRY_USE_CRY1) || defined(__DOXYGEN__) +#define PLATFORM_CRY_USE_CRY1 FALSE +#endif +/** @} */ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief CRY key identifier type. + */ +typedef uint32_t crykey_t; + +/** + * @brief Type of a structure representing an CRY driver. + */ +typedef struct CRYDriver CRYDriver; + +/** + * @brief CRY notification callback type. + * + * @param[in] cryp pointer to the @p CRYDriver object triggering the + * callback + */ +typedef void (*crycallback_t)(CRYDriver *cryp); + +/** + * @brief CRY error callback type. + * + * @param[in] cryp pointer to the @p CRYDriver object triggering the + * callback + * @param[in] err CRY error code + */ +typedef void (*cryerrorcallback_t)(CRYDriver *cryp, cryerror_t err); + +/** + * @brief Driver configuration structure. + * @note It could be empty on some architectures. + */ +typedef struct { + uint32_t dummy; +} CRYConfig; + +/** + * @brief Structure representing an CRY driver. + */ +struct CRYDriver { + /** + * @brief Driver state. + */ + crystate_t state; + /** + * @brief Current configuration data. + */ + const CRYConfig *config; + /** + * @brief Waiting thread. + */ + thread_reference_t thread; +#if (CRY_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__) + /** + * @brief Mutex protecting the peripheral. + */ + mutex_t mutex; +#endif +#if defined(CRY_DRIVER_EXT_FIELDS) + CRY_DRIVER_EXT_FIELDS +#endif + /* End of the mandatory fields.*/ +}; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#if (PLATFORM_CRY_USE_CRY1 == TRUE) && !defined(__DOXYGEN__) +extern CRYDriver CRYD1; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + void cry_lld_init(void); + void cry_lld_start(CRYDriver *cryp); + void cry_lld_stop(CRYDriver *cryp); +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_CRY == TRUE */ + +#endif /* HAL_CRYPTO_LLD_H */ + +/** @} */ diff --git a/os/hal/ports/STM32/STM32F7xx/platform.mk b/os/hal/ports/STM32/STM32F7xx/platform.mk index 542099329..580694126 100644 --- a/os/hal/ports/STM32/STM32F7xx/platform.mk +++ b/os/hal/ports/STM32/STM32F7xx/platform.mk @@ -23,6 +23,7 @@ endif # Drivers compatible with the platform. include $(CHIBIOS)/os/hal/ports/STM32/LLD/ADCv2/driver.mk include $(CHIBIOS)/os/hal/ports/STM32/LLD/CANv1/driver.mk +include $(CHIBIOS)/os/hal/ports/STM32/LLD/CRYPv1/driver.mk include $(CHIBIOS)/os/hal/ports/STM32/LLD/DACv1/driver.mk include $(CHIBIOS)/os/hal/ports/STM32/LLD/DMAv2/driver.mk include $(CHIBIOS)/os/hal/ports/STM32/LLD/EXTIv1/driver.mk diff --git a/os/hal/src/hal.c b/os/hal/src/hal.c index 583702bdc..1991987a8 100644 --- a/os/hal/src/hal.c +++ b/os/hal/src/hal.c @@ -70,6 +70,9 @@ void halInit(void) { #if (HAL_USE_CAN == TRUE) || defined(__DOXYGEN__) canInit(); #endif +#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__) + cryInit(); +#endif #if (HAL_USE_DAC == TRUE) || defined(__DOXYGEN__) dacInit(); #endif diff --git a/os/hal/src/hal_crypto.c b/os/hal/src/hal_crypto.c new file mode 100644 index 000000000..0493da872 --- /dev/null +++ b/os/hal/src/hal_crypto.c @@ -0,0 +1,424 @@ +/* + ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file hal_crypto.c + * @brief Cryptographic Driver code. + * + * @addtogroup CRYPTO + * @{ + */ + +#include "hal.h" + +#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Crypto Driver initialization. + * @note This function is implicitly invoked by @p halInit(), there is + * no need to explicitly initialize the driver. + * + * @init + */ +void cryInit(void) { + + cry_lld_init(); +} + +/** + * @brief Initializes the standard part of a @p CRYDriver structure. + * + * @param[out] cryp pointer to the @p CRYDriver object + * + * @init + */ +void cryObjectInit(CRYDriver *cryp) { + + cryp->state = CRY_STOP; + cryp->config = NULL; + cryp->thread = NULL; +#if CRY_USE_MUTUAL_EXCLUSION == TRUE + osalMutexObjectInit(&cryp->mutex); +#endif +#if defined(CRY_DRIVER_EXT_INIT_HOOK) + CRY_DRIVER_EXT_INIT_HOOK(cryp); +#endif +} + +/** + * @brief Configures and activates the CRY peripheral. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @param[in] config pointer to the @p CRYConfig object. Depending on + * the implementation the value can be @p NULL. + * + * @api + */ +void cryStart(CRYDriver *cryp, const CRYConfig *config) { + + osalDbgCheck(cryp != NULL); + + osalSysLock(); + osalDbgAssert((cryp->state == CRY_STOP) || (cryp->state == CRY_READY), + "invalid state"); + cryp->config = config; + cry_lld_start(cryp); + cryp->state = CRY_READY; + osalSysUnlock(); +} + +/** + * @brief Deactivates the CRY peripheral. + * + * @param[in] cryp pointer to the @p CRYDriver object + * + * @api + */ +void cryStop(CRYDriver *cryp) { + + osalDbgCheck(cryp != NULL); + + osalSysLock(); + + osalDbgAssert((cryp->state == CRY_STOP) || (cryp->state == CRY_READY), + "invalid state"); + + cry_lld_stop(cryp); + cryp->config = NULL; + cryp->state = CRY_STOP; + + osalSysUnlock(); +} + +/** + * @brief Initializes the transient key for a specific algorithm. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @param[in] algorithm the algorithm identifier + * @param[in] size key size in bytes + * @param[in] keyp pointer to the key data + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the specified algorithm is unknown or + * unsupported. + * @retval CRY_ERR_INV_KEY_SIZE if the specified key size is invalid. + * + * @api + */ +cryerror_t cryLoadTransientKey(CRYDriver *cryp, + cryalgorithm_t algorithm, + size_t size, + const uint8_t *keyp); + +#if (CRY_LLD_SUPPORTS_AES_ECB == TRUE) || defined(__DOXYGEN__) +/** + * @brief Encryption operation using AES-ECB. + * + * @param[in] key_id the key to be used for the operation, zero is the + * transient key, other values are keys stored in an + * unspecified way + * @param[in] size size of the plaintext buffer, this number must be a + * multiple of the selected key size + * @param[in] in buffer containing the input plaintext + * @param[out] out buffer for the output cyphertext + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation. + * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid + * or refers and empty key slot. + * + * @api + */ +cryerror_t cryEncryptAES_ECB(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out) { + +} + +/** + * @brief Decryption operation using AES-ECB. + * + * @param[in] key_id the key to be used for the operation, zero is the + * transient key, other values are keys stored in an + * unspecified way + * @param[in] size size of the plaintext buffer, this number must be a + * multiple of the selected key size + * @param[in] in buffer containing the input plaintext + * @param[out] out buffer for the output cyphertext + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation. + * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid + * or refers and empty key slot. + * + * @api + */ +cryerror_t cryDecryptAES_ECB(crykey_t key_id, + size_t blocks, + const uint8_t *in, + uint8_t *out) { + +} +#endif /* CRY_LLD_SUPPORTS_AES_ECB == TRUE */ + +#if (CRY_LLD_SUPPORTS_AES_CBC == TRUE) || defined(__DOXYGEN__) +/** + * @brief Encryption operation using AES-CBC. + * + * @param[in] key_id the key to be used for the operation, zero is the + * transient key, other values are keys stored in an + * unspecified way + * @param[in] size size of the plaintext buffer, this number must be a + * multiple of the selected key size + * @param[in] in buffer containing the input plaintext + * @param[out] out buffer for the output cyphertext + * @param[in] iv input vector + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation. + * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid + * or refers and empty key slot. + * + * @api + */ +cryerror_t cryEncryptAES_CBC(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *iv){ + +} + +/** + * @brief Decryption operation using AES-CBC. + * + * @param[in] key_id the key to be used for the operation, zero is the + * transient key, other values are keys stored in an + * unspecified way + * @param[in] size size of the plaintext buffer, this number must be a + * multiple of the selected key size + * @param[in] in buffer containing the input plaintext + * @param[out] out buffer for the output cyphertext + * @param[in] iv input vector + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation. + * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid + * or refers and empty key slot. + * + * @api + */ +cryerror_t cryDecryptAES_CBC(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *iv) { + +} +#endif /* CRY_LLD_SUPPORTS_AES_CBC == TRUE */ + +#if (CRY_LLD_SUPPORTS_AES_CFB == TRUE) || defined(__DOXYGEN__) +/** + * @brief Encryption operation using AES-CFB. + * + * @param[in] key_id the key to be used for the operation, zero is the + * transient key, other values are keys stored in an + * unspecified way + * @param[in] size size of the plaintext buffer, this number must be a + * multiple of the selected key size + * @param[in] in buffer containing the input plaintext + * @param[out] out buffer for the output cyphertext + * @param[in] iv input vector + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation. + * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid + * or refers and empty key slot. + * + * @api + */ +cryerror_t cryEncryptAES_CFB(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *iv) { + +} + +/** + * @brief Decryption operation using AES-CFB. + * + * @param[in] key_id the key to be used for the operation, zero is the + * transient key, other values are keys stored in an + * unspecified way + * @param[in] size size of the plaintext buffer, this number must be a + * multiple of the selected key size + * @param[in] in buffer containing the input plaintext + * @param[out] out buffer for the output cyphertext + * @param[in] iv input vector + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation. + * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid + * or refers and empty key slot. + * + * @api + */ +cryerror_t cryDecryptAES_CFB(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *iv) { + +} +#endif /* CRY_LLD_SUPPORTS_AES_CFB == TRUE */ + +#if (CRY_LLD_SUPPORTS_AES_CTR == TRUE) || defined(__DOXYGEN__) +/** + * @brief Encryption operation using AES-CTR. + * + * @param[in] key_id the key to be used for the operation, zero is the + * transient key, other values are keys stored in an + * unspecified way + * @param[in] size size of the plaintext buffer, this number must be a + * multiple of the selected key size + * @param[in] in buffer containing the input plaintext + * @param[out] out buffer for the output cyphertext + * @param[in] nonce the "nonce" constant + * @param[in,out] cnt the initial value of the counter, normally zero + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation. + * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid + * or refers and empty key slot. + * + * @api + */ +cryerror_t cryEncryptAES_CTR(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *nonce, + uint8_t *cnt) { + +} + +/** + * @brief Decryption operation using AES-CTR. + * + * @param[in] key_id the key to be used for the operation, zero is the + * transient key, other values are keys stored in an + * unspecified way + * @param[in] size size of the plaintext buffer, this number must be a + * multiple of the selected key size + * @param[in] in buffer containing the input plaintext + * @param[out] out buffer for the output cyphertext + * @param[in] nonce the "nonce" constant + * @param[in,out] cnt the initial value of the counter, normally zero + * @return The operation status. + * @retval CRY_NOERROR if the operation succeeded. + * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this + * device instance. + * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation. + * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid + * or refers and empty key slot. + * + * @api + */ +cryerror_t cryDecryptAES_CTR(crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *nonce, + uint8_t *cnt) { + +} +#endif /* CRY_LLD_SUPPORTS_AES_CTR == TRUE */ + +#if (CRY_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__) +/** + * @brief Gains exclusive access to the CRY peripheral. + * @details This function tries to gain ownership to CRY bus, if the bus + * is already being used then the invoking thread is queued. + * @pre In order to use this function the option + * @p CRY_USE_MUTUAL_EXCLUSION must be enabled. + * + * @param[in] cryp pointer to the @p CRYDriver object + * + * @api + */ +void cryAcquireBus(CRYDriver *cryp) { + + osalDbgCheck(cryp != NULL); + + osalMutexLock(&cryp->mutex); +} + +/** + * @brief Releases exclusive access to the CRY peripheral. + * @pre In order to use this function the option + * @p CRY_USE_MUTUAL_EXCLUSION must be enabled. + * + * @param[in] cryp pointer to the @p CRYDriver object + * + * @api + */ +void cryReleaseBus(CRYDriver *cryp) { + + osalDbgCheck(cryp != NULL); + + osalMutexUnlock(&cryp->mutex); +} +#endif /* CRY_USE_MUTUAL_EXCLUSION == TRUE */ + +#endif /* HAL_USE_CRY == TRUE */ + +/** @} */ diff --git a/os/hal/templates/hal_crypto_lld.h b/os/hal/templates/hal_crypto_lld.h new file mode 100644 index 000000000..24a19bad7 --- /dev/null +++ b/os/hal/templates/hal_crypto_lld.h @@ -0,0 +1,149 @@ +/* + ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file hal_cry_lld.h + * @brief PLATFORM CRY subsystem low level driver header. + * + * @addtogroup CRY + * @{ + */ + +#ifndef HAL_CRYPTO_LLD_H +#define HAL_CRYPTO_LLD_H + +#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name PLATFORM configuration options + * @{ + */ +/** + * @brief CRY1 driver enable switch. + * @details If set to @p TRUE the support for CRY1 is included. + * @note The default is @p FALSE. + */ +#if !defined(PLATFORM_CRY_USE_CRY1) || defined(__DOXYGEN__) +#define PLATFORM_CRY_USE_CRY1 FALSE +#endif +/** @} */ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief CRY key identifier type. + */ +typedef uint32_t crykey_t; + +/** + * @brief Type of a structure representing an CRY driver. + */ +typedef struct CRYDriver CRYDriver; + +/** + * @brief CRY notification callback type. + * + * @param[in] cryp pointer to the @p CRYDriver object triggering the + * callback + */ +typedef void (*crycallback_t)(CRYDriver *cryp); + +/** + * @brief CRY error callback type. + * + * @param[in] cryp pointer to the @p CRYDriver object triggering the + * callback + * @param[in] err CRY error code + */ +typedef void (*cryerrorcallback_t)(CRYDriver *cryp, cryerror_t err); + +/** + * @brief Driver configuration structure. + * @note It could be empty on some architectures. + */ +typedef struct { + uint32_t dummy; +} CRYConfig; + +/** + * @brief Structure representing an CRY driver. + */ +struct CRYDriver { + /** + * @brief Driver state. + */ + crystate_t state; + /** + * @brief Current configuration data. + */ + const CRYConfig *config; + /** + * @brief Waiting thread. + */ + thread_reference_t thread; +#if (CRY_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__) + /** + * @brief Mutex protecting the peripheral. + */ + mutex_t mutex; +#endif +#if defined(CRY_DRIVER_EXT_FIELDS) + CRY_DRIVER_EXT_FIELDS +#endif + /* End of the mandatory fields.*/ +}; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#if (PLATFORM_CRY_USE_CRY1 == TRUE) && !defined(__DOXYGEN__) +extern CRYDriver CRYD1; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + void cry_lld_init(void); + void cry_lld_start(CRYDriver *cryp); + void cry_lld_stop(CRYDriver *cryp); +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_CRY == TRUE */ + +#endif /* HAL_CRYPTO_LLD_H */ + +/** @} */ -- cgit v1.2.3