/* ChibiOS - Copyright (C) 2006..2018 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 Cryptographic Driver initialization. * @note This function is implicitly invoked by @p halInit(), there is * no need to explicitly initialize the driver. * * @init */ void cryInit(void) { #if HAL_CRY_ENFORCE_FALLBACK == FALSE cry_lld_init(); #endif } /** * @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; #if defined(CRY_DRIVER_EXT_INIT_HOOK) CRY_DRIVER_EXT_INIT_HOOK(cryp); #endif } /** * @brief Configures and activates the cryptographic 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; #if HAL_CRY_ENFORCE_FALLBACK == FALSE cry_lld_start(cryp); #endif cryp->state = CRY_READY; osalSysUnlock(); } /** * @brief Deactivates the cryptographic 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"); #if HAL_CRY_ENFORCE_FALLBACK == FALSE cry_lld_stop(cryp); #endif cryp->config = NULL; cryp->state = CRY_STOP; osalSysUnlock(); } /** * @brief Initializes the transient key for a specific algorithm. * @note It is the underlying implementation to decide which combinations * of algorithm and key size are allowable. * * @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 for * the specified algorithm. * * @api */ cryerror_t cryLoadTransientKey(CRYDriver *cryp, cryalgorithm_t algorithm, size_t size, const uint8_t *keyp) { cryerror_t err; osalDbgCheck((cryp != NULL) && (size <= HAL_CRY_MAX_KEY_SIZE) && (keyp != NULL)); #if HAL_CRY_ENFORCE_FALLBACK == FALSE /* Key setup in the low level driver.*/ err = cry_lld_loadkey(cryp, algorithm, size, keyp); #else err = CRY_ERR_INV_ALGO; #endif #if HAL_CRY_USE_FALLBACK == TRUE if (err == CRY_ERR_INV_ALGO) { err = cry_fallback_loadkey(cryp, algorithm, size, keyp); } #endif if (err == CRY_NOERROR) { /* Storing the transient key info.*/ cryp->key0_type = algorithm; cryp->key0_size = size; } return err; } /** * @brief Encryption of a single block using AES. * @note The implementation of this function must guarantee that it can * be called from any context. * * @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 *
/*
Copyright (C) 2015 Robert Lippert
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.
*/
#include "ch.h"
#include "hal.h"
#include "shell.h"
#include "usbcfg.h"
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {
(void)arg;
chRegSetThreadName("blinker");
while (true) {
palTogglePad(IOPORT4, BOARD_LED1);
chThdSleepMilliseconds(1000);
}
}
#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(512)
static const ShellConfig shell_cfg1 = {
(BaseSequentialStream *)&SDU1,
NULL
};
/*
* Application entry point.
*/
int main(void) {
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
palClearPad(IOPORT4, BOARD_LED1);
/*
* Shell manager initialization.
*/
shellInit();
/* Initializes a serial-over-USB CDC driver. */
sduObjectInit(&SDU1);
sduStart(&SDU1, &serusbcfg);
/* Initialize USB */
usbStart(serusbcfg.usbp, &usbcfg);
/*
* Starts the LED blinker thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/*
* Normal main() thread activity.
*/
while (true) {
if (SDU1.config->usbp->state == USB_ACTIVE) {
thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
"shell", NORMALPRIO + 1,
shellThread, (void *)&shell_cfg1);
chThdWait(shelltp); /* Waiting termination. */
}
chThdSleepMilliseconds(1000);
}
}