30 #ifndef __CRYPTO_AES128_HW_H__ 31 #define __CRYPTO_AES128_HW_H__ 36 #define CRYPTO_AES_PMODE_DECIPHER 0 // decipher 37 #define CRYPTO_AES_PMODE_ENCIPHER 1 // cipher 40 #define CRYPTO_AES_START_MODE_MANUAL 0 // Manual mode 41 #define CRYPTO_AES_START_MODE_AUTO 1 // Auto mode 42 #define CRYPTO_AES_START_MODE_DMA 2 // DMA mode 45 #define CRYPTO_AES_KEY_SIZE_128 0 // 128-bit 46 #define CRYPTO_AES_KEY_SIZE_192 1 // 192-bit 47 #define CRYPTO_AES_KEY_SIZE_256 2 // 256-bit 50 #define CRYPTO_AES_ECB_MODE 0 // Electronic Code Book mode 51 #define CRYPTO_AES_CBC_MODE 1 // Cipher Block Chaining mode 52 #define CRYPTO_AES_OFB_MODE 2 // Output FeedBack mode 53 #define CRYPTO_AES_CFB_MODE 3 // Cipher FeedBack mode 54 #define CRYPTO_AES_CTR_MODE 4 // Counter mode 57 #define CRYPTO_AES_URAT_INPUTWRITE_DMA 0 // Input Data Register written during the data processing in DMA mode 58 #define CRYPTO_AES_URAT_OUTPUTREAD_PROCESS 1 // Output Data Register read during the data processing 59 #define CRYPTO_AES_URAT_MRWRITE_PROCESS 2 // Mode Register written during the data processing 60 #define CRYPTO_AES_URAT_OUTPUTREAD_SUBKEY 3 // Output Data Register read during the sub-keys generation 61 #define CRYPTO_AES_URAT_MRWRITE_SUBKEY 4 // Mode Register written during the sub-keys generation 62 #define CRYPTO_AES_URAT_READ_WRITEONLY 5 // Write-only register read access 65 #define CRYPTO_AES_KEY_SIZE 16 66 typedef uint8_t CryptoAESKey_t[CRYPTO_AES_KEY_SIZE];
68 #define CRYPTO_AES_BLOCK_SIZE 16 69 typedef uint8_t CryptoAESBlock_t[CRYPTO_AES_BLOCK_SIZE];
71 #define CryptoAESBytesToBlocks(byteCount) \ 72 ((byteCount + CRYPTO_AES_BLOCK_SIZE - 1) / CRYPTO_AES_BLOCK_SIZE) 74 typedef enum aes_dec {
76 AES_DECRYPT = AES_DECRYPT_bm,
80 typedef enum aes_auto {
82 AES_AUTO = AES_AUTO_bm,
86 typedef enum aes_xor {
88 AES_XOR_ON = AES_XOR_bm,
92 typedef enum aes_intlvl {
93 AES_INTLVL_OFF = AES_INTLVL_OFF_gc,
94 AES_INTLVL_LO = AES_INTLVL_LO_gc,
95 AES_INTLVL_MED = AES_INTLVL_MED_gc,
96 AES_INTLVL_HI = AES_INTLVL_HI_gc,
100 typedef void (*aes_callback_t)(void);
103 CryptoAESDec_t ProcessingMode;
104 uint8_t ProcessingDelay;
105 CryptoAESAuto_t StartMode;
106 unsigned char OpMode;
107 CryptoAESXor_t XorMode;
111 unsigned char datrdy;
113 } CryptoAES_ISRConfig_t;
116 #define AES_ENCRYPTION_COMPLETE (1UL << 0) 119 #define AES_GF_MULTI_COMPLETE (1UL << 1) 121 void aes_start(
void);
122 void aes_software_reset(
void);
123 bool aes_is_busy(
void);
124 bool aes_is_error(
void);
125 void aes_clear_interrupt_flag(
void);
126 void aes_clear_error_flag(
void);
127 void aes_configure(CryptoAESDec_t op_mode, CryptoAESAuto_t auto_start, CryptoAESXor_t xor_mode);
128 void aes_configure_encrypt(CryptoAESAuto_t auto_start, CryptoAESXor_t xor_mode);
129 void aes_configure_decrypt(CryptoAESAuto_t auto_start, CryptoAESXor_t xor_mode);
130 void aes_set_key(uint8_t *key_in);
131 void aes_get_key(uint8_t *key_out);
132 void aes_write_inputdata(uint8_t *data_in);
133 void aes_read_outputdata(uint8_t *data_out);
134 void aes_isr_configure(CryptoAESIntlvl_t intlvl);
135 void aes_set_callback(
const aes_callback_t callback);
137 void CryptoAESGetConfigDefaults(CryptoAESConfig_t *ctx);
138 void CryptoAESInitContext(CryptoAESConfig_t *ctx);
139 uint16_t CryptoAESGetPaddedBufferSize(uint16_t bufSize);
141 void CryptoAESEncryptBlock(uint8_t *Plaintext, uint8_t *Ciphertext,
const uint8_t *Key,
bool);
142 void CryptoAESDecryptBlock(uint8_t *Plaintext, uint8_t *Ciphertext,
const uint8_t *Key);
143 uint8_t CryptoAESEncryptBuffer(uint16_t Count, uint8_t *Plaintext, uint8_t *Ciphertext,
144 const uint8_t *IV,
const uint8_t *Key);
145 uint8_t CryptoAESDecryptBuffer(uint16_t Count, uint8_t *Plaintext, uint8_t *Ciphertext,
146 const uint8_t *IV,
const uint8_t *Key);
148 typedef uint8_t (*CryptoAESFuncType)(uint8_t *, uint8_t *, uint8_t *);
150 CryptoAESFuncType cryptFunc;
152 } CryptoAES_CBCSpec_t;
154 void CryptoAES_CBCSend(uint16_t Count,
void *Plaintext,
void *Ciphertext,
155 uint8_t *IV, uint8_t *Key,
156 CryptoAES_CBCSpec_t CryptoSpec);
157 void CryptoAES_CBCRecv(uint16_t Count,
void *Plaintext,
void *Ciphertext,
158 uint8_t *IV, uint8_t *Key,
159 CryptoAES_CBCSpec_t CryptoSpec);
161 void CryptoAESEncrypt_CBCSend(uint16_t Count, uint8_t *PlainText, uint8_t *CipherText,
162 uint8_t *Key, uint8_t *IV);
163 void CryptoAESDecrypt_CBCSend(uint16_t Count, uint8_t *PlainText, uint8_t *CipherText,
164 uint8_t *Key, uint8_t *IV);
165 void CryptoAESEncrypt_CBCReceive(uint16_t Count, uint8_t *PlainText, uint8_t *CipherText,
166 uint8_t *Key, uint8_t *IV);
167 void CryptoAESDecrypt_CBCReceive(uint16_t Count, uint8_t *PlainText, uint8_t *CipherText,
168 uint8_t *Key, uint8_t *IV);
171 #define CRYPTO_BYTES_TO_BLOCKS(numBytes, blockSize) \ 172 ( ((numBytes) + (blockSize) - 1) / (blockSize) ) 174 #define CryptoMemoryXOR(inputBuf, destBuf, bufSize) ({ \ 175 uint8_t *in = (uint8_t *) inputBuf; \ 176 uint8_t *out = (uint8_t *) destBuf; \ 177 uint16_t count = (uint16_t) bufSize; \ 178 while(count-- > 0) { \ 179 *(out++) ^= *(in++); \