| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277 |
- /***************************************************************************//**
- * @file
- * @brief Advanced Encryption Standard (AES) accelerator peripheral API
- * for EFM32
- * @author Energy Micro AS
- * @version 2.3.2
- *******************************************************************************
- * @section License
- * <b>(C) Copyright 2010 Energy Micro AS, http://www.energymicro.com</b>
- *******************************************************************************
- *
- * This source code is the property of Energy Micro AS. The source and compiled
- * code may only be used on Energy Micro "EFM32" microcontrollers.
- *
- * This copyright notice may not be removed from the source code nor changed.
- *
- * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
- * obligation to support this Software. Energy Micro AS is providing the
- * Software "AS IS", with no express or implied warranties of any kind,
- * including, but not limited to, any implied warranties of merchantability
- * or fitness for any particular purpose or warranties against infringement
- * of any proprietary rights of a third party.
- *
- * Energy Micro AS will not be liable for any consequential, incidental, or
- * special damages, or any other relief, or for any claim by any third party,
- * arising from your use of this Software.
- *
- ******************************************************************************/
- #include "efm32_aes.h"
- #include "efm32_assert.h"
- #if defined(AES_COUNT) && (AES_COUNT > 0)
- /***************************************************************************//**
- * @addtogroup EFM32_Library
- * @{
- ******************************************************************************/
- /***************************************************************************//**
- * @addtogroup AES
- * @brief Advanced Encryption Standard Accelerator (AES) Peripheral API for
- * EFM32
- * @details
- * This API is intended for use on EFM32 target devices, and the following
- * input/output notations should be noted:
- *
- * @li Input/output data (plaintext, ciphertext, key etc) are treated as
- * byte arrays, starting with most significant byte. Ie, 32 bytes of
- * plaintext (B0...B31) is located in memory in the same order, with B0 at
- * the lower address and B31 at the higher address.
- *
- * @li Byte arrays must always be a multiple of AES block size, ie a multiple
- * of 16. Padding, if required, is done at the end of the byte array.
- *
- * @li Byte arrays should be word (32 bit) aligned for performance
- * considerations, since the array is accessed with 32 bit access type.
- * The EFM32 supports unaligned accesses, but with a performance penalty.
- *
- * @li It is possible to specify the same output buffer as input buffer
- * as long as they point to the same address. In that case the provided input
- * buffer is replaced with the encrypted/decrypted output. Notice that the
- * buffers must be exactly overlapping. If partly overlapping, the
- * behaviour is undefined.
- *
- * It is up to the user to use a cipher mode according to its requirements
- * in order to not break security. Please refer to specific cipher mode
- * theory for details.
- *
- * References:
- * @li Wikipedia - Cipher modes, http://en.wikipedia.org/wiki/Cipher_modes
- *
- * @li Recommendation for Block Cipher Modes of Operation,
- * NIST Special Publication 800-38A, 2001 Edition,
- * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
- * @{
- ******************************************************************************/
- /*******************************************************************************
- ******************************* DEFINES ***********************************
- ******************************************************************************/
- /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
- #define AES_BLOCKSIZE 16
- /** @endcond */
- /*******************************************************************************
- ************************** GLOBAL FUNCTIONS *******************************
- ******************************************************************************/
- /***************************************************************************//**
- * @brief
- * Cipher-block chaining (CBC) cipher mode encryption/decryption, 128 bit key.
- *
- * @details
- * Encryption:
- * @verbatim
- * Plaintext Plaintext
- * | |
- * V V
- * InitVector ->XOR +-------------->XOR
- * | | |
- * V | V
- * +--------------+ | +--------------+
- * Key ->| Block cipher | | Key ->| Block cipher |
- * | encryption | | | encryption |
- * +--------------+ | +--------------+
- * |---------+ |
- * V V
- * Ciphertext Ciphertext
- * @endverbatim
- * Decryption:
- * @verbatim
- * Ciphertext Ciphertext
- * |----------+ |
- * V | V
- * +--------------+ | +--------------+
- * Key ->| Block cipher | | Key ->| Block cipher |
- * | decryption | | | decryption |
- * +--------------+ | +--------------+
- * | | |
- * V | V
- * InitVector ->XOR +-------------->XOR
- * | |
- * V V
- * Plaintext Plaintext
- * @endverbatim
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * When doing encryption, this is the 128 bit encryption key. When doing
- * decryption, this is the 128 bit decryption key. The decryption key may
- * be generated from the encryption key with AES_DecryptKey128().
- *
- * @param[in] iv
- * 128 bit initalization vector to use.
- *
- * @param[in] encrypt
- * Set to true to encrypt, false to decrypt.
- ******************************************************************************/
- void AES_CBC128(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- const uint8_t *iv,
- bool encrypt)
- {
- int i;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- const uint32_t *_iv = (const uint32_t *)iv;
- /* Need to buffer one block when decrypting in case 'out' replaces 'in' */
- uint32_t prev[4];
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- /* Number of blocks to process */
- len /= AES_BLOCKSIZE;
- /* Load key into high key for key buffer usage */
- for (i = 3; i >= 0; i--)
- {
- AES->KEYHA = __REV(_key[i]);
- }
- if (encrypt)
- {
- /* Enable encryption with auto start using XOR */
- AES->CTRL = AES_CTRL_KEYBUFEN | AES_CTRL_XORSTART;
- /* Load initialization vector, since writing to DATA, it will */
- /* not trigger encryption. */
- for (i = 3; i >= 0; i--)
- {
- AES->DATA = __REV(_iv[i]);
- }
- /* Encrypt data */
- while (len--)
- {
- /* Load data and trigger encryption */
- for (i = 3; i >= 0; i--)
- {
- AES->XORDATA = __REV(_in[i]);
- }
- _in += 4;
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA);
- }
- _out += 4;
- }
- }
- else
- {
- /* Select decryption mode */
- AES->CTRL = AES_CTRL_DECRYPT | AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
- /* Copy init vector to previous buffer to avoid special handling */
- for (i = 0; i < 4; i++)
- {
- prev[i] = _iv[i];
- }
- /* Decrypt data */
- while (len--)
- {
- /* Load data and trigger decryption */
- for (i = 3; i >= 0; i--)
- {
- AES->DATA = __REV(_in[i]);
- }
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* In order to avoid additional buffer, we use HW directly for XOR and buffer */
- /* (Writing to XORDATA will not trigger encoding, triggering enabled on DATA.) */
- for (i = 3; i >= 0; i--)
- {
- AES->XORDATA = __REV(prev[i]);
- prev[i] = _in[i];
- }
- _in += 4;
- /* Then fetch decrypted data, we have to do it in a separate loop */
- /* due to internal auto-shifting of words */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA);
- }
- _out += 4;
- }
- }
- }
- /***************************************************************************//**
- * @brief
- * Cipher-block chaining (CBC) cipher mode encryption/decryption, 256 bit key.
- *
- * @details
- * Please see AES_CBC128() for CBC figure.
- *
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * When doing encryption, this is the 256 bit encryption key. When doing
- * decryption, this is the 256 bit decryption key. The decryption key may
- * be generated from the encryption key with AES_DecryptKey256().
- *
- * @param[in] iv
- * 128 bit initalization vector to use.
- *
- * @param[in] encrypt
- * Set to true to encrypt, false to decrypt.
- ******************************************************************************/
- void AES_CBC256(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- const uint8_t *iv,
- bool encrypt)
- {
- int i;
- int j;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- const uint32_t *_iv = (const uint32_t *)iv;
- /* Need to buffer one block when decrypting in case output replaces input */
- uint32_t prev[4];
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- /* Number of blocks to process */
- len /= AES_BLOCKSIZE;
- if (encrypt)
- {
- /* Enable encryption with auto start using XOR */
- AES->CTRL = AES_CTRL_AES256 | AES_CTRL_XORSTART;
- /* Load initialization vector, since writing to DATA, it will */
- /* not trigger encryption. */
- for (i = 3; i >= 0; i--)
- {
- AES->DATA = __REV(_iv[i]);
- }
- /* Encrypt data */
- while (len--)
- {
- /* Load key and data and trigger encryption */
- for (i = 3, j = 7; i >= 0; i--, j--)
- {
- AES->KEYLA = __REV(_key[j]);
- AES->KEYHA = __REV(_key[i]);
- /* Write data last, since will trigger encryption on last iteration */
- AES->XORDATA = __REV(_in[i]);
- }
- _in += 4;
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA);
- }
- _out += 4;
- }
- }
- else
- {
- /* Select decryption mode */
- AES->CTRL = AES_CTRL_AES256 | AES_CTRL_DECRYPT | AES_CTRL_DATASTART;
- /* Copy init vector to previous buffer to avoid special handling */
- for (i = 0; i < 4; i++)
- {
- prev[i] = _iv[i];
- }
- /* Decrypt data */
- while (len--)
- {
- /* Load key and data and trigger decryption */
- for (i = 3, j = 7; i >= 0; i--, j--)
- {
- AES->KEYLA = __REV(_key[j]);
- AES->KEYHA = __REV(_key[i]);
- /* Write data last, since will trigger encryption on last iteration */
- AES->DATA = __REV(_in[i]);
- }
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* In order to avoid additional buffer, we use HW directly for XOR and buffer */
- for (i = 3; i >= 0; i--)
- {
- AES->XORDATA = __REV(prev[i]);
- prev[i] = _in[i];
- }
- _in += 4;
- /* Then fetch decrypted data, we have to do it in a separate loop */
- /* due to internal auto-shifting of words */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA);
- }
- _out += 4;
- }
- }
- }
- /***************************************************************************//**
- * @brief
- * Cipher feedback (CFB) cipher mode encryption/decryption, 128 bit key.
- *
- * @details
- * Encryption:
- * @verbatim
- * InitVector +----------------+
- * | | |
- * V | V
- * +--------------+ | +--------------+
- * Key ->| Block cipher | | Key ->| Block cipher |
- * | encryption | | | encryption |
- * +--------------+ | +--------------+
- * | | |
- * V | V
- * Plaintext ->XOR | Plaintext ->XOR
- * |---------+ |
- * V V
- * Ciphertext Ciphertext
- * @endverbatim
- * Decryption:
- * @verbatim
- * InitVector +----------------+
- * | | |
- * V | V
- * +--------------+ | +--------------+
- * Key ->| Block cipher | | Key ->| Block cipher |
- * | encryption | | | encryption |
- * +--------------+ | +--------------+
- * | | |
- * V | V
- * XOR<- Ciphertext XOR<- Ciphertext
- * | |
- * V V
- * Plaintext Plaintext
- * @endverbatim
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * 128 bit encryption key is used for both encryption and decryption modes.
- *
- * @param[in] iv
- * 128 bit initalization vector to use.
- *
- * @param[in] encrypt
- * Set to true to encrypt, false to decrypt.
- ******************************************************************************/
- void AES_CFB128(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- const uint8_t *iv,
- bool encrypt)
- {
- int i;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- const uint32_t *_iv = (const uint32_t *)iv;
- const uint32_t *data;
- uint32_t tmp[4];
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- /* Select encryption mode */
- AES->CTRL = AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
- /* Load key into high key for key buffer usage */
- for (i = 3; i >= 0; i--)
- {
- AES->KEYHA = __REV(_key[i]);
- }
- /* Encrypt/decrypt data */
- data = _iv;
- len /= AES_BLOCKSIZE;
- while (len--)
- {
- /* Load data and trigger encryption */
- for (i = 3; i >= 0; i--)
- {
- AES->DATA = __REV(data[i]);
- }
- /* Do some required processing before waiting for completion */
- if (encrypt)
- {
- data = _out;
- }
- else
- {
- /* Must copy current ciphertext block since it may be overwritten */
- for (i = 0; i < 4; i++)
- {
- tmp[i] = _in[i];
- }
- data = tmp;
- }
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted/decrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA) ^ _in[i];
- }
- _out += 4;
- _in += 4;
- }
- }
- /***************************************************************************//**
- * @brief
- * Cipher feedback (CFB) cipher mode encryption/decryption, 256 bit key.
- *
- * @details
- * Please see AES_CFB128() for CFB figure.
- *
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * 256 bit encryption key is used for both encryption and decryption modes.
- *
- * @param[in] iv
- * 128 bit initalization vector to use.
- *
- * @param[in] encrypt
- * Set to true to encrypt, false to decrypt.
- ******************************************************************************/
- void AES_CFB256(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- const uint8_t *iv,
- bool encrypt)
- {
- int i;
- int j;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- const uint32_t *_iv = (const uint32_t *)iv;
- const uint32_t *data;
- uint32_t tmp[4];
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- /* Select encryption mode */
- AES->CTRL = AES_CTRL_AES256 | AES_CTRL_DATASTART;
- /* Encrypt/decrypt data */
- data = _iv;
- len /= AES_BLOCKSIZE;
- while (len--)
- {
- /* Load key and block to be encrypted/decrypted */
- for (i = 3, j = 7; i >= 0; i--, j--)
- {
- AES->KEYLA = __REV(_key[j]);
- AES->KEYHA = __REV(_key[i]);
- /* Write data last, since will trigger encryption on last iteration */
- AES->DATA = __REV(data[i]);
- }
- /* Do some required processing before waiting for completion */
- if (encrypt)
- {
- data = _out;
- }
- else
- {
- /* Must copy current ciphertext block since it may be overwritten */
- for (i = 0; i < 4; i++)
- {
- tmp[i] = _in[i];
- }
- data = tmp;
- }
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted/decrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA) ^ _in[i];
- }
- _out += 4;
- _in += 4;
- }
- }
- /***************************************************************************//**
- * @brief
- * Counter (CTR) cipher mode encryption/decryption, 128 bit key.
- *
- * @details
- * Encryption:
- * @verbatim
- * Counter Counter
- * | |
- * V V
- * +--------------+ +--------------+
- * Key ->| Block cipher | Key ->| Block cipher |
- * | encryption | | encryption |
- * +--------------+ +--------------+
- * | |
- * Plaintext ->XOR Plaintext ->XOR
- * | |
- * V V
- * Ciphertext Ciphertext
- * @endverbatim
- * Decryption:
- * @verbatim
- * Counter Counter
- * | |
- * V V
- * +--------------+ +--------------+
- * Key ->| Block cipher | Key ->| Block cipher |
- * | encryption | | encryption |
- * +--------------+ +--------------+
- * | |
- * Ciphertext ->XOR Ciphertext ->XOR
- * | |
- * V V
- * Plaintext Plaintext
- * @endverbatim
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * 128 bit encryption key.
- *
- * @param[in,out] ctr
- * 128 bit initial counter value. The counter is updated after each AES
- * block encoding through use of @p ctrFunc.
- *
- * @param[in] ctrFunc
- * Function used to update counter value.
- ******************************************************************************/
- void AES_CTR128(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- uint8_t *ctr,
- AES_CtrFuncPtr_TypeDef ctrFunc)
- {
- int i;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- uint32_t *_ctr = (uint32_t *)ctr;
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- EFM_ASSERT(ctrFunc);
- /* Select encryption mode, with auto trigger */
- AES->CTRL = AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
- /* Load key into high key for key buffer usage */
- for (i = 3; i >= 0; i--)
- {
- AES->KEYHA = __REV(_key[i]);
- }
- /* Encrypt/decrypt data */
- len /= AES_BLOCKSIZE;
- while (len--)
- {
- /* Load ctr to be encrypted/decrypted */
- for (i = 3; i >= 0; i--)
- {
- AES->DATA = __REV(_ctr[i]);
- }
- /* Increment ctr for next use */
- ctrFunc(ctr);
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted/decrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA) ^ _in[i];
- }
- _out += 4;
- _in += 4;
- }
- }
- /***************************************************************************//**
- * @brief
- * Counter (CTR) cipher mode encryption/decryption, 256 bit key.
- *
- * @details
- * Please see AES_CTR128() for CTR figure.
- *
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * 256 bit encryption key.
- *
- * @param[in,out] ctr
- * 128 bit initial counter value. The counter is updated after each AES
- * block encoding through use of @p ctrFunc.
- *
- * @param[in] ctrFunc
- * Function used to update counter value.
- ******************************************************************************/
- void AES_CTR256(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- uint8_t *ctr,
- AES_CtrFuncPtr_TypeDef ctrFunc)
- {
- int i;
- int j;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- uint32_t *_ctr = (uint32_t *)ctr;
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- EFM_ASSERT(ctrFunc);
- /* Select encryption mode, with auto trigger */
- AES->CTRL = AES_CTRL_AES256 | AES_CTRL_DATASTART;
- /* Encrypt/decrypt data */
- len /= AES_BLOCKSIZE;
- while (len--)
- {
- /* Load key and block to be encrypted/decrypted */
- for (i = 3, j = 7; i >= 0; i--, j--)
- {
- AES->KEYLA = __REV(_key[j]);
- AES->KEYHA = __REV(_key[i]);
- /* Write data last, since will trigger encryption on last iteration */
- AES->DATA = __REV(_ctr[i]);
- }
- /* Increment ctr for next use */
- ctrFunc(ctr);
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted/decrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA) ^ _in[i];
- }
- _out += 4;
- _in += 4;
- }
- }
- /***************************************************************************//**
- * @brief
- * Update last 32 bits of 128 bit counter, by incrementing with 1.
- *
- * @details
- * Notice that no special consideration is given to possible wrap around. If
- * 32 least significant bits are 0xFFFFFFFF, they will be updated to 0x00000000,
- * ignoring overflow.
- *
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[in,out] ctr
- * Buffer holding 128 bit counter to be updated.
- ******************************************************************************/
- void AES_CTRUpdate32Bit(uint8_t *ctr)
- {
- uint32_t *_ctr = (uint32_t *)ctr;
- _ctr[3] = __REV(__REV(_ctr[3]) + 1);
- }
- /***************************************************************************//**
- * @brief
- * Generate 128 bit decryption key from 128 bit encryption key. The decryption
- * key is used for some cipher modes when decrypting.
- *
- * @details
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place 128 bit decryption key. Must be at least 16 bytes long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding 128 bit encryption key. Must be at least 16 bytes long.
- ******************************************************************************/
- void AES_DecryptKey128(uint8_t *out, const uint8_t *in)
- {
- int i;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- /* Load key */
- for (i = 3; i >= 0; i--)
- {
- AES->KEYLA = __REV(_in[i]);
- }
- /* Do dummy encryption to generate decrypt key */
- AES->CTRL = 0;
- AES_IntClear(AES_IF_DONE);
- AES->CMD = AES_CMD_START;
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save decryption key */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->KEYLA);
- }
- }
- /***************************************************************************//**
- * @brief
- * Generate 256 bit decryption key from 256 bit encryption key. The decryption
- * key is used for some cipher modes when decrypting.
- *
- * @details
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place 256 bit decryption key. Must be at least 32 bytes long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding 256 bit encryption key. Must be at least 32 bytes long.
- ******************************************************************************/
- void AES_DecryptKey256(uint8_t *out, const uint8_t *in)
- {
- int i;
- int j;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- /* Load key */
- for (i = 3, j = 7; i >= 0; i--, j--)
- {
- AES->KEYLA = __REV(_in[j]);
- AES->KEYHA = __REV(_in[i]);
- }
- /* Do dummy encryption to generate decrypt key */
- AES->CTRL = AES_CTRL_AES256;
- AES->CMD = AES_CMD_START;
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save decryption key */
- for (i = 3, j = 7; i >= 0; i--, j--)
- {
- _out[j] = __REV(AES->KEYLA);
- _out[i] = __REV(AES->KEYHA);
- }
- }
- /***************************************************************************//**
- * @brief
- * Electronic Codebook (ECB) cipher mode encryption/decryption, 128 bit key.
- *
- * @details
- * Encryption:
- * @verbatim
- * Plaintext Plaintext
- * | |
- * V V
- * +--------------+ +--------------+
- * Key ->| Block cipher | Key ->| Block cipher |
- * | encryption | | encryption |
- * +--------------+ +--------------+
- * | |
- * V V
- * Ciphertext Ciphertext
- * @endverbatim
- * Decryption:
- * @verbatim
- * Ciphertext Ciphertext
- * | |
- * V V
- * +--------------+ +--------------+
- * Key ->| Block cipher | Key ->| Block cipher |
- * | decryption | | decryption |
- * +--------------+ +--------------+
- * | |
- * V V
- * Plaintext Plaintext
- * @endverbatim
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * When doing encryption, this is the 128 bit encryption key. When doing
- * decryption, this is the 128 bit decryption key. The decryption key may
- * be generated from the encryption key with AES_DecryptKey128().
- *
- * @param[in] encrypt
- * Set to true to encrypt, false to decrypt.
- ******************************************************************************/
- void AES_ECB128(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- bool encrypt)
- {
- int i;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- /* Load key into high key for key buffer usage */
- for (i = 3; i >= 0; i--)
- {
- AES->KEYHA = __REV(_key[i]);
- }
- if (encrypt)
- {
- /* Select encryption mode */
- AES->CTRL = AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
- }
- else
- {
- /* Select decryption mode */
- AES->CTRL = AES_CTRL_DECRYPT | AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
- }
- /* Encrypt/decrypt data */
- len /= AES_BLOCKSIZE;
- while (len--)
- {
- /* Load block to be encrypted/decrypted */
- for (i = 3; i >= 0; i--)
- {
- AES->DATA = __REV(_in[i]);
- }
- _in += 4;
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted/decrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA);
- }
- _out += 4;
- }
- }
- /***************************************************************************//**
- * @brief
- * Electronic Codebook (ECB) cipher mode encryption/decryption, 256 bit key.
- *
- * @details
- * Please see AES_ECB128() for ECB figure.
- *
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * When doing encryption, this is the 256 bit encryption key. When doing
- * decryption, this is the 256 bit decryption key. The decryption key may
- * be generated from the encryption key with AES_DecryptKey256().
- *
- * @param[in] encrypt
- * Set to true to encrypt, false to decrypt.
- ******************************************************************************/
- void AES_ECB256(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- bool encrypt)
- {
- int i;
- int j;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- if (encrypt)
- {
- /* Select encryption mode */
- AES->CTRL = AES_CTRL_AES256 | AES_CTRL_DATASTART;
- }
- else
- {
- /* Select decryption mode */
- AES->CTRL = AES_CTRL_DECRYPT | AES_CTRL_AES256 | AES_CTRL_DATASTART;
- }
- /* Encrypt/decrypt data */
- len /= AES_BLOCKSIZE;
- while (len--)
- {
- /* Load key and block to be encrypted/decrypted */
- for (i = 3, j = 7; i >= 0; i--, j--)
- {
- AES->KEYLA = __REV(_key[j]);
- AES->KEYHA = __REV(_key[i]);
- /* Write data last, since will trigger encryption on last iteration */
- AES->DATA = __REV(_in[i]);
- }
- _in += 4;
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted/decrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA);
- }
- _out += 4;
- }
- }
- /***************************************************************************//**
- * @brief
- * Output feedback (OFB) cipher mode encryption/decryption, 128 bit key.
- *
- * @details
- * Encryption:
- * @verbatim
- * InitVector +----------------+
- * | | |
- * V | V
- * +--------------+ | +--------------+
- * Key ->| Block cipher | | Key ->| Block cipher |
- * | encryption | | | encryption |
- * +--------------+ | +--------------+
- * | | |
- * |---------+ |
- * V V
- * Plaintext ->XOR Plaintext ->XOR
- * | |
- * V V
- * Ciphertext Ciphertext
- * @endverbatim
- * Decryption:
- * @verbatim
- * InitVector +----------------+
- * | | |
- * V | V
- * +--------------+ | +--------------+
- * Key ->| Block cipher | | Key ->| Block cipher |
- * | encryption | | | encryption |
- * +--------------+ | +--------------+
- * | | |
- * |---------+ |
- * V V
- * Ciphertext ->XOR Ciphertext ->XOR
- * | |
- * V V
- * Plaintext Plaintext
- * @endverbatim
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * 128 bit encryption key.
- *
- * @param[in] iv
- * 128 bit initalization vector to use.
- ******************************************************************************/
- void AES_OFB128(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- const uint8_t *iv)
- {
- int i;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- const uint32_t *_iv = (const uint32_t *)iv;
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- /* Select encryption mode, trigger explicitly by command */
- AES->CTRL = AES_CTRL_KEYBUFEN;
- /* Load key into high key for key buffer usage */
- /* Load initialization vector */
- for (i = 3; i >= 0; i--)
- {
- AES->KEYHA = __REV(_key[i]);
- AES->DATA = __REV(_iv[i]);
- }
- /* Encrypt/decrypt data */
- len /= AES_BLOCKSIZE;
- while (len--)
- {
- AES->CMD = AES_CMD_START;
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted/decrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA) ^ _in[i];
- }
- _out += 4;
- _in += 4;
- }
- }
- /***************************************************************************//**
- * @brief
- * Output feedback (OFB) cipher mode encryption/decryption, 256 bit key.
- *
- * @details
- * Please see AES_OFB128() for OFB figure.
- *
- * Please refer to general comments on layout and byte ordering of parameters.
- *
- * @param[out] out
- * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
- * may be set equal to @p in, in which case the input buffer is overwritten.
- *
- * @param[in] in
- * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
- *
- * @param[in] len
- * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
- *
- * @param[in] key
- * 256 bit encryption key.
- *
- * @param[in] iv
- * 128 bit initalization vector to use.
- ******************************************************************************/
- void AES_OFB256(uint8_t *out,
- const uint8_t *in,
- unsigned int len,
- const uint8_t *key,
- const uint8_t *iv)
- {
- int i;
- int j;
- uint32_t *_out = (uint32_t *)out;
- const uint32_t *_in = (const uint32_t *)in;
- const uint32_t *_key = (const uint32_t *)key;
- const uint32_t *_iv = (const uint32_t *)iv;
- EFM_ASSERT(!(len % AES_BLOCKSIZE));
- /* Select encryption mode, trigger explicitly by command */
- AES->CTRL = AES_CTRL_AES256;
- /* Load initialization vector */
- for (i = 3; i >= 0; i--)
- {
- AES->DATA = __REV(_iv[i]);
- }
- /* Encrypt/decrypt data */
- len /= AES_BLOCKSIZE;
- while (len--)
- {
- /* Load key */
- for (i = 3, j = 7; i >= 0; i--, j--)
- {
- AES->KEYLA = __REV(_key[j]);
- AES->KEYHA = __REV(_key[i]);
- }
- AES->CMD = AES_CMD_START;
- /* Wait for completion */
- while (AES->STATUS & AES_STATUS_RUNNING)
- ;
- /* Save encrypted/decrypted data */
- for (i = 3; i >= 0; i--)
- {
- _out[i] = __REV(AES->DATA) ^ _in[i];
- }
- _out += 4;
- _in += 4;
- }
- }
- /** @} (end addtogroup AES) */
- /** @} (end addtogroup EFM32_Library) */
- #endif /* defined(AES_COUNT) && (AES_COUNT > 0) */
|