-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_bench.c
76 lines (61 loc) · 1.54 KB
/
aes_bench.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
#include <stdio.h>
#include "mbedtls/aes.h"
#include "mbedtls/gcm.h"
int main()
{
unsigned char key[32];
unsigned char iv[16];
unsigned char plaintext[128];
unsigned char ciphertext[128];
unsigned char plaintext_result[128];
size_t input_len = 40;
size_t output_len = 0;
memset(plaintext, 0, sizeof(unsigned char));
memset(plaintext_result, 0, sizeof(unsigned char));
for(int i=0; i< 40; ++i)
{
if(i < 32)
{
key[i] = i;
}
if(i < 16)
{
iv[i] = i;
}
plaintext[i] = i;
plaintext_result[i] = -1;
}
printf("Plaintext: ");
for(int i=0; i<40; i++)
{
printf("%x ", plaintext[i]);
}
mbedtls_aes_context aes;
unsigned char tag[16];
mbedtls_gcm_context ctx;
mbedtls_gcm_init(&ctx);
mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, 256);
mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, 40, iv, 16, NULL, 0, plaintext, ciphertext, 16, tag);
printf("\nCiphertext: ");
for(int i=0; i<40; i++)
{
printf("%x ", ciphertext[i]);
}
for(int i=0; i<16; i++)
{
iv[i] = i;
}
printf("\nTag: ");
for(int i=0; i<16; i++)
{
printf("%x ", tag[i]);
}
mbedtls_gcm_auth_decrypt(&ctx, 40, iv, 16, NULL, 0, tag, 16, ciphertext, plaintext_result);
mbedtls_gcm_free(&ctx);
mbedtls_aes_free(&aes);
printf("\nPlaintext result: ");
for(int i=0; i<40; i++)
{
printf("%x ", plaintext_result[i]);
}
}