1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747 |
- /*
- 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 AES transient key.
- * @note It is the underlying implementation to decide which key sizes are
- * allowable.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @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 algorithm is unsupported.
- * @retval CRY_ERR_INV_KEY_SIZE if the specified key size is invalid for
- * the specified algorithm.
- *
- * @api
- */
- cryerror_t cryLoadAESTransientKey(CRYDriver *cryp,
- size_t size,
- const uint8_t *keyp) {
- osalDbgCheck((cryp != NULL) && (keyp != NULL));
- #if CRY_LLD_SUPPORTS_AES == TRUE
- return cry_lld_aes_loadkey(cryp, size, keyp);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_aes_loadkey(cryp, size, keyp);
- #else
- (void)cryp;
- (void)size;
- (void)keyp;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @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
- * in an unspecified way
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @special
- */
- cryerror_t cryEncryptAES(CRYDriver *cryp,
- crykey_t key_id,
- const uint8_t *in,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES == TRUE
- return cry_lld_encrypt_AES(cryp, key_id, in, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_AES(cryp, key_id, in, out);
- #else
- (void)cryp;
- (void)key_id;
- (void)in;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption 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
- * in an unspecified way
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @special
- */
- cryerror_t cryDecryptAES(CRYDriver *cryp,
- crykey_t key_id,
- const uint8_t *in,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES == TRUE
- return cry_lld_decrypt_AES(cryp, key_id, in, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_AES(cryp, key_id, in, out);
- #else
- (void)cryp;
- (void)key_id;
- (void)in;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Encryption operation using AES-ECB.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 16
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryEncryptAES_ECB(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- ((size & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_ECB == TRUE
- return cry_lld_encrypt_AES_ECB(cryp, key_id, size, in, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_AES_ECB(cryp, key_id, size, in, out);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption operation using AES-ECB.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryDecryptAES_ECB(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- ((size & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_ECB == TRUE
- return cry_lld_decrypt_AES_ECB(cryp, key_id, size, in, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_AES_ECB(cryp, key_id, size, in, out);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Encryption operation using AES-CBC.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits 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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryEncryptAES_CBC(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out,
- const uint8_t *iv) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && ((size & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_CBC == TRUE
- return cry_lld_encrypt_AES_CBC(cryp, key_id, size, in, out, iv);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_AES_CBC(cryp, key_id, size, in, out, iv);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption operation using AES-CBC.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @param[in] iv 128 bits 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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryDecryptAES_CBC(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out,
- const uint8_t *iv) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && ((size & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_CBC == TRUE
- return cry_lld_decrypt_AES_CBC(cryp, key_id, size, in, out, iv);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_AES_CBC(cryp, key_id, size, in, out, iv);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Encryption operation using AES-CFB.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits 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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryEncryptAES_CFB(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out,
- const uint8_t *iv) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && ((size & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_CFB == TRUE
- return cry_lld_encrypt_AES_CFB(cryp, key_id, size, in, out, iv);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_AES_CFB(cryp, key_id, size, in, out, iv);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption operation using AES-CFB.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @param[in] iv 128 bits 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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryDecryptAES_CFB(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out,
- const uint8_t *iv) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && ((size & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_CFB == TRUE
- return cry_lld_decrypt_AES_CFB(cryp, key_id, size, in, out, iv);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_AES_CFB(cryp, key_id, size, in, out, iv);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Encryption operation using AES-CTR.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits input vector + counter, it contains
- * a 96 bits IV and a 32 bits counter
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryEncryptAES_CTR(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out,
- const uint8_t *iv) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && ((size & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_CTR == TRUE
- return cry_lld_encrypt_AES_CTR(cryp, key_id, size, in, out, iv);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_AES_CTR(cryp, key_id, size, in, out, iv);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption operation using AES-CTR.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @param[in] iv 128 bits input vector + counter, it contains
- * a 96 bits IV and a 32 bits counter
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryDecryptAES_CTR(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out,
- const uint8_t *iv) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && ((size & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_CTR == TRUE
- return cry_lld_decrypt_AES_CTR(cryp, key_id, size, in, out, iv);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_AES_CTR(cryp, key_id, size, in, out, iv);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Encryption operation using AES-GCM.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 text buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 128 bits input vector + counter, it contains
- * a 96 bits IV and a 32 bits counter
- * @param[in] aadsize size of the authentication data, this number
- * must be a multiple of 16
- * @param[in] aad buffer containing the authentication data
- * @param[in] authtag 128 bits buffer for the generated authentication
- * tag
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryEncryptAES_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) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && (aad != NULL) && (authtag != NULL) &&
- ((size & (size_t)15) == (size_t)0) &&
- ((aadsize & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_GCM== TRUE
- return cry_lld_encrypt_AES_GCM(cryp, key_id, size, in, out, iv,
- aadsize, aad, authtag);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_AES_GCM(cryp, key_id, size, in, out, iv,
- aadsize, aad, authtag);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- (void)aadsize;
- (void)aad;
- (void)authtag;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption operation using AES-GCM.
- * @note The function operates on data buffers whose length is a multiple
- * of an AES block, this means that padding must be done by the
- * caller.
- *
- * @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 text buffers, this number must be a
- * multiple of 16
- * @param[in] in buffer for the output cyphertext
- * @param[out] out buffer containing the input plaintext
- * @param[in] iv 128 bits input vector + counter, it contains
- * a 96 bits IV and a 32 bits counter
- * @param[in] aadsize size of the authentication data, this number
- * must be a multiple of 16
- * @param[in] aad buffer containing the authentication data
- * @param[in] authtag 128 bits buffer for the generated authentication
- * tag
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryDecryptAES_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) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && (aad != NULL) && (authtag != NULL) &&
- ((size & (size_t)15) == (size_t)0) &&
- ((aadsize & (size_t)15) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_AES_GCM== TRUE
- return cry_lld_decrypt_AES_GCM(cryp, key_id, size, in, out, iv,
- aadsize, aad, authtag);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_AES_GCM(cryp, key_id, size, in, out, iv,
- aadsize, aad, authtag);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- (void)aadsize;
- (void)aad;
- (void)authtag;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Initializes the DES transient key.
- * @note It is the underlying implementation to decide which key sizes are
- * allowable.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @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 algorithm is unsupported.
- * @retval CRY_ERR_INV_KEY_SIZE if the specified key size is invalid for
- * the specified algorithm.
- *
- * @api
- */
- cryerror_t cryLoadDESTransientKey(CRYDriver *cryp,
- size_t size,
- const uint8_t *keyp) {
- osalDbgCheck((cryp != NULL) && (keyp != NULL));
- #if CRY_LLD_SUPPORTS_DES == TRUE
- return cry_lld_des_loadkey(cryp, size, keyp);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_des_loadkey(cryp, size, keyp);
- #else
- (void)cryp;
- (void)size;
- (void)keyp;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Encryption of a single block using (T)DES.
- * @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
- * in an unspecified way
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @special
- */
- cryerror_t cryEncryptDES(CRYDriver *cryp,
- crykey_t key_id,
- const uint8_t *in,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_DES == TRUE
- return cry_lld_encrypt_DES(cryp, key_id, in, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_DES(cryp, key_id, in, out);
- #else
- (void)cryp;
- (void)key_id;
- (void)in;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption of a single block using (T)DES.
- * @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
- * in an unspecified way
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @special
- */
- cryerror_t cryDecryptDES(CRYDriver *cryp,
- crykey_t key_id,
- const uint8_t *in,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_DES == TRUE
- return cry_lld_decrypt_DES(cryp, key_id, in, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_DES(cryp, key_id, in, out);
- #else
- (void)cryp;
- (void)key_id;
- (void)in;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Encryption operation using (T)DES-ECB.
- * @note The function operates on data buffers whose length is a multiple
- * of an DES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 8
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryEncryptDES_ECB(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- ((size & (size_t)7) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_DES_ECB == TRUE
- return cry_lld_encrypt_DES_ECB(cryp, key_id, size, in, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_DES_ECB(cryp, key_id, size, in, out);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption operation using (T)DES-ECB.
- * @note The function operates on data buffers whose length is a multiple
- * of an DES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 8
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryDecryptDES_ECB(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- ((size & (size_t)7) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_DES_ECB == TRUE
- return cry_lld_decrypt_DES_ECB(cryp, key_id, size, in, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_DES_ECB(cryp, key_id, size, in, out);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Encryption operation using (T)DES-CBC.
- * @note The function operates on data buffers whose length is a multiple
- * of an DES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 8
- * @param[in] in buffer containing the input plaintext
- * @param[out] out buffer for the output cyphertext
- * @param[in] iv 64 bits 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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryEncryptDES_CBC(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out,
- const uint8_t *iv) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && ((size & (size_t)7) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_DES_CBC == TRUE
- return cry_lld_encrypt_DES_CBC(cryp, key_id, size, in, out, iv);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_encrypt_DES_CBC(cryp, key_id, size, in, out, iv);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Decryption operation using (T)DES-CBC.
- * @note The function operates on data buffers whose length is a multiple
- * of an DES block, this means that padding must be done by the
- * caller.
- *
- * @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 both buffers, this number must be a
- * multiple of 8
- * @param[in] in buffer containing the input cyphertext
- * @param[out] out buffer for the output plaintext
- * @param[in] iv 64 bits 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 to an empty key slot.
- * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryDecryptDES_CBC(CRYDriver *cryp,
- crykey_t key_id,
- size_t size,
- const uint8_t *in,
- uint8_t *out,
- const uint8_t *iv) {
- osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
- (iv != NULL) && ((size & (size_t)7) == (size_t)0));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_DES_CBC == TRUE
- return cry_lld_decrypt_DES_CBC(cryp, key_id, size, in, out, iv);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_decrypt_DES_CBC(cryp, key_id, size, in, out, iv);
- #else
- (void)cryp;
- (void)key_id;
- (void)size;
- (void)in;
- (void)out;
- (void)iv;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash initialization using SHA1.
- * @note Use of this algorithm is not recommended because proven weak.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[out] sha1ctxp pointer to a SHA1 context to be initialized
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA1Init(CRYDriver *cryp, SHA1Context *sha1ctxp) {
- osalDbgCheck((cryp != NULL) && (sha1ctxp != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA1 == TRUE
- return cry_lld_SHA1_init(cryp, sha1ctxp);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA1_init(cryp, sha1ctxp);
- #else
- (void)cryp;
- (void)sha1ctxp;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash update using SHA1.
- * @note Use of this algorithm is not recommended because proven weak.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] sha1ctxp pointer to a SHA1 context
- * @param[in] size size of input buffer
- * @param[in] in buffer containing the input text
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA1Update(CRYDriver *cryp, SHA1Context *sha1ctxp,
- size_t size, const uint8_t *in) {
- osalDbgCheck((cryp != NULL) && (sha1ctxp != NULL) && (in != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA1 == TRUE
- return cry_lld_SHA1_update(cryp, sha1ctxp, size, in);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA1_update(cryp, sha1ctxp, size, in);
- #else
- (void)cryp;
- (void)sha1ctxp;
- (void)size;
- (void)in;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash finalization using SHA1.
- * @note Use of this algorithm is not recommended because proven weak.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] sha1ctxp pointer to a SHA1 context
- * @param[out] out 160 bits output buffer
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA1Final(CRYDriver *cryp, SHA1Context *sha1ctxp, uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (sha1ctxp != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA1 == TRUE
- return cry_lld_SHA1_final(cryp, sha1ctxp, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA1_final(cryp, sha1ctxp, out);
- #else
- (void)cryp;
- (void)sha1ctxp;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash initialization using SHA256.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[out] sha256ctxp pointer to a SHA256 context to be initialized
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA256Init(CRYDriver *cryp, SHA256Context *sha256ctxp) {
- osalDbgCheck((cryp != NULL) && (sha256ctxp != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA256 == TRUE
- return cry_lld_SHA256_init(cryp, sha256ctxp);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA256_init(cryp, sha256ctxp);
- #else
- (void)cryp;
- (void)sha256ctxp;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash update using SHA256.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] sha256ctxp pointer to a SHA256 context
- * @param[in] size size of input buffer
- * @param[in] in buffer containing the input text
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA256Update(CRYDriver *cryp, SHA256Context *sha256ctxp,
- size_t size, const uint8_t *in) {
- osalDbgCheck((cryp != NULL) && (sha256ctxp != NULL) && (in != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA256 == TRUE
- return cry_lld_SHA256_update(cryp, sha256ctxp, size, in);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA256_update(cryp, sha256ctxp, size, in);
- #else
- (void)cryp;
- (void)sha256ctxp;
- (void)size;
- (void)in;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash finalization using SHA256.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] sha256ctxp pointer to a SHA256 context
- * @param[out] out 256 bits output buffer
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA256Final(CRYDriver *cryp, SHA256Context *sha256ctxp,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (sha256ctxp != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA256 == TRUE
- return cry_lld_SHA256_final(cryp, sha256ctxp, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA256_final(cryp, sha256ctxp, out);
- #else
- (void)cryp;
- (void)sha256ctxp;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash initialization using SHA512.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[out] sha512ctxp pointer to a SHA512 context to be initialized
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA512Init(CRYDriver *cryp, SHA512Context *sha512ctxp) {
- osalDbgCheck((cryp != NULL) && (sha512ctxp != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA512 == TRUE
- return cry_lld_SHA512_init(cryp, sha512ctxp);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA512_init(cryp, sha512ctxp);
- #else
- (void)cryp;
- (void)sha512ctxp;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash update using SHA512.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] sha512ctxp pointer to a SHA512 context
- * @param[in] size size of input buffer
- * @param[in] in buffer containing the input text
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA512Update(CRYDriver *cryp, SHA512Context *sha512ctxp,
- size_t size, const uint8_t *in) {
- osalDbgCheck((cryp != NULL) && (sha512ctxp != NULL) && (in != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA512 == TRUE
- return cry_lld_SHA512_update(cryp, sha512ctxp, size, in);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA512_update(cryp, sha512ctxp, size, in);
- #else
- (void)cryp;
- (void)sha512ctxp;
- (void)size;
- (void)in;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash finalization using SHA512.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] sha512ctxp pointer to a SHA512 context
- * @param[out] out 512 bits output buffer
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t crySHA512Final(CRYDriver *cryp, SHA512Context *sha512ctxp,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (sha512ctxp != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_SHA512 == TRUE
- return cry_lld_SHA512_final(cryp, sha512ctxp, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_SHA512_final(cryp, sha512ctxp, out);
- #else
- (void)cryp;
- (void)sha512ctxp;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Initializes the HMAC transient key.
- * @note It is the underlying implementation to decide which key sizes are
- * allowable.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @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 algorithm is unsupported.
- * @retval CRY_ERR_INV_KEY_SIZE if the specified key size is invalid for
- * the specified algorithm.
- *
- * @api
- */
- cryerror_t cryLoadHMACTransientKey(CRYDriver *cryp,
- size_t size,
- const uint8_t *keyp) {
- osalDbgCheck((cryp != NULL) && (keyp != NULL));
- #if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || \
- (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE)
- return cry_lld_hmac_loadkey(cryp, size, keyp);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_hmac_loadkey(cryp, size, keyp);
- #else
- (void)cryp;
- (void)size;
- (void)keyp;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash initialization using HMAC_SHA256.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[out] hmacsha256ctxp pointer to a HMAC_SHA256 context to be
- * initialized
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryHMACSHA256Init(CRYDriver *cryp,
- HMACSHA256Context *hmacsha256ctxp) {
- osalDbgCheck((cryp != NULL) && (hmacsha256ctxp != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE
- return cry_lld_HMACSHA256_init(cryp, hmacsha256ctxp);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_HMACSHA256_init(cryp, hmacsha256ctxp);
- #else
- (void)cryp;
- (void)hmacsha256ctxp;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash update using HMAC.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] hmacsha256ctxp pointer to a HMAC_SHA256 context
- * @param[in] size size of input buffer
- * @param[in] in buffer containing the input text
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryHMACSHA256Update(CRYDriver *cryp,
- HMACSHA256Context *hmacsha256ctxp,
- size_t size,
- const uint8_t *in) {
- osalDbgCheck((cryp != NULL) && (hmacsha256ctxp != NULL) && (in != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE
- return cry_lld_HMACSHA256_update(cryp, hmacsha256ctxp, size, in);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_HMACSHA256_update(cryp, hmacsha256ctxp, size, in);
- #else
- (void)cryp;
- (void)hmacsha256ctxp;
- (void)size;
- (void)in;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash finalization using HMAC.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] hmacsha256ctxp pointer to a HMAC_SHA256 context
- * @param[out] out 256 bits output buffer
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryHMACSHA256Final(CRYDriver *cryp,
- HMACSHA256Context *hmacsha256ctxp,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (hmacsha256ctxp != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE
- return cry_lld_HMACSHA256_final(cryp, hmacsha256ctxp, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_HMACSHA256_final(cryp, hmacsha256ctxp, out);
- #else
- (void)cryp;
- (void)hmacsha256ctxp;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash initialization using HMAC_SHA512.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[out] hmacsha512ctxp pointer to a HMAC_SHA512 context to be
- * initialized
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryHMACSHA512Init(CRYDriver *cryp,
- HMACSHA512Context *hmacsha512ctxp) {
- osalDbgCheck((cryp != NULL) && (hmacsha512ctxp != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE
- return cry_lld_HMACSHA512_init(cryp, hmacsha512ctxp);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_HMACSHA512_init(cryp, hmacsha512ctxp);
- #else
- (void)cryp;
- (void)hmacsha512ctxp;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash update using HMAC.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] hmacsha512ctxp pointer to a HMAC_SHA512 context
- * @param[in] size size of input buffer
- * @param[in] in buffer containing the input text
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryHMACSHA512Update(CRYDriver *cryp,
- HMACSHA512Context *hmacsha512ctxp,
- size_t size,
- const uint8_t *in) {
- osalDbgCheck((cryp != NULL) && (hmacsha512ctxp != NULL) && (in != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE
- return cry_lld_HMACSHA512_update(cryp, hmacsha512ctxp, size, in);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_HMACSHA512_update(cryp, hmacsha512ctxp, size, in);
- #else
- (void)cryp;
- (void)hmacsha512ctxp;
- (void)size;
- (void)in;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- /**
- * @brief Hash finalization using HMAC.
- *
- * @param[in] cryp pointer to the @p CRYDriver object
- * @param[in] hmacsha512ctxp pointer to a HMAC_SHA512 context
- * @param[out] out 512 bits output buffer
- * @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_OP_FAILURE if the operation failed, implementation
- * dependent.
- *
- * @api
- */
- cryerror_t cryHMACSHA512Final(CRYDriver *cryp,
- HMACSHA512Context *hmacsha512ctxp,
- uint8_t *out) {
- osalDbgCheck((cryp != NULL) && (hmacsha512ctxp != NULL) && (out != NULL));
- osalDbgAssert(cryp->state == CRY_READY, "not ready");
- #if CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE
- return cry_lld_HMACSHA512_final(cryp, hmacsha512ctxp, out);
- #elif HAL_CRY_USE_FALLBACK == TRUE
- return cry_fallback_HMACSHA512_final(cryp, hmacsha512ctxp, out);
- #else
- (void)cryp;
- (void)hmacsha512ctxp;
- (void)out;
- return CRY_ERR_INV_ALGO;
- #endif
- }
- #endif /* HAL_USE_CRY == TRUE */
- /** @} */
|