-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathaes_client.c
More file actions
112 lines (93 loc) · 2.81 KB
/
aes_client.c
File metadata and controls
112 lines (93 loc) · 2.81 KB
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "aes.h"
#include "aes_cbc.h"
#include "aes_cmac.h"
char teststring[] = "If someone loves a flower, of which just one single blossom \
grows in all the millions and millions of stars, it is enough to make him \
happy just to look at the stars. He can say to himself, \"Somewhere, my \
flower is there...\" But if the sheep eats the flower, in one moment all his \
stars will be darkened... And you think that is not important!";
static void print_hex(uint8_t *ptr, int len)
{
int i;
for(i = 0; i < len; i++)
printf("%2.2x ", ptr[i]);
printf("\n");
}
void aes_128_test()
{
uint8_t key[16] = {};
uint8_t plaintext[16] = "helloworld~";
uint8_t ciphertext[16];
uint8_t decrypted[16];
aes_ctx_t *ctx;
ctx = AES_ctx_alloc(key, 16);
AES_encrypt(ctx, plaintext, ciphertext);
AES_decrypt(ctx, ciphertext, decrypted);
free(ctx);
puts("\n*********AES-128*********");
printf("plaintext: %s\n", plaintext);
printf("encrypted:\n");
print_hex(ciphertext, 16);
printf("decrypted: %s\n", decrypted);
}
void aes_256_test()
{
uint8_t key[32] = {};
uint8_t plaintext[16] = "I'm plaintext";
uint8_t ciphertext[16];
uint8_t decrypted[16];
aes_ctx_t *ctx;
ctx = AES_ctx_alloc(key, 32);
AES_encrypt(ctx, plaintext, ciphertext);
AES_decrypt(ctx, ciphertext, decrypted);
free(ctx);
puts("\n*********AES-256**********");
printf("plaintext: %s\n", plaintext);
printf("encrypted:\n");
print_hex(ciphertext, 16);
printf("decrypted: %s\n", decrypted);
}
void aes_128_cbc_test()
{
uint8_t *plaintext = teststring;
uint8_t ciphertext[512], decrypted[512];
uint8_t key[16] = "secretkey";
uint8_t iv[16] = "initialvec";
unsigned long ciphertext_len;
unsigned long plaintext_len;
puts("\n*********AES-128-CBC**********");
plaintext_len = strlen(plaintext);
ciphertext_len = AES_CBC_encrypt(plaintext, ciphertext, plaintext_len, key, 16, iv);
printf("key:\n");
print_hex(key, 16);
printf("iv:\n");
print_hex(iv, 16);
printf("plaintext: %lu bytes\n%s\n", plaintext_len, plaintext);
printf("ciphertext: %lu bytes\n", ciphertext_len);
print_hex(ciphertext, ciphertext_len);
AES_CBC_decrypt(ciphertext, decrypted, plaintext_len, key, 16, iv);
printf("decrypted:\n%s\n", decrypted);
}
void aes_cmac_test()
{
uint8_t key[16] = "secretkey";
uint8_t mac[16];
uint8_t *input = teststring;
puts("\n*********AES-CMAC**********");
aes_cmac(input, strlen(input), key, mac);
printf("message:\n%s\n", input);
printf("key: %s\n", key);
printf("CMAC result:\n");
print_hex(mac, 16);
}
int main()
{
aes_128_test();
// aes_256_test();
aes_128_cbc_test();
aes_cmac_test();
}