stm8l15x_aes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_aes.c
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Advanced Encryption Standard (AES) peripheral:
  9. * - Configuration
  10. * - Read/Write operations
  11. * - DMA transfers management
  12. * - Interrupts and flags management
  13. *
  14. * @verbatim
  15. * ===================================================================
  16. * How to use this driver
  17. * ===================================================================
  18. * 1- Enable AES clock to get write access to AES registers
  19. * using CLK_PeripheralClockConfig(CLK_Peripheral_AES, ENABLE);
  20. *
  21. * 2- Configure the AES operation mode using AES_OperationModeConfig()
  22. *
  23. * 3- If required, enable interrupt source using AES_ITConfig()
  24. *
  25. * 4- If required, when using the DMA mode
  26. * - Configure the DMA using DMA_Init()
  27. * - Enable DMA requests using AES_DMAConfig()
  28. *
  29. * 5- Enable the AES peripheral using AES_Cmd()
  30. *
  31. * 6- Write data/key using AES_WriteSubData() / AES_WriteSubKey()
  32. *
  33. * @endverbatim
  34. ******************************************************************************
  35. * @attention
  36. *
  37. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  38. *
  39. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  40. * You may not use this file except in compliance with the License.
  41. * You may obtain a copy of the License at:
  42. *
  43. * http://www.st.com/software_license_agreement_liberty_v2
  44. *
  45. * Unless required by applicable law or agreed to in writing, software
  46. * distributed under the License is distributed on an "AS IS" BASIS,
  47. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  48. * See the License for the specific language governing permissions and
  49. * limitations under the License.
  50. *
  51. ******************************************************************************
  52. */
  53. /* Includes ------------------------------------------------------------------*/
  54. #include "stm8l15x_aes.h"
  55. /** @addtogroup STM8L15x_StdPeriph_Driver
  56. * @{
  57. */
  58. /** @defgroup AES
  59. * @brief AES driver modules
  60. * @{
  61. */
  62. /* Private typedef -----------------------------------------------------------*/
  63. /* Private define ------------------------------------------------------------*/
  64. /* Private macro -------------------------------------------------------------*/
  65. /* Private variables ---------------------------------------------------------*/
  66. /* Private function prototypes -----------------------------------------------*/
  67. /* Private functions ---------------------------------------------------------*/
  68. /** @defgroup AES_Private_Functions
  69. * @{
  70. */
  71. /** @defgroup AES_Group1 Configuration
  72. * @brief Configuration
  73. *
  74. @verbatim
  75. ===============================================================================
  76. Configuration
  77. ===============================================================================
  78. @endverbatim
  79. * @{
  80. */
  81. /**
  82. * @brief Deinitializes the AES peripheral.
  83. * @param None.
  84. * @retval None
  85. */
  86. void AES_DeInit(void)
  87. {
  88. /* Set AES_CR to reset value 0x00, AES_SR is reset by setting ERRC and CCFC bits in CR */
  89. AES->CR = AES_CR_ERRC | AES_CR_CCFC;
  90. AES->DINR = AES_DINR_RESET_VALUE; /* Set AES_DINR to reset value 0x00 */
  91. AES->DOUTR = AES_DOUTR_RESET_VALUE; /* Set AES_DOUTR to reset value 0x00 */
  92. }
  93. /**
  94. * @brief Configures the AES operation mode.
  95. * @param AES_Operation : the selected AES operation mode.
  96. * This parameter can be one of the following values:
  97. * @arg AES_Operation_Encryp: AES in Encryption mode
  98. * @arg AES_Operation_KeyDeriv: AES in Key Derivation mode
  99. * @arg AES_Operation_Decryp: AES in Decryption mode
  100. * @arg AES_Operation_KeyDerivAndDecryp: AES in Key Derivation and Decryption mode
  101. * @note The operation mode must be configured when AES peripheral is disabled.
  102. * @retval None
  103. */
  104. void AES_OperationModeConfig(AES_Operation_TypeDef AES_Operation)
  105. {
  106. /* Check the parameter */
  107. assert_param(IS_AES_MODE(AES_Operation));
  108. /* Reset the operation mode bits in CR register */
  109. AES->CR &= (uint8_t) (~AES_CR_MODE);
  110. /* Set the new operation mode bits in CR register */
  111. AES->CR |= (uint8_t) (AES_Operation);
  112. }
  113. /**
  114. * @brief Enable the AES peripheral.
  115. * @param NewState : The new state of the AES peripheral.
  116. * This parameter can be: ENABLE or DISABLE.
  117. * @note AES peripheral can be enabled once operation mode is configured using
  118. * AES_OperationModeConfig()
  119. * @retval None
  120. */
  121. void AES_Cmd(FunctionalState NewState)
  122. {
  123. /* Check the parameter */
  124. assert_param(IS_FUNCTIONAL_STATE(NewState));
  125. if (NewState != DISABLE)
  126. {
  127. AES->CR |= (uint8_t) AES_CR_EN; /**< AES Enable */
  128. }
  129. else
  130. {
  131. AES->CR &= (uint8_t)(~AES_CR_EN); /**< AES Disable */
  132. }
  133. }
  134. /**
  135. * @}
  136. */
  137. /** @defgroup AES_Group2 AES Read and Write
  138. * @brief AES Read and Write
  139. *
  140. @verbatim
  141. ===============================================================================
  142. AES Read and Write operations
  143. ===============================================================================
  144. @endverbatim
  145. * @{
  146. */
  147. /**
  148. * @brief Write data in DINR register to be processed by AES peripheral.
  149. * @param Data: The data to be processed.
  150. * @note When an unexpected write to DINR register is detected, WRERR flag is
  151. * set.
  152. * @retval None
  153. */
  154. void AES_WriteSubData(uint8_t Data)
  155. {
  156. /* Write Data */
  157. AES->DINR = Data;
  158. }
  159. /**
  160. * @brief Write key in DINR register.
  161. * @param Key: The key to be used for encryption/decryption.
  162. * @note When an unexpected write to DINR register is detected, WRERR flag is
  163. * set.
  164. * @retval None
  165. */
  166. void AES_WriteSubKey(uint8_t Key)
  167. {
  168. /* Write key */
  169. AES->DINR = Key;
  170. }
  171. /**
  172. * @brief Returns the data in DOUTR register processed by AES peripheral.
  173. * @note When an unexpected read of DOUTR register is detected, RDERR flag is
  174. * set
  175. * @retval The processed data.
  176. */
  177. uint8_t AES_ReadSubData(void)
  178. {
  179. return AES->DOUTR;
  180. }
  181. /**
  182. * @brief Returns the DOUTR register content.
  183. * @retval The derivation key.
  184. * @note When an unexpected read of DOUTR register is detected, RDERR flag is
  185. * set.
  186. */
  187. uint8_t AES_ReadSubKey(void)
  188. {
  189. return AES->DOUTR;
  190. }
  191. /**
  192. * @}
  193. */
  194. /** @defgroup AES_Group3 DMA transfers management functions
  195. * @brief DMA transfers management function
  196. *
  197. @verbatim
  198. ===============================================================================
  199. DMA transfers management functions
  200. ===============================================================================
  201. @endverbatim
  202. * @{
  203. */
  204. /**
  205. * @brief Configures the AES DMA interface.
  206. * @param AES_DMATransfer: Specifies the AES DMA transfer.
  207. * This parameter can be one of the following values:
  208. * @arg AES_DMATransfer_InOut: DMA requests enabled for input/Output transfer phase
  209. * @param NewState Indicates the new state of the AES DMA interface.
  210. * This parameter can be: ENABLE or DISABLE.
  211. * @retval None
  212. * @note CCF bit has no meaning when DMA requests are enabled (DMAEN = 1).
  213. */
  214. void AES_DMAConfig(AES_DMATransfer_TypeDef AES_DMATransfer, FunctionalState NewState)
  215. {
  216. /* Check the parameter */
  217. assert_param(IS_AES_DMATRANSFER(AES_DMATransfer));
  218. if (NewState != DISABLE)
  219. {
  220. /* Enable the DMA transfer */
  221. AES->CR |= (uint8_t) AES_DMATransfer;
  222. }
  223. else
  224. {
  225. /* Disable the DMA transfer */
  226. AES->CR &= (uint8_t)(~AES_DMATransfer);
  227. }
  228. }
  229. /**
  230. * @}
  231. */
  232. /** @defgroup AES_Group4 Interrupts and flags management functions
  233. * @brief Interrupts and flags management functions
  234. *
  235. @verbatim
  236. ===============================================================================
  237. Interrupts and flags management functions
  238. ===============================================================================
  239. @endverbatim
  240. * @{
  241. */
  242. /**
  243. * @brief Enables or disables the specified AES interrupt.
  244. * @param AES_IT: Specifies the AES interrupt source to enable/disable.
  245. * This parameter can be one of the following values:
  246. * @arg AES_IT_CCIE: Computation Complete interrupt enable
  247. * @arg AES_IT_ERRIE: Error interrupt enable
  248. * @param NewState : The new state of the AES peripheral.
  249. * This parameter can be: ENABLE or DISABLE.
  250. * @retval None
  251. */
  252. void AES_ITConfig(AES_IT_TypeDef AES_IT, FunctionalState NewState)
  253. {
  254. /* Check the parameters */
  255. assert_param(IS_FUNCTIONAL_STATE(NewState));
  256. assert_param(IS_AES_IT(AES_IT));
  257. if (NewState != DISABLE)
  258. {
  259. AES->CR |= (uint8_t) AES_IT; /**< AES_IT Enable */
  260. }
  261. else
  262. {
  263. AES->CR &= (uint8_t)(~AES_IT); /**< AES_IT Disable */
  264. }
  265. }
  266. /**
  267. * @brief Checks whether the specified AES flag is set or not.
  268. * @param AES_FLAG specifies the flag to check.
  269. * This parameter can be one of the following values:
  270. * @arg AES_FLAG_CCF: Computation Complete Flag
  271. * @arg AES_FLAG_RDERR: Read Error Flag
  272. * @arg AES_FLAG_WRERR: Write Error Flag
  273. * @retval FlagStatus (SET or RESET)
  274. * @note CCF bit has a meaning only when DMA requests are disabled (DMAEN bit = 0).
  275. */
  276. FlagStatus AES_GetFlagStatus(AES_FLAG_TypeDef AES_FLAG)
  277. {
  278. FlagStatus status = RESET;
  279. /* Check parameters */
  280. assert_param(IS_AES_FLAG(AES_FLAG));
  281. if (AES_FLAG == AES_FLAG_CCF)
  282. {
  283. if ((AES->SR & (uint8_t)AES_FLAG_CCF) != (uint8_t)0x00)
  284. {
  285. /* Computation Complete Flag CCF is set */
  286. status = (FlagStatus) SET;
  287. }
  288. else
  289. {
  290. /* Computation Complete Flag CCF is reset */
  291. status = (FlagStatus) RESET;
  292. }
  293. }
  294. else if (AES_FLAG == AES_FLAG_RDERR)
  295. {
  296. if ((AES->SR & (uint8_t)AES_FLAG_RDERR) != (uint8_t)0x00)
  297. {
  298. /* Read Error Flag RDERR is set */
  299. status = (FlagStatus) SET;
  300. }
  301. else
  302. {
  303. /* Read Error Flag RDERR is reset */
  304. status = (FlagStatus) RESET;
  305. }
  306. }
  307. else
  308. {
  309. if ((AES->SR & (uint8_t)AES_FLAG_WRERR) != (uint8_t)0x00)
  310. {
  311. /* Write Error Flag WRERR is set */
  312. status = (FlagStatus) SET;
  313. }
  314. else
  315. {
  316. /* Write Error Flag WRERR is reset */
  317. status = (FlagStatus) RESET;
  318. }
  319. }
  320. /* Return the AES_FLAG status */
  321. return ((FlagStatus) status);
  322. }
  323. /**
  324. * @brief Clears the AES flags.
  325. * @param AES_FLAG: specifies the flag to clear.
  326. * This parameter can be one of the following values:
  327. * @arg AES_FLAG_CCF: Computation Complete Flag
  328. * @arg AES_FLAG_RDERR: Read Error Flag
  329. * @arg AES_FLAG_WRERR: Write Error Flag
  330. * @retval None
  331. */
  332. void AES_ClearFlag(AES_FLAG_TypeDef AES_FLAG)
  333. {
  334. /* Check the parameters */
  335. assert_param(IS_AES_FLAG(AES_FLAG));
  336. /* Check if AES_FLAG is AES_FLAG_CCF */
  337. if (AES_FLAG == AES_FLAG_CCF)
  338. {
  339. /* Clear CCF flag by setting CCFC bit */
  340. AES->CR |= (uint8_t) AES_CR_CCFC;
  341. }
  342. else /* AES_FLAG is AES_FLAG_RDERR or AES_FLAG_WRERR */
  343. {
  344. /* Clear RDERR and WRERR flags by setting ERRC bit */
  345. AES->CR |= (uint8_t) AES_CR_ERRC;
  346. }
  347. }
  348. /**
  349. * @brief Checks whether the specified AES interrupt has occurred or not.
  350. * @param AES_IT: Specifies the AES interrupt pending bit to check.
  351. * This parameter can be one of the following values:
  352. * @arg AES_IT_CCIE: Computation Complete interrupt enable
  353. * @arg AES_IT_ERRIE: Error interrupt enable
  354. * @retval ITStatus The new state of AES_IT (SET or RESET).
  355. */
  356. ITStatus AES_GetITStatus(AES_IT_TypeDef AES_IT)
  357. {
  358. ITStatus itstatus = RESET;
  359. BitStatus cciebitstatus, ccfbitstatus = RESET;
  360. /* Check parameters */
  361. assert_param(IS_AES_IT(AES_IT));
  362. cciebitstatus = (BitStatus) (AES->CR & AES_CR_CCIE);
  363. ccfbitstatus = (BitStatus) (AES->SR & AES_SR_CCF);
  364. /* Check if AES_IT is AES_IT_CCIE */
  365. if (AES_IT == AES_IT_CCIE)
  366. {
  367. /* Check the status of the specified AES interrupt */
  368. if (((cciebitstatus) != RESET) && ((ccfbitstatus) != RESET))
  369. {
  370. /* Interrupt occurred */
  371. itstatus = (ITStatus) SET;
  372. }
  373. else
  374. {
  375. /* Interrupt not occurred */
  376. itstatus = (ITStatus) RESET;
  377. }
  378. }
  379. else /* AES_IT is AES_IT_ERRIE */
  380. {
  381. /* Check the status of the specified AES interrupt */
  382. if ((AES->CR & AES_CR_ERRIE) != RESET)
  383. {
  384. /* Check if WRERR or RDERR flags are set */
  385. if ((AES->SR & (uint8_t)(AES_SR_WRERR | AES_SR_RDERR)) != RESET)
  386. {
  387. /* Interrupt occurred */
  388. itstatus = (ITStatus) SET;
  389. }
  390. else
  391. {
  392. /* Interrupt not occurred */
  393. itstatus = (ITStatus) RESET;
  394. }
  395. }
  396. else
  397. {
  398. /* Interrupt not occurred */
  399. itstatus = (ITStatus) RESET;
  400. }
  401. }
  402. /* Return the AES_IT status */
  403. return ((ITStatus)itstatus);
  404. }
  405. /**
  406. * @brief Clears the AES's interrupt pending bits.
  407. * @param AES_IT: specifies the interrupt pending bit to clear.
  408. * This parameter can be one of the following values:
  409. * @arg AES_IT_CCIE: Computation Complete interrupt enable
  410. * @arg AES_IT_ERRIE: Error interrupt enable
  411. * @retval None
  412. */
  413. void AES_ClearITPendingBit(AES_IT_TypeDef AES_IT)
  414. {
  415. /* Check the parameters */
  416. assert_param(IS_AES_IT(AES_IT));
  417. /* Check if AES_IT is AES_IT_CCIE */
  418. if (AES_IT == AES_IT_CCIE)
  419. {
  420. /* Clear CCF flag by setting CCFC bit */
  421. AES->CR |= (uint8_t) AES_CR_CCFC;
  422. }
  423. else /* AES_IT is AES_IT_ERRIE */
  424. {
  425. /* Clear RDERR and WRERR flags by setting ERRC bit */
  426. AES->CR |= (uint8_t) AES_CR_ERRC;
  427. }
  428. }
  429. /**
  430. * @}
  431. */
  432. /**
  433. * @}
  434. */
  435. /**
  436. * @}
  437. */
  438. /**
  439. * @}
  440. */
  441. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/