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