em_aes.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Advanced Encryption Standard (AES) accelerator peripheral API
  4. * @author Energy Micro AS
  5. * @version 3.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. *
  21. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  22. * obligation to support this Software. Energy Micro AS is providing the
  23. * Software "AS IS", with no express or implied warranties of any kind,
  24. * including, but not limited to, any implied warranties of merchantability
  25. * or fitness for any particular purpose or warranties against infringement
  26. * of any proprietary rights of a third party.
  27. *
  28. * Energy Micro AS will not be liable for any consequential, incidental, or
  29. * special damages, or any other relief, or for any claim by any third party,
  30. * arising from your use of this Software.
  31. *
  32. ******************************************************************************/
  33. #include "em_aes.h"
  34. #include "em_assert.h"
  35. #if defined(AES_COUNT) && (AES_COUNT > 0)
  36. /***************************************************************************//**
  37. * @addtogroup EM_Library
  38. * @{
  39. ******************************************************************************/
  40. /***************************************************************************//**
  41. * @addtogroup AES
  42. * @brief Advanced Encryption Standard Accelerator (AES) Peripheral API for
  43. * EFM32
  44. * @details
  45. * This API is intended for use on EFM32 target devices, and the following
  46. * input/output notations should be noted:
  47. *
  48. * @li Input/output data (plaintext, ciphertext, key etc) are treated as
  49. * byte arrays, starting with most significant byte. Ie, 32 bytes of
  50. * plaintext (B0...B31) is located in memory in the same order, with B0 at
  51. * the lower address and B31 at the higher address.
  52. *
  53. * @li Byte arrays must always be a multiple of AES block size, ie a multiple
  54. * of 16. Padding, if required, is done at the end of the byte array.
  55. *
  56. * @li Byte arrays should be word (32 bit) aligned for performance
  57. * considerations, since the array is accessed with 32 bit access type.
  58. * The EFM32 supports unaligned accesses, but with a performance penalty.
  59. *
  60. * @li It is possible to specify the same output buffer as input buffer
  61. * as long as they point to the same address. In that case the provided input
  62. * buffer is replaced with the encrypted/decrypted output. Notice that the
  63. * buffers must be exactly overlapping. If partly overlapping, the
  64. * behaviour is undefined.
  65. *
  66. * It is up to the user to use a cipher mode according to its requirements
  67. * in order to not break security. Please refer to specific cipher mode
  68. * theory for details.
  69. *
  70. * References:
  71. * @li Wikipedia - Cipher modes, http://en.wikipedia.org/wiki/Cipher_modes
  72. *
  73. * @li Recommendation for Block Cipher Modes of Operation,
  74. * NIST Special Publication 800-38A, 2001 Edition,
  75. * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  76. * @{
  77. ******************************************************************************/
  78. /*******************************************************************************
  79. ******************************* DEFINES ***********************************
  80. ******************************************************************************/
  81. /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
  82. #define AES_BLOCKSIZE 16
  83. /** @endcond */
  84. /*******************************************************************************
  85. ************************** GLOBAL FUNCTIONS *******************************
  86. ******************************************************************************/
  87. /***************************************************************************//**
  88. * @brief
  89. * Cipher-block chaining (CBC) cipher mode encryption/decryption, 128 bit key.
  90. *
  91. * @details
  92. * Encryption:
  93. * @verbatim
  94. * Plaintext Plaintext
  95. * | |
  96. * V V
  97. * InitVector ->XOR +-------------->XOR
  98. * | | |
  99. * V | V
  100. * +--------------+ | +--------------+
  101. * Key ->| Block cipher | | Key ->| Block cipher |
  102. * | encryption | | | encryption |
  103. * +--------------+ | +--------------+
  104. * |---------+ |
  105. * V V
  106. * Ciphertext Ciphertext
  107. * @endverbatim
  108. * Decryption:
  109. * @verbatim
  110. * Ciphertext Ciphertext
  111. * |----------+ |
  112. * V | V
  113. * +--------------+ | +--------------+
  114. * Key ->| Block cipher | | Key ->| Block cipher |
  115. * | decryption | | | decryption |
  116. * +--------------+ | +--------------+
  117. * | | |
  118. * V | V
  119. * InitVector ->XOR +-------------->XOR
  120. * | |
  121. * V V
  122. * Plaintext Plaintext
  123. * @endverbatim
  124. * Please refer to general comments on layout and byte ordering of parameters.
  125. *
  126. * @param[out] out
  127. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  128. * may be set equal to @p in, in which case the input buffer is overwritten.
  129. *
  130. * @param[in] in
  131. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  132. *
  133. * @param[in] len
  134. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  135. *
  136. * @param[in] key
  137. * When doing encryption, this is the 128 bit encryption key. When doing
  138. * decryption, this is the 128 bit decryption key. The decryption key may
  139. * be generated from the encryption key with AES_DecryptKey128().
  140. * If this argument is null, the key will not be loaded, as it is assumed
  141. * the key has been loaded into KEYHA previously.
  142. *
  143. * @param[in] iv
  144. * 128 bit initalization vector to use.
  145. *
  146. * @param[in] encrypt
  147. * Set to true to encrypt, false to decrypt.
  148. ******************************************************************************/
  149. void AES_CBC128(uint8_t *out,
  150. const uint8_t *in,
  151. unsigned int len,
  152. const uint8_t *key,
  153. const uint8_t *iv,
  154. bool encrypt)
  155. {
  156. int i;
  157. uint32_t *_out = (uint32_t *)out;
  158. const uint32_t *_in = (const uint32_t *)in;
  159. const uint32_t *_iv = (const uint32_t *)iv;
  160. /* Need to buffer one block when decrypting in case 'out' replaces 'in' */
  161. uint32_t prev[4];
  162. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  163. /* Number of blocks to process */
  164. len /= AES_BLOCKSIZE;
  165. if (key)
  166. {
  167. const uint32_t *_key = (const uint32_t *)key;
  168. /* Load key into high key for key buffer usage */
  169. for (i = 3; i >= 0; i--)
  170. {
  171. AES->KEYHA = __REV(_key[i]);
  172. }
  173. }
  174. if (encrypt)
  175. {
  176. /* Enable encryption with auto start using XOR */
  177. AES->CTRL = AES_CTRL_KEYBUFEN | AES_CTRL_XORSTART;
  178. /* Load initialization vector, since writing to DATA, it will */
  179. /* not trigger encryption. */
  180. for (i = 3; i >= 0; i--)
  181. {
  182. AES->DATA = __REV(_iv[i]);
  183. }
  184. /* Encrypt data */
  185. while (len--)
  186. {
  187. /* Load data and trigger encryption */
  188. for (i = 3; i >= 0; i--)
  189. {
  190. AES->XORDATA = __REV(_in[i]);
  191. }
  192. _in += 4;
  193. /* Wait for completion */
  194. while (AES->STATUS & AES_STATUS_RUNNING)
  195. ;
  196. /* Save encrypted data */
  197. for (i = 3; i >= 0; i--)
  198. {
  199. _out[i] = __REV(AES->DATA);
  200. }
  201. _out += 4;
  202. }
  203. }
  204. else
  205. {
  206. /* Select decryption mode */
  207. AES->CTRL = AES_CTRL_DECRYPT | AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
  208. /* Copy init vector to previous buffer to avoid special handling */
  209. for (i = 0; i < 4; i++)
  210. {
  211. prev[i] = _iv[i];
  212. }
  213. /* Decrypt data */
  214. while (len--)
  215. {
  216. /* Load data and trigger decryption */
  217. for (i = 3; i >= 0; i--)
  218. {
  219. AES->DATA = __REV(_in[i]);
  220. }
  221. /* Wait for completion */
  222. while (AES->STATUS & AES_STATUS_RUNNING)
  223. ;
  224. /* In order to avoid additional buffer, we use HW directly for XOR and buffer */
  225. /* (Writing to XORDATA will not trigger encoding, triggering enabled on DATA.) */
  226. for (i = 3; i >= 0; i--)
  227. {
  228. AES->XORDATA = __REV(prev[i]);
  229. prev[i] = _in[i];
  230. }
  231. _in += 4;
  232. /* Then fetch decrypted data, we have to do it in a separate loop */
  233. /* due to internal auto-shifting of words */
  234. for (i = 3; i >= 0; i--)
  235. {
  236. _out[i] = __REV(AES->DATA);
  237. }
  238. _out += 4;
  239. }
  240. }
  241. }
  242. /***************************************************************************//**
  243. * @brief
  244. * Cipher-block chaining (CBC) cipher mode encryption/decryption, 256 bit key.
  245. *
  246. * @details
  247. * Please see AES_CBC128() for CBC figure.
  248. *
  249. * Please refer to general comments on layout and byte ordering of parameters.
  250. *
  251. * @param[out] out
  252. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  253. * may be set equal to @p in, in which case the input buffer is overwritten.
  254. *
  255. * @param[in] in
  256. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  257. *
  258. * @param[in] len
  259. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  260. *
  261. * @param[in] key
  262. * When doing encryption, this is the 256 bit encryption key. When doing
  263. * decryption, this is the 256 bit decryption key. The decryption key may
  264. * be generated from the encryption key with AES_DecryptKey256().
  265. *
  266. * @param[in] iv
  267. * 128 bit initalization vector to use.
  268. *
  269. * @param[in] encrypt
  270. * Set to true to encrypt, false to decrypt.
  271. ******************************************************************************/
  272. void AES_CBC256(uint8_t *out,
  273. const uint8_t *in,
  274. unsigned int len,
  275. const uint8_t *key,
  276. const uint8_t *iv,
  277. bool encrypt)
  278. {
  279. int i;
  280. int j;
  281. uint32_t *_out = (uint32_t *)out;
  282. const uint32_t *_in = (const uint32_t *)in;
  283. const uint32_t *_key = (const uint32_t *)key;
  284. const uint32_t *_iv = (const uint32_t *)iv;
  285. /* Need to buffer one block when decrypting in case output replaces input */
  286. uint32_t prev[4];
  287. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  288. /* Number of blocks to process */
  289. len /= AES_BLOCKSIZE;
  290. if (encrypt)
  291. {
  292. /* Enable encryption with auto start using XOR */
  293. AES->CTRL = AES_CTRL_AES256 | AES_CTRL_XORSTART;
  294. /* Load initialization vector, since writing to DATA, it will */
  295. /* not trigger encryption. */
  296. for (i = 3; i >= 0; i--)
  297. {
  298. AES->DATA = __REV(_iv[i]);
  299. }
  300. /* Encrypt data */
  301. while (len--)
  302. {
  303. /* Load key and data and trigger encryption */
  304. for (i = 3, j = 7; i >= 0; i--, j--)
  305. {
  306. AES->KEYLA = __REV(_key[j]);
  307. AES->KEYHA = __REV(_key[i]);
  308. /* Write data last, since will trigger encryption on last iteration */
  309. AES->XORDATA = __REV(_in[i]);
  310. }
  311. _in += 4;
  312. /* Wait for completion */
  313. while (AES->STATUS & AES_STATUS_RUNNING)
  314. ;
  315. /* Save encrypted data */
  316. for (i = 3; i >= 0; i--)
  317. {
  318. _out[i] = __REV(AES->DATA);
  319. }
  320. _out += 4;
  321. }
  322. }
  323. else
  324. {
  325. /* Select decryption mode */
  326. AES->CTRL = AES_CTRL_AES256 | AES_CTRL_DECRYPT | AES_CTRL_DATASTART;
  327. /* Copy init vector to previous buffer to avoid special handling */
  328. for (i = 0; i < 4; i++)
  329. {
  330. prev[i] = _iv[i];
  331. }
  332. /* Decrypt data */
  333. while (len--)
  334. {
  335. /* Load key and data and trigger decryption */
  336. for (i = 3, j = 7; i >= 0; i--, j--)
  337. {
  338. AES->KEYLA = __REV(_key[j]);
  339. AES->KEYHA = __REV(_key[i]);
  340. /* Write data last, since will trigger encryption on last iteration */
  341. AES->DATA = __REV(_in[i]);
  342. }
  343. /* Wait for completion */
  344. while (AES->STATUS & AES_STATUS_RUNNING)
  345. ;
  346. /* In order to avoid additional buffer, we use HW directly for XOR and buffer */
  347. for (i = 3; i >= 0; i--)
  348. {
  349. AES->XORDATA = __REV(prev[i]);
  350. prev[i] = _in[i];
  351. }
  352. _in += 4;
  353. /* Then fetch decrypted data, we have to do it in a separate loop */
  354. /* due to internal auto-shifting of words */
  355. for (i = 3; i >= 0; i--)
  356. {
  357. _out[i] = __REV(AES->DATA);
  358. }
  359. _out += 4;
  360. }
  361. }
  362. }
  363. /***************************************************************************//**
  364. * @brief
  365. * Cipher feedback (CFB) cipher mode encryption/decryption, 128 bit key.
  366. *
  367. * @details
  368. * Encryption:
  369. * @verbatim
  370. * InitVector +----------------+
  371. * | | |
  372. * V | V
  373. * +--------------+ | +--------------+
  374. * Key ->| Block cipher | | Key ->| Block cipher |
  375. * | encryption | | | encryption |
  376. * +--------------+ | +--------------+
  377. * | | |
  378. * V | V
  379. * Plaintext ->XOR | Plaintext ->XOR
  380. * |---------+ |
  381. * V V
  382. * Ciphertext Ciphertext
  383. * @endverbatim
  384. * Decryption:
  385. * @verbatim
  386. * InitVector +----------------+
  387. * | | |
  388. * V | V
  389. * +--------------+ | +--------------+
  390. * Key ->| Block cipher | | Key ->| Block cipher |
  391. * | encryption | | | encryption |
  392. * +--------------+ | +--------------+
  393. * | | |
  394. * V | V
  395. * XOR<- Ciphertext XOR<- Ciphertext
  396. * | |
  397. * V V
  398. * Plaintext Plaintext
  399. * @endverbatim
  400. * Please refer to general comments on layout and byte ordering of parameters.
  401. *
  402. * @param[out] out
  403. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  404. * may be set equal to @p in, in which case the input buffer is overwritten.
  405. *
  406. * @param[in] in
  407. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  408. *
  409. * @param[in] len
  410. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  411. *
  412. * @param[in] key
  413. * 128 bit encryption key is used for both encryption and decryption modes.
  414. *
  415. * @param[in] iv
  416. * 128 bit initalization vector to use.
  417. *
  418. * @param[in] encrypt
  419. * Set to true to encrypt, false to decrypt.
  420. ******************************************************************************/
  421. void AES_CFB128(uint8_t *out,
  422. const uint8_t *in,
  423. unsigned int len,
  424. const uint8_t *key,
  425. const uint8_t *iv,
  426. bool encrypt)
  427. {
  428. int i;
  429. uint32_t *_out = (uint32_t *)out;
  430. const uint32_t *_in = (const uint32_t *)in;
  431. const uint32_t *_key = (const uint32_t *)key;
  432. const uint32_t *_iv = (const uint32_t *)iv;
  433. const uint32_t *data;
  434. uint32_t tmp[4];
  435. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  436. /* Select encryption mode */
  437. AES->CTRL = AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
  438. /* Load key into high key for key buffer usage */
  439. for (i = 3; i >= 0; i--)
  440. {
  441. AES->KEYHA = __REV(_key[i]);
  442. }
  443. /* Encrypt/decrypt data */
  444. data = _iv;
  445. len /= AES_BLOCKSIZE;
  446. while (len--)
  447. {
  448. /* Load data and trigger encryption */
  449. for (i = 3; i >= 0; i--)
  450. {
  451. AES->DATA = __REV(data[i]);
  452. }
  453. /* Do some required processing before waiting for completion */
  454. if (encrypt)
  455. {
  456. data = _out;
  457. }
  458. else
  459. {
  460. /* Must copy current ciphertext block since it may be overwritten */
  461. for (i = 0; i < 4; i++)
  462. {
  463. tmp[i] = _in[i];
  464. }
  465. data = tmp;
  466. }
  467. /* Wait for completion */
  468. while (AES->STATUS & AES_STATUS_RUNNING)
  469. ;
  470. /* Save encrypted/decrypted data */
  471. for (i = 3; i >= 0; i--)
  472. {
  473. _out[i] = __REV(AES->DATA) ^ _in[i];
  474. }
  475. _out += 4;
  476. _in += 4;
  477. }
  478. }
  479. /***************************************************************************//**
  480. * @brief
  481. * Cipher feedback (CFB) cipher mode encryption/decryption, 256 bit key.
  482. *
  483. * @details
  484. * Please see AES_CFB128() for CFB figure.
  485. *
  486. * Please refer to general comments on layout and byte ordering of parameters.
  487. *
  488. * @param[out] out
  489. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  490. * may be set equal to @p in, in which case the input buffer is overwritten.
  491. *
  492. * @param[in] in
  493. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  494. *
  495. * @param[in] len
  496. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  497. *
  498. * @param[in] key
  499. * 256 bit encryption key is used for both encryption and decryption modes.
  500. *
  501. * @param[in] iv
  502. * 128 bit initalization vector to use.
  503. *
  504. * @param[in] encrypt
  505. * Set to true to encrypt, false to decrypt.
  506. ******************************************************************************/
  507. void AES_CFB256(uint8_t *out,
  508. const uint8_t *in,
  509. unsigned int len,
  510. const uint8_t *key,
  511. const uint8_t *iv,
  512. bool encrypt)
  513. {
  514. int i;
  515. int j;
  516. uint32_t *_out = (uint32_t *)out;
  517. const uint32_t *_in = (const uint32_t *)in;
  518. const uint32_t *_key = (const uint32_t *)key;
  519. const uint32_t *_iv = (const uint32_t *)iv;
  520. const uint32_t *data;
  521. uint32_t tmp[4];
  522. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  523. /* Select encryption mode */
  524. AES->CTRL = AES_CTRL_AES256 | AES_CTRL_DATASTART;
  525. /* Encrypt/decrypt data */
  526. data = _iv;
  527. len /= AES_BLOCKSIZE;
  528. while (len--)
  529. {
  530. /* Load key and block to be encrypted/decrypted */
  531. for (i = 3, j = 7; i >= 0; i--, j--)
  532. {
  533. AES->KEYLA = __REV(_key[j]);
  534. AES->KEYHA = __REV(_key[i]);
  535. /* Write data last, since will trigger encryption on last iteration */
  536. AES->DATA = __REV(data[i]);
  537. }
  538. /* Do some required processing before waiting for completion */
  539. if (encrypt)
  540. {
  541. data = _out;
  542. }
  543. else
  544. {
  545. /* Must copy current ciphertext block since it may be overwritten */
  546. for (i = 0; i < 4; i++)
  547. {
  548. tmp[i] = _in[i];
  549. }
  550. data = tmp;
  551. }
  552. while (AES->STATUS & AES_STATUS_RUNNING)
  553. ;
  554. /* Save encrypted/decrypted data */
  555. for (i = 3; i >= 0; i--)
  556. {
  557. _out[i] = __REV(AES->DATA) ^ _in[i];
  558. }
  559. _out += 4;
  560. _in += 4;
  561. }
  562. }
  563. /***************************************************************************//**
  564. * @brief
  565. * Counter (CTR) cipher mode encryption/decryption, 128 bit key.
  566. *
  567. * @details
  568. * Encryption:
  569. * @verbatim
  570. * Counter Counter
  571. * | |
  572. * V V
  573. * +--------------+ +--------------+
  574. * Key ->| Block cipher | Key ->| Block cipher |
  575. * | encryption | | encryption |
  576. * +--------------+ +--------------+
  577. * | |
  578. * Plaintext ->XOR Plaintext ->XOR
  579. * | |
  580. * V V
  581. * Ciphertext Ciphertext
  582. * @endverbatim
  583. * Decryption:
  584. * @verbatim
  585. * Counter Counter
  586. * | |
  587. * V V
  588. * +--------------+ +--------------+
  589. * Key ->| Block cipher | Key ->| Block cipher |
  590. * | encryption | | encryption |
  591. * +--------------+ +--------------+
  592. * | |
  593. * Ciphertext ->XOR Ciphertext ->XOR
  594. * | |
  595. * V V
  596. * Plaintext Plaintext
  597. * @endverbatim
  598. * Please refer to general comments on layout and byte ordering of parameters.
  599. *
  600. * @param[out] out
  601. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  602. * may be set equal to @p in, in which case the input buffer is overwritten.
  603. *
  604. * @param[in] in
  605. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  606. *
  607. * @param[in] len
  608. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  609. *
  610. * @param[in] key
  611. * 128 bit encryption key.
  612. * If this argument is null, the key will not be loaded, as it is assumed
  613. * the key has been loaded into KEYHA previously.
  614. *
  615. * @param[in,out] ctr
  616. * 128 bit initial counter value. The counter is updated after each AES
  617. * block encoding through use of @p ctrFunc.
  618. *
  619. * @param[in] ctrFunc
  620. * Function used to update counter value.
  621. ******************************************************************************/
  622. void AES_CTR128(uint8_t *out,
  623. const uint8_t *in,
  624. unsigned int len,
  625. const uint8_t *key,
  626. uint8_t *ctr,
  627. AES_CtrFuncPtr_TypeDef ctrFunc)
  628. {
  629. int i;
  630. uint32_t *_out = (uint32_t *)out;
  631. const uint32_t *_in = (const uint32_t *)in;
  632. uint32_t *_ctr = (uint32_t *)ctr;
  633. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  634. EFM_ASSERT(ctrFunc);
  635. /* Select encryption mode, with auto trigger */
  636. AES->CTRL = AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
  637. if (key)
  638. {
  639. const uint32_t *_key = (const uint32_t *)key;
  640. /* Load key into high key for key buffer usage */
  641. for (i = 3; i >= 0; i--)
  642. {
  643. AES->KEYHA = __REV(_key[i]);
  644. }
  645. }
  646. /* Encrypt/decrypt data */
  647. len /= AES_BLOCKSIZE;
  648. while (len--)
  649. {
  650. /* Load ctr to be encrypted/decrypted */
  651. for (i = 3; i >= 0; i--)
  652. {
  653. AES->DATA = __REV(_ctr[i]);
  654. }
  655. /* Increment ctr for next use */
  656. ctrFunc(ctr);
  657. /* Wait for completion */
  658. while (AES->STATUS & AES_STATUS_RUNNING)
  659. ;
  660. /* Save encrypted/decrypted data */
  661. for (i = 3; i >= 0; i--)
  662. {
  663. _out[i] = __REV(AES->DATA) ^ _in[i];
  664. }
  665. _out += 4;
  666. _in += 4;
  667. }
  668. }
  669. /***************************************************************************//**
  670. * @brief
  671. * Counter (CTR) cipher mode encryption/decryption, 256 bit key.
  672. *
  673. * @details
  674. * Please see AES_CTR128() for CTR figure.
  675. *
  676. * Please refer to general comments on layout and byte ordering of parameters.
  677. *
  678. * @param[out] out
  679. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  680. * may be set equal to @p in, in which case the input buffer is overwritten.
  681. *
  682. * @param[in] in
  683. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  684. *
  685. * @param[in] len
  686. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  687. *
  688. * @param[in] key
  689. * 256 bit encryption key.
  690. *
  691. * @param[in,out] ctr
  692. * 128 bit initial counter value. The counter is updated after each AES
  693. * block encoding through use of @p ctrFunc.
  694. *
  695. * @param[in] ctrFunc
  696. * Function used to update counter value.
  697. ******************************************************************************/
  698. void AES_CTR256(uint8_t *out,
  699. const uint8_t *in,
  700. unsigned int len,
  701. const uint8_t *key,
  702. uint8_t *ctr,
  703. AES_CtrFuncPtr_TypeDef ctrFunc)
  704. {
  705. int i;
  706. int j;
  707. uint32_t *_out = (uint32_t *)out;
  708. const uint32_t *_in = (const uint32_t *)in;
  709. const uint32_t *_key = (const uint32_t *)key;
  710. uint32_t *_ctr = (uint32_t *)ctr;
  711. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  712. EFM_ASSERT(ctrFunc);
  713. /* Select encryption mode, with auto trigger */
  714. AES->CTRL = AES_CTRL_AES256 | AES_CTRL_DATASTART;
  715. /* Encrypt/decrypt data */
  716. len /= AES_BLOCKSIZE;
  717. while (len--)
  718. {
  719. /* Load key and block to be encrypted/decrypted */
  720. for (i = 3, j = 7; i >= 0; i--, j--)
  721. {
  722. AES->KEYLA = __REV(_key[j]);
  723. AES->KEYHA = __REV(_key[i]);
  724. /* Write data last, since will trigger encryption on last iteration */
  725. AES->DATA = __REV(_ctr[i]);
  726. }
  727. /* Increment ctr for next use */
  728. ctrFunc(ctr);
  729. /* Wait for completion */
  730. while (AES->STATUS & AES_STATUS_RUNNING)
  731. ;
  732. /* Save encrypted/decrypted data */
  733. for (i = 3; i >= 0; i--)
  734. {
  735. _out[i] = __REV(AES->DATA) ^ _in[i];
  736. }
  737. _out += 4;
  738. _in += 4;
  739. }
  740. }
  741. /***************************************************************************//**
  742. * @brief
  743. * Update last 32 bits of 128 bit counter, by incrementing with 1.
  744. *
  745. * @details
  746. * Notice that no special consideration is given to possible wrap around. If
  747. * 32 least significant bits are 0xFFFFFFFF, they will be updated to 0x00000000,
  748. * ignoring overflow.
  749. *
  750. * Please refer to general comments on layout and byte ordering of parameters.
  751. *
  752. * @param[in,out] ctr
  753. * Buffer holding 128 bit counter to be updated.
  754. ******************************************************************************/
  755. void AES_CTRUpdate32Bit(uint8_t *ctr)
  756. {
  757. uint32_t *_ctr = (uint32_t *)ctr;
  758. _ctr[3] = __REV(__REV(_ctr[3]) + 1);
  759. }
  760. /***************************************************************************//**
  761. * @brief
  762. * Generate 128 bit decryption key from 128 bit encryption key. The decryption
  763. * key is used for some cipher modes when decrypting.
  764. *
  765. * @details
  766. * Please refer to general comments on layout and byte ordering of parameters.
  767. *
  768. * @param[out] out
  769. * Buffer to place 128 bit decryption key. Must be at least 16 bytes long. It
  770. * may be set equal to @p in, in which case the input buffer is overwritten.
  771. *
  772. * @param[in] in
  773. * Buffer holding 128 bit encryption key. Must be at least 16 bytes long.
  774. ******************************************************************************/
  775. void AES_DecryptKey128(uint8_t *out, const uint8_t *in)
  776. {
  777. int i;
  778. uint32_t *_out = (uint32_t *)out;
  779. const uint32_t *_in = (const uint32_t *)in;
  780. /* Load key */
  781. for (i = 3; i >= 0; i--)
  782. {
  783. AES->KEYLA = __REV(_in[i]);
  784. }
  785. /* Do dummy encryption to generate decrypt key */
  786. AES->CTRL = 0;
  787. AES_IntClear(AES_IF_DONE);
  788. AES->CMD = AES_CMD_START;
  789. /* Wait for completion */
  790. while (AES->STATUS & AES_STATUS_RUNNING)
  791. ;
  792. /* Save decryption key */
  793. for (i = 3; i >= 0; i--)
  794. {
  795. _out[i] = __REV(AES->KEYLA);
  796. }
  797. }
  798. /***************************************************************************//**
  799. * @brief
  800. * Generate 256 bit decryption key from 256 bit encryption key. The decryption
  801. * key is used for some cipher modes when decrypting.
  802. *
  803. * @details
  804. * Please refer to general comments on layout and byte ordering of parameters.
  805. *
  806. * @param[out] out
  807. * Buffer to place 256 bit decryption key. Must be at least 32 bytes long. It
  808. * may be set equal to @p in, in which case the input buffer is overwritten.
  809. *
  810. * @param[in] in
  811. * Buffer holding 256 bit encryption key. Must be at least 32 bytes long.
  812. ******************************************************************************/
  813. void AES_DecryptKey256(uint8_t *out, const uint8_t *in)
  814. {
  815. int i;
  816. int j;
  817. uint32_t *_out = (uint32_t *)out;
  818. const uint32_t *_in = (const uint32_t *)in;
  819. /* Load key */
  820. for (i = 3, j = 7; i >= 0; i--, j--)
  821. {
  822. AES->KEYLA = __REV(_in[j]);
  823. AES->KEYHA = __REV(_in[i]);
  824. }
  825. /* Do dummy encryption to generate decrypt key */
  826. AES->CTRL = AES_CTRL_AES256;
  827. AES->CMD = AES_CMD_START;
  828. /* Wait for completion */
  829. while (AES->STATUS & AES_STATUS_RUNNING)
  830. ;
  831. /* Save decryption key */
  832. for (i = 3, j = 7; i >= 0; i--, j--)
  833. {
  834. _out[j] = __REV(AES->KEYLA);
  835. _out[i] = __REV(AES->KEYHA);
  836. }
  837. }
  838. /***************************************************************************//**
  839. * @brief
  840. * Electronic Codebook (ECB) cipher mode encryption/decryption, 128 bit key.
  841. *
  842. * @details
  843. * Encryption:
  844. * @verbatim
  845. * Plaintext Plaintext
  846. * | |
  847. * V V
  848. * +--------------+ +--------------+
  849. * Key ->| Block cipher | Key ->| Block cipher |
  850. * | encryption | | encryption |
  851. * +--------------+ +--------------+
  852. * | |
  853. * V V
  854. * Ciphertext Ciphertext
  855. * @endverbatim
  856. * Decryption:
  857. * @verbatim
  858. * Ciphertext Ciphertext
  859. * | |
  860. * V V
  861. * +--------------+ +--------------+
  862. * Key ->| Block cipher | Key ->| Block cipher |
  863. * | decryption | | decryption |
  864. * +--------------+ +--------------+
  865. * | |
  866. * V V
  867. * Plaintext Plaintext
  868. * @endverbatim
  869. * Please refer to general comments on layout and byte ordering of parameters.
  870. *
  871. * @param[out] out
  872. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  873. * may be set equal to @p in, in which case the input buffer is overwritten.
  874. *
  875. * @param[in] in
  876. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  877. *
  878. * @param[in] len
  879. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  880. *
  881. * @param[in] key
  882. * When doing encryption, this is the 128 bit encryption key. When doing
  883. * decryption, this is the 128 bit decryption key. The decryption key may
  884. * be generated from the encryption key with AES_DecryptKey128().
  885. *
  886. * @param[in] encrypt
  887. * Set to true to encrypt, false to decrypt.
  888. ******************************************************************************/
  889. void AES_ECB128(uint8_t *out,
  890. const uint8_t *in,
  891. unsigned int len,
  892. const uint8_t *key,
  893. bool encrypt)
  894. {
  895. int i;
  896. uint32_t *_out = (uint32_t *)out;
  897. const uint32_t *_in = (const uint32_t *)in;
  898. const uint32_t *_key = (const uint32_t *)key;
  899. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  900. /* Load key into high key for key buffer usage */
  901. for (i = 3; i >= 0; i--)
  902. {
  903. AES->KEYHA = __REV(_key[i]);
  904. }
  905. if (encrypt)
  906. {
  907. /* Select encryption mode */
  908. AES->CTRL = AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
  909. }
  910. else
  911. {
  912. /* Select decryption mode */
  913. AES->CTRL = AES_CTRL_DECRYPT | AES_CTRL_KEYBUFEN | AES_CTRL_DATASTART;
  914. }
  915. /* Encrypt/decrypt data */
  916. len /= AES_BLOCKSIZE;
  917. while (len--)
  918. {
  919. /* Load block to be encrypted/decrypted */
  920. for (i = 3; i >= 0; i--)
  921. {
  922. AES->DATA = __REV(_in[i]);
  923. }
  924. _in += 4;
  925. /* Wait for completion */
  926. while (AES->STATUS & AES_STATUS_RUNNING)
  927. ;
  928. /* Save encrypted/decrypted data */
  929. for (i = 3; i >= 0; i--)
  930. {
  931. _out[i] = __REV(AES->DATA);
  932. }
  933. _out += 4;
  934. }
  935. }
  936. /***************************************************************************//**
  937. * @brief
  938. * Electronic Codebook (ECB) cipher mode encryption/decryption, 256 bit key.
  939. *
  940. * @details
  941. * Please see AES_ECB128() for ECB figure.
  942. *
  943. * Please refer to general comments on layout and byte ordering of parameters.
  944. *
  945. * @param[out] out
  946. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  947. * may be set equal to @p in, in which case the input buffer is overwritten.
  948. *
  949. * @param[in] in
  950. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  951. *
  952. * @param[in] len
  953. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  954. *
  955. * @param[in] key
  956. * When doing encryption, this is the 256 bit encryption key. When doing
  957. * decryption, this is the 256 bit decryption key. The decryption key may
  958. * be generated from the encryption key with AES_DecryptKey256().
  959. *
  960. * @param[in] encrypt
  961. * Set to true to encrypt, false to decrypt.
  962. ******************************************************************************/
  963. void AES_ECB256(uint8_t *out,
  964. const uint8_t *in,
  965. unsigned int len,
  966. const uint8_t *key,
  967. bool encrypt)
  968. {
  969. int i;
  970. int j;
  971. uint32_t *_out = (uint32_t *)out;
  972. const uint32_t *_in = (const uint32_t *)in;
  973. const uint32_t *_key = (const uint32_t *)key;
  974. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  975. if (encrypt)
  976. {
  977. /* Select encryption mode */
  978. AES->CTRL = AES_CTRL_AES256 | AES_CTRL_DATASTART;
  979. }
  980. else
  981. {
  982. /* Select decryption mode */
  983. AES->CTRL = AES_CTRL_DECRYPT | AES_CTRL_AES256 | AES_CTRL_DATASTART;
  984. }
  985. /* Encrypt/decrypt data */
  986. len /= AES_BLOCKSIZE;
  987. while (len--)
  988. {
  989. /* Load key and block to be encrypted/decrypted */
  990. for (i = 3, j = 7; i >= 0; i--, j--)
  991. {
  992. AES->KEYLA = __REV(_key[j]);
  993. AES->KEYHA = __REV(_key[i]);
  994. /* Write data last, since will trigger encryption on last iteration */
  995. AES->DATA = __REV(_in[i]);
  996. }
  997. _in += 4;
  998. /* Wait for completion */
  999. while (AES->STATUS & AES_STATUS_RUNNING)
  1000. ;
  1001. /* Save encrypted/decrypted data */
  1002. for (i = 3; i >= 0; i--)
  1003. {
  1004. _out[i] = __REV(AES->DATA);
  1005. }
  1006. _out += 4;
  1007. }
  1008. }
  1009. /***************************************************************************//**
  1010. * @brief
  1011. * Output feedback (OFB) cipher mode encryption/decryption, 128 bit key.
  1012. *
  1013. * @details
  1014. * Encryption:
  1015. * @verbatim
  1016. * InitVector +----------------+
  1017. * | | |
  1018. * V | V
  1019. * +--------------+ | +--------------+
  1020. * Key ->| Block cipher | | Key ->| Block cipher |
  1021. * | encryption | | | encryption |
  1022. * +--------------+ | +--------------+
  1023. * | | |
  1024. * |---------+ |
  1025. * V V
  1026. * Plaintext ->XOR Plaintext ->XOR
  1027. * | |
  1028. * V V
  1029. * Ciphertext Ciphertext
  1030. * @endverbatim
  1031. * Decryption:
  1032. * @verbatim
  1033. * InitVector +----------------+
  1034. * | | |
  1035. * V | V
  1036. * +--------------+ | +--------------+
  1037. * Key ->| Block cipher | | Key ->| Block cipher |
  1038. * | encryption | | | encryption |
  1039. * +--------------+ | +--------------+
  1040. * | | |
  1041. * |---------+ |
  1042. * V V
  1043. * Ciphertext ->XOR Ciphertext ->XOR
  1044. * | |
  1045. * V V
  1046. * Plaintext Plaintext
  1047. * @endverbatim
  1048. * Please refer to general comments on layout and byte ordering of parameters.
  1049. *
  1050. * @param[out] out
  1051. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  1052. * may be set equal to @p in, in which case the input buffer is overwritten.
  1053. *
  1054. * @param[in] in
  1055. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  1056. *
  1057. * @param[in] len
  1058. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  1059. *
  1060. * @param[in] key
  1061. * 128 bit encryption key.
  1062. *
  1063. * @param[in] iv
  1064. * 128 bit initalization vector to use.
  1065. ******************************************************************************/
  1066. void AES_OFB128(uint8_t *out,
  1067. const uint8_t *in,
  1068. unsigned int len,
  1069. const uint8_t *key,
  1070. const uint8_t *iv)
  1071. {
  1072. int i;
  1073. uint32_t *_out = (uint32_t *)out;
  1074. const uint32_t *_in = (const uint32_t *)in;
  1075. const uint32_t *_key = (const uint32_t *)key;
  1076. const uint32_t *_iv = (const uint32_t *)iv;
  1077. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  1078. /* Select encryption mode, trigger explicitly by command */
  1079. AES->CTRL = AES_CTRL_KEYBUFEN;
  1080. /* Load key into high key for key buffer usage */
  1081. /* Load initialization vector */
  1082. for (i = 3; i >= 0; i--)
  1083. {
  1084. AES->KEYHA = __REV(_key[i]);
  1085. AES->DATA = __REV(_iv[i]);
  1086. }
  1087. /* Encrypt/decrypt data */
  1088. len /= AES_BLOCKSIZE;
  1089. while (len--)
  1090. {
  1091. AES->CMD = AES_CMD_START;
  1092. /* Wait for completion */
  1093. while (AES->STATUS & AES_STATUS_RUNNING)
  1094. ;
  1095. /* Save encrypted/decrypted data */
  1096. for (i = 3; i >= 0; i--)
  1097. {
  1098. _out[i] = __REV(AES->DATA) ^ _in[i];
  1099. }
  1100. _out += 4;
  1101. _in += 4;
  1102. }
  1103. }
  1104. /***************************************************************************//**
  1105. * @brief
  1106. * Output feedback (OFB) cipher mode encryption/decryption, 256 bit key.
  1107. *
  1108. * @details
  1109. * Please see AES_OFB128() for OFB figure.
  1110. *
  1111. * Please refer to general comments on layout and byte ordering of parameters.
  1112. *
  1113. * @param[out] out
  1114. * Buffer to place encrypted/decrypted data. Must be at least @p len long. It
  1115. * may be set equal to @p in, in which case the input buffer is overwritten.
  1116. *
  1117. * @param[in] in
  1118. * Buffer holding data to encrypt/decrypt. Must be at least @p len long.
  1119. *
  1120. * @param[in] len
  1121. * Number of bytes to encrypt/decrypt. Must be a multiple of 16.
  1122. *
  1123. * @param[in] key
  1124. * 256 bit encryption key.
  1125. *
  1126. * @param[in] iv
  1127. * 128 bit initalization vector to use.
  1128. ******************************************************************************/
  1129. void AES_OFB256(uint8_t *out,
  1130. const uint8_t *in,
  1131. unsigned int len,
  1132. const uint8_t *key,
  1133. const uint8_t *iv)
  1134. {
  1135. int i;
  1136. int j;
  1137. uint32_t *_out = (uint32_t *)out;
  1138. const uint32_t *_in = (const uint32_t *)in;
  1139. const uint32_t *_key = (const uint32_t *)key;
  1140. const uint32_t *_iv = (const uint32_t *)iv;
  1141. EFM_ASSERT(!(len % AES_BLOCKSIZE));
  1142. /* Select encryption mode, trigger explicitly by command */
  1143. AES->CTRL = AES_CTRL_AES256;
  1144. /* Load initialization vector */
  1145. for (i = 3; i >= 0; i--)
  1146. {
  1147. AES->DATA = __REV(_iv[i]);
  1148. }
  1149. /* Encrypt/decrypt data */
  1150. len /= AES_BLOCKSIZE;
  1151. while (len--)
  1152. {
  1153. /* Load key */
  1154. for (i = 3, j = 7; i >= 0; i--, j--)
  1155. {
  1156. AES->KEYLA = __REV(_key[j]);
  1157. AES->KEYHA = __REV(_key[i]);
  1158. }
  1159. AES->CMD = AES_CMD_START;
  1160. /* Wait for completion */
  1161. while (AES->STATUS & AES_STATUS_RUNNING)
  1162. ;
  1163. /* Save encrypted/decrypted data */
  1164. for (i = 3; i >= 0; i--)
  1165. {
  1166. _out[i] = __REV(AES->DATA) ^ _in[i];
  1167. }
  1168. _out += 4;
  1169. _in += 4;
  1170. }
  1171. }
  1172. /** @} (end addtogroup AES) */
  1173. /** @} (end addtogroup EM_Library) */
  1174. #endif /* defined(AES_COUNT) && (AES_COUNT > 0) */