-
Notifications
You must be signed in to change notification settings - Fork 1
/
api-EVP.c
257 lines (208 loc) · 5.81 KB
/
api-EVP.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
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
253
254
255
256
257
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define testingFmt " %s:"
#define resultFmt " %s\n"
#include <wolfssl/test.h>
#include <tests/unit.h>
#ifdef WOLFSSL_TEST
#include <wolfssl/options.h>
#include <wolfssl/openssl/ssl.h>
#include <wolfssl/openssl/err.h>
#else
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
static void binary_dump(void *ptr, int size)
{
#ifdef WOLFSSL_EVP_PRINT
int i = 0;
unsigned char *p = (unsigned char *) ptr;
printf("{");
while((p != NULL) && (i < size)) {
if((i % 8) == 0) {
printf("\n");
printf(" ");
}
printf("0x%02x, ", p[i]);
i++;
}
printf("\n};\n");
#else
(void) ptr;
(void) size;
#endif
}
static int last_val = 0x0f;
static int check_result(unsigned char *data, int len)
{
int i, j;
for( ; len; ) {
last_val = (last_val + 1) % 16;
for(i = 0; i < 16; len--, i++, data++)
if(*data != last_val) {
printf("*data=%02x, last_val=%02x, i=%d, len=%d\n",
*data, last_val, i, len);
return -1;
}
}
return 0;
}
static int r_offset;
static int w_offset;
static void init_offset()
{
r_offset = 0;
w_offset = 0;
}
static void get_record(unsigned char *data, unsigned char *buf, int len)
{
memcpy(buf, data+r_offset, len);
r_offset += len;
}
static void set_record(unsigned char *data, unsigned char *buf, int len)
{
memcpy(data+w_offset, buf, len);
w_offset += len;
}
static void set_plain(unsigned char *plain, int rec)
{
int i, j;
unsigned char *p = plain;
#define BLOCKSZ 16
for(i=0; i<(rec/BLOCKSZ); i++){
for(j=0; j<BLOCKSZ; j++)
*p++ = (i % 16);
}
//binary_dump(plain, rec);
}
static int test_wolfSSL_EVP_Cipher(void)
{
/* aes128-cbc, keylen=16, ivlen=16 */
byte aes128_cbc_key[] = {
0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
};
byte aes128_cbc_iv[] = {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
};
int test_drive0[] = {8, 3, 0};
int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
int test_drive2[] = {8, 3, 8, 512, 0};
int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
int *test_drive[] = {test_drive0, test_drive2, test_drive3, NULL};
int test_drive_len[100];
int drive_len;
int ret = 0;
EVP_CIPHER_CTX *evp = NULL;
int klen = 0;
int i, j;
const EVP_CIPHER *type = EVP_aes_128_cbc();
byte *iv;
byte *key;
int keylen;
#define RECORDS 16
#define BUFFSZ 512
byte plain [BUFFSZ * RECORDS];
byte cipher[BUFFSZ * RECORDS];
byte inb[BUFFSZ];
byte outb[BUFFSZ];
int outl, inl;
iv = aes128_cbc_iv;
key = aes128_cbc_key;
keylen = sizeof(aes128_cbc_key);
type = EVP_aes_128_cbc();
printf("Starting\n");
set_plain(plain, BUFFSZ * RECORDS);
printf("Plain Done\n");
#ifdef WOLFSSL_TEST
wolfSSL_Debugging_ON();
#endif
SSL_library_init();
printf(testingFmt, "wolfSSL_EVP_Cipher");
AssertNotNull(evp = EVP_CIPHER_CTX_new());
AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
klen = EVP_CIPHER_CTX_key_length(evp);
if (klen > 0 && keylen != (u_int)klen) {
AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
}
AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
for (j = 0; j<RECORDS; j++)
{
inl = BUFFSZ;
get_record(plain, inb, inl);
//binary_dump(inb, inl);
AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
set_record(cipher, outb, outl);
//binary_dump(outb, outl);
}
for (i = 0; test_drive[i]; i++) {
AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
printf("Enc TEST #%d\n", i);
init_offset();
test_drive_len[i] = 0;
for (j = 0; test_drive[i][j]; j++)
{
inl = test_drive[i][j];
test_drive_len[i] += inl;
get_record(plain, inb, inl);
printf("inl=%d\n", inl);
//binary_dump(inb, inl);
AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
printf("outl=%d\n", outl);
/* output to cipher buffer, so that following Dec test can detect
if any error */
set_record(cipher, outb, outl);
//binary_dump(outb, outl);
if (outl > (inl/16*(16 + 1)) && outl > 16) {
printf("ERROR: outl=%d\n", outl);
return 0;
}
}
ret = EVP_CipherFinal(evp, outb, &outl);
printf("ret=%d, outl=%d, test_drive_len[i]=%d\n", ret, outl, test_drive_len[i]);
if(outl > 0)
set_record(cipher, outb, outl);
}
for (i = 0; test_drive[i]; i++) {
last_val = 0x0f;
drive_len = 0;
AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
printf("Dec TEST #%d\n", i);
init_offset();
for (j = 0; test_drive[i][j]; j++){
inl = test_drive[i][j];
get_record(cipher, inb, inl);
printf("inl=%d\n", inl);
AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
printf("ret=%d, outl=%d\n", ret, outl);
binary_dump(outb, outl);
AssertIntEQ((ret = check_result(outb, outl)), 0);
AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
drive_len += inl;
}
ret = EVP_CipherFinal(evp, outb, &outl);
printf("ret=%d, outl=%d\n", ret, outl);
binary_dump(outb, outl);
if (ret == 0)
{
drive_len += outl;
}
printf("drive_len=%d, test_drive_len[%d]=%d\n", drive_len, i, test_drive_len[i]);
AssertTrue(drive_len == test_drive_len[i]);
}
EVP_CIPHER_CTX_free(evp);
printf("END OF TEST\n");
return 0;
}
int main(int argc, char **argv)
{
test_wolfSSL_EVP_Cipher();
}