nu_crypto.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**************************************************************************//**
  2. * @file crypto.c
  3. * @version V1.10
  4. * $Revision: 3 $
  5. * $Date: 15/06/12 9:42a $
  6. * @brief Cryptographic Accelerator driver source file
  7. *
  8. * @note
  9. * SPDX-License-Identifier: Apache-2.0
  10. * Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
  11. *****************************************************************************/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "N9H30.h"
  15. #include "nu_crypto.h"
  16. /** @addtogroup N9H30_Device_Driver N9H30 Device Driver
  17. @{
  18. */
  19. /** @addtogroup N9H30_CRYPTO_Driver CRYPTO Driver
  20. @{
  21. */
  22. /** @addtogroup N9H30_CRYPTO_EXPORTED_FUNCTIONS CRYPTO Exported Functions
  23. @{
  24. */
  25. /// @cond HIDDEN_SYMBOLS
  26. static uint32_t g_AES_CTL[4];
  27. static uint32_t g_TDES_CTL[4];
  28. /// @endcond HIDDEN_SYMBOLS
  29. /**
  30. * @brief Open PRNG function
  31. * @param[in] u32KeySize is PRNG key size, including:
  32. * - \ref PRNG_KEY_SIZE_64
  33. * - \ref PRNG_KEY_SIZE_128
  34. * - \ref PRNG_KEY_SIZE_192
  35. * - \ref PRNG_KEY_SIZE_256
  36. * @param[in] u32SeedReload is PRNG seed reload or not, including:
  37. * - \ref PRNG_SEED_CONT
  38. * - \ref PRNG_SEED_RELOAD
  39. * @param[in] u32Seed The new seed. Only valid when u32SeedReload is PRNG_SEED_RELOAD.
  40. * @return None
  41. */
  42. void PRNG_Open(uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed)
  43. {
  44. if (u32SeedReload)
  45. CRPT->PRNG_SEED = u32Seed;
  46. CRPT->PRNG_CTL = (u32KeySize << CRPT_PRNG_CTL_KEYSZ_Pos) |
  47. (u32SeedReload << CRPT_PRNG_CTL_SEEDRLD_Pos);
  48. }
  49. /**
  50. * @brief Start to generate one PRNG key.
  51. * @return None
  52. */
  53. void PRNG_Start(void)
  54. {
  55. CRPT->PRNG_CTL |= CRPT_PRNG_CTL_START_Msk;
  56. }
  57. /**
  58. * @brief Read the PRNG key.
  59. * @param[out] u32RandKey The key buffer to store newly generated PRNG key.
  60. * @return None
  61. */
  62. void PRNG_Read(uint32_t u32RandKey[])
  63. {
  64. uint32_t i, wcnt;
  65. wcnt = (((CRPT->PRNG_CTL & CRPT_PRNG_CTL_KEYSZ_Msk) >> CRPT_PRNG_CTL_KEYSZ_Pos) + 1U) * 2U;
  66. for (i = 0U; i < wcnt; i++)
  67. {
  68. u32RandKey[i] = CRPT->PRNG_KEY[i];
  69. }
  70. CRPT->PRNG_CTL &= ~CRPT_PRNG_CTL_SEEDRLD_Msk;
  71. }
  72. /**
  73. * @brief Open AES encrypt/decrypt function.
  74. * @param[in] u32Channel AES channel. Must be 0~3.
  75. * @param[in] u32EncDec 1: AES encode; 0: AES decode
  76. * @param[in] u32OpMode AES operation mode, including:
  77. * - \ref AES_MODE_ECB
  78. * - \ref AES_MODE_CBC
  79. * - \ref AES_MODE_CFB
  80. * - \ref AES_MODE_OFB
  81. * - \ref AES_MODE_CTR
  82. * - \ref AES_MODE_CBC_CS1
  83. * - \ref AES_MODE_CBC_CS2
  84. * - \ref AES_MODE_CBC_CS3
  85. * @param[in] u32KeySize is AES key size, including:
  86. * - \ref AES_KEY_SIZE_128
  87. * - \ref AES_KEY_SIZE_192
  88. * - \ref AES_KEY_SIZE_256
  89. * @param[in] u32SwapType is AES input/output data swap control, including:
  90. * - \ref AES_NO_SWAP
  91. * - \ref AES_OUT_SWAP
  92. * - \ref AES_IN_SWAP
  93. * - \ref AES_IN_OUT_SWAP
  94. * @return None
  95. */
  96. void AES_Open(uint32_t u32Channel, uint32_t u32EncDec,
  97. uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType)
  98. {
  99. CRPT->AES_CTL = (u32Channel << CRPT_AES_CTL_CHANNEL_Pos) |
  100. (u32EncDec << CRPT_AES_CTL_ENCRPT_Pos) |
  101. (u32OpMode << CRPT_AES_CTL_OPMODE_Pos) |
  102. (u32KeySize << CRPT_AES_CTL_KEYSZ_Pos) |
  103. (u32SwapType << CRPT_AES_CTL_OUTSWAP_Pos);
  104. g_AES_CTL[u32Channel] = CRPT->AES_CTL;
  105. }
  106. /**
  107. * @brief Start AES encrypt/decrypt
  108. * @param[in] u32Channel AES channel. Must be 0~3.
  109. * @param[in] u32DMAMode AES DMA control, including:
  110. * - \ref CRYPTO_DMA_ONE_SHOT One shop AES encrypt/decrypt.
  111. * - \ref CRYPTO_DMA_CONTINUE Continuous AES encrypt/decrypt.
  112. * - \ref CRYPTO_DMA_LAST Last AES encrypt/decrypt of a series of AES_Start.
  113. * @return None
  114. */
  115. void AES_Start(int32_t u32Channel, uint32_t u32DMAMode)
  116. {
  117. CRPT->AES_CTL = g_AES_CTL[u32Channel];
  118. CRPT->AES_CTL |= CRPT_AES_CTL_START_Msk | (u32DMAMode << CRPT_AES_CTL_DMALAST_Pos);
  119. }
  120. /**
  121. * @brief Set AES keys
  122. * @param[in] u32Channel AES channel. Must be 0~3.
  123. * @param[in] au32Keys An word array contains AES keys.
  124. * @param[in] u32KeySize is AES key size, including:
  125. * - \ref AES_KEY_SIZE_128
  126. * - \ref AES_KEY_SIZE_192
  127. * - \ref AES_KEY_SIZE_256
  128. * @return None
  129. */
  130. void AES_SetKey(uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize)
  131. {
  132. int i, wcnt;
  133. uint32_t *key_ptr;
  134. key_ptr = (uint32_t *)((uint32_t)&CRPT->AES0_KEY0 + (u32Channel * 0x3C));
  135. wcnt = 4 + u32KeySize * 2;
  136. for (i = 0; i < wcnt; i++, key_ptr++)
  137. *key_ptr = au32Keys[i];
  138. }
  139. /**
  140. * @brief Set AES initial vectors
  141. * @param[in] u32Channel AES channel. Must be 0~3.
  142. * @param[in] au32IV A four entry word array contains AES initial vectors.
  143. * @return None
  144. */
  145. void AES_SetInitVect(uint32_t u32Channel, uint32_t au32IV[])
  146. {
  147. int i;
  148. uint32_t *key_ptr;
  149. key_ptr = (uint32_t *)((uint32_t)&CRPT->AES0_IV0 + (u32Channel * 0x3C));
  150. for (i = 0; i < 4; i++, key_ptr++)
  151. *key_ptr = au32IV[i];
  152. }
  153. /**
  154. * @brief Set AES DMA transfer configuration.
  155. * @param[in] u32Channel AES channel. Must be 0~3.
  156. * @param[in] u32SrcAddr AES DMA source address
  157. * @param[in] u32DstAddr AES DMA destination address
  158. * @param[in] u32TransCnt AES DMA transfer byte count
  159. * @return None
  160. */
  161. void AES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr,
  162. uint32_t u32DstAddr, uint32_t u32TransCnt)
  163. {
  164. *(uint32_t *)((uint32_t)&CRPT->AES0_SADDR + (u32Channel * 0x3C)) = u32SrcAddr;
  165. *(uint32_t *)((uint32_t)&CRPT->AES0_DADDR + (u32Channel * 0x3C)) = u32DstAddr;
  166. *(uint32_t *)((uint32_t)&CRPT->AES0_CNT + (u32Channel * 0x3C)) = u32TransCnt;
  167. }
  168. /**
  169. * @brief Open TDES encrypt/decrypt function.
  170. * @param[in] u32Channel TDES channel. Must be 0~3.
  171. * @param[in] u32EncDec 1: TDES encode; 0: TDES decode
  172. * @param[in] u32OpMode TDES operation mode, including:
  173. * - \ref TDES_MODE_ECB
  174. * - \ref TDES_MODE_CBC
  175. * - \ref TDES_MODE_CFB
  176. * - \ref TDES_MODE_OFB
  177. * - \ref TDES_MODE_CTR
  178. * @param[in] u32SwapType is TDES input/output data swap control and word swap control, including:
  179. * - \ref TDES_NO_SWAP
  180. * - \ref TDES_WHL_SWAP
  181. * - \ref TDES_OUT_SWAP
  182. * - \ref TDES_OUT_WHL_SWAP
  183. * - \ref TDES_IN_SWAP
  184. * - \ref TDES_IN_WHL_SWAP
  185. * - \ref TDES_IN_OUT_SWAP
  186. * - \ref TDES_IN_OUT_WHL_SWAP
  187. * @return None
  188. */
  189. void TDES_Open(uint32_t u32Channel, uint32_t u32EncDec, int32_t Is3DES, int32_t Is3Key,
  190. uint32_t u32OpMode, uint32_t u32SwapType)
  191. {
  192. g_TDES_CTL[u32Channel] = (u32Channel << CRPT_TDES_CTL_CHANNEL_Pos) |
  193. (u32EncDec << CRPT_TDES_CTL_ENCRPT_Pos) |
  194. u32OpMode | (u32SwapType << CRPT_TDES_CTL_BLKSWAP_Pos);
  195. if (Is3DES)
  196. {
  197. g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_TMODE_Msk;
  198. }
  199. if (Is3Key)
  200. {
  201. g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_3KEYS_Msk;
  202. }
  203. }
  204. /**
  205. * @brief Start TDES encrypt/decrypt
  206. * @param[in] u32Channel TDES channel. Must be 0~3.
  207. * @param[in] u32DMAMode TDES DMA control, including:
  208. * - \ref CRYPTO_DMA_ONE_SHOT One shop TDES encrypt/decrypt.
  209. * - \ref CRYPTO_DMA_CONTINUE Continuous TDES encrypt/decrypt.
  210. * - \ref CRYPTO_DMA_LAST Last TDES encrypt/decrypt of a series of TDES_Start.
  211. * @return None
  212. */
  213. void TDES_Start(int32_t u32Channel, uint32_t u32DMAMode)
  214. {
  215. g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_START_Msk | (u32DMAMode << CRPT_TDES_CTL_DMALAST_Pos);
  216. CRPT->TDES_CTL = g_TDES_CTL[u32Channel];
  217. }
  218. /**
  219. * @brief Set TDES keys
  220. * @param[in] u32Channel TDES channel. Must be 0~3.
  221. * @param[in] au8Keys The TDES keys.
  222. * @return None
  223. */
  224. void TDES_SetKey(uint32_t u32Channel, uint32_t au32Keys[3][2])
  225. {
  226. int i;
  227. uint32_t *pu32TKey;
  228. pu32TKey = (uint32_t *)((uint32_t)&CRPT->TDES0_KEY1H + (0x40 * u32Channel));
  229. for (i = 0; i < 3; i++)
  230. {
  231. *pu32TKey = au32Keys[i][0]; /* TDESn_KEYxH */
  232. pu32TKey++;
  233. *pu32TKey = au32Keys[i][1]; /* TDESn_KEYxL */
  234. pu32TKey++;
  235. }
  236. }
  237. /**
  238. * @brief Set TDES initial vectors
  239. * @param[in] u32Channel TDES channel. Must be 0~3.
  240. * @param[in] u32IVH TDES initial vector high word.
  241. * @param[in] u32IVL TDES initial vector low word.
  242. * @return None
  243. */
  244. void TDES_SetInitVect(uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL)
  245. {
  246. *(uint32_t *)((uint32_t)&CRPT->TDES0_IVH + 0x40 * u32Channel) = u32IVH;
  247. *(uint32_t *)((uint32_t)&CRPT->TDES0_IVL + 0x40 * u32Channel) = u32IVL;
  248. }
  249. /**
  250. * @brief Set TDES DMA transfer configuration.
  251. * @param[in] u32Channel TDES channel. Must be 0~3.
  252. * @param[in] u32SrcAddr TDES DMA source address
  253. * @param[in] u32DstAddr TDES DMA destination address
  254. * @param[in] u32TransCnt TDES DMA transfer byte count
  255. * @return None
  256. */
  257. void TDES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr,
  258. uint32_t u32DstAddr, uint32_t u32TransCnt)
  259. {
  260. *(uint32_t *)((uint32_t)&CRPT->TDES0_SADDR + (u32Channel * 0x40)) = u32SrcAddr;
  261. *(uint32_t *)((uint32_t)&CRPT->TDES0_DADDR + (u32Channel * 0x40)) = u32DstAddr;
  262. *(uint32_t *)((uint32_t)&CRPT->TDES0_CNT + (u32Channel * 0x40)) = u32TransCnt;
  263. }
  264. /**
  265. * @brief Open SHA encrypt function.
  266. * @param[in] u32OpMode SHA operation mode, including:
  267. * - \ref SHA_MODE_SHA1
  268. * - \ref SHA_MODE_SHA224
  269. * - \ref SHA_MODE_SHA256
  270. * - \ref SHA_MODE_SHA384
  271. * - \ref SHA_MODE_SHA512
  272. * @param[in] u32SwapType is SHA input/output data swap control, including:
  273. * - \ref SHA_NO_SWAP
  274. * - \ref SHA_OUT_SWAP
  275. * - \ref SHA_IN_SWAP
  276. * - \ref SHA_IN_OUT_SWAP
  277. * @param[in] hmac_key_len The length of HMAC key if HMAC is employed.
  278. * If HMAC is not used, just give hmac_key_len a zero value.
  279. * @return None
  280. */
  281. void SHA_Open(uint32_t u32OpMode, uint32_t u32SwapType, int hmac_key_len)
  282. {
  283. CRPT->HMAC_CTL = (u32OpMode << CRPT_HMAC_CTL_OPMODE_Pos) |
  284. (u32SwapType << CRPT_HMAC_CTL_OUTSWAP_Pos);
  285. if (hmac_key_len > 0)
  286. {
  287. CRPT->HMAC_KEYCNT = hmac_key_len;
  288. CRPT->HMAC_CTL |= CRPT_HMAC_CTL_HMACEN_Msk;
  289. }
  290. }
  291. /**
  292. * @brief Start SHA encrypt
  293. * @param[in] u32DMAMode TDES DMA control, including:
  294. * - \ref CRYPTO_DMA_ONE_SHOT One shop SHA encrypt.
  295. * - \ref CRYPTO_DMA_CONTINUE Continuous SHA encrypt.
  296. * - \ref CRYPTO_DMA_LAST Last SHA encrypt of a series of SHA_Start.
  297. * @return None
  298. */
  299. void SHA_Start(uint32_t u32DMAMode)
  300. {
  301. CRPT->HMAC_CTL &= ~(0x7 << CRPT_HMAC_CTL_DMALAST_Pos);
  302. CRPT->HMAC_CTL |= CRPT_HMAC_CTL_START_Msk | (u32DMAMode << CRPT_HMAC_CTL_DMALAST_Pos);
  303. }
  304. /**
  305. * @brief Set SHA DMA transfer
  306. * @param[in] u32SrcAddr SHA DMA source address
  307. * @param[in] u32TransCnt SHA DMA transfer byte count
  308. * @return None
  309. */
  310. void SHA_SetDMATransfer(uint32_t u32SrcAddr, uint32_t u32TransCnt)
  311. {
  312. CRPT->HMAC_SADDR = u32SrcAddr;
  313. CRPT->HMAC_DMACNT = u32TransCnt;
  314. }
  315. /**
  316. * @brief Read the SHA digest.
  317. * @param[out] u32Digest The SHA encrypt output digest.
  318. * @return None
  319. */
  320. void SHA_Read(uint32_t u32Digest[])
  321. {
  322. uint32_t i, wcnt;
  323. i = (CRPT->HMAC_CTL & CRPT_HMAC_CTL_OPMODE_Msk) >> CRPT_HMAC_CTL_OPMODE_Pos;
  324. if (i == SHA_MODE_SHA1)
  325. {
  326. wcnt = 5UL;
  327. }
  328. else if (i == SHA_MODE_SHA224)
  329. {
  330. wcnt = 7UL;
  331. }
  332. else if (i == SHA_MODE_SHA256)
  333. {
  334. wcnt = 8UL;
  335. }
  336. else if (i == SHA_MODE_SHA384)
  337. {
  338. wcnt = 12UL;
  339. }
  340. else
  341. {
  342. /* SHA_MODE_SHA512 */
  343. wcnt = 16UL;
  344. }
  345. for (i = 0; i < wcnt; i++)
  346. u32Digest[i] = *(uint32_t *)((uint32_t) & (CRPT->HMAC_DGST0) + (i * 4));
  347. }
  348. /*@}*/ /* end of group N9H30_CRYPTO_EXPORTED_FUNCTIONS */
  349. /*@}*/ /* end of group N9H30_CRYPTO_Driver */
  350. /*@}*/ /* end of group N9H30_Device_Driver */
  351. /*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/