efm32_aes.c 39 KB

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