1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699 |
- /**
- ******************************************************************************
- * @file stm32f4xx_cryp_aes.c
- * @author MCD Application Team
- * @version V1.8.1
- * @date 27-January-2022
- * @brief This file provides high level functions to encrypt and decrypt an
- * input message using AES in ECB/CBC/CTR/GCM/CCM modes.
- * It uses the stm32f4xx_cryp.c/.h drivers to access the STM32F4xx CRYP
- * peripheral.
- * AES-ECB/CBC/CTR/GCM/CCM modes are available on STM32F437x Devices.
- * For STM32F41xx Devices, only AES-ECB/CBC/CTR modes are available.
- *
- @verbatim
- ===================================================================
- ##### How to use this driver #####
- ===================================================================
- [..]
- (#) Enable The CRYP controller clock using
- RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
-
- (#) Encrypt and decrypt using AES in ECB Mode using CRYP_AES_ECB() function.
-
- (#) Encrypt and decrypt using AES in CBC Mode using CRYP_AES_CBC() function.
-
- (#) Encrypt and decrypt using AES in CTR Mode using CRYP_AES_CTR() function.
- (#) Encrypt and decrypt using AES in GCM Mode using CRYP_AES_GCM() function.
-
- (#) Encrypt and decrypt using AES in CCM Mode using CRYP_AES_CCM() function.
-
- @endverbatim
- *
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2016 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f4xx_cryp.h"
- /** @addtogroup STM32F4xx_StdPeriph_Driver
- * @{
- */
- /** @defgroup CRYP
- * @brief CRYP driver modules
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define AESBUSY_TIMEOUT ((uint32_t) 0x00010000)
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /** @defgroup CRYP_Private_Functions
- * @{
- */
- /** @defgroup CRYP_Group6 High Level AES functions
- * @brief High Level AES functions
- *
- @verbatim
- ===============================================================================
- ##### High Level AES functions #####
- ===============================================================================
- @endverbatim
- * @{
- */
- /**
- * @brief Encrypt and decrypt using AES in ECB Mode
- * @param Mode: encryption or decryption Mode.
- * This parameter can be one of the following values:
- * @arg MODE_ENCRYPT: Encryption
- * @arg MODE_DECRYPT: Decryption
- * @param Key: Key used for AES algorithm.
- * @param Keysize: length of the Key, must be a 128, 192 or 256.
- * @param Input: pointer to the Input buffer.
- * @param Ilength: length of the Input buffer, must be a multiple of 16.
- * @param Output: pointer to the returned buffer.
- * @retval An ErrorStatus enumeration value:
- * - SUCCESS: Operation done
- * - ERROR: Operation failed
- */
- ErrorStatus CRYP_AES_ECB(uint8_t Mode, uint8_t* Key, uint16_t Keysize,
- uint8_t* Input, uint32_t Ilength, uint8_t* Output)
- {
- CRYP_InitTypeDef AES_CRYP_InitStructure;
- CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
- __IO uint32_t counter = 0;
- uint32_t busystatus = 0;
- ErrorStatus status = SUCCESS;
- uint32_t keyaddr = (uint32_t)Key;
- uint32_t inputaddr = (uint32_t)Input;
- uint32_t outputaddr = (uint32_t)Output;
- uint32_t i = 0;
- /* Crypto structures initialisation*/
- CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
- switch(Keysize)
- {
- case 128:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 192:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 256:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
- AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- default:
- break;
- }
- /*------------------ AES Decryption ------------------*/
- if(Mode == MODE_DECRYPT) /* AES decryption */
- {
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
- /* Crypto Init for Key preparation for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;
- CRYP_Init(&AES_CRYP_InitStructure);
- /* Key Initialisation */
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
- /* wait until the Busy flag is RESET */
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Crypto Init for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- }
- }
- /*------------------ AES Encryption ------------------*/
- else /* AES encryption */
- {
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
- /* Crypto Init for Encryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
- }
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
- CRYP_Init(&AES_CRYP_InitStructure);
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(i=0; ((i<Ilength) && (status != ERROR)); i+=16)
- {
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Read the Output block from the Output FIFO */
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- }
- }
- /* Disable Crypto */
- CRYP_Cmd(DISABLE);
- return status;
- }
- /**
- * @brief Encrypt and decrypt using AES in CBC Mode
- * @param Mode: encryption or decryption Mode.
- * This parameter can be one of the following values:
- * @arg MODE_ENCRYPT: Encryption
- * @arg MODE_DECRYPT: Decryption
- * @param InitVectors: Initialisation Vectors used for AES algorithm.
- * @param Key: Key used for AES algorithm.
- * @param Keysize: length of the Key, must be a 128, 192 or 256.
- * @param Input: pointer to the Input buffer.
- * @param Ilength: length of the Input buffer, must be a multiple of 16.
- * @param Output: pointer to the returned buffer.
- * @retval An ErrorStatus enumeration value:
- * - SUCCESS: Operation done
- * - ERROR: Operation failed
- */
- ErrorStatus CRYP_AES_CBC(uint8_t Mode, uint8_t InitVectors[16], uint8_t *Key,
- uint16_t Keysize, uint8_t *Input, uint32_t Ilength,
- uint8_t *Output)
- {
- CRYP_InitTypeDef AES_CRYP_InitStructure;
- CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
- CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
- __IO uint32_t counter = 0;
- uint32_t busystatus = 0;
- ErrorStatus status = SUCCESS;
- uint32_t keyaddr = (uint32_t)Key;
- uint32_t inputaddr = (uint32_t)Input;
- uint32_t outputaddr = (uint32_t)Output;
- uint32_t ivaddr = (uint32_t)InitVectors;
- uint32_t i = 0;
- /* Crypto structures initialisation*/
- CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
- switch(Keysize)
- {
- case 128:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 192:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 256:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
- AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- default:
- break;
- }
- /* CRYP Initialization Vectors */
- AES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV1Left = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV1Right= __REV(*(uint32_t*)(ivaddr));
- /*------------------ AES Decryption ------------------*/
- if(Mode == MODE_DECRYPT) /* AES decryption */
- {
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
- /* Crypto Init for Key preparation for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;
- CRYP_Init(&AES_CRYP_InitStructure);
- /* Key Initialisation */
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
- /* wait until the Busy flag is RESET */
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Crypto Init for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- }
- }
- /*------------------ AES Encryption ------------------*/
- else /* AES encryption */
- {
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
- /* Crypto Init for Encryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
- }
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
- CRYP_Init(&AES_CRYP_InitStructure);
- /* CRYP Initialization Vectors */
- CRYP_IVInit(&AES_CRYP_IVInitStructure);
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(i=0; ((i<Ilength) && (status != ERROR)); i+=16)
- {
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Read the Output block from the Output FIFO */
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- }
- }
- /* Disable Crypto */
- CRYP_Cmd(DISABLE);
- return status;
- }
- /**
- * @brief Encrypt and decrypt using AES in CTR Mode
- * @param Mode: encryption or decryption Mode.
- * This parameter can be one of the following values:
- * @arg MODE_ENCRYPT: Encryption
- * @arg MODE_DECRYPT: Decryption
- * @param InitVectors: Initialisation Vectors used for AES algorithm.
- * @param Key: Key used for AES algorithm.
- * @param Keysize: length of the Key, must be a 128, 192 or 256.
- * @param Input: pointer to the Input buffer.
- * @param Ilength: length of the Input buffer, must be a multiple of 16.
- * @param Output: pointer to the returned buffer.
- * @retval An ErrorStatus enumeration value:
- * - SUCCESS: Operation done
- * - ERROR: Operation failed
- */
- ErrorStatus CRYP_AES_CTR(uint8_t Mode, uint8_t InitVectors[16], uint8_t *Key,
- uint16_t Keysize, uint8_t *Input, uint32_t Ilength,
- uint8_t *Output)
- {
- CRYP_InitTypeDef AES_CRYP_InitStructure;
- CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
- CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
- __IO uint32_t counter = 0;
- uint32_t busystatus = 0;
- ErrorStatus status = SUCCESS;
- uint32_t keyaddr = (uint32_t)Key;
- uint32_t inputaddr = (uint32_t)Input;
- uint32_t outputaddr = (uint32_t)Output;
- uint32_t ivaddr = (uint32_t)InitVectors;
- uint32_t i = 0;
- /* Crypto structures initialisation*/
- CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
- switch(Keysize)
- {
- case 128:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 192:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 256:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
- AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- default:
- break;
- }
- /* CRYP Initialization Vectors */
- AES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV1Left = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV1Right= __REV(*(uint32_t*)(ivaddr));
- /* Key Initialisation */
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
- /*------------------ AES Decryption ------------------*/
- if(Mode == MODE_DECRYPT) /* AES decryption */
- {
- /* Crypto Init for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- }
- /*------------------ AES Encryption ------------------*/
- else /* AES encryption */
- {
- /* Crypto Init for Encryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
- }
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CTR;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
- CRYP_Init(&AES_CRYP_InitStructure);
- /* CRYP Initialization Vectors */
- CRYP_IVInit(&AES_CRYP_IVInitStructure);
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(i=0; ((i<Ilength) && (status != ERROR)); i+=16)
- {
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Read the Output block from the Output FIFO */
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- }
- }
- /* Disable Crypto */
- CRYP_Cmd(DISABLE);
- return status;
- }
- /**
- * @brief Encrypt and decrypt using AES in GCM Mode. The GCM and CCM modes
- * are available only on STM32F437x Devices.
- * @param Mode: encryption or decryption Mode.
- * This parameter can be one of the following values:
- * @arg MODE_ENCRYPT: Encryption
- * @arg MODE_DECRYPT: Decryption
- * @param InitVectors: Initialisation Vectors used for AES algorithm.
- * @param Key: Key used for AES algorithm.
- * @param Keysize: length of the Key, must be a 128, 192 or 256.
- * @param Input: pointer to the Input buffer.
- * @param Ilength: length of the Input buffer in bytes, must be a multiple of 16.
- * @param Header: pointer to the header buffer.
- * @param Hlength: length of the header buffer in bytes, must be a multiple of 16.
- * @param Output: pointer to the returned buffer.
- * @param AuthTAG: pointer to the authentication TAG buffer.
- * @retval An ErrorStatus enumeration value:
- * - SUCCESS: Operation done
- * - ERROR: Operation failed
- */
- ErrorStatus CRYP_AES_GCM(uint8_t Mode, uint8_t InitVectors[16],
- uint8_t *Key, uint16_t Keysize,
- uint8_t *Input, uint32_t ILength,
- uint8_t *Header, uint32_t HLength,
- uint8_t *Output, uint8_t *AuthTAG)
- {
- CRYP_InitTypeDef AES_CRYP_InitStructure;
- CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
- CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
- __IO uint32_t counter = 0;
- uint32_t busystatus = 0;
- ErrorStatus status = SUCCESS;
- uint32_t keyaddr = (uint32_t)Key;
- uint32_t inputaddr = (uint32_t)Input;
- uint32_t outputaddr = (uint32_t)Output;
- uint32_t ivaddr = (uint32_t)InitVectors;
- uint32_t headeraddr = (uint32_t)Header;
- uint32_t tagaddr = (uint32_t)AuthTAG;
- uint64_t headerlength = HLength * 8;/* header length in bits */
- uint64_t inputlength = ILength * 8;/* input length in bits */
- uint32_t loopcounter = 0;
- /* Crypto structures initialisation*/
- CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
- switch(Keysize)
- {
- case 128:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 192:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 256:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
- AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- default:
- break;
- }
-
- /* CRYP Initialization Vectors */
- AES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV1Left = __REV(*(uint32_t*)(ivaddr));
- ivaddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV1Right= __REV(*(uint32_t*)(ivaddr));
-
- /*------------------ AES Encryption ------------------*/
- if(Mode == MODE_ENCRYPT) /* AES encryption */
- {
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- /* Key Initialisation */
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
-
- /* CRYP Initialization Vectors */
- CRYP_IVInit(&AES_CRYP_IVInitStructure);
-
- /* Crypto Init for Key preparation for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_GCM;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
- CRYP_Init(&AES_CRYP_InitStructure);
-
- /***************************** Init phase *********************************/
- /* Select init phase */
- CRYP_PhaseConfig(CRYP_Phase_Init);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* Wait for CRYPEN bit to be 0 */
- while(CRYP_GetCmdStatus() == ENABLE)
- {
- }
-
- /***************************** header phase *******************************/
- if(HLength != 0)
- {
- /* Select header phase */
- CRYP_PhaseConfig(CRYP_Phase_Header);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(loopcounter = 0; (loopcounter < HLength); loopcounter+=16)
- {
- /* Wait until the IFEM flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_IFEM) == RESET)
- {
- }
-
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- }
-
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- }
-
- /**************************** payload phase *******************************/
- if(ILength != 0)
- {
- /* Select payload phase */
- CRYP_PhaseConfig(CRYP_Phase_Payload);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(loopcounter = 0; ((loopcounter < ILength) && (status != ERROR)); loopcounter+=16)
- {
- /* Wait until the IFEM flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_IFEM) == RESET)
- {
- }
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
-
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Wait until the OFNE flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_OFNE) == RESET)
- {
- }
-
- /* Read the Output block from the Output FIFO */
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- }
- }
- }
-
- /***************************** final phase ********************************/
- /* Select final phase */
- CRYP_PhaseConfig(CRYP_Phase_Final);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- /* Write number of bits concatenated with header in the IN FIFO */
- CRYP_DataIn(__REV(headerlength>>32));
- CRYP_DataIn(__REV(headerlength));
- CRYP_DataIn(__REV(inputlength>>32));
- CRYP_DataIn(__REV(inputlength));
- /* Wait until the OFNE flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_OFNE) == RESET)
- {
- }
-
- tagaddr = (uint32_t)AuthTAG;
- /* Read the Auth TAG in the IN FIFO */
- *(uint32_t*)(tagaddr) = CRYP_DataOut();
- tagaddr+=4;
- *(uint32_t*)(tagaddr) = CRYP_DataOut();
- tagaddr+=4;
- *(uint32_t*)(tagaddr) = CRYP_DataOut();
- tagaddr+=4;
- *(uint32_t*)(tagaddr) = CRYP_DataOut();
- tagaddr+=4;
- }
- /*------------------ AES Decryption ------------------*/
- else /* AES decryption */
- {
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- /* Key Initialisation */
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
-
- /* CRYP Initialization Vectors */
- CRYP_IVInit(&AES_CRYP_IVInitStructure);
-
- /* Crypto Init for Key preparation for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_GCM;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
- CRYP_Init(&AES_CRYP_InitStructure);
-
- /***************************** Init phase *********************************/
- /* Select init phase */
- CRYP_PhaseConfig(CRYP_Phase_Init);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* Wait for CRYPEN bit to be 0 */
- while(CRYP_GetCmdStatus() == ENABLE)
- {
- }
-
- /***************************** header phase *******************************/
- if(HLength != 0)
- {
- /* Select header phase */
- CRYP_PhaseConfig(CRYP_Phase_Header);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(loopcounter = 0; (loopcounter < HLength); loopcounter+=16)
- {
- /* Wait until the IFEM flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_IFEM) == RESET)
- {
- }
-
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- }
-
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- }
-
- /**************************** payload phase *******************************/
- if(ILength != 0)
- {
- /* Select payload phase */
- CRYP_PhaseConfig(CRYP_Phase_Payload);
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(loopcounter = 0; ((loopcounter < ILength) && (status != ERROR)); loopcounter+=16)
- {
- /* Wait until the IFEM flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_IFEM) == RESET)
- {
- }
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
-
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Wait until the OFNE flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_OFNE) == RESET)
- {
- }
-
- /* Read the Output block from the Output FIFO */
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- }
- }
- }
-
- /***************************** final phase ********************************/
- /* Select final phase */
- CRYP_PhaseConfig(CRYP_Phase_Final);
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- /* Write number of bits concatenated with header in the IN FIFO */
- CRYP_DataIn(__REV(headerlength>>32));
- CRYP_DataIn(__REV(headerlength));
- CRYP_DataIn(__REV(inputlength>>32));
- CRYP_DataIn(__REV(inputlength));
- /* Wait until the OFNE flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_OFNE) == RESET)
- {
- }
-
- tagaddr = (uint32_t)AuthTAG;
- /* Read the Auth TAG in the IN FIFO */
- *(uint32_t*)(tagaddr) = CRYP_DataOut();
- tagaddr+=4;
- *(uint32_t*)(tagaddr) = CRYP_DataOut();
- tagaddr+=4;
- *(uint32_t*)(tagaddr) = CRYP_DataOut();
- tagaddr+=4;
- *(uint32_t*)(tagaddr) = CRYP_DataOut();
- tagaddr+=4;
- }
- /* Disable Crypto */
- CRYP_Cmd(DISABLE);
- return status;
- }
- /**
- * @brief Encrypt and decrypt using AES in CCM Mode. The GCM and CCM modes
- * are available only on STM32F437x Devices.
- * @param Mode: encryption or decryption Mode.
- * This parameter can be one of the following values:
- * @arg MODE_ENCRYPT: Encryption
- * @arg MODE_DECRYPT: Decryption
- * @param Nonce: the nonce used for AES algorithm. It shall be unique for each processing.
- * @param Key: Key used for AES algorithm.
- * @param Keysize: length of the Key, must be a 128, 192 or 256.
- * @param Input: pointer to the Input buffer.
- * @param Ilength: length of the Input buffer in bytes, must be a multiple of 16.
- * @param Header: pointer to the header buffer.
- * @param Hlength: length of the header buffer in bytes.
- * @param HBuffer: pointer to temporary buffer used to append the header
- * HBuffer size must be equal to Hlength + 21
- * @param Output: pointer to the returned buffer.
- * @param AuthTAG: pointer to the authentication TAG buffer.
- * @param TAGSize: the size of the TAG (called also MAC).
- * @retval An ErrorStatus enumeration value:
- * - SUCCESS: Operation done
- * - ERROR: Operation failed
- */
- ErrorStatus CRYP_AES_CCM(uint8_t Mode,
- uint8_t* Nonce, uint32_t NonceSize,
- uint8_t *Key, uint16_t Keysize,
- uint8_t *Input, uint32_t ILength,
- uint8_t *Header, uint32_t HLength, uint8_t *HBuffer,
- uint8_t *Output,
- uint8_t *AuthTAG, uint32_t TAGSize)
- {
- CRYP_InitTypeDef AES_CRYP_InitStructure;
- CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
- CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
- __IO uint32_t counter = 0;
- uint32_t busystatus = 0;
- ErrorStatus status = SUCCESS;
- uint32_t keyaddr = (uint32_t)Key;
- uint32_t inputaddr = (uint32_t)Input;
- uint32_t outputaddr = (uint32_t)Output;
- uint32_t headeraddr = (uint32_t)Header;
- uint32_t tagaddr = (uint32_t)AuthTAG;
- uint32_t headersize = HLength;
- uint32_t loopcounter = 0;
- uint32_t bufferidx = 0;
- uint8_t blockb0[16] = {0};/* Block B0 */
- uint8_t ctr[16] = {0}; /* Counter */
- uint32_t temptag[4] = {0}; /* temporary TAG (MAC) */
- uint32_t ctraddr = (uint32_t)ctr;
- uint32_t b0addr = (uint32_t)blockb0;
-
- /************************ Formatting the header block ***********************/
- if(headersize != 0)
- {
- /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
- if(headersize < 65280)
- {
- HBuffer[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
- HBuffer[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
- headersize += 2;
- }
- else
- {
- /* header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
- HBuffer[bufferidx++] = 0xFF;
- HBuffer[bufferidx++] = 0xFE;
- HBuffer[bufferidx++] = headersize & 0xff000000;
- HBuffer[bufferidx++] = headersize & 0x00ff0000;
- HBuffer[bufferidx++] = headersize & 0x0000ff00;
- HBuffer[bufferidx++] = headersize & 0x000000ff;
- headersize += 6;
- }
- /* Copy the header buffer in internal buffer "HBuffer" */
- for(loopcounter = 0; loopcounter < headersize; loopcounter++)
- {
- HBuffer[bufferidx++] = Header[loopcounter];
- }
- /* Check if the header size is modulo 16 */
- if ((headersize % 16) != 0)
- {
- /* Padd the header buffer with 0s till the HBuffer length is modulo 16 */
- for(loopcounter = headersize; loopcounter <= ((headersize/16) + 1) * 16; loopcounter++)
- {
- HBuffer[loopcounter] = 0;
- }
- /* Set the header size to modulo 16 */
- headersize = ((headersize/16) + 1) * 16;
- }
- /* set the pointer headeraddr to HBuffer */
- headeraddr = (uint32_t)HBuffer;
- }
- /************************* Formatting the block B0 **************************/
- if(headersize != 0)
- {
- blockb0[0] = 0x40;
- }
- /* Flags byte */
- blockb0[0] |= 0u | (((( (uint8_t) TAGSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - NonceSize) - 1) & 0x07);
-
- for (loopcounter = 0; loopcounter < NonceSize; loopcounter++)
- {
- blockb0[loopcounter+1] = Nonce[loopcounter];
- }
- for ( ; loopcounter < 13; loopcounter++)
- {
- blockb0[loopcounter+1] = 0;
- }
-
- blockb0[14] = ((ILength >> 8) & 0xFF);
- blockb0[15] = (ILength & 0xFF);
-
- /************************* Formatting the initial counter *******************/
- /* Byte 0:
- Bits 7 and 6 are reserved and shall be set to 0
- Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter blocks
- are distinct from B0
- Bits 0, 1, and 2 contain the same encoding of q as in B0
- */
- ctr[0] = blockb0[0] & 0x07;
- /* byte 1 to NonceSize is the IV (Nonce) */
- for(loopcounter = 1; loopcounter < NonceSize + 1; loopcounter++)
- {
- ctr[loopcounter] = blockb0[loopcounter];
- }
- /* Set the LSB to 1 */
- ctr[15] |= 0x01;
-
- /* Crypto structures initialisation*/
- CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
-
- switch(Keysize)
- {
- case 128:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 192:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- case 256:
- AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
- AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
- keyaddr+=4;
- AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
- break;
- default:
- break;
- }
-
- /* CRYP Initialization Vectors */
- AES_CRYP_IVInitStructure.CRYP_IV0Left = (__REV(*(uint32_t*)(ctraddr)));
- ctraddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV0Right= (__REV(*(uint32_t*)(ctraddr)));
- ctraddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV1Left = (__REV(*(uint32_t*)(ctraddr)));
- ctraddr+=4;
- AES_CRYP_IVInitStructure.CRYP_IV1Right= (__REV(*(uint32_t*)(ctraddr)));
-
- /*------------------ AES Encryption ------------------*/
- if(Mode == MODE_ENCRYPT) /* AES encryption */
- {
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- /* Key Initialisation */
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
-
- /* CRYP Initialization Vectors */
- CRYP_IVInit(&AES_CRYP_IVInitStructure);
-
- /* Crypto Init for Key preparation for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CCM;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
- CRYP_Init(&AES_CRYP_InitStructure);
-
- /***************************** Init phase *********************************/
- /* Select init phase */
- CRYP_PhaseConfig(CRYP_Phase_Init);
-
- b0addr = (uint32_t)blockb0;
- /* Write the blockb0 block in the IN FIFO */
- CRYP_DataIn((*(uint32_t*)(b0addr)));
- b0addr+=4;
- CRYP_DataIn((*(uint32_t*)(b0addr)));
- b0addr+=4;
- CRYP_DataIn((*(uint32_t*)(b0addr)));
- b0addr+=4;
- CRYP_DataIn((*(uint32_t*)(b0addr)));
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* Wait for CRYPEN bit to be 0 */
- while(CRYP_GetCmdStatus() == ENABLE)
- {
- }
- /***************************** header phase *******************************/
- if(headersize != 0)
- {
- /* Select header phase */
- CRYP_PhaseConfig(CRYP_Phase_Header);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(loopcounter = 0; (loopcounter < headersize); loopcounter+=16)
- {
- /* Wait until the IFEM flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_IFEM) == RESET)
- {
- }
-
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- }
-
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- }
-
- /**************************** payload phase *******************************/
- if(ILength != 0)
- {
- /* Select payload phase */
- CRYP_PhaseConfig(CRYP_Phase_Payload);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(loopcounter = 0; ((loopcounter < ILength) && (status != ERROR)); loopcounter+=16)
- {
- /* Wait until the IFEM flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_IFEM) == RESET)
- {
- }
-
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
-
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Wait until the OFNE flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_OFNE) == RESET)
- {
- }
-
- /* Read the Output block from the Output FIFO */
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- }
- }
- }
-
- /***************************** final phase ********************************/
- /* Select final phase */
- CRYP_PhaseConfig(CRYP_Phase_Final);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- ctraddr = (uint32_t)ctr;
- /* Write the counter block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(ctraddr));
- ctraddr+=4;
- CRYP_DataIn(*(uint32_t*)(ctraddr));
- ctraddr+=4;
- CRYP_DataIn(*(uint32_t*)(ctraddr));
- ctraddr+=4;
- /* Reset bit 0 (after 8-bit swap) is equivalent to reset bit 24 (before 8-bit swap) */
- CRYP_DataIn(*(uint32_t*)(ctraddr) & 0xfeffffff);
-
- /* Wait until the OFNE flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_OFNE) == RESET)
- {
- }
-
- /* Read the Auth TAG in the IN FIFO */
- temptag[0] = CRYP_DataOut();
- temptag[1] = CRYP_DataOut();
- temptag[2] = CRYP_DataOut();
- temptag[3] = CRYP_DataOut();
- }
- /*------------------ AES Decryption ------------------*/
- else /* AES decryption */
- {
- /* Flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- /* Key Initialisation */
- CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
-
- /* CRYP Initialization Vectors */
- CRYP_IVInit(&AES_CRYP_IVInitStructure);
-
- /* Crypto Init for Key preparation for decryption process */
- AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CCM;
- AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
- CRYP_Init(&AES_CRYP_InitStructure);
-
- /***************************** Init phase *********************************/
- /* Select init phase */
- CRYP_PhaseConfig(CRYP_Phase_Init);
-
- b0addr = (uint32_t)blockb0;
- /* Write the blockb0 block in the IN FIFO */
- CRYP_DataIn((*(uint32_t*)(b0addr)));
- b0addr+=4;
- CRYP_DataIn((*(uint32_t*)(b0addr)));
- b0addr+=4;
- CRYP_DataIn((*(uint32_t*)(b0addr)));
- b0addr+=4;
- CRYP_DataIn((*(uint32_t*)(b0addr)));
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* Wait for CRYPEN bit to be 0 */
- while(CRYP_GetCmdStatus() == ENABLE)
- {
- }
-
- /***************************** header phase *******************************/
- if(headersize != 0)
- {
- /* Select header phase */
- CRYP_PhaseConfig(CRYP_Phase_Header);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(loopcounter = 0; (loopcounter < headersize); loopcounter+=16)
- {
- /* Wait until the IFEM flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_IFEM) == RESET)
- {
- }
-
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- CRYP_DataIn(*(uint32_t*)(headeraddr));
- headeraddr+=4;
- }
-
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- }
-
- /**************************** payload phase *******************************/
- if(ILength != 0)
- {
- /* Select payload phase */
- CRYP_PhaseConfig(CRYP_Phase_Payload);
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- for(loopcounter = 0; ((loopcounter < ILength) && (status != ERROR)); loopcounter+=16)
- {
- /* Wait until the IFEM flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_IFEM) == RESET)
- {
- }
-
- /* Write the Input block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
- CRYP_DataIn(*(uint32_t*)(inputaddr));
- inputaddr+=4;
-
- /* Wait until the complete message has been processed */
- counter = 0;
- do
- {
- busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
- counter++;
- }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
- if (busystatus != RESET)
- {
- status = ERROR;
- }
- else
- {
- /* Wait until the OFNE flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_OFNE) == RESET)
- {
- }
-
- /* Read the Output block from the Output FIFO */
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- *(uint32_t*)(outputaddr) = CRYP_DataOut();
- outputaddr+=4;
- }
- }
- }
-
- /***************************** final phase ********************************/
- /* Select final phase */
- CRYP_PhaseConfig(CRYP_Phase_Final);
-
- /* Enable Crypto processor */
- CRYP_Cmd(ENABLE);
-
- if(CRYP_GetCmdStatus() == DISABLE)
- {
- /* The CRYP peripheral clock is not enabled or the device doesn't embed
- the CRYP peripheral (please check the device sales type. */
- return(ERROR);
- }
-
- ctraddr = (uint32_t)ctr;
- /* Write the counter block in the IN FIFO */
- CRYP_DataIn(*(uint32_t*)(ctraddr));
- ctraddr+=4;
- CRYP_DataIn(*(uint32_t*)(ctraddr));
- ctraddr+=4;
- CRYP_DataIn(*(uint32_t*)(ctraddr));
- ctraddr+=4;
- /* Reset bit 0 (after 8-bit swap) is equivalent to reset bit 24 (before 8-bit swap) */
- CRYP_DataIn(*(uint32_t*)(ctraddr) & 0xfeffffff);
-
- /* Wait until the OFNE flag is reset */
- while(CRYP_GetFlagStatus(CRYP_FLAG_OFNE) == RESET)
- {
- }
-
- /* Read the Authentification TAG (MAC) in the IN FIFO */
- temptag[0] = CRYP_DataOut();
- temptag[1] = CRYP_DataOut();
- temptag[2] = CRYP_DataOut();
- temptag[3] = CRYP_DataOut();
- }
-
- /* Copy temporary authentication TAG in user TAG buffer */
- for(loopcounter = 0; (loopcounter < TAGSize); loopcounter++)
- {
- /* Set the authentication TAG buffer */
- *((uint8_t*)tagaddr+loopcounter) = *((uint8_t*)temptag+loopcounter);
- }
-
- /* Disable Crypto */
- CRYP_Cmd(DISABLE);
- return status;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
|