aes.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. //*****************************************************************************
  2. //
  3. // aes.c - Driver for the AES module.
  4. //
  5. // Copyright (c) 2012-2014 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // This is part of revision 2.1.0.12573 of the Tiva Peripheral Driver Library.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup aes_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdint.h>
  46. #include <stdbool.h>
  47. #include <stdint.h>
  48. #include "inc/hw_aes.h"
  49. #include "inc/hw_ccm.h"
  50. #include "inc/hw_ints.h"
  51. #include "inc/hw_memmap.h"
  52. #include "inc/hw_nvic.h"
  53. #include "inc/hw_types.h"
  54. #include "driverlib/aes.h"
  55. #include "driverlib/debug.h"
  56. #include "driverlib/interrupt.h"
  57. //*****************************************************************************
  58. //
  59. //! Resets the AES module.
  60. //!
  61. //! \param ui32Base is the base address of the AES module.
  62. //!
  63. //! This function performs a softreset the AES module.
  64. //!
  65. //! \return None.
  66. //
  67. //*****************************************************************************
  68. void
  69. AESReset(uint32_t ui32Base)
  70. {
  71. //
  72. // Check the arguments.
  73. //
  74. ASSERT(ui32Base == AES_BASE);
  75. //
  76. // Trigger the reset.
  77. //
  78. HWREG(ui32Base + AES_O_SYSCONFIG) |= AES_SYSCONFIG_SOFTRESET;
  79. //
  80. // Wait for the reset to finish.
  81. //
  82. while((HWREG(ui32Base + AES_O_SYSSTATUS) &
  83. AES_SYSSTATUS_RESETDONE) == 0)
  84. {
  85. }
  86. }
  87. //*****************************************************************************
  88. //
  89. //! Configures the AES module.
  90. //!
  91. //! \param ui32Base is the base address of the AES module.
  92. //! \param ui32Config is the configuration of the AES module.
  93. //!
  94. //! This function configures the AES module based on the specified parameters.
  95. //! It does not change any DMA- or interrupt-related parameters.
  96. //!
  97. //! The ui32Config parameter is a bit-wise OR of a number of configuration
  98. //! flags. The valid flags are grouped based on their function.
  99. //!
  100. //! The direction of the operation is specified with only of following flags:
  101. //!
  102. //! - \b AES_CFG_DIR_ENCRYPT - Encryption mode
  103. //! - \b AES_CFG_DIR_DECRYPT - Decryption mode
  104. //!
  105. //! The key size is specified with only one of the following flags:
  106. //!
  107. //! - \b AES_CFG_KEY_SIZE_128BIT - Key size of 128 bits
  108. //! - \b AES_CFG_KEY_SIZE_192BIT - Key size of 192 bits
  109. //! - \b AES_CFG_KEY_SIZE_256BIT - Key size of 256 bits
  110. //!
  111. //! The mode of operation is specified with only one of the following flags.
  112. //!
  113. //! - \b AES_CFG_MODE_ECB - Electronic codebook mode
  114. //! - \b AES_CFG_MODE_CBC - Cipher-block chaining mode
  115. //! - \b AES_CFG_MODE_CFB - Cipher feedback mode
  116. //! - \b AES_CFG_MODE_CTR - Counter mode
  117. //! - \b AES_CFG_MODE_ICM - Integer counter mode
  118. //! - \b AES_CFG_MODE_XTS - Ciphertext stealing mode
  119. //! - \b AES_CFG_MODE_XTS_TWEAKJL - XEX-based tweaked-codebook mode with
  120. //! ciphertext stealing with previous/intermediate tweak value and j loaded
  121. //! - \b AES_CFG_MODE_XTS_K2IJL - XEX-based tweaked-codebook mode with
  122. //! ciphertext stealing with key2, i and j loaded
  123. //! - \b AES_CFG_MODE_XTS_K2ILJ0 - XEX-based tweaked-codebook mode with
  124. //! ciphertext stealing with key2 and i loaded, j = 0
  125. //! - \b AES_CFG_MODE_F8 - F8 mode
  126. //! - \b AES_CFG_MODE_F9 - F9 mode
  127. //! - \b AES_CFG_MODE_CBCMAC - Cipher block chaining message authentication
  128. //! code mode
  129. //! - \b AES_CFG_MODE_GCM_HLY0ZERO - Galois/counter mode with GHASH with H
  130. //! loaded, Y0-encrypted forced to zero and counter is not enabled.
  131. //! - \b AES_CFG_MODE_GCM_HLY0CALC - Galois/counter mode with GHASH with H
  132. //! loaded, Y0-encrypted calculated internally and counter is enabled.
  133. //! - \b AES_CFG_MODE_GCM_HY0CALC - Galois/Counter mode with autonomous GHASH
  134. //! (both H and Y0-encrypted calculated internally) and counter is enabled.
  135. //! - \b AES_CFG_MODE_CCM - Counter with CBC-MAC mode
  136. //!
  137. //! The following defines are used to specify the counter width. It is only
  138. //! required to be defined when using CTR, CCM, or GCM modes, only one of the
  139. //! following defines must be used to specify the counter width length:
  140. //!
  141. //! - \b AES_CFG_CTR_WIDTH_32 - Counter is 32 bits
  142. //! - \b AES_CFG_CTR_WIDTH_64 - Counter is 64 bits
  143. //! - \b AES_CFG_CTR_WIDTH_96 - Counter is 96 bits
  144. //! - \b AES_CFG_CTR_WIDTH_128 - Counter is 128 bits
  145. //!
  146. //! Only one of the following defines must be used to specify the length field
  147. //! for CCM operations (L):
  148. //!
  149. //! - \b AES_CFG_CCM_L_1 - 1 byte
  150. //! - \b AES_CFG_CCM_L_2 - 2 bytes
  151. //! - \b AES_CFG_CCM_L_3 - 3 bytes
  152. //! - \b AES_CFG_CCM_L_4 - 4 bytes
  153. //! - \b AES_CFG_CCM_L_5 - 5 bytes
  154. //! - \b AES_CFG_CCM_L_6 - 6 bytes
  155. //! - \b AES_CFG_CCM_L_7 - 7 bytes
  156. //! - \b AES_CFG_CCM_L_8 - 8 bytes
  157. //!
  158. //! Only one of the following defines must be used to specify the length of the
  159. //! authentication field for CCM operations (M) through the \e ui32Config
  160. //! argument in the AESConfigSet() function:
  161. //!
  162. //! - \b AES_CFG_CCM_M_4 - 4 bytes
  163. //! - \b AES_CFG_CCM_M_6 - 6 bytes
  164. //! - \b AES_CFG_CCM_M_8 - 8 bytes
  165. //! - \b AES_CFG_CCM_M_10 - 10 bytes
  166. //! - \b AES_CFG_CCM_M_12 - 12 bytes
  167. //! - \b AES_CFG_CCM_M_14 - 14 bytes
  168. //! - \b AES_CFG_CCM_M_16 - 16 bytes
  169. //!
  170. //! \note When performing a basic GHASH operation for used with GCM mode, use
  171. //! the \b AES_CFG_MODE_GCM_HLY0ZERO and do not specify a direction.
  172. //!
  173. //! \return None.
  174. //
  175. //*****************************************************************************
  176. void
  177. AESConfigSet(uint32_t ui32Base, uint32_t ui32Config)
  178. {
  179. //
  180. // Check the arguments.
  181. //
  182. ASSERT(ui32Base == AES_BASE);
  183. ASSERT((ui32Config & AES_CFG_DIR_ENCRYPT) ||
  184. (ui32Config & AES_CFG_DIR_DECRYPT));
  185. ASSERT((ui32Config & AES_CFG_KEY_SIZE_128BIT) ||
  186. (ui32Config & AES_CFG_KEY_SIZE_192BIT) ||
  187. (ui32Config & AES_CFG_KEY_SIZE_256BIT));
  188. ASSERT((ui32Config & AES_CFG_MODE_ECB) ||
  189. (ui32Config & AES_CFG_MODE_CBC) ||
  190. (ui32Config & AES_CFG_MODE_CTR) ||
  191. (ui32Config & AES_CFG_MODE_ICM) ||
  192. (ui32Config & AES_CFG_MODE_CFB) ||
  193. (ui32Config & AES_CFG_MODE_XTS_TWEAKJL) ||
  194. (ui32Config & AES_CFG_MODE_XTS_K2IJL) ||
  195. (ui32Config & AES_CFG_MODE_XTS_K2ILJ0) ||
  196. (ui32Config & AES_CFG_MODE_F8) ||
  197. (ui32Config & AES_CFG_MODE_F9) ||
  198. (ui32Config & AES_CFG_MODE_CTR) ||
  199. (ui32Config & AES_CFG_MODE_CBCMAC) ||
  200. (ui32Config & AES_CFG_MODE_GCM_HLY0ZERO) ||
  201. (ui32Config & AES_CFG_MODE_GCM_HLY0CALC) ||
  202. (ui32Config & AES_CFG_MODE_GCM_HY0CALC) ||
  203. (ui32Config & AES_CFG_MODE_CCM));
  204. ASSERT(((ui32Config & AES_CFG_MODE_CTR) ||
  205. (ui32Config & AES_CFG_MODE_GCM_HLY0ZERO) ||
  206. (ui32Config & AES_CFG_MODE_GCM_HLY0CALC) ||
  207. (ui32Config & AES_CFG_MODE_GCM_HY0CALC) ||
  208. (ui32Config & AES_CFG_MODE_CCM)) &&
  209. ((ui32Config & AES_CFG_CTR_WIDTH_32) ||
  210. (ui32Config & AES_CFG_CTR_WIDTH_64) ||
  211. (ui32Config & AES_CFG_CTR_WIDTH_96) ||
  212. (ui32Config & AES_CFG_CTR_WIDTH_128)));
  213. ASSERT((ui32Config & AES_CFG_MODE_CCM) &&
  214. ((ui32Config & AES_CFG_CCM_L_1) ||
  215. (ui32Config & AES_CFG_CCM_L_2) ||
  216. (ui32Config & AES_CFG_CCM_L_3) ||
  217. (ui32Config & AES_CFG_CCM_L_4) ||
  218. (ui32Config & AES_CFG_CCM_L_5) ||
  219. (ui32Config & AES_CFG_CCM_L_6) ||
  220. (ui32Config & AES_CFG_CCM_L_7) ||
  221. (ui32Config & AES_CFG_CCM_L_8)) &&
  222. ((ui32Config & AES_CFG_CCM_M_4) ||
  223. (ui32Config & AES_CFG_CCM_M_6) ||
  224. (ui32Config & AES_CFG_CCM_M_8) ||
  225. (ui32Config & AES_CFG_CCM_M_10) ||
  226. (ui32Config & AES_CFG_CCM_M_12) ||
  227. (ui32Config & AES_CFG_CCM_M_14) ||
  228. (ui32Config & AES_CFG_CCM_M_16)));
  229. //
  230. // Backup the save context field before updating the register.
  231. //
  232. if(HWREG(ui32Base + AES_O_CTRL) & AES_CTRL_SAVE_CONTEXT)
  233. {
  234. ui32Config |= AES_CTRL_SAVE_CONTEXT;
  235. }
  236. //
  237. // Write the CTRL register with the new value
  238. //
  239. HWREG(ui32Base + AES_O_CTRL) = ui32Config;
  240. }
  241. //*****************************************************************************
  242. //
  243. //! Writes the key 1 configuration registers, which are used for encryption or
  244. //! decryption.
  245. //!
  246. //! \param ui32Base is the base address for the AES module.
  247. //! \param pui32Key is an array of 32-bit words, containing the key to be
  248. //! configured. The least significant word in the 0th index.
  249. //! \param ui32Keysize is the size of the key, which must be one of the
  250. //! following values: \b AES_CFG_KEY_SIZE_128, \b AES_CFG_KEY_SIZE_192, or
  251. //! \b AES_CFG_KEY_SIZE_256.
  252. //!
  253. //! This function writes key 1 configuration registers based on the key
  254. //! size. This function is used in all modes.
  255. //!
  256. //! \return None.
  257. //
  258. //*****************************************************************************
  259. void
  260. AESKey1Set(uint32_t ui32Base, uint32_t *pui32Key, uint32_t ui32Keysize)
  261. {
  262. //
  263. // Check the arguments.
  264. //
  265. ASSERT(ui32Base == AES_BASE);
  266. ASSERT((ui32Keysize == AES_CFG_KEY_SIZE_128BIT) ||
  267. (ui32Keysize == AES_CFG_KEY_SIZE_192BIT) ||
  268. (ui32Keysize == AES_CFG_KEY_SIZE_256BIT));
  269. //
  270. // With all key sizes, the first 4 words are written.
  271. //
  272. HWREG(ui32Base + AES_O_KEY1_0) = pui32Key[0];
  273. HWREG(ui32Base + AES_O_KEY1_1) = pui32Key[1];
  274. HWREG(ui32Base + AES_O_KEY1_2) = pui32Key[2];
  275. HWREG(ui32Base + AES_O_KEY1_3) = pui32Key[3];
  276. //
  277. // The key is 192 or 256 bits. Write the next 2 words.
  278. //
  279. if(ui32Keysize != AES_CFG_KEY_SIZE_128BIT)
  280. {
  281. HWREG(ui32Base + AES_O_KEY1_4) = pui32Key[4];
  282. HWREG(ui32Base + AES_O_KEY1_5) = pui32Key[5];
  283. }
  284. //
  285. // The key is 256 bits. Write the last 2 words.
  286. //
  287. if(ui32Keysize == AES_CFG_KEY_SIZE_256BIT)
  288. {
  289. HWREG(ui32Base + AES_O_KEY1_6) = pui32Key[6];
  290. HWREG(ui32Base + AES_O_KEY1_7) = pui32Key[7];
  291. }
  292. }
  293. //*****************************************************************************
  294. //
  295. //! Writes the key 2 configuration registers, which are used for encryption or
  296. //! decryption.
  297. //!
  298. //! \param ui32Base is the base address for the AES module.
  299. //! \param pui32Key is an array of 32-bit words, containing the key to be
  300. //! configured. The least significant word in the 0th index.
  301. //! \param ui32Keysize is the size of the key, which must be one of the
  302. //! following values: \b AES_CFG_KEY_SIZE_128, \b AES_CFG_KEY_SIZE_192, or
  303. //! \b AES_CFG_KEY_SIZE_256.
  304. //!
  305. //! This function writes the key 2 configuration registers based on the key
  306. //! size. This function is used in the F8, F9, XTS, CCM, and CBC-MAC modes.
  307. //!
  308. //! \return None.
  309. //
  310. //*****************************************************************************
  311. void
  312. AESKey2Set(uint32_t ui32Base, uint32_t *pui32Key, uint32_t ui32Keysize)
  313. {
  314. //
  315. // Check the arguments.
  316. //
  317. ASSERT(ui32Base == AES_BASE);
  318. ASSERT((ui32Keysize == AES_CFG_KEY_SIZE_128BIT) ||
  319. (ui32Keysize == AES_CFG_KEY_SIZE_192BIT) ||
  320. (ui32Keysize == AES_CFG_KEY_SIZE_256BIT));
  321. //
  322. // With all key sizes, the first 4 words are written.
  323. //
  324. HWREG(ui32Base + AES_O_KEY2_0) = pui32Key[0];
  325. HWREG(ui32Base + AES_O_KEY2_1) = pui32Key[1];
  326. HWREG(ui32Base + AES_O_KEY2_2) = pui32Key[2];
  327. HWREG(ui32Base + AES_O_KEY2_3) = pui32Key[3];
  328. //
  329. // The key is 192 or 256 bits. Write the next 2 words.
  330. //
  331. if(ui32Keysize != AES_CFG_KEY_SIZE_128BIT)
  332. {
  333. HWREG(ui32Base + AES_O_KEY2_4) = pui32Key[4];
  334. HWREG(ui32Base + AES_O_KEY2_5) = pui32Key[5];
  335. }
  336. //
  337. // The key is 256 bits. Write the last 2 words.
  338. //
  339. if(ui32Keysize == AES_CFG_KEY_SIZE_256BIT)
  340. {
  341. HWREG(ui32Base + AES_O_KEY2_6) = pui32Key[6];
  342. HWREG(ui32Base + AES_O_KEY2_7) = pui32Key[7];
  343. }
  344. }
  345. //*****************************************************************************
  346. //
  347. //! Writes key 3 configuration registers, which are used for encryption or
  348. //! decryption.
  349. //!
  350. //! \param ui32Base is the base address for the AES module.
  351. //! \param pui32Key is a pointer to an array of 4 words (128 bits), containing
  352. //! the key to be configured. The least significant word is in the 0th index.
  353. //!
  354. //! This function writes the key 2 configuration registers with key 3 data
  355. //! used in CBC-MAC and F8 modes. This key is always 128 bits.
  356. //!
  357. //! \return None.
  358. //
  359. //*****************************************************************************
  360. void
  361. AESKey3Set(uint32_t ui32Base, uint32_t *pui32Key)
  362. {
  363. //
  364. // Check the arguments.
  365. //
  366. ASSERT(ui32Base == AES_BASE);
  367. //
  368. // Write the key into the upper 4 key registers
  369. //
  370. HWREG(ui32Base + AES_O_KEY2_4) = pui32Key[0];
  371. HWREG(ui32Base + AES_O_KEY2_5) = pui32Key[1];
  372. HWREG(ui32Base + AES_O_KEY2_6) = pui32Key[2];
  373. HWREG(ui32Base + AES_O_KEY2_7) = pui32Key[3];
  374. }
  375. //*****************************************************************************
  376. //
  377. //! Writes the Initial Vector (IV) register, needed in some of the AES Modes.
  378. //!
  379. //! \param ui32Base is the base address of the AES module.
  380. //! \param pui32IVdata is an array of 4 words (128 bits), containing the IV
  381. //! value to be configured. The least significant word is in the 0th index.
  382. //!
  383. //! This functions writes the initial vector registers in the AES module.
  384. //!
  385. //! \return None.
  386. //
  387. //*****************************************************************************
  388. void
  389. AESIVSet(uint32_t ui32Base, uint32_t *pui32IVdata)
  390. {
  391. //
  392. // Check the arguments.
  393. //
  394. ASSERT(ui32Base == AES_BASE);
  395. //
  396. // Write the initial vector registers.
  397. //
  398. HWREG(ui32Base + AES_O_IV_IN_0) = pui32IVdata[0];
  399. HWREG(ui32Base + AES_O_IV_IN_1) = pui32IVdata[1];
  400. HWREG(ui32Base + AES_O_IV_IN_2) = pui32IVdata[2];
  401. HWREG(ui32Base + AES_O_IV_IN_3) = pui32IVdata[3];
  402. }
  403. //*****************************************************************************
  404. //
  405. //! Saves the Initial Vector (IV) registers to a user-defined location.
  406. //!
  407. //! \param ui32Base is the base address of the AES module.
  408. //! \param pui32IVData is pointer to the location that stores the IV data.
  409. //!
  410. //! This function stores the IV for use with authenticated encryption and
  411. //! decryption operations. It is assumed that the AES_CTRL_SAVE_CONTEXT
  412. //! bit is set in the AES_CTRL register.
  413. //!
  414. //! \return None.
  415. //
  416. //*****************************************************************************
  417. void
  418. AESIVRead(uint32_t ui32Base, uint32_t *pui32IVData)
  419. {
  420. //
  421. // Check the arguments.
  422. //
  423. ASSERT(ui32Base == AES_BASE);
  424. //
  425. // Wait for the output context to be ready.
  426. //
  427. while((AES_CTRL_SVCTXTRDY & (HWREG(ui32Base + AES_O_CTRL))) == 0)
  428. {
  429. }
  430. //
  431. // Read the tag data.
  432. //
  433. pui32IVData[0] = HWREG((ui32Base + AES_O_IV_IN_0));
  434. pui32IVData[1] = HWREG((ui32Base + AES_O_IV_IN_1));
  435. pui32IVData[2] = HWREG((ui32Base + AES_O_IV_IN_2));
  436. pui32IVData[3] = HWREG((ui32Base + AES_O_IV_IN_3));
  437. }
  438. //*****************************************************************************
  439. //
  440. //! Saves the tag registers to a user-defined location.
  441. //!
  442. //! \param ui32Base is the base address of the AES module.
  443. //! \param pui32TagData is pointer to the location that stores the tag data.
  444. //!
  445. //! This function stores the tag data for use authenticated encryption and
  446. //! decryption operations. It is assumed that the AES_CTRL_SAVE_CONTEXT
  447. //! bit is set in the AES_CTRL register.
  448. //!
  449. //! \return None.
  450. //
  451. //*****************************************************************************
  452. void
  453. AESTagRead(uint32_t ui32Base, uint32_t *pui32TagData)
  454. {
  455. //
  456. // Check the arguments.
  457. //
  458. ASSERT(ui32Base == AES_BASE);
  459. //
  460. // Wait for the output context to be ready.
  461. //
  462. while((AES_CTRL_SVCTXTRDY & (HWREG(ui32Base + AES_O_CTRL))) == 0)
  463. {
  464. }
  465. //
  466. // Read the tag data.
  467. //
  468. pui32TagData[0] = HWREG((ui32Base + AES_O_TAG_OUT_0));
  469. pui32TagData[1] = HWREG((ui32Base + AES_O_TAG_OUT_1));
  470. pui32TagData[2] = HWREG((ui32Base + AES_O_TAG_OUT_2));
  471. pui32TagData[3] = HWREG((ui32Base + AES_O_TAG_OUT_3));
  472. }
  473. //*****************************************************************************
  474. //
  475. //! Used to set the write crypto data length in the AES module.
  476. //!
  477. //! \param ui32Base is the base address of the AES module.
  478. //! \param ui64Length is the crypto data length in bytes.
  479. //!
  480. //! This function stores the cryptographic data length in blocks for all modes.
  481. //! Data lengths up to (2^61 - 1) bytes are allowed. For GCM, any value up
  482. //! to (2^36 - 2) bytes are allowed because a 32-bit block counter is used.
  483. //! For basic modes (ECB/CBC/CTR/ICM/CFB128), zero can be programmed into the
  484. //! length field, indicating that the length is infinite.
  485. //!
  486. //! When this function is called, the engine is triggered to start using
  487. //! this context.
  488. //!
  489. //! \note This length does not include the authentication-only data used in
  490. //! some modes. Use the AESAuthLengthSet() function to specify the
  491. //! authentication data length.
  492. //!
  493. //! \return None
  494. //
  495. //*****************************************************************************
  496. void
  497. AESLengthSet(uint32_t ui32Base, uint64_t ui64Length)
  498. {
  499. //
  500. // Check the arguments.
  501. //
  502. ASSERT(ui32Base == AES_BASE);
  503. //
  504. // Write the length register by shifting the 64-bit ui64Length.
  505. //
  506. HWREG(ui32Base + AES_O_C_LENGTH_0) = (uint32_t)(ui64Length);
  507. HWREG(ui32Base + AES_O_C_LENGTH_1) = (uint32_t)(ui64Length >> 32);
  508. }
  509. //*****************************************************************************
  510. //
  511. //! Sets the authentication data length in the AES module.
  512. //!
  513. //! \param ui32Base is the base address of the AES module.
  514. //! \param ui32Length is the length in bytes.
  515. //!
  516. //! This function is only used to write the authentication data length in the
  517. //! combined modes (GCM or CCM) and XTS mode. Supported AAD lengths for CCM
  518. //! are from 0 to (2^16 - 28) bytes. For GCM, any value up to (2^32 - 1) can
  519. //! be used. For XTS mode, this register is used to load j. Loading of j is
  520. //! only required if j != 0. j represents the sequential number of the 128-bit
  521. //! blocks inside the data unit. Consequently, j must be multiplied by 16
  522. //! when passed to this function, thereby placing the block number in
  523. //! bits [31:4] of the register.
  524. //!
  525. //! When this function is called, the engine is triggered to start using
  526. //! this context for GCM and CCM.
  527. //!
  528. //! \return None
  529. //
  530. //*****************************************************************************
  531. void
  532. AESAuthLengthSet(uint32_t ui32Base, uint32_t ui32Length)
  533. {
  534. //
  535. // Check the arguments.
  536. //
  537. ASSERT(ui32Base == AES_BASE);
  538. //
  539. // Write the length into the register.
  540. //
  541. HWREG(ui32Base + AES_O_AUTH_LENGTH) = ui32Length;
  542. }
  543. //*****************************************************************************
  544. //
  545. //! Reads plaintext/ciphertext from data registers without blocking.
  546. //!
  547. //! \param ui32Base is the base address of the AES module.
  548. //! \param pui32Dest is a pointer to an array of words of data.
  549. //!
  550. //! This function reads a block of either plaintext or ciphertext out of the
  551. //! AES module. If the output data is not ready, the function returns
  552. //! false. If the read completed successfully, the function returns true.
  553. //! A block is 16 bytes or 4 words.
  554. //!
  555. //! \return true or false.
  556. //
  557. //*****************************************************************************
  558. bool
  559. AESDataReadNonBlocking(uint32_t ui32Base, uint32_t *pui32Dest)
  560. {
  561. //
  562. // Check the arguments.
  563. //
  564. ASSERT(ui32Base == AES_BASE);
  565. //
  566. // Check if the output is ready before reading the data. If it not ready,
  567. // return false.
  568. //
  569. if((AES_CTRL_OUTPUT_READY & (HWREG(ui32Base + AES_O_CTRL))) == 0)
  570. {
  571. return(false);
  572. }
  573. //
  574. // Read a block of data from the data registers
  575. //
  576. pui32Dest[0] = HWREG(ui32Base + AES_O_DATA_IN_3);
  577. pui32Dest[1] = HWREG(ui32Base + AES_O_DATA_IN_2);
  578. pui32Dest[2] = HWREG(ui32Base + AES_O_DATA_IN_1);
  579. pui32Dest[3] = HWREG(ui32Base + AES_O_DATA_IN_0);
  580. //
  581. // Read successful, return true.
  582. //
  583. return(true);
  584. }
  585. //*****************************************************************************
  586. //
  587. //! Reads plaintext/ciphertext from data registers with blocking.
  588. //!
  589. //! \param ui32Base is the base address of the AES module.
  590. //! \param pui32Dest is a pointer to an array of words.
  591. //!
  592. //! This function reads a block of either plaintext or ciphertext out of the
  593. //! AES module. If the output is not ready, the function waits until it is
  594. //! ready. A block is 16 bytes or 4 words.
  595. //!
  596. //! \return None.
  597. //
  598. //*****************************************************************************
  599. void
  600. AESDataRead(uint32_t ui32Base, uint32_t *pui32Dest)
  601. {
  602. //
  603. // Check the arguments.
  604. //
  605. ASSERT(ui32Base == AES_BASE);
  606. //
  607. // Wait for the output to be ready before reading the data.
  608. //
  609. while((AES_CTRL_OUTPUT_READY & (HWREG(ui32Base + AES_O_CTRL))) == 0)
  610. {
  611. }
  612. //
  613. // Read a block of data from the data registers
  614. //
  615. pui32Dest[0] = HWREG(ui32Base + AES_O_DATA_IN_3);
  616. pui32Dest[1] = HWREG(ui32Base + AES_O_DATA_IN_2);
  617. pui32Dest[2] = HWREG(ui32Base + AES_O_DATA_IN_1);
  618. pui32Dest[3] = HWREG(ui32Base + AES_O_DATA_IN_0);
  619. }
  620. //*****************************************************************************
  621. //
  622. //! Writes plaintext/ciphertext to data registers without blocking.
  623. //!
  624. //! \param ui32Base is the base address of the AES module.
  625. //! \param pui32Src is a pointer to an array of words of data.
  626. //!
  627. //! This function writes a block of either plaintext or ciphertext into the
  628. //! AES module. If the input is not ready, the function returns false. If the
  629. //! write completed successfully, the function returns true. A block is 16
  630. //! bytes or 4 words.
  631. //!
  632. //! \return True or false.
  633. //
  634. //*****************************************************************************
  635. bool
  636. AESDataWriteNonBlocking(uint32_t ui32Base, uint32_t *pui32Src)
  637. {
  638. //
  639. // Check the arguments.
  640. //
  641. ASSERT(ui32Base == AES_BASE);
  642. //
  643. // Check if the input is ready. If not, then return false.
  644. //
  645. if(!(AES_CTRL_INPUT_READY & (HWREG(ui32Base + AES_O_CTRL))))
  646. {
  647. return(false);
  648. }
  649. //
  650. // Write a block of data into the data registers.
  651. //
  652. HWREG(ui32Base + AES_O_DATA_IN_3) = pui32Src[0];
  653. HWREG(ui32Base + AES_O_DATA_IN_2) = pui32Src[1];
  654. HWREG(ui32Base + AES_O_DATA_IN_1) = pui32Src[2];
  655. HWREG(ui32Base + AES_O_DATA_IN_0) = pui32Src[3];
  656. //
  657. // Write successful, return true.
  658. //
  659. return(true);
  660. }
  661. //*****************************************************************************
  662. //
  663. //! Writes plaintext/ciphertext to data registers with blocking.
  664. //!
  665. //! \param ui32Base is the base address of the AES module.
  666. //! \param pui32Src is a pointer to an array of bytes.
  667. //!
  668. //! This function writes a block of either plaintext or ciphertext into the
  669. //! AES module. If the input is not ready, the function waits until it is
  670. //! ready before performing the write. A block is 16 bytes or 4 words.
  671. //!
  672. //! \return None.
  673. //
  674. //*****************************************************************************
  675. void
  676. AESDataWrite(uint32_t ui32Base, uint32_t *pui32Src)
  677. {
  678. //
  679. // Check the arguments.
  680. //
  681. ASSERT(ui32Base == AES_BASE);
  682. //
  683. // Wait for input ready.
  684. //
  685. while((AES_CTRL_INPUT_READY & (HWREG(ui32Base + AES_O_CTRL))) == 0)
  686. {
  687. }
  688. //
  689. // Write a block of data into the data registers.
  690. //
  691. HWREG(ui32Base + AES_O_DATA_IN_3) = pui32Src[0];
  692. HWREG(ui32Base + AES_O_DATA_IN_2) = pui32Src[1];
  693. HWREG(ui32Base + AES_O_DATA_IN_1) = pui32Src[2];
  694. HWREG(ui32Base + AES_O_DATA_IN_0) = pui32Src[3];
  695. }
  696. //*****************************************************************************
  697. //
  698. //! Used to process(transform) blocks of data, either encrypt or decrypt it.
  699. //!
  700. //! \param ui32Base is the base address of the AES module.
  701. //! \param pui32Src is a pointer to the memory location where the input data
  702. //! is stored. The data must be padded to the 16-byte boundary.
  703. //! \param pui32Dest is a pointer to the memory location output is written.
  704. //! The space for written data must be rounded up to the 16-byte boundary.
  705. //! \param ui32Length is the length of the cryptographic data in bytes.
  706. //!
  707. //! This function iterates the encryption or decryption mechanism number over
  708. //! the data length. Before calling this function, ensure that the AES
  709. //! module is properly configured the key, data size, mode, etc. Only ECB,
  710. //! CBC, CTR, ICM, CFB, XTS and F8 operating modes should be used. The data
  711. //! is processed in 4-word (16-byte) blocks.
  712. //!
  713. //! \note This function only supports values of \e ui32Length less than 2^32,
  714. //! because the memory size is restricted to between 0 to 2^32 bytes.
  715. //!
  716. //! \return Returns true if data was processed successfully. Returns false
  717. //! if data processing failed.
  718. //
  719. //*****************************************************************************
  720. bool
  721. AESDataProcess(uint32_t ui32Base, uint32_t *pui32Src, uint32_t *pui32Dest,
  722. uint32_t ui32Length)
  723. {
  724. uint32_t ui32Count;
  725. //
  726. // Check the arguments.
  727. //
  728. ASSERT(ui32Base == AES_BASE);
  729. //
  730. // Write the length register first, which triggers the engine to start
  731. // using this context.
  732. //
  733. AESLengthSet(AES_BASE, (uint64_t)ui32Length);
  734. //
  735. // Now loop until the blocks are written.
  736. //
  737. for(ui32Count = 0; ui32Count < ui32Length; ui32Count += 16)
  738. {
  739. //
  740. // Write the data registers.
  741. //
  742. AESDataWrite(ui32Base, pui32Src + (ui32Count / 4));
  743. //
  744. // Read the data registers.
  745. //
  746. AESDataRead(ui32Base, pui32Dest + (ui32Count / 4));
  747. }
  748. //
  749. // Return true to indicate successful completion of the function.
  750. //
  751. return(true);
  752. }
  753. //*****************************************************************************
  754. //
  755. //! Used to authenticate blocks of data by generating a hash tag.
  756. //!
  757. //! \param ui32Base is the base address of the AES module.
  758. //! \param pui32Src is a pointer to the memory location where the input data
  759. //! is stored. The data must be padded to the 16-byte boundary.
  760. //! \param ui32Length is the length of the cryptographic data in bytes.
  761. //! \param pui32Tag is a pointer to a 4-word array where the hash tag is
  762. //! written.
  763. //!
  764. //! This function processes data to produce a hash tag that can be used tor
  765. //! authentication. Before calling this function, ensure that the AES
  766. //! module is properly configured the key, data size, mode, etc. Only
  767. //! CBC-MAC and F9 modes should be used.
  768. //!
  769. //! \return Returns true if data was processed successfully. Returns false
  770. //! if data processing failed.
  771. //
  772. //*****************************************************************************
  773. bool
  774. AESDataAuth(uint32_t ui32Base, uint32_t *pui32Src, uint32_t ui32Length,
  775. uint32_t *pui32Tag)
  776. {
  777. uint32_t ui32Count;
  778. //
  779. // Check the arguments.
  780. //
  781. ASSERT(ui32Base == AES_BASE);
  782. //
  783. // Write the length register first, which triggers the engine to start
  784. // using this context.
  785. //
  786. AESLengthSet(ui32Base, (uint64_t)ui32Length);
  787. //
  788. // Now loop until the blocks are written.
  789. //
  790. for(ui32Count = 0; ui32Count < ui32Length; ui32Count += 16)
  791. {
  792. //
  793. // Write the data registers.
  794. //
  795. AESDataWrite(ui32Base, pui32Src + (ui32Count / 4));
  796. }
  797. //
  798. // Read the hash tag value.
  799. //
  800. AESTagRead(ui32Base, pui32Tag);
  801. //
  802. // Return true to indicate successful completion of the function.
  803. //
  804. return(true);
  805. }
  806. //*****************************************************************************
  807. //
  808. //! Processes and authenticates blocks of data, either encrypt it or decrypts
  809. //! it.
  810. //!
  811. //! \param ui32Base is the base address of the AES module.
  812. //! \param pui32Src is a pointer to the memory location where the input data
  813. //! is stored. The data must be padded to the 16-byte boundary.
  814. //! \param pui32Dest is a pointer to the memory location output is written.
  815. //! The space for written data must be rounded up to the 16-byte boundary.
  816. //! \param ui32Length is the length of the cryptographic data in bytes.
  817. //! \param pui32AuthSrc is a pointer to the memory location where the
  818. //! additional authentication data is stored. The data must be padded to the
  819. //! 16-byte boundary.
  820. //! \param ui32AuthLength is the length of the additional authentication
  821. //! data in bytes.
  822. //! \param pui32Tag is a pointer to a 4-word array where the hash tag is
  823. //! written.
  824. //!
  825. //! This function encrypts or decrypts blocks of data in addition to
  826. //! authentication data. A hash tag is also produced. Before calling this
  827. //! function, ensure that the AES module is properly configured the key,
  828. //! data size, mode, etc. Only CCM and GCM modes should be used.
  829. //!
  830. //! \return Returns true if data was processed successfully. Returns false
  831. //! if data processing failed.
  832. //
  833. //*****************************************************************************
  834. bool
  835. AESDataProcessAuth(uint32_t ui32Base, uint32_t *pui32Src,
  836. uint32_t *pui32Dest, uint32_t ui32Length,
  837. uint32_t *pui32AuthSrc, uint32_t ui32AuthLength,
  838. uint32_t *pui32Tag)
  839. {
  840. uint32_t ui32Count;
  841. //
  842. // Check the arguments.
  843. //
  844. ASSERT(ui32Base == AES_BASE);
  845. //
  846. // Set the data length.
  847. //
  848. AESLengthSet(ui32Base, (uint64_t)ui32Length);
  849. //
  850. // Set the additional authentication data length.
  851. //
  852. AESAuthLengthSet(ui32Base, ui32AuthLength);
  853. //
  854. // Now loop until the authentication data blocks are written.
  855. //
  856. for(ui32Count = 0; ui32Count < ui32AuthLength; ui32Count += 16)
  857. {
  858. //
  859. // Write the data registers.
  860. //
  861. AESDataWrite(ui32Base, pui32AuthSrc + (ui32Count / 4));
  862. }
  863. //
  864. // Now loop until the data blocks are written.
  865. //
  866. for(ui32Count = 0; ui32Count < ui32Length; ui32Count += 16)
  867. {
  868. //
  869. // Write the data registers.
  870. //
  871. AESDataWrite(ui32Base, pui32Src + (ui32Count / 4));
  872. //
  873. //
  874. // Read the data registers.
  875. //
  876. AESDataRead(ui32Base, pui32Dest + (ui32Count / 4));
  877. }
  878. //
  879. // Read the hash tag value.
  880. //
  881. AESTagRead(ui32Base, pui32Tag);
  882. //
  883. // Return true to indicate successful completion of the function.
  884. //
  885. return(true);
  886. }
  887. //*****************************************************************************
  888. //
  889. //! Returns the current AES module interrupt status.
  890. //!
  891. //! \param ui32Base is the base address of the AES module.
  892. //! \param bMasked is \b false if the raw interrupt status is required and
  893. //! \b true if the masked interrupt status is required.
  894. //!
  895. //! \return Returns a bit mask of the interrupt sources, which is a logical OR
  896. //! of any of the following:
  897. //!
  898. //! - \b AES_INT_CONTEXT_IN - Context interrupt
  899. //! - \b AES_INT_CONTEXT_OUT - Authentication tag (and IV) interrupt.
  900. //! - \b AES_INT_DATA_IN - Data input interrupt
  901. //! - \b AES_INT_DATA_OUT - Data output interrupt
  902. //! - \b AES_INT_DMA_CONTEXT_IN - Context DMA done interrupt
  903. //! - \b AES_INT_DMA_CONTEXT_OUT - Authentication tag (and IV) DMA done
  904. //! interrupt
  905. //! - \b AES_INT_DMA_DATA_IN - Data input DMA done interrupt
  906. //! - \b AES_INT_DMA_DATA_OUT - Data output DMA done interrupt
  907. //
  908. //*****************************************************************************
  909. uint32_t
  910. AESIntStatus(uint32_t ui32Base, bool bMasked)
  911. {
  912. uint32_t ui32Status, ui32Enable, ui32Temp;
  913. //
  914. // Check the arguments.
  915. //
  916. ASSERT(ui32Base == AES_BASE);
  917. //
  918. // Read the IRQ status register and return the value.
  919. //
  920. ui32Status = HWREG(ui32Base + AES_O_IRQSTATUS);
  921. if(bMasked)
  922. {
  923. ui32Enable = HWREG(ui32Base + AES_O_IRQENABLE);
  924. ui32Temp = HWREG(ui32Base + AES_O_DMAMIS);
  925. return((ui32Status & ui32Enable) |
  926. (((ui32Temp & 0x00000001) << 16) |
  927. ((ui32Temp & 0x00000002) << 18) |
  928. ((ui32Temp & 0x0000000c) << 15)));
  929. }
  930. else
  931. {
  932. ui32Temp = HWREG(ui32Base + AES_O_DMARIS);
  933. return(ui32Status |
  934. (((ui32Temp & 0x00000001) << 16) |
  935. ((ui32Temp & 0x00000002) << 18) |
  936. ((ui32Temp & 0x0000000c) << 15)));
  937. }
  938. }
  939. //*****************************************************************************
  940. //
  941. //! Enables AES module interrupts.
  942. //!
  943. //! \param ui32Base is the base address of the AES module.
  944. //! \param ui32IntFlags is a bit mask of the interrupt sources to enable.
  945. //!
  946. //! This function enables the interrupts in the AES module. The
  947. //! \e ui32IntFlags parameter is the logical OR of any of the following:
  948. //!
  949. //! - \b AES_INT_CONTEXT_IN - Context interrupt
  950. //! - \b AES_INT_CONTEXT_OUT - Authentication tag (and IV) interrupt
  951. //! - \b AES_INT_DATA_IN - Data input interrupt
  952. //! - \b AES_INT_DATA_OUT - Data output interrupt
  953. //! - \b AES_INT_DMA_CONTEXT_IN - Context DMA done interrupt
  954. //! - \b AES_INT_DMA_CONTEXT_OUT - Authentication tag (and IV) DMA done
  955. //! interrupt
  956. //! - \b AES_INT_DMA_DATA_IN - Data input DMA done interrupt
  957. //! - \b AES_INT_DMA_DATA_OUT - Data output DMA done interrupt
  958. //!
  959. //! \note Interrupts that have been previously been enabled are not disabled
  960. //! when this function is called.
  961. //!
  962. //! \return None.
  963. //
  964. //*****************************************************************************
  965. void
  966. AESIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
  967. {
  968. //
  969. // Check the arguments.
  970. //
  971. ASSERT(ui32Base == AES_BASE);
  972. ASSERT((ui32IntFlags == AES_INT_CONTEXT_IN) ||
  973. (ui32IntFlags == AES_INT_CONTEXT_OUT) ||
  974. (ui32IntFlags == AES_INT_DATA_IN) ||
  975. (ui32IntFlags == AES_INT_DATA_OUT) ||
  976. (ui32IntFlags == AES_INT_DMA_CONTEXT_IN) ||
  977. (ui32IntFlags == AES_INT_DMA_CONTEXT_OUT) ||
  978. (ui32IntFlags == AES_INT_DMA_DATA_IN) ||
  979. (ui32IntFlags == AES_INT_DMA_DATA_OUT));
  980. //
  981. // Set the flags.
  982. //
  983. HWREG(ui32Base + AES_O_DMAIM) |= (((ui32IntFlags & 0x00010000) >> 16) |
  984. ((ui32IntFlags & 0x00060000) >> 15) |
  985. ((ui32IntFlags & 0x00080000) >> 18));
  986. HWREG(ui32Base + AES_O_IRQENABLE) |= ui32IntFlags & 0x0000ffff;
  987. }
  988. //*****************************************************************************
  989. //
  990. //! Disables AES module interrupts.
  991. //!
  992. //! \param ui32Base is the base address of the AES module.
  993. //! \param ui32IntFlags is a bit mask of the interrupt sources to disable.
  994. //!
  995. //! This function disables the interrupt sources in the AES module. The
  996. //! \e ui32IntFlags parameter is the logical OR of any of the following:
  997. //!
  998. //! - \b AES_INT_CONTEXT_IN - Context interrupt
  999. //! - \b AES_INT_CONTEXT_OUT - Authentication tag (and IV) interrupt
  1000. //! - \b AES_INT_DATA_IN - Data input interrupt
  1001. //! - \b AES_INT_DATA_OUT - Data output interrupt
  1002. //! - \b AES_INT_DMA_CONTEXT_IN - Context DMA done interrupt
  1003. //! - \b AES_INT_DMA_CONTEXT_OUT - Authentication tag (and IV) DMA done
  1004. //! interrupt
  1005. //! - \b AES_INT_DMA_DATA_IN - Data input DMA done interrupt
  1006. //! - \b AES_INT_DMA_DATA_OUT - Data output DMA done interrupt
  1007. //!
  1008. //! \note The DMA done interrupts are the only interrupts that can be cleared.
  1009. //! The remaining interrupts can be disabled instead using AESIntDisable().
  1010. //!
  1011. //! \return None.
  1012. //
  1013. //*****************************************************************************
  1014. void
  1015. AESIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
  1016. {
  1017. //
  1018. // Check the arguments.
  1019. //
  1020. ASSERT(ui32Base == AES_BASE);
  1021. ASSERT((ui32IntFlags == AES_INT_CONTEXT_IN) ||
  1022. (ui32IntFlags == AES_INT_CONTEXT_OUT) ||
  1023. (ui32IntFlags == AES_INT_DATA_IN) ||
  1024. (ui32IntFlags == AES_INT_DATA_OUT) ||
  1025. (ui32IntFlags == AES_INT_DMA_CONTEXT_IN) ||
  1026. (ui32IntFlags == AES_INT_DMA_CONTEXT_OUT) ||
  1027. (ui32IntFlags == AES_INT_DMA_DATA_IN) ||
  1028. (ui32IntFlags == AES_INT_DMA_DATA_OUT));
  1029. //
  1030. // Clear the flags.
  1031. //
  1032. HWREG(ui32Base + AES_O_DMAIM) &= ~(((ui32IntFlags & 0x00010000) >> 16) |
  1033. ((ui32IntFlags & 0x00060000) >> 15) |
  1034. ((ui32IntFlags & 0x00080000) >> 18));
  1035. HWREG(ui32Base + AES_O_IRQENABLE) &= ~(ui32IntFlags & 0x0000ffff);
  1036. }
  1037. //*****************************************************************************
  1038. //
  1039. //! Clears AES module interrupts.
  1040. //!
  1041. //! \param ui32Base is the base address of the AES module.
  1042. //! \param ui32IntFlags is a bit mask of the interrupt sources to disable.
  1043. //!
  1044. //! This function clears the interrupt sources in the AES module. The
  1045. //! \e ui32IntFlags parameter is the logical OR of any of the following:
  1046. //!
  1047. //! - \b AES_INT_DMA_CONTEXT_IN - Context DMA done interrupt
  1048. //! - \b AES_INT_DMA_CONTEXT_OUT - Authentication tag (and IV) DMA done
  1049. //! interrupt
  1050. //! - \b AES_INT_DMA_DATA_IN - Data input DMA done interrupt
  1051. //! - \b AES_INT_DMA_DATA_OUT - Data output DMA done interrupt
  1052. //!
  1053. //! \note Only the DMA done interrupts can be cleared. The remaining
  1054. //! interrupts should be disabled with AESIntDisable().
  1055. //!
  1056. //! \return None.
  1057. //
  1058. //*****************************************************************************
  1059. void
  1060. AESIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
  1061. {
  1062. //
  1063. // Check the arguments.
  1064. //
  1065. ASSERT(ui32Base == AES_BASE);
  1066. ASSERT((ui32IntFlags == AES_INT_DMA_CONTEXT_IN) ||
  1067. (ui32IntFlags == AES_INT_DMA_CONTEXT_OUT) ||
  1068. (ui32IntFlags == AES_INT_DMA_DATA_IN) ||
  1069. (ui32IntFlags == AES_INT_DMA_DATA_OUT));
  1070. HWREG(ui32Base + AES_O_DMAIC) = (((ui32IntFlags & 0x00010000) >> 16) |
  1071. ((ui32IntFlags & 0x00060000) >> 15) |
  1072. ((ui32IntFlags & 0x00080000) >> 18));
  1073. }
  1074. //*****************************************************************************
  1075. //
  1076. //! Registers an interrupt handler for the AES module.
  1077. //!
  1078. //! \param ui32Base is the base address of the AES module.
  1079. //! \param pfnHandler is a pointer to the function to be called when the
  1080. //! enabled AES interrupts occur.
  1081. //!
  1082. //! This function registers the interrupt handler in the interrupt vector
  1083. //! table, and enables AES interrupts on the interrupt controller; specific AES
  1084. //! interrupt sources must be enabled using AESIntEnable(). The interrupt
  1085. //! handler being registered must clear the source of the interrupt using
  1086. //! AESIntClear().
  1087. //!
  1088. //! If the application is using a static interrupt vector table stored in
  1089. //! flash, then it is not necessary to register the interrupt handler this way.
  1090. //! Instead, IntEnable() is used to enable AES interrupts on the
  1091. //! interrupt controller.
  1092. //!
  1093. //! \sa IntRegister() for important information about registering interrupt
  1094. //! handlers.
  1095. //!
  1096. //! \return None.
  1097. //
  1098. //*****************************************************************************
  1099. void
  1100. AESIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
  1101. {
  1102. //
  1103. // Check the arguments.
  1104. //
  1105. ASSERT(ui32Base == AES_BASE);
  1106. //
  1107. // Register the interrupt handler.
  1108. //
  1109. IntRegister(INT_AES0_TM4C129, pfnHandler);
  1110. //
  1111. // Enable the interrupt
  1112. //
  1113. IntEnable(INT_AES0_TM4C129);
  1114. }
  1115. //*****************************************************************************
  1116. //
  1117. //! Unregisters an interrupt handler for the AES module.
  1118. //!
  1119. //! \param ui32Base is the base address of the AES module.
  1120. //!
  1121. //! This function unregisters the previously registered interrupt handler and
  1122. //! disables the interrupt in the interrupt controller.
  1123. //!
  1124. //! \sa IntRegister() for important information about registering interrupt
  1125. //! handlers.
  1126. //!
  1127. //! \return None.
  1128. //
  1129. //*****************************************************************************
  1130. void
  1131. AESIntUnregister(uint32_t ui32Base)
  1132. {
  1133. //
  1134. // Check the arguments.
  1135. //
  1136. ASSERT(ui32Base == AES_BASE);
  1137. //
  1138. // Disable the interrupt.
  1139. //
  1140. IntDisable(INT_AES0_TM4C129);
  1141. //
  1142. // Unregister the interrupt handler.
  1143. //
  1144. IntUnregister(INT_AES0_TM4C129);
  1145. }
  1146. //*****************************************************************************
  1147. //
  1148. //! Enables uDMA requests for the AES module.
  1149. //!
  1150. //! \param ui32Base is the base address of the AES module.
  1151. //! \param ui32Flags is a bit mask of the uDMA requests to be enabled.
  1152. //!
  1153. //! This function enables the uDMA request sources in the AES module.
  1154. //! The \e ui32Flags parameter is the logical OR of any of the following:
  1155. //!
  1156. //! - \b AES_DMA_DATA_IN
  1157. //! - \b AES_DMA_DATA_OUT
  1158. //! - \b AES_DMA_CONTEXT_IN
  1159. //! - \b AES_DMA_CONTEXT_OUT
  1160. //!
  1161. //! \return None.
  1162. //
  1163. //*****************************************************************************
  1164. void
  1165. AESDMAEnable(uint32_t ui32Base, uint32_t ui32Flags)
  1166. {
  1167. //
  1168. // Check the arguments.
  1169. //
  1170. ASSERT(ui32Base == AES_BASE);
  1171. ASSERT((ui32Flags == AES_DMA_DATA_IN) ||
  1172. (ui32Flags == AES_DMA_DATA_OUT) ||
  1173. (ui32Flags == AES_DMA_CONTEXT_IN) ||
  1174. (ui32Flags == AES_DMA_CONTEXT_OUT));
  1175. //
  1176. // Set the flags in the current register value.
  1177. //
  1178. HWREG(ui32Base + AES_O_SYSCONFIG) |= ui32Flags;
  1179. }
  1180. //*****************************************************************************
  1181. //
  1182. //! Disables uDMA requests for the AES module.
  1183. //!
  1184. //! \param ui32Base is the base address of the AES module.
  1185. //! \param ui32Flags is a bit mask of the uDMA requests to be disabled.
  1186. //!
  1187. //! This function disables the uDMA request sources in the AES module.
  1188. //! The \e ui32Flags parameter is the logical OR of any of the
  1189. //! following:
  1190. //!
  1191. //! - \b AES_DMA_DATA_IN
  1192. //! - \b AES_DMA_DATA_OUT
  1193. //! - \b AES_DMA_CONTEXT_IN
  1194. //! - \b AES_DMA_CONTEXT_OUT
  1195. //!
  1196. //! \return None.
  1197. //
  1198. //*****************************************************************************
  1199. void
  1200. AESDMADisable(uint32_t ui32Base, uint32_t ui32Flags)
  1201. {
  1202. //
  1203. // Check the arguments.
  1204. //
  1205. ASSERT(ui32Base == AES_BASE);
  1206. ASSERT((ui32Flags == AES_DMA_DATA_IN) ||
  1207. (ui32Flags == AES_DMA_DATA_OUT) ||
  1208. (ui32Flags == AES_DMA_CONTEXT_IN) ||
  1209. (ui32Flags == AES_DMA_CONTEXT_OUT));
  1210. //
  1211. // Clear the flags in the current register value.
  1212. //
  1213. HWREG(ui32Base + AES_O_SYSCONFIG) &= ~ui32Flags;
  1214. }
  1215. //*****************************************************************************
  1216. //
  1217. // Close the Doxygen group.
  1218. //! @}
  1219. //
  1220. //*****************************************************************************