-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_128_ecb.c
163 lines (136 loc) · 4.1 KB
/
aes_128_ecb.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
155
156
157
158
159
160
161
162
163
#include <CommonCrypto/CommonCrypto.h>
#include <CommonCrypto/CommonRandom.h>
#include <stdio.h>
#include <stdbool.h>
#include "aes_128_ecb.h"
#include "utility.h"
#include "hex_to_base64.h"
int aes_128_ecb_encrypt(const char *plaintext, size_t plaintext_len, const char *key, size_t key_len, char **out_ciphertext, size_t *out_ciphertext_len)
{
// Ciphertext will be at most one block (16 bytes) larger than the plaintext
char *ciphertext = calloc(1, plaintext_len + 16);
size_t ciphertext_len;
CCCryptorStatus status = CCCrypt(kCCEncrypt, kCCAlgorithmAES, kCCOptionECBMode, key, key_len, NULL, plaintext, plaintext_len, ciphertext, plaintext_len + 16, &ciphertext_len);
if (status != kCCSuccess) {
free(ciphertext);
print_fail("failed to encrypt: %d", status);
return -1;
}
if (out_ciphertext) {
*out_ciphertext = ciphertext;
} else {
free(ciphertext);
}
if (out_ciphertext_len) {
*out_ciphertext_len = ciphertext_len;
}
return 0;
}
int aes_128_ecb_decrypt(const char *ciphertext, size_t ciphertext_len, const char *key, size_t key_len, char **out_plaintext, size_t *out_plaintext_len)
{
// Plaintext has to fit into ciphertext since we can only remove padding
char *plaintext = calloc(1, ciphertext_len);
size_t plaintext_len;
CCCryptorStatus status = CCCrypt(kCCDecrypt, kCCAlgorithmAES, kCCOptionECBMode, key, key_len, NULL, ciphertext, ciphertext_len, plaintext, ciphertext_len, &plaintext_len);
if (status != kCCSuccess) {
free(plaintext);
print_fail("failed to decrypt: %d", status);
return -1;
}
if (out_plaintext) {
*out_plaintext = plaintext;
} else {
free(plaintext);
}
if (out_plaintext_len) {
*out_plaintext_len = plaintext_len;
}
return 0;
}
bool is_aes_128_ecb(const char *ciphertext, size_t ciphertext_len)
{
size_t block_count = ciphertext_len / 16;
for (size_t i = 0; i < block_count; i++) {
for (size_t j = i + 1; j < block_count; j++) {
if (memcmp(ciphertext + (i * 16), ciphertext + (j * 16), 16) == 0) {
return true;
}
}
}
return false;
}
char *aes_generate_key(void)
{
// AES key is 16 bytes
void *key = calloc(1, 16);
CCRNGStatus status = CCRandomGenerateBytes(key, 16);
if (status != kCCSuccess) {
print_fail("failed to generate AES key: %d", status);
free(key);
return NULL;
}
return (char *)key;
}
#if AES_128_ECB_TEST
int main(int argc, char **argv)
{
if (argc < 4) {
print_fail("no input file for AES decrypt");
exit(-1);
}
size_t encrypted_len;
char *encrypted = load_buffer_from_file(argv[1], &encrypted_len);
if (encrypted == NULL) {
print_fail("failed to open input file %s", argv[1]);
exit(-1);
}
size_t raw_encrypted_len;
char *raw_encrypted = NULL;
if (!base64_to_raw(encrypted, encrypted_len, &raw_encrypted, &raw_encrypted_len)) {
print_fail("failed to convert input to raw bytes");
exit(-1);
}
char *plaintext = NULL;
size_t plaintext_len;
if (aes_128_ecb_decrypt(raw_encrypted, raw_encrypted_len, argv[2], strlen(argv[2]), &plaintext, &plaintext_len) != 0 || plaintext == NULL) {
print_fail("AES 128 ECB: failed to decrypt");
exit(-1);
}
char *reencrypted = NULL;
size_t reencrypted_len;
if (aes_128_ecb_encrypt(plaintext, plaintext_len, argv[2], strlen(argv[2]), &reencrypted, &reencrypted_len) != 0 || reencrypted == NULL) {
print_fail("AES 128 ECB: failed to reencrypt");
exit(-1);
}
if (strncmp(reencrypted, raw_encrypted, raw_encrypted_len) != 0) {
print_fail("AES 128 ECB: input and reencrypted don't match");
exit(-1);
}
foreach_line_in_file(argv[3], ^(const char *line, size_t line_len, int index) {
if (line[line_len - 1] == '\n') {
line_len--;
}
size_t raw_len;
char *raw = hex_to_raw(line, line_len, &raw_len);
if (raw == NULL) {
print_fail("failed to convert hex to raw");
exit(-1);
}
if (is_aes_128_ecb(raw, raw_len) && index != 132) {
print_fail("AES 128 ECB: detected wrong line (%d)", index);
}
free(raw);
});
char *key = aes_generate_key();
if (key == NULL) {
print_fail("AES 128: failed to generate AES key");
}
print_success("AES 128 CBC OK");
free(key);
free(reencrypted);
free(plaintext);
free(encrypted);
free(raw_encrypted);
return 0;
}
#endif