From dddff7b1f5fd6a56081b8f2af105e6eb05df904f Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sun, 22 Oct 2017 10:31:34 +0000 Subject: Added crypto templates. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10873 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/templates/hal_crypto_lld.c | 435 ++++++++++++++++++++++++++++++++++++++ os/hal/templates/hal_crypto_lld.h | 80 ++++++- os/hal/templates/platform.mk | 11 +- 3 files changed, 515 insertions(+), 11 deletions(-) create mode 100644 os/hal/templates/hal_crypto_lld.c diff --git a/os/hal/templates/hal_crypto_lld.c b/os/hal/templates/hal_crypto_lld.c new file mode 100644 index 000000000..9268e5398 --- /dev/null +++ b/os/hal/templates/hal_crypto_lld.c @@ -0,0 +1,435 @@ +/* + 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 == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/** @brief CRY1 driver identifier.*/ +#if PLATFORM_CRY_USE_CRY1 || 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) { + + } +} + +/** + * @brief Initializes the transient key for a specific algorithm. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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. + * + * @notapi + */ +cryerror_t cry_lld_loadkey(CRYDriver *cryp, + cryalgorithm_t algorithm, + size_t size, + const uint8_t *keyp) { + + (void)cryp; + (void)algorithm; + (void)size; + (void)keyp; + + return CRY_NOERROR; +} + +/** + * @brief Encryption operation using AES-ECB. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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 cry_lld_encrypt_AES_ECB(CRYDriver *cryp, + crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out) { + + (void)cryp; + (void)key_id; + (void)size; + (void)in; + (void)out; + + return CRY_NOERROR; +} + +/** + * @brief Decryption operation using AES-ECB. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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 cry_lld_decrypt_AES_ECB(CRYDriver *cryp, + crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out) { + + (void)cryp; + (void)key_id; + (void)size; + (void)in; + (void)out; + + return CRY_NOERROR; +} + +/** + * @brief Encryption operation using AES-CBC. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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 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) { + + (void)cryp; + (void)key_id; + (void)size; + (void)in; + (void)out; + (void)iv; + + return CRY_NOERROR; +} + +/** + * @brief Decryption operation using AES-CBC. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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 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) { + + (void)cryp; + (void)key_id; + (void)size; + (void)in; + (void)out; + (void)iv; + + return CRY_NOERROR; +} + +/** + * @brief Encryption operation using AES-CFB. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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 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) { + + (void)cryp; + (void)key_id; + (void)size; + (void)in; + (void)out; + (void)iv; + + return CRY_NOERROR; +} + +/** + * @brief Decryption operation using AES-CFB. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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 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) { + + (void)cryp; + (void)key_id; + (void)size; + (void)in; + (void)out; + (void)iv; + + return CRY_NOERROR; +} + +/** + * @brief Encryption operation using AES-CTR. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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 cry_lld_encrypt_AES_CTR(CRYDriver *cryp, + crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out, + const uint8_t *nonce, + uint8_t *cnt) { + + (void)cryp; + (void)key_id; + (void)size; + (void)in; + (void)out; + (void)nonce; + (void)cnt; + + return CRY_NOERROR; +} + +/** + * @brief Decryption operation using AES-CTR. + * + * @param[in] cryp pointer to the @p CRYDriver object + * @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. + * + * @notapi + */ +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 *nonce, + uint8_t *cnt) { + + (void)cryp; + (void)key_id; + (void)size; + (void)in; + (void)out; + (void)nonce; + (void)cnt; + + return CRY_NOERROR; +} + +#endif /* HAL_USE_CRY == TRUE */ + +/** @} */ diff --git a/os/hal/templates/hal_crypto_lld.h b/os/hal/templates/hal_crypto_lld.h index 24a19bad7..53046d814 100644 --- a/os/hal/templates/hal_crypto_lld.h +++ b/os/hal/templates/hal_crypto_lld.h @@ -16,9 +16,9 @@ /** * @file hal_cry_lld.h - * @brief PLATFORM CRY subsystem low level driver header. + * @brief PLATFORM cryptographic subsystem low level driver header. * - * @addtogroup CRY + * @addtogroup CRYPTO * @{ */ @@ -31,6 +31,16 @@ /* 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. */ /*===========================================================================*/ @@ -97,23 +107,21 @@ typedef struct { */ struct CRYDriver { /** - * @brief Driver state. + * @brief Driver state. */ crystate_t state; /** - * @brief Current configuration data. + * @brief Current configuration data. */ const CRYConfig *config; /** - * @brief Waiting thread. + * @brief Algorithm type of transient key. */ - thread_reference_t thread; -#if (CRY_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__) + cryalgorithm_t key0_type; /** - * @brief Mutex protecting the peripheral. + * @brief Size of transient key. */ - mutex_t mutex; -#endif + size_t key0_size; #if defined(CRY_DRIVER_EXT_FIELDS) CRY_DRIVER_EXT_FIELDS #endif @@ -138,6 +146,58 @@ extern "C" { void cry_lld_init(void); void cry_lld_start(CRYDriver *cryp); void cry_lld_stop(CRYDriver *cryp); + cryerror_t cry_lld_loadkey(CRYDriver *cryp, + cryalgorithm_t algorithm, + size_t size, + const uint8_t *keyp); + cryerror_t cry_lld_encrypt_AES_ECB(CRYDriver *cryp, + crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out); + cryerror_t cry_lld_decrypt_AES_ECB(CRYDriver *cryp, + crykey_t key_id, + size_t size, + const uint8_t *in, + uint8_t *out); + 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); + 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); + 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); + 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); + 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 *nonce, + uint8_t *cnt); + 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 *nonce, + uint8_t *cnt); #ifdef __cplusplus } #endif diff --git a/os/hal/templates/platform.mk b/os/hal/templates/platform.mk index dfa472e94..fac62e0b0 100644 --- a/os/hal/templates/platform.mk +++ b/os/hal/templates/platform.mk @@ -1,6 +1,12 @@ # List of all the template platform files. ifeq ($(USE_SMART_BUILD),yes) -HALCONF := $(strip $(shell cat halconf.h | egrep -e "\#define")) + +# Configuration files directory +ifeq ($(CONFDIR),) + CONFDIR = . +endif + +HALCONF := $(strip $(shell cat $(CONFDIR)/halconf.h | egrep -e "\#define")) PLATFORMSRC := ${CHIBIOS}/os/hal/templates/hal_lld.c \ ${CHIBIOS}/os/hal/templates/hal_st_lld.c @@ -10,6 +16,9 @@ endif ifneq ($(findstring HAL_USE_CAN TRUE,$(HALCONF)),) PLATFORMSRC += ${CHIBIOS}/os/hal/templates/hal_can_lld.c endif +ifneq ($(findstring HAL_USE_CRY TRUE,$(HALCONF)),) +PLATFORMSRC += ${CHIBIOS}/os/hal/templates/hal_cry_lld.c +endif ifneq ($(findstring HAL_USE_DAC TRUE,$(HALCONF)),) PLATFORMSRC += ${CHIBIOS}/os/hal/templates/hal_dac_lld.c endif -- cgit v1.2.3