基于OpenSSL 1.1.0g版本的AES128/CCM实现。
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
/* AES-CCM test data from NIST public test vectors */
static const unsigned char ccm_key[] = {
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b,
0x4c, 0x4d, 0x4e, 0x4f
};
static const unsigned char ccm_nonce[] = {
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b
};
static int aes_128_ccm_encrypt(const char *key, unsigned int key_len,
unsigned int tag_len,
const char *nonce, unsigned int nonce_len,
const char *plaintext, unsigned int plaintext_len,
const char *aad, unsigned int aad_len,
unsigned int ciphertext_buf_len,
char *ciphertext, unsigned int *ciphertext_len
)
{
EVP_CIPHER_CTX *ctx;
int tmp_len;
if (key == NULL || key_len != 16) {
return -1;
}
if (tag_len < 4 || tag_len > 16 || tag_len % 2 != 0) {
return -1;
}
if (nonce == NULL || nonce_len < 7 || nonce_len > 13) {
return -1;
}
if (plaintext == NULL || plaintext_len == 0) {
return -1;
}
if ((aad == NULL && aad_len != 0) ||
(aad != NULL && aad_len == 0) ||
aad_len > 0xFFFFFFFFFFFFFFFFULL) {
return -1;
}
if (ciphertext == NULL) {
return -1;
}
if (ciphertext_buf_len < plaintext_len + tag_len) {
printf("No enough room for ciphertext\n");
return -1;
}
ctx = EVP_CIPHER_CTX_new();
/* Set cipher type and mode */
EVP_EncryptInit_ex(ctx, EVP_aes_128_ccm(), NULL, NULL, NULL);
/* Set nonce length if default 96 bits is not appropriate */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce_len, NULL);
/* Set tag length */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, NULL);
/* Initialise key and IV */
EVP_EncryptInit_ex(ctx, NULL, NULL, key, nonce);
if (aad != NULL) {
/* Set plaintext length: only needed if AAD is used */
EVP_EncryptUpdate(ctx, NULL, &tmp_len, NULL, plaintext_len);
/* Zero or one call to specify any AAD */
EVP_EncryptUpdate(ctx, NULL, &tmp_len, aad, aad_len);
}
/* Encrypt plaintext: can only be called once */
EVP_EncryptUpdate(ctx, ciphertext, &tmp_len, plaintext, plaintext_len);
/* Finalise: note get no output for CCM */
EVP_EncryptFinal_ex(ctx, ciphertext, &tmp_len);
/* Get tag */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, tag_len, ciphertext + plaintext_len);
/* Output Cipher text */
printf("Cipher text:\n");
BIO_dump_fp(stdout, ciphertext, plaintext_len + tag_len);
EVP_CIPHER_CTX_free(ctx);
*ciphertext_len = plaintext_len + tag_len;
return 0;
}
static int aes_128_ccm_decrypt(const char *key, unsigned int key_len,
unsigned int tag_len,
const char *nonce, unsigned int nonce_len,
const char *ciphertext, unsigned int ciphertext_len,
const char *aad, unsigned int aad_len,
unsigned int plaintext_buf_len,
char *plaintext, unsigned int *plaintext_len
)
{
EVP_CIPHER_CTX *ctx;
int tmp_len, rv;
if (key == NULL || key_len != 16) {
return -1;
}
if (tag_len < 4 || tag_len > 16 || tag_len % 2 != 0) {
return -1;
}
if (nonce == NULL || nonce_len < 7 || nonce_len > 13) {
return -1;
}
if (ciphertext == NULL || ciphertext_len == 0) {
return -1;
}
if ((aad == NULL && aad_len != 0) ||
(aad != NULL && aad_len == 0) ||
aad_len > 0xFFFFFFFFFFFFFFFFULL) {
return -1;
}
if (plaintext == NULL) {
return -1;
}
if (plaintext_buf_len < ciphertext_len - tag_len) {
printf("No enough room for plaintext\n");
return -1;
}
ctx = EVP_CIPHER_CTX_new();
/* Select cipher */
EVP_DecryptInit_ex(ctx, EVP_aes_128_ccm(), NULL, NULL, NULL);
/* Set nonce length, omit for 96 bits */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce_len, NULL);
/* Set expected tag value */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, (void *)(ciphertext + ciphertext_len - tag_len));
/* Specify key and IV */
EVP_DecryptInit_ex(ctx, NULL, NULL, key, nonce);
if (aad != NULL) {
/* Set ciphertext length: only needed if we have AAD */
EVP_DecryptUpdate(ctx, NULL, &tmp_len, NULL, ciphertext_len - tag_len);
/* Zero or one call to specify any AAD */
EVP_DecryptUpdate(ctx, NULL, &tmp_len, aad, aad_len);
}
/* Decrypt plaintext, verify tag: can only be called once */
rv = EVP_DecryptUpdate(ctx, plaintext, &tmp_len, ciphertext, ciphertext_len - tag_len);
EVP_CIPHER_CTX_free(ctx);
/* Output decrypted block: if tag verify failed we get nothing */
if (rv > 0) {
printf("Plaintext:\n");
BIO_dump_fp(stdout, plaintext, tmp_len);
} else {
printf("Plaintext not available: tag verify failed.\n");
return -1;
}
*plaintext_len = tmp_len;
return 0;
}
// In xxxx, aad should be NULL, aad_len should be 0,
// tag_len should be 16, nonce_len should be 12.
int aes_128_ccm_encrypt_for_xxxx(const char *key, unsigned int key_len,
const char *nonce, unsigned int nonce_len,
const char *plaintext, unsigned int plaintext_len,
unsigned int ciphertext_buf_len,
char *ciphertext, unsigned int *ciphertext_len
)
{
if (nonce_len != 12) {
return -1;
}
return aes_128_ccm_encrypt(key, key_len, 16, nonce, 12, plaintext, plaintext_len,
NULL, 0, ciphertext_buf_len, ciphertext, ciphertext_len);
}
int aes_128_ccm_decrypt_for_xxxx(const char *key, unsigned int key_len,
const char *nonce, unsigned int nonce_len,
const char *ciphertext, unsigned int ciphertext_len,
unsigned int plaintext_buf_len,
char *plaintext, unsigned int *plaintext_len
)
{
if (nonce_len != 12) {
return -1;
}
return aes_128_ccm_decrypt(key, key_len, 16, nonce, 12, ciphertext, ciphertext_len,
NULL, 0, plaintext_buf_len, plaintext, plaintext_len);
}
int main(int argc, char **argv)
{
unsigned char *ccm_pt = NULL;
unsigned char *ciphertext_buf = NULL;
unsigned char *plaintext_buf = NULL;
int err = 0;
unsigned int len = 0;
int i = 0;
ccm_pt = (char *)malloc(110 * 1024);
if (ccm_pt == NULL) {
printf("malloc for ccm_pt failed\n");
return -1;
}
memset(ccm_pt, 0x5a, 110 * 1024);
ciphertext_buf = (char *)malloc(110 * 1024);
if (ciphertext_buf == NULL) {
printf("malloc for ciphertext_buf failed\n");
return -1;
}
plaintext_buf = (char *)malloc(110 * 1024);
if (plaintext_buf == NULL) {
printf("malloc for plaintext_buf failed\n");
return -1;
}
for (i = 1; i < 10 * 1024 + 1; i++) {
memset(plaintext_buf, 0, 110 * 1024);
memset(ciphertext_buf, 0, 110 * 1024);
err = aes_128_ccm_encrypt_for_xxxx(ccm_key, 16, ccm_nonce, 12, ccm_pt, i, 110 *1024, ciphertext_buf, &len);
if (err < 0) {
printf("encrypt failed[%d]\n", i);
return -1;
} else {
printf("cipher text[%d] len is %d\n", i, len);
}
err = aes_128_ccm_decrypt_for_xxxx(ccm_key, 16, ccm_nonce, 12, ciphertext_buf, len, 110 * 1024, plaintext_buf, &len);
if (err < 0) {
printf("decrypt failed[%d]\n", i);
return -1;
} else {
printf("plain text[%d] len is %d\n", i, len);
}
err = memcmp(ccm_pt, plaintext_buf, i);
if (err != 0) {
printf("compare failed[%d]\n\n", i);
return -1;
} else {
printf("compare success[%d]\n\n", i);
}
}
free(ccm_pt);
free(ciphertext_buf);
free(plaintext_buf);
return 0;
}