Skip to content

Commit 896a665

Browse files
committed
add apps
1 parent abd5e66 commit 896a665

File tree

3 files changed

+195
-30
lines changed

3 files changed

+195
-30
lines changed

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,73 @@ View the full speed benchmark [here](https://concyclics.github.io/HiAE/Speed/spe
1616

1717
![speed](image/README/Throughput.png)
1818

19+
#### Bench HiAE on your machine
20+
21+
```bash
22+
gcc -O3 -march=native -I code code/*.c test/performance_test.c -o perf_test
23+
./perf_test
24+
```
25+
1926
## Applications
2027

28+
#### HiAE-HMAC
29+
30+
The File verification with HiAE MAC mode.
31+
32+
**compile**
33+
34+
```bash
35+
gcc -O3 -march=native -I code code/*.c app/HiAE-MAC.c -o HiAE-MAC
36+
```
37+
38+
**Usage**
39+
40+
```bash
41+
./HiAE-MAC <input_file> <key> <iv> [buffer_size (MB)]
42+
```
43+
44+
**Example**
45+
46+
```bash
47+
$ ./HiAE-MAC README.md 1926 0817
48+
KEY = 1926000000000000000000000000000000000000000000000000000000000000
49+
IV = 08170000000000000000000000000000
50+
MAC: c9e1326675a5e70c3609c8eacfe89b83
51+
MAC file README.md (2079 bytes) in 0.000100 seconds
52+
Speed: 0.019362 GB/s
53+
```
54+
2155
#### File AEAD Encryption & Decryption
2256

2357
The File encryption will auto append the AEAD tag after the file for integrity protection.
2458

2559
**compile**
60+
2661
```bash
27-
gcc -O3 -march=native -I code/ code/HiAE.c app/file_enc.c -o FileHiAE
62+
gcc -O3 -march=native -I code code/*.c app/HiAE-File-AEAD.c -o HiAE-File-AEAD
2863
```
2964

3065
**Usage**
3166

3267
```bash
33-
./FileHiAE <encrypt/decrypt> <input_file> <output_file> <key: 32 chars> <iv: 16 chars> [buffer_size(MB): Default 4MB]
68+
./HiAE-File-AEAD <encrypt/decrypt> <input_file> <output_file> <key> <iv> [buffer_size (MB)]
69+
```
70+
71+
**Example**
72+
73+
```bash
74+
$ ./HiAE-File-AEAD encrypt README.md README.enc 1926 0817
75+
KEY = 1926000000000000000000000000000000000000000000000000000000000000
76+
IV = 08170000000000000000000000000000
77+
Encrypted README.md to README.enc (2079 bytes) in 0.000178 seconds
78+
Speed: 0.010878 GB/s
79+
80+
$ ./HiAE-File-AEAD decrypt README.enc README.md 1926 0817
81+
KEY = 1926000000000000000000000000000000000000000000000000000000000000
82+
IV = 08170000000000000000000000000000
83+
Authorization Passed.
84+
Decrypted README.enc to README.md (2079 bytes) in 0.000220 seconds
85+
Speed: 0.008801 GB/s
3486
```
3587

3688
## Acknowledgements
@@ -58,4 +110,3 @@ Their contributions were crucial in ensuring the scalability of HiAE across vari
58110
url = {https://eprint.iacr.org/2025/377}
59111
}
60112
```
61-

app/file_enc.c renamed to app/HiAE-File-AEAD.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <time.h>
66
#include <stdint.h>
77

8-
size_t encrypt_file(const char* input_file, const char* output_file, const char* key, const char* iv, size_t buffer_size) {
8+
size_t encrypt_file(const char* input_file, const char* output_file, const uint8_t* key, const uint8_t* iv, size_t buffer_size) {
99
FILE* in = fopen(input_file, "rb");
1010
if (in == NULL) {
1111
printf("Error: Could not open input file\n");
@@ -18,13 +18,6 @@ size_t encrypt_file(const char* input_file, const char* output_file, const char*
1818
return 0;
1919
}
2020

21-
if (strlen(key) != 32 || strlen(iv) != 16) {
22-
printf("Error: Key and IV must be 32 and 16 characters long respectively\n");
23-
fclose(in);
24-
fclose(out);
25-
return 0;
26-
}
27-
2821
if (buffer_size % 16 != 0) {
2922
printf("Error: Buffer size must be a multiple of 16\n");
3023
fclose(in);
@@ -51,13 +44,8 @@ size_t encrypt_file(const char* input_file, const char* output_file, const char*
5144

5245
uint8_t* tag = (uint8_t*)malloc(16);
5346

54-
uint8_t key_data[32];
55-
uint8_t iv_data[16];
56-
memcpy(key_data, key, 32);
57-
memcpy(iv_data, iv, 16);
58-
5947
DATA128b state[16];
60-
HiAE_stream_init(state, key_data, iv_data);
48+
HiAE_stream_init(state, key, iv);
6149
fseek(in, 0, SEEK_END);
6250
size_t total_size = ftell(in);
6351
fseek(in, 0, SEEK_SET);
@@ -86,7 +74,7 @@ size_t encrypt_file(const char* input_file, const char* output_file, const char*
8674
return total_size;
8775
}
8876

89-
size_t decrypt_file(const char* input_file, const char* output_file, const char* key, const char* iv, size_t buffer_size) {
77+
size_t decrypt_file(const char* input_file, const char* output_file, const uint8_t* key, const uint8_t* iv, size_t buffer_size) {
9078
FILE* in = fopen(input_file, "rb");
9179
if (in == NULL) {
9280
printf("Error: Could not open input file\n");
@@ -130,13 +118,8 @@ size_t decrypt_file(const char* input_file, const char* output_file, const char*
130118

131119
fseek(in, 0, SEEK_SET);
132120

133-
uint8_t key_data[32];
134-
uint8_t iv_data[16];
135-
memcpy(key_data, key, 32);
136-
memcpy(iv_data, iv, 16);
137-
138121
DATA128b state[16];
139-
HiAE_stream_init(state, key_data, iv_data);
122+
HiAE_stream_init(state, key, iv);
140123

141124
//read in[0, -16] into buffer, decrypt, write to out, dont read last 16 bytes
142125
fseek(in, 0, SEEK_END);
@@ -193,15 +176,26 @@ int main(int argc, char** argv) {
193176
}
194177

195178
// check key and iv length
196-
if (strlen(argv[4]) != 32) {
197-
printf("Error: Key must be 32 characters\n");
179+
if (strlen(argv[4]) > 64) {
180+
printf("Error: Key must be in 32 characters\n");
198181
return 1;
199182
}
200-
if (strlen(argv[5]) != 16) {
201-
printf("Error: IV must be 16 characters\n");
183+
if (strlen(argv[5]) > 32) {
184+
printf("Error: IV must be in 16 characters\n");
202185
return 1;
203186
}
204187

188+
uint8_t key[32];
189+
uint8_t iv[16];
190+
memset(key, 0, 32);
191+
memset(iv, 0, 16);
192+
//str to hex
193+
for (int i = 0; i < strlen(argv[4]); i += 2) {
194+
sscanf(argv[4] + i, "%2hhx", &key[i / 2]);
195+
}
196+
for (int i = 0; i < strlen(argv[5]); i += 2) {
197+
sscanf(argv[5] + i, "%2hhx", &iv[i / 2]);
198+
}
205199
size_t buffer_size = 4 * 1024 * 1024; // default buffer size is 4MB
206200

207201
if (argc > 6) {
@@ -213,16 +207,26 @@ int main(int argc, char** argv) {
213207
buffer_size = buffer_size_temp * 1024 * 1024; // convert MB to bytes
214208
}
215209

210+
printf("KEY = ");
211+
for (int i = 0; i < 32; i++) {
212+
printf("%02x", key[i]);
213+
}
214+
printf("\nIV = ");
215+
for (int i = 0; i < 16; i++) {
216+
printf("%02x", iv[i]);
217+
}
218+
printf("\n");
219+
216220
if (strcmp(argv[1], "encrypt") == 0) {
217221
clock_t start = clock();
218-
size_t file_size = encrypt_file(argv[2], argv[3], argv[4], argv[5], buffer_size);
222+
size_t file_size = encrypt_file(argv[2], argv[3], key, iv, buffer_size);
219223
clock_t end = clock();
220224
printf("Encrypted %s to %s (%lu bytes) in %f seconds\n", argv[2], argv[3], file_size, (double)(end - start) / CLOCKS_PER_SEC);
221225
double speed = (double)file_size / (1024 * 1024 * 1024) / ((double)(end - start) / CLOCKS_PER_SEC);
222226
printf("Speed: %f GB/s\n", speed);
223227
} else if (strcmp(argv[1], "decrypt") == 0) {
224228
clock_t start = clock();
225-
size_t file_size = decrypt_file(argv[2], argv[3], argv[4], argv[5], buffer_size);
229+
size_t file_size = decrypt_file(argv[2], argv[3], key, iv, buffer_size);
226230
clock_t end = clock();
227231
printf("Decrypted %s to %s (%lu bytes) in %f seconds\n", argv[2], argv[3], file_size, (double)(end - start) / CLOCKS_PER_SEC);
228232
double speed = (double)file_size / (1024 * 1024 * 1024) / ((double)(end - start) / CLOCKS_PER_SEC);

app/HiAE-MAC.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "HiAE.h"
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <time.h>
6+
#include <stdint.h>
7+
8+
size_t H_Mac(const char* input_file, const uint8_t* key, const uint8_t* iv, uint8_t* tag, size_t buffer_size) {
9+
FILE* in = fopen(input_file, "rb");
10+
if (in == NULL) {
11+
printf("Error: Could not open input file\n");
12+
return 0;
13+
}
14+
15+
if (buffer_size % 16 != 0) {
16+
printf("Error: Buffer size must be a multiple of 16\n");
17+
fclose(in);
18+
return 0;
19+
}
20+
21+
uint8_t* buffer = (uint8_t*)malloc(buffer_size);
22+
if (buffer == NULL) {
23+
printf("Error: Could not allocate buffer\n");
24+
fclose(in);
25+
return 0;
26+
}
27+
28+
29+
DATA128b state[16];
30+
HiAE_stream_init(state, key, iv);
31+
fseek(in, 0, SEEK_END);
32+
size_t total_size = ftell(in);
33+
fseek(in, 0, SEEK_SET);
34+
size_t read_size = 0;
35+
while (read_size < total_size) {
36+
size_t read = fread(buffer, 1, buffer_size, in);
37+
read_size += read;
38+
HiAE_stream_proc_ad(state, buffer, read);
39+
}
40+
41+
HiAE_stream_finalize(state, total_size, 0, tag);
42+
43+
free(buffer);
44+
fclose(in);
45+
return total_size;
46+
}
47+
48+
int main(int argc, char** argv) {
49+
if (argc < 3) {
50+
printf("Usage: %s <input_file> <key> <iv> [buffer_size (MB)]\n", argv[0]);
51+
return 1;
52+
}
53+
54+
// check key and iv length
55+
if (strlen(argv[2]) > 64) {
56+
printf("Error: Key must be in 32 characters\n");
57+
return 1;
58+
}
59+
if (strlen(argv[3]) > 32) {
60+
printf("Error: IV must be in 16 characters\n");
61+
return 1;
62+
}
63+
64+
uint8_t key[32];
65+
uint8_t iv[16];
66+
memset(key, 0, 32);
67+
memset(iv, 0, 16);
68+
//str to hex
69+
for (int i = 0; i < strlen(argv[2]); i += 2) {
70+
sscanf(argv[2] + i, "%2hhx", &key[i / 2]);
71+
}
72+
for (int i = 0; i < strlen(argv[3]); i += 2) {
73+
sscanf(argv[3] + i, "%2hhx", &iv[i / 2]);
74+
}
75+
76+
size_t buffer_size = 4 * 1024 * 1024; // default buffer size is 4MB
77+
78+
if (argc > 4) {
79+
size_t buffer_size_temp = atoi(argv[4]);
80+
if (buffer_size_temp < 1) {
81+
printf("Error: Buffer size must be greater than 0\n");
82+
return 1;
83+
}
84+
buffer_size = buffer_size_temp * 1024 * 1024; // convert MB to bytes
85+
}
86+
87+
printf("KEY = ");
88+
for (int i = 0; i < 32; i++) {
89+
printf("%02x", key[i]);
90+
}
91+
printf("\nIV = ");
92+
for (int i = 0; i < 16; i++) {
93+
printf("%02x", iv[i]);
94+
}
95+
printf("\n");
96+
97+
clock_t start = clock();
98+
uint8_t tag[16];
99+
size_t file_size = H_Mac(argv[1], key, iv, tag, buffer_size);
100+
printf("MAC: ");
101+
for (int i = 0; i < 16; i++) {
102+
printf("%02x", tag[i]);
103+
} printf("\n");
104+
clock_t end = clock();
105+
printf("MAC file %s (%lu bytes) in %f seconds\n", argv[1], file_size, (double)(end - start) / CLOCKS_PER_SEC);
106+
double speed = (double)file_size / (1024 * 1024 * 1024) / ((double)(end - start) / CLOCKS_PER_SEC);
107+
printf("Speed: %f GB/s\n", speed);
108+
109+
return 0;
110+
}

0 commit comments

Comments
 (0)