-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
154 lines (141 loc) · 4.28 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
/************************************************************************
Lab 9 Nios Software
Dong Kai Wang, Fall 2017
Christine Chen, Fall 2013
For use with ECE 385 Experiment 9
University of Illinois ECE Department
************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "aes.h"
// Pointer to base address of AES module, make sure it matches Qsys
volatile unsigned int * AES_PTR = (unsigned int *) 0x00000100;
// Execution mode: 0 for testing, 1 for benchmarking
int run_mode = 0;
/** charToHex
* Convert a single character to the 4-bit value it represents.
*
* Input: a character c (e.g. 'A')
* Output: converted 4-bit value (e.g. 0xA)
*/
char charToHex(char c)
{
char hex = c;
if (hex >= '0' && hex <= '9')
hex -= '0';
else if (hex >= 'A' && hex <= 'F')
{
hex -= 'A';
hex += 10;
}
else if (hex >= 'a' && hex <= 'f')
{
hex -= 'a';
hex += 10;
}
return hex;
}
/** charsToHex
* Convert two characters to byte value it represents.
* Inputs must be 0-9, A-F, or a-f.
*
* Input: two characters c1 and c2 (e.g. 'A' and '7')
* Output: converted byte value (e.g. 0xA7)
*/
char charsToHex(char c1, char c2)
{
char hex1 = charToHex(c1);
char hex2 = charToHex(c2);
return (hex1 << 4) + hex2;
}
/** encrypt
* Perform AES encryption in software.
*
* Input: msg_ascii - Pointer to 32x 8-bit char array that contains the input message in ASCII format
* key_ascii - Pointer to 32x 8-bit char array that contains the input key in ASCII format
* Output: msg_enc - Pointer to 4x 32-bit int array that contains the encrypted message
* key - Pointer to 4x 32-bit int array that contains the input key
*/
void encrypt(unsigned char * msg_ascii, unsigned char * key_ascii, unsigned int * msg_enc, unsigned int * key)
{
// Implement this function
}
/** decrypt
* Perform AES decryption in hardware.
*
* Input: msg_enc - Pointer to 4x 32-bit int array that contains the encrypted message
* key - Pointer to 4x 32-bit int array that contains the input key
* Output: msg_dec - Pointer to 4x 32-bit int array that contains the decrypted message
*/
void decrypt(unsigned int * msg_enc, unsigned int * msg_dec, unsigned int * key)
{
// Implement this function
}
/** main
* Allows the user to enter the message, key, and select execution mode
*
*/
int main()
{
// Input Message and Key as 32x 8-bit ASCII Characters ([33] is for NULL terminator)
unsigned char msg_ascii[33];
unsigned char key_ascii[33];
// Key, Encrypted Message, and Decrypted Message in 4x 32-bit Format to facilitate Read/Write to Hardware
unsigned int key[4];
unsigned int msg_enc[4];
unsigned int msg_dec[4];
printf("Select execution mode: 0 for testing, 1 for benchmarking: ");
scanf("%d", &run_mode);
if (run_mode == 0) {
// Continuously Perform Encryption and Decryption
while (1) {
int i = 0;
printf("\nEnter Message:\n");
scanf("%s", msg_ascii);
printf("\n");
printf("\nEnter Key:\n");
scanf("%s", key_ascii);
printf("\n");
encrypt(msg_ascii, key_ascii, msg_enc, key);
printf("\nEncrpted message is: \n");
for(i = 0; i < 4; i++){
printf("%08x", msg_enc[i]);
}
printf("\n");
decrypt(msg_enc, msg_dec, key);
printf("\nDecrypted message is: \n");
for(i = 0; i < 4; i++){
printf("%08x", msg_dec[i]);
}
printf("\n");
}
}
else {
// Run the Benchmark
int i = 0;
int size_KB = 2;
// Choose a random Plaintext and Key
for (i = 0; i < 32; i++) {
msg_ascii[i] = 'a';
key_ascii[i] = 'b';
}
// Run Encryption
clock_t begin = clock();
for (i = 0; i < size_KB * 64; i++)
encrypt(msg_ascii, key_ascii, msg_enc, key);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
double speed = size_KB / time_spent;
printf("Software Encryption Speed: %f KB/s \n", speed);
// Run Decryption
begin = clock();
for (i = 0; i < size_KB * 64; i++)
decrypt(msg_enc, msg_dec, key);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
speed = size_KB / time_spent;
printf("Hardware Encryption Speed: %f KB/s \n", speed);
}
return 0;
}