-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
365 lines (316 loc) · 13.8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the PSoC 6 MCU Cryptography: AES
* Demonstration Example for ModusToolbox.
*
* Related Document: See README.md
*
*
*******************************************************************************
* Copyright 2019-2024, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
*
* This software, including source code, documentation and related
* materials ("Software") is owned by Cypress Semiconductor Corporation
* or one of its affiliates ("Cypress") and is protected by and subject to
* worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ("EULA").
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software
* source code solely for use in connection with Cypress's
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
*******************************************************************************/
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
#include <string.h>
/*******************************************************************************
* Macros
********************************************************************************/
/* The input message size (inclusive of the string terminating character '\0').
* Edit this macro to suit your message size.
*/
#define MAX_MESSAGE_SIZE (100u)
/* Size of the message block that can be processed by Crypto hardware for
* AES encryption.
*/
#define AES128_ENCRYPTION_LENGTH (uint32_t)(16u)
#define AES128_KEY_LENGTH (uint32_t)(16u)
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen. */
#define CLEAR_SCREEN "\x1b[2J\x1b[;H"
/* Number of bytes per line to be printed on the UART terminal. */
#define BYTES_PER_LINE (16u)
/* Time to wait to receive a character from UART terminal. */
#define UART_INPUT_TIMEOUT_MS (1u)
#define SCREEN_HEADER "\r\n__________________________________________________"\
"____________________________\r\n*\t\tCE220465 PSoC 6 "\
"Cryptography: AES Demonstration\r\n*\r\n*\tThis code "\
"example demonstrates encryption and decryption of data "\
"using\r\n*\tthe Advanced Encryption Scheme (AES) algorithm"\
" in PSoC 6 MCU.\r\n*\r\n*\tUART Terminal Settings: Baud Rate"\
"- 115200 bps, 8N1\r\n*"\
"\r\n__________________________________________________"\
"____________________________\r\n"
#define SCREEN_HEADER1 "\r\n\n__________________________________________________"\
"____________________________\r\n"
/*******************************************************************************
* Data type definitions
********************************************************************************/
/* Data type definition to track the state machine accepting the user message */
typedef enum
{
MESSAGE_ENTER_NEW,
MESSAGE_READY
} message_status_t;
/*******************************************************************************
* Function Prototypes
********************************************************************************/
void print_data(uint8_t* data, uint8_t len);
void encrypt_message(uint8_t* message, uint8_t size);
void decrypt_message(uint8_t* message, uint8_t size);
/*******************************************************************************
* Global Variables
********************************************************************************/
/* UART object used for reading character from terminal */
extern cyhal_uart_t cy_retarget_io_uart_obj;
/* Variables to hold the user message and the corresponding encrypted message */
CY_ALIGN(4) uint8_t message[MAX_MESSAGE_SIZE];
CY_ALIGN(4) uint8_t encrypted_msg[MAX_MESSAGE_SIZE];
CY_ALIGN(4) uint8_t decrypted_msg[MAX_MESSAGE_SIZE];
cy_stc_crypto_aes_state_t aes_state;
/* Key used for AES encryption*/
CY_ALIGN(4) uint8_t aes_key[AES128_KEY_LENGTH] = {0xAA, 0xBB, 0xCC, 0xDD,
0xEE, 0xFF, 0xFF, 0xEE,
0xDD, 0xCC, 0xBB, 0xAA,
0xAA, 0xBB, 0xCC, 0xDD,};
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* Main function
*
* Parameters:
* void
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result = CY_RSLT_SUCCESS;
/* Variable to track the status of the message entered by the user */
message_status_t msg_status = MESSAGE_ENTER_NEW;
uint8_t msg_size = 0;
bool uart_status = false;
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Enable global interrupts */
__enable_irq();
/* Initialize retarget-io to use the debug UART port */
cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf(CLEAR_SCREEN);
printf(SCREEN_HEADER);
/* Enable the Crypto block */
Cy_Crypto_Core_Enable(CRYPTO);
printf("\r\nEnter the message:\r\n");
for (;;)
{
switch (msg_status)
{
case MESSAGE_ENTER_NEW:
{
result = cyhal_uart_getc(&cy_retarget_io_uart_obj, &message[msg_size], UART_INPUT_TIMEOUT_MS);
uart_status = cyhal_uart_is_rx_active(&cy_retarget_io_uart_obj);
if (!uart_status && result == CY_RSLT_SUCCESS)
{
/* Check if the ENTER Key is pressed. If pressed, set the message
* status as MESSAGE_READY.
*/
if (message[msg_size] == '\r' || message[msg_size] == '\n')
{
message[msg_size]='\0';
msg_status = MESSAGE_READY;
}
else
{
cyhal_uart_putc(&cy_retarget_io_uart_obj, message[msg_size]);
/* Check if Backspace is pressed by the user. */
if(message[msg_size] != '\b')
{
msg_size++;
}
else
{
if(msg_size > 0)
{
msg_size--;
}
}
/* Check if size of the message exceeds MAX_MESSAGE_SIZE
* (inclusive of the string terminating character '\0').
*/
if (msg_size > (MAX_MESSAGE_SIZE - 1))
{
printf("\r\n\nMessage length exceeds %d characters!!!"\
" Please enter a shorter message\r\nor edit the macro MAX_MESSAGE_SIZE"\
" to suit your message size\r\n", MAX_MESSAGE_SIZE);
/* Clear the message buffer and set the msg_status to accept
* new message from the user.
*/
msg_status = MESSAGE_ENTER_NEW;
memset(message, 0, MAX_MESSAGE_SIZE);
msg_size = 0;
printf("\r\nEnter the message:\r\n");
break;
}
}
}
break;
}
case MESSAGE_READY:
{
encrypt_message(message, msg_size);
decrypt_message(message, msg_size);
/* Clear the message buffer and set the msg_status to accept
* new message from the user.
*/
msg_status = MESSAGE_ENTER_NEW;
memset(message, 0, MAX_MESSAGE_SIZE);
msg_size = 0;
printf("\r\nEnter the message:\r\n");
break;
}
}
}
}
/*******************************************************************************
* Function Name: print_data()
********************************************************************************
* Summary: Function used to display the data in hexadecimal format
*
* Parameters:
* uint8_t* data - Pointer to location of data to be printed
* uint8_t len - length of data to be printed
*
* Return:
* void
*
*******************************************************************************/
void print_data(uint8_t* data, uint8_t len)
{
char print[10];
for (uint32 i=0; i < len; i++)
{
if ((i % BYTES_PER_LINE) == 0)
{
printf("\r\n");
}
sprintf(print,"0x%02X ", *(data+i));
printf("%s", print);
}
printf("\r\n");
}
/*******************************************************************************
* Function Name: encrypt_message
********************************************************************************
* Summary: Function used to encrypt the message.
*
* Parameters:
* char * message - pointer to the message to be encrypted
* uint8_t size - size of message to be encrypted.
*
* Return:
* void
*
*******************************************************************************/
void encrypt_message(uint8_t* message, uint8_t size)
{
uint8_t aes_block_count = 0;
aes_block_count = (size % AES128_ENCRYPTION_LENGTH == 0) ?
(size / AES128_ENCRYPTION_LENGTH)
: (1 + size / AES128_ENCRYPTION_LENGTH);
/* Initializes the AES operation by setting key and key length */
Cy_Crypto_Core_Aes_Init(CRYPTO, aes_key, CY_CRYPTO_KEY_AES_128, &aes_state);
for (int i = 0; i < aes_block_count ; i++)
{
/* Perform AES ECB Encryption mode of operation */
Cy_Crypto_Core_Aes_Ecb(CRYPTO, CY_CRYPTO_ENCRYPT,
(encrypted_msg + AES128_ENCRYPTION_LENGTH * i),
(message + AES128_ENCRYPTION_LENGTH * i),
&aes_state);
/* Wait for Crypto Block to be available */
Cy_Crypto_Core_WaitForReady(CRYPTO);
}
printf("\r\n\nKey used for Encryption:\r\n");
print_data(aes_key, AES128_KEY_LENGTH);
printf("\r\nResult of Encryption:\r\n");
print_data((uint8_t*) encrypted_msg, aes_block_count * AES128_ENCRYPTION_LENGTH);
Cy_Crypto_Core_Aes_Free(CRYPTO, &aes_state);
}
/*******************************************************************************
* Function Name: decrypt_message
********************************************************************************
* Summary: Function used to decrypt the message.
*
* Parameters:
* char * message - pointer to the message to be decrypted
* uint8_t size - size of message to be decrypted.
*
* Return:
* void
*
*******************************************************************************/
void decrypt_message(uint8_t* message, uint8_t size)
{
uint8_t aes_block_count = 0;
aes_block_count = (size % AES128_ENCRYPTION_LENGTH == 0) ?
(size / AES128_ENCRYPTION_LENGTH)
: (1 + size / AES128_ENCRYPTION_LENGTH);
/* Initializes the AES operation by setting key and key length */
Cy_Crypto_Core_Aes_Init(CRYPTO, aes_key, CY_CRYPTO_KEY_AES_128, &aes_state);
/* Start decryption operation*/
for (int i = 0; i < aes_block_count ; i++)
{
/* Perform AES ECB Decryption mode of operation */
Cy_Crypto_Core_Aes_Ecb(CRYPTO, CY_CRYPTO_DECRYPT,
(decrypted_msg + AES128_ENCRYPTION_LENGTH * i),
(encrypted_msg + AES128_ENCRYPTION_LENGTH * i),
&aes_state);
/* Wait for Crypto Block to be available */
Cy_Crypto_Core_WaitForReady(CRYPTO);
}
decrypted_msg[size]='\0';
/* Print the decrypted message on the UART terminal */
printf("\r\nResult of Decryption:\r\n\n");
printf("%s", decrypted_msg);
printf("%s", SCREEN_HEADER1);
Cy_Crypto_Core_Aes_Free(CRYPTO, &aes_state);
}
/* [] END OF FILE */