Chameleon-Mini
CryptoAES128.h
1 /* CryptoAES128.h : Adds support for XMega HW accelerated 128-bit AES encryption
2  * in ECB and CBC modes.
3  * Based in part on the the source code for Microchip's ASF library
4  * available at https://github.com/avrxml/asf (see license below).
5  * Author: Maxie D. Schmidt (@maxieds)
6  */
7 
8 /*****************************************************************************
9  * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
10  *
11  * Subject to your compliance with these terms, you may use Microchip
12  * software and any derivatives exclusively with Microchip products.
13  * It is your responsibility to comply with third party license terms applicable
14  * to your use of third party software (including open source software) that
15  * may accompany Microchip software.
16  *
17  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
18  * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
19  * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
20  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
21  * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
22  * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
23  * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
24  * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
25  * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
26  * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
27  * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
28  *****************************************************************************/
29 
30 #ifndef __CRYPTO_AES128_HW_H__
31 #define __CRYPTO_AES128_HW_H__
32 
33 #include <avr/io.h>
34 
35 /* AES Processing mode */
36 #define CRYPTO_AES_PMODE_DECIPHER 0 // decipher
37 #define CRYPTO_AES_PMODE_ENCIPHER 1 // cipher
38 
39 /* AES Start mode */
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
43 
44 /* AES Cryptographic key size */
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
48 
49 /* AES Operation cipher mode */
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
55 
56 /* AES URAD Type */
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
63 
64 /* Define types for the AES-128 specific implementation: */
65 #define CRYPTO_AES_KEY_SIZE 16
66 typedef uint8_t CryptoAESKey_t[CRYPTO_AES_KEY_SIZE];
67 
68 #define CRYPTO_AES_BLOCK_SIZE 16
69 typedef uint8_t CryptoAESBlock_t[CRYPTO_AES_BLOCK_SIZE];
70 
71 #define CryptoAESBytesToBlocks(byteCount) \
72  ((byteCount + CRYPTO_AES_BLOCK_SIZE - 1) / CRYPTO_AES_BLOCK_SIZE)
73 
74 typedef enum aes_dec {
75  AES_ENCRYPT,
76  AES_DECRYPT = AES_DECRYPT_bm,
77 } CryptoAESDec_t;
78 
79 /* AES Auto Start Trigger settings */
80 typedef enum aes_auto {
81  AES_MANUAL, // manual start mode.
82  AES_AUTO = AES_AUTO_bm, // auto start mode.
83 } CryptoAESAuto_t;
84 
85 /* AES State XOR Load Enable settings */
86 typedef enum aes_xor {
87  AES_XOR_OFF, // state direct load.
88  AES_XOR_ON = AES_XOR_bm, // state XOR load.
89 } CryptoAESXor_t;
90 
91 /* AES Interrupt Enable / Level settings */
92 typedef enum aes_intlvl {
93  AES_INTLVL_OFF = AES_INTLVL_OFF_gc, // Interrupt Disabled
94  AES_INTLVL_LO = AES_INTLVL_LO_gc, // Low Level
95  AES_INTLVL_MED = AES_INTLVL_MED_gc, // Medium Level
96  AES_INTLVL_HI = AES_INTLVL_HI_gc, // High Level
97 } CryptoAESIntlvl_t;
98 
99 /* AES interrupt callback function pointer. */
100 typedef void (*aes_callback_t)(void);
101 
102 typedef struct {
103  CryptoAESDec_t ProcessingMode;
104  uint8_t ProcessingDelay; // [0,15]
105  CryptoAESAuto_t StartMode;
106  unsigned char OpMode; // 0 = ECB, 1 = CBC, 2 = OFB, 3 = CFB, 4 = CTR
107  CryptoAESXor_t XorMode;
108 } CryptoAESConfig_t;
109 
110 typedef struct {
111  unsigned char datrdy; // ENABLE/DISABLE; Data ready interrupt
112  unsigned char urad; // ENABLE/DISABLE; Unspecified Register Access Detection
113 } CryptoAES_ISRConfig_t;
114 
115 /* AES encryption complete. */
116 #define AES_ENCRYPTION_COMPLETE (1UL << 0)
117 
118 /* AES GF multiplication complete. */
119 #define AES_GF_MULTI_COMPLETE (1UL << 1)
120 
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);
136 
137 void CryptoAESGetConfigDefaults(CryptoAESConfig_t *ctx);
138 void CryptoAESInitContext(CryptoAESConfig_t *ctx);
139 uint16_t CryptoAESGetPaddedBufferSize(uint16_t bufSize);
140 
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);
147 
148 typedef uint8_t (*CryptoAESFuncType)(uint8_t *, uint8_t *, uint8_t *);
149 typedef struct {
150  CryptoAESFuncType cryptFunc;
151  uint16_t blockSize;
152 } CryptoAES_CBCSpec_t;
153 
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);
160 
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);
169 
170 /* Crypto utility functions: */
171 #define CRYPTO_BYTES_TO_BLOCKS(numBytes, blockSize) \
172  ( ((numBytes) + (blockSize) - 1) / (blockSize) )
173 
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++); \
180  } \
181  })
182 
183 #endif