aes.c 45 KB

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