aes.c 43 KB

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