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
258
259
260
261
262
263
264
265
266
|
/*
* Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/rand.h>
#include "testutil.h"
#define ENCRYPT 1
#define DECRYPT 0
#define DATA_SIZE 1024
#define MAX_IV 32
#define BUF_SIZE (DATA_SIZE + MAX_IV)
static const unsigned char KEY[] = {
0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c,
0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b
};
static const unsigned char IV[] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
static int do_bio_cipher(const EVP_CIPHER* cipher, const unsigned char* key,
const unsigned char* iv)
{
BIO *b, *mem;
static unsigned char inp[BUF_SIZE] = { 0 };
unsigned char out[BUF_SIZE], ref[BUF_SIZE];
int i, lref, len;
/* Fill buffer with non-zero data so that over steps can be detected */
if (!TEST_int_gt(RAND_bytes(inp, DATA_SIZE), 0))
return 0;
/* Encrypt tests */
/* reference output for single-chunk operation */
b = BIO_new(BIO_f_cipher());
if (!TEST_ptr(b))
return 0;
if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT)))
goto err;
mem = BIO_new_mem_buf(inp, DATA_SIZE);
if (!TEST_ptr(mem))
goto err;
BIO_push(b, mem);
lref = BIO_read(b, ref, sizeof(ref));
BIO_free_all(b);
/* perform split operations and compare to reference */
for (i = 1; i < lref; i++) {
b = BIO_new(BIO_f_cipher());
if (!TEST_ptr(b))
return 0;
if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) {
TEST_info("Split encrypt failed @ operation %d", i);
goto err;
}
mem = BIO_new_mem_buf(inp, DATA_SIZE);
if (!TEST_ptr(mem))
goto err;
BIO_push(b, mem);
memset(out, 0, sizeof(out));
out[i] = ~ref[i];
len = BIO_read(b, out, i);
/* check for overstep */
if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) {
TEST_info("Encrypt overstep check failed @ operation %d", i);
goto err;
}
len += BIO_read(b, out + len, sizeof(out) - len);
BIO_free_all(b);
if (!TEST_mem_eq(out, len, ref, lref)) {
TEST_info("Encrypt compare failed @ operation %d", i);
return 0;
}
}
/* perform small-chunk operations and compare to reference */
for (i = 1; i < lref / 2; i++) {
int delta;
b = BIO_new(BIO_f_cipher());
if (!TEST_ptr(b))
return 0;
if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) {
TEST_info("Small chunk encrypt failed @ operation %d", i);
goto err;
}
mem = BIO_new_mem_buf(inp, DATA_SIZE);
if (!TEST_ptr(mem))
goto err;
BIO_push(b, mem);
memset(out, 0, sizeof(out));
for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
len += delta;
}
BIO_free_all(b);
if (!TEST_mem_eq(out, len, ref, lref)) {
TEST_info("Small chunk encrypt compare failed @ operation %d", i);
return 0;
}
}
/* Decrypt tests */
/* reference output for single-chunk operation */
b = BIO_new(BIO_f_cipher());
if (!TEST_ptr(b))
return 0;
if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT)))
goto err;
/* Use original reference output as input */
mem = BIO_new_mem_buf(ref, lref);
if (!TEST_ptr(mem))
goto err;
BIO_push(b, mem);
(void)BIO_flush(b);
memset(out, 0, sizeof(out));
len = BIO_read(b, out, sizeof(out));
BIO_free_all(b);
if (!TEST_mem_eq(inp, DATA_SIZE, out, len))
return 0;
/* perform split operations and compare to reference */
for (i = 1; i < lref; i++) {
b = BIO_new(BIO_f_cipher());
if (!TEST_ptr(b))
return 0;
if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) {
TEST_info("Split decrypt failed @ operation %d", i);
goto err;
}
mem = BIO_new_mem_buf(ref, lref);
if (!TEST_ptr(mem))
goto err;
BIO_push(b, mem);
memset(out, 0, sizeof(out));
out[i] = ~ref[i];
len = BIO_read(b, out, i);
/* check for overstep */
if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) {
TEST_info("Decrypt overstep check failed @ operation %d", i);
goto err;
}
len += BIO_read(b, out + len, sizeof(out) - len);
BIO_free_all(b);
if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) {
TEST_info("Decrypt compare failed @ operation %d", i);
return 0;
}
}
/* perform small-chunk operations and compare to reference */
for (i = 1; i < lref / 2; i++) {
int delta;
b = BIO_new(BIO_f_cipher());
if (!TEST_ptr(b))
return 0;
if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) {
TEST_info("Small chunk decrypt failed @ operation %d", i);
goto err;
}
mem = BIO_new_mem_buf(ref, lref);
if (!TEST_ptr(mem))
goto err;
BIO_push(b, mem);
memset(out, 0, sizeof(out));
for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
len += delta;
}
BIO_free_all(b);
if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) {
TEST_info("Small chunk decrypt compare failed @ operation %d", i);
return 0;
}
}
return 1;
err:
BIO_free_all(b);
return 0;
}
static int do_test_bio_cipher(const EVP_CIPHER* cipher, int idx)
{
switch(idx)
{
case 0:
return do_bio_cipher(cipher, KEY, NULL);
case 1:
return do_bio_cipher
|