| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291 |
- /***************************************************************************//**
- * @file
- * @brief Advanced Encryption Standard (AES) accelerator peripheral API
- * @author Energy Micro AS
- * @version 3.0.0
- *******************************************************************************
- * @section License
- * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
- *******************************************************************************
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- *
- * 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 "em_aes.h"
- #include "em_assert.h"
- #if defined(AES_COUNT) && (AES_COUNT > 0)
- /***************************************************************************//**
- * @addtogroup EM_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().
- * If this argument is null, the key will not be loaded, as it is assumed
- * the key has been loaded into KEYHA previously.
- *
- * @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 *_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;
- if (key)
- {
- const uint32_t *_key = (const uint32_t *)key;
- /* 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.
- * If this argument is null, the key will not be loaded, as it is assumed
- * the key has been loaded into KEYHA previously.
- *
- * @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;
- 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;
- if (key)
- {
- const uint32_t *_key = (const uint32_t *)key;
- /* 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 EM_Library) */
- #endif /* defined(AES_COUNT) && (AES_COUNT > 0) */
|