Skip to content

Commit 5037d39

Browse files
committed
Add AES with Kyber
1 parent ee37c21 commit 5037d39

File tree

6 files changed

+164
-58
lines changed

6 files changed

+164
-58
lines changed

AES/AES_func.c

+57-18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@ void print_stream(int row, int column, uint8_t (*input)[column]){
1818
printf("\n");
1919
}
2020

21+
void print_text(int row, int column, uint8_t (*input)[column]){
22+
for(int i = 0; i < row; i++){
23+
for(int j = 0; j < column; j++){
24+
if(input[j][i] == '\0'){
25+
printf("\n");
26+
return;
27+
}
28+
printf("%c", input[j][i]);
29+
}
30+
}
31+
printf("\n");
32+
}
33+
34+
void set_text(uint8_t Text[standard_row][standard_column], char *plaintext){
35+
for(int i = 0; i < 4; i++){
36+
for(int j = 0; j < 4; j++)
37+
Text[j][i] = (int)plaintext[4*i + j];
38+
}
39+
}
40+
41+
void set_key(int col, uint8_t cipher_key[standard_row][col], uint8_t *key){
42+
for(int i = 0; i < col; i++){
43+
for(int j = 0; j < 4; j++)
44+
cipher_key[j][i] = key[4*i + j];
45+
}
46+
}
47+
2148
void SubBytes_test(uint8_t (*input)[standard_column]){
2249
printf("--------- Origin block State ---------\n");
2350
print_block(standard_row, standard_column, input);
@@ -214,59 +241,71 @@ void seperate_round_key(int column, uint8_t (*output)[standard_row][standard_col
214241

215242
/* ------------------------------- AES 128,192,256bit ------------------------------- */
216243

217-
void AES_128bit(uint8_t Text[standard_row][standard_column], uint8_t cipher_key[standard_row][4]){
244+
void AES_128bit(char *plaintext, uint8_t key[standard_row * 4]){
218245
printf("\n----------------- AES 128bit ------------------------\n");
246+
uint8_t Text[standard_row][standard_column];
247+
uint8_t cipher_key[standard_row][4];
248+
set_text(Text, plaintext);
249+
set_key(4, cipher_key, key);
219250
uint8_t G_round_key[11][4][4] = {0};
220251
uint8_t round_key[11][4][4] = {0};
221252
Key_Scheduling(10, 4, cipher_key, G_round_key);
222253
seperate_round_key(4, round_key, G_round_key, 128);
223254

224-
printf("plain text\n");
255+
printf("plain text : ");
225256
print_stream(standard_row, standard_column, Text);
226257

227258
encrypt(Text, round_key, 10);
228-
printf("\nencrypted text\n");
229-
print_stream(standard_row, standard_column, Text);
259+
printf("\nencrypted text : ");
260+
print_text(standard_row, standard_column, Text);
230261

231262
decrypt(Text, round_key, 10);
232-
printf("\ndecrypted text\n");
233-
print_stream(standard_row, standard_column, Text);
263+
printf("\ndecrypted text : ");
264+
print_text(standard_row, standard_column, Text);
234265
}
235266

236-
void AES_192bit(uint8_t Text[standard_row][standard_column], uint8_t cipher_key[standard_row][6]){
267+
void AES_192bit(char *plaintext, uint8_t key[standard_row * 6]){
237268
printf("\n----------------- AES 192bit ------------------------\n");
269+
uint8_t Text[standard_row][standard_column];
270+
uint8_t cipher_key[standard_row][6];
271+
set_text(Text, plaintext);
272+
set_key(6, cipher_key, key);
238273
uint8_t G_round_key[9][4][6] = {0};
239274
uint8_t round_key[13][4][4] = {0};
240275
Key_Scheduling(8, 6, cipher_key, G_round_key);
241276
seperate_round_key(6, round_key, G_round_key, 192);
242277

243-
printf("plain text\n");
244-
print_stream(standard_row, standard_column, Text);
278+
printf("plain text : ");
279+
print_text(standard_row, standard_column, Text);
245280

246281
encrypt(Text, round_key, 12);
247-
printf("\nencrypted text\n");
282+
printf("\nencrypted text : ");
248283
print_stream(standard_row, standard_column, Text);
249284

250285
decrypt(Text, round_key, 12);
251-
printf("\ndecrypted text\n");
252-
print_stream(standard_row, standard_column, Text);
286+
printf("\ndecrypted text : ");
287+
print_text(standard_row, standard_column, Text);
253288
}
254289

255-
void AES_256bit(uint8_t Text[standard_row][standard_column], uint8_t cipher_key[standard_row][8]){
290+
void AES_256bit(char *plaintext, uint8_t key[standard_row * 8]){
256291
printf("\n----------------- AES 256bit ------------------------\n");
292+
uint8_t Text[standard_row][standard_column];
293+
uint8_t cipher_key[standard_row][8];
294+
set_text(Text, plaintext);
295+
set_key(8, cipher_key, key);
257296
uint8_t G_round_key[8][4][8] = {0};
258297
uint8_t round_key[16][4][4] = {0};
259298
Key_Scheduling(7, 8, cipher_key, G_round_key);
260299
seperate_round_key(8, round_key, G_round_key, 256);
261300

262-
printf("plain text\n");
263-
print_stream(standard_row, standard_column, Text);
301+
printf("plain text : ");
302+
print_text(standard_row, standard_column, Text);
264303

265304
encrypt(Text, round_key, 14);
266-
printf("\nencrypted text\n");
305+
printf("\nencrypted text : ");
267306
print_stream(standard_row, standard_column, Text);
268307

269308
decrypt(Text, round_key, 14);
270-
printf("\ndecrypted text\n");
271-
print_stream(standard_row, standard_column, Text);
309+
printf("\ndecrypted text : ");
310+
print_text(standard_row, standard_column, Text);
272311
}

AES/AES_func.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ extern const int standard_column;
77
/* ------------------------------- Debugging or Testing Functions -------------------------------*/
88
void print_block(int row, int column, uint8_t (*input)[column]);
99
void print_stream(int row, int column, uint8_t (*input)[column]);
10+
void print_text(int row, int column, uint8_t (*input)[column]);
1011
void SubBytes_test(uint8_t (*input)[standard_column]);
1112
void Shift_Row_test(uint8_t (*input)[standard_column]);
1213
void Mix_columns_test(uint8_t (*input)[standard_column]);
1314

15+
void set_text(uint8_t Text[standard_row][standard_column], char *plaintext);
16+
void set_key(int col, uint8_t cipher_key[standard_row][col], uint8_t *key);
17+
1418
/* ------------------------------- SubBytes -------------------------------*/
1519
void SubBytes(uint8_t (*input)[standard_column], int type);
1620

@@ -40,6 +44,6 @@ void decrypt(uint8_t (*ciphertext)[], uint8_t (*round_key)[standard_row][standar
4044
/* ------------------------------- AES 192bit round_key seperate --------------------- */
4145
void seperate_round_key(int column, uint8_t (*output)[standard_row][standard_column], uint8_t (*input)[standard_row][column], int bit);
4246

43-
void AES_128bit(uint8_t Text[standard_row][standard_column], uint8_t cipher_key[standard_row][4]);
44-
void AES_192bit(uint8_t Text[standard_row][standard_column], uint8_t cipher_key[standard_row][6]);
45-
void AES_256bit(uint8_t Text[standard_row][standard_column], uint8_t cipher_key[standard_row][8]);
47+
void AES_128bit(char *plaintext, uint8_t key[standard_row * 4]);
48+
void AES_192bit(char *plaintext, uint8_t key[standard_row * 6]);
49+
void AES_256bit(char *plaintext, uint8_t key[standard_row * 8]);

Kyber_Dilithium.c

+92-25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
#include "Kyber_Dilithium.h"
2+
#include "./kyber/symmetric.h"
23

34
void print(uint8_t *sub, int lengths){
45
for(int i = 0; i < lengths; i++) printf("%02X", sub[i]);
56
printf("\n");
67
}
78

9+
void sum_buf(uint8_t *A, uint8_t *B, int A_cur_len, int B_len){
10+
for(int i = 0; i < B_len; i++) A[A_cur_len + i] = B[i];
11+
}
12+
13+
void print_mess(uint8_t *arr, int len){
14+
for(int i = 0; i < len; i++){
15+
if(arr[i] == '\0'){
16+
printf("\n");
17+
return;
18+
}
19+
printf("%c", arr[i]);
20+
}
21+
}
22+
823
int Kyber_KE()
924
{
1025
printf("\n--------------------------------- KYBER KE ver. ----------------------------------\n");
@@ -36,7 +51,7 @@ int Kyber_KE()
3651
}
3752

3853
int Kyber_KE_MITM_Attack(){
39-
printf("\n------------------------------------ KYBER KE MITM Attack -------------------------------\n");
54+
printf("\n\n\n\n------------------------------------ KYBER KE MITM Attack -------------------------------\n");
4055
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
4156
uint8_t sk[CRYPTO_SECRETKEYBYTES];
4257
uint8_t ct[CRYPTO_CIPHERTEXTBYTES];
@@ -88,29 +103,82 @@ int Kyber_KE_MITM_Attack(){
88103
return 0;
89104
}
90105

91-
// int kyber_AKE(){
92-
// uint8_t pk_A_auth[CRYPTO_PUBLICKEYBYTES];
93-
// uint8_t sk_A_auth[CRYPTO_SECRETKEYBYTES];
94-
// uint8_t pk_B_auth[CRYPTO_PUBLICKEYBYTES];
95-
// uint8_t sk_B_auth[CRYPTO_SECRETKEYBYTES];
96-
// uint8_t pk[CRYPTO_SECRETKEYBYTES];
97-
// uint8_t sk[CRYPTO_SECRETKEYBYTES];
98-
// uint8_t K_2[2*KYBER_SYMBYTES];
99-
// uint8_t ct[CRYPTO_CIPHERTEXTBYTES];
100-
// uint8_t key_a[CRYPTO_BYTES];
101-
// uint8_t key_b[CRYPTO_BYTES];
102-
103-
// // Alice and Bob generates a auth public key, auth private key
104-
// crypto_kem_keypair(pk_A_auth, sk_A_auth);
105-
// crypto_kem_keypair(pk_B_auth, sk_B_auth);
106-
107-
// // Alice Generates public key, private key.
108-
// crypto_kem_keypair(pk, sk);
109-
// // crypto_kem_enc(ct, key_b, pk_B_auth, K_2);
110-
// }
106+
int Kyber_AKE(uint8_t *cipher_key){
107+
printf("\n\n\n\n--------------------------------- KYBER AKE ver. ----------------------------------\n");
108+
uint8_t pk_A_auth[CRYPTO_PUBLICKEYBYTES];
109+
uint8_t sk_A_auth[CRYPTO_SECRETKEYBYTES];
110+
uint8_t pk_B_auth[CRYPTO_PUBLICKEYBYTES];
111+
uint8_t sk_B_auth[CRYPTO_SECRETKEYBYTES];
112+
uint8_t pk[CRYPTO_SECRETKEYBYTES];
113+
uint8_t sk[CRYPTO_SECRETKEYBYTES];
114+
uint8_t EK[3*KYBER_SYMBYTES];
115+
uint8_t EK_p[3*KYBER_SYMBYTES];
116+
uint8_t EK_T[1*KYBER_SYMBYTES];
117+
uint8_t ct_1[CRYPTO_CIPHERTEXTBYTES];
118+
uint8_t ct_2[CRYPTO_CIPHERTEXTBYTES];
119+
uint8_t key_a[CRYPTO_BYTES];
120+
uint8_t key_b[CRYPTO_BYTES];
121+
122+
// Alice and Bob generates a auth public key, auth private key
123+
crypto_kem_keypair(pk_A_auth, sk_A_auth);
124+
crypto_kem_keypair(pk_B_auth, sk_B_auth);
125+
126+
// Alice Generates public key, private key.
127+
crypto_kem_keypair(pk, sk);
128+
129+
// Alice encapsulating Bob auth public key
130+
crypto_kem_enc(ct_2, EK_T, pk_B_auth);
131+
sum_buf(EK, EK_T, 2*KYBER_SYMBYTES, KYBER_SYMBYTES);
132+
// and send ct_2, pk
133+
134+
// Bob receive ct, pk, and decapsulating ct_2 using Bob auth private key
135+
crypto_kem_dec(EK_T, ct_2, sk_B_auth);
136+
sum_buf(EK_p, EK_T, 2*KYBER_SYMBYTES, KYBER_SYMBYTES);
137+
// And encapuslating Alice public key, and Alice auth public key
138+
crypto_kem_enc(ct_1, EK_T, pk_A_auth);
139+
sum_buf(EK_p, EK_T, KYBER_SYMBYTES, KYBER_SYMBYTES);
140+
crypto_kem_enc(ct_2, EK_T, pk);
141+
sum_buf(EK_p, EK_T, 0, KYBER_SYMBYTES);
142+
// send ct_1, ct_2
143+
144+
// Alice receive ct_1, ct_2, and decapsulating ct_2 using Alice private key, ct_1 using Alice auth private key
145+
crypto_kem_dec(EK_T, ct_2, sk);
146+
sum_buf(EK, EK_T, 0, KYBER_SYMBYTES);
147+
crypto_kem_dec(EK_T, ct_1, sk_A_auth);
148+
sum_buf(EK, EK_T, KYBER_SYMBYTES, KYBER_SYMBYTES);
149+
150+
// Alice Bob generate share key
151+
hash_h(key_a, EK, 3*KYBER_SYMBYTES);
152+
hash_h(key_b, EK_p, 3*KYBER_SYMBYTES);
153+
hash_h(cipher_key, EK, 3*KYBER_SYMBYTES);
154+
155+
printf("Alice Key : ");
156+
print(key_a, CRYPTO_BYTES);
157+
printf("Bob Key : ");
158+
print(key_b, CRYPTO_BYTES);
159+
160+
return 0;
161+
}
162+
163+
void AES_with_Kyber(){
164+
printf("\n\n\nRun AES algorithm using Kyber Symmetric key\n\n");
165+
printf("create key using Kyber\n");
166+
uint8_t cipher_key_256[32];
167+
Kyber_AKE(cipher_key_256);
168+
169+
char plain[16];
170+
printf("> input send message(max 16) : ");
171+
if (fgets(plain, sizeof(plain), stdin) == NULL){
172+
printf("input error");
173+
return;
174+
}
175+
176+
printf("Encrypt & Decrypt AES algorithm using Kyber symmetric key\n");
177+
AES_256bit(plain, cipher_key_256);
178+
}
111179

112180
int dilithium5(){
113-
printf("\n---------------------------- DILITHIUM -----------------------\n");
181+
printf("\n\n\n\n---------------------------- DILITHIUM -----------------------\n");
114182
size_t j;
115183
int ret;
116184
size_t mlen, smlen;
@@ -122,8 +190,7 @@ int dilithium5(){
122190
uint8_t sk[CRYPTO_SECRETKEYBYTES_DILI];
123191

124192
// randombytes(m, MLEN)
125-
printf("Message : ");
126-
printf("%s\n", m);
193+
printf("Message : %s\n", m);
127194

128195
crypto_sign_keypair(pk, sk);
129196

@@ -133,7 +200,7 @@ int dilithium5(){
133200

134201
ret = crypto_sign_open(m2, &mlen, sm, smlen, pk);
135202
printf("Verification Message : ");
136-
printf("%s\n", m2);
203+
print_mess(m2, MLEN);
137204

138205
if(ret) {
139206
fprintf(stderr, "Verification failed\n");

Kyber_Dilithium.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
#define MLEN 59
1111

1212
void print(uint8_t *sub, int lengths);
13+
void sum_buf(uint8_t *A, uint8_t *B, int A_cur_len, int B_len);
14+
void print_mess(uint8_t *arr, int len);
1315

1416
// Kyber
1517
int Kyber_KE();
1618
int Kyber_KE_MITM_Attack();
17-
// int Kyber_AKE();
19+
int Kyber_AKE(uint8_t *cipher_key);
20+
void AES_with_Kyber();
1821

1922
// Dilithium
2023
int dilithium5();

main

328 Bytes
Binary file not shown.

main.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1+
#include <stdio.h>
12
#include "Kyber_Dilithium.h"
23

34
int main(void)
45
{
5-
uint8_t plaintext[4][4] = {{0x32, 0x88, 0x31, 0xe0},
6-
{0x43, 0x5a, 0x31, 0x37},
7-
{0xf6, 0x30, 0x98, 0x07},
8-
{0xa8, 0x8d, 0xa2, 0x34}};
9-
10-
uint8_t cipher_key_256[4][8] = {{0x60, 0x15, 0x2b, 0x85, 0x1f, 0x3b, 0x2d, 0x09},
11-
{0x3d, 0xca, 0x73, 0x7d, 0x35, 0x61, 0x98, 0x14},
12-
{0xeb, 0x71, 0xae, 0x77, 0x2c, 0x08, 0x10, 0xdf},
13-
{0x10, 0xbe, 0xf0, 0x81, 0x07, 0xd7, 0xa3, 0xf4}};
14-
AES_256bit(plaintext, cipher_key_256);
6+
uint8_t cipher_key_256[32];
157
Kyber_KE();
168
Kyber_KE_MITM_Attack();
9+
Kyber_AKE(cipher_key_256);
1710
dilithium5();
18-
11+
AES_with_Kyber();
1912
return 0;
2013
}

0 commit comments

Comments
 (0)