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
|
/*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (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>
int main()
{
BIO *b;
static const unsigned char key[16] = { 0 };
static unsigned char inp[1024] = { 0 };
unsigned char out[1024], ref[1024];
int i, lref, len;
b = BIO_new(BIO_f_cipher());
if (!BIO_set_cipher(b, EVP_camellia_128_ctr(), key, NULL, 0))
return -1;
BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
lref = BIO_read(b, inp, sizeof(inp));
BIO_free_all(b);
/*
* Exercise CBC cipher
*/
/* reference output for single-chunk operation */
b = BIO_new(BIO_f_cipher());
if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
return -1;
BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
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 (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
return -1;
BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
memset(out, 0, sizeof(out));
out[i] = ~ref[i];
len = BIO_read(b, out, i);
/* check for overstep */
if (out[i] != (unsigned char)~ref[i]) {
fprintf(stderr, "CBC output overstep@%d\n", i);
return 1;
}
len += BIO_read(b, out + len, sizeof(out) - len);
BIO_free_all(b);
if (len != lref || memcmp(out, ref, len)) {
fprintf(stderr, "CBC output mismatch@%d\n", i);
return 2;
}
}
/* perform small-chunk operations and compare to reference */
for (i = 1; i < lref / 2; i++) {
int delta;
b = BIO_new(BIO_f_cipher());
if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
return -1;
BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
memset(out, 0, sizeof(out));
for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
len += delta;
}
BIO_free_all(b);
if (len != lref || memcmp(out, ref, len)) {
fprintf(stderr, "CBC output mismatch@%d\n", i);
return 3;
}
}
/*
* Exercise CTR cipher
*/
/* reference output for single-chunk operation */
b = BIO_new(BIO_f_cipher());
if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
return -1;
BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
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 (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
return -1;
BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
memset(out, 0, sizeof(out));
out[i] = ~ref[i];
len = BIO_read(b, out, i);
/* check for overstep */
if (out[i] != (unsigned char)~ref[i]) {
fprintf(stderr, "CTR output overstep@%d\n", i);
return 4;
}
len += BIO_read(b, out + len, sizeof(out) - len);
BIO_free_all(b);
if (len != lref || memcmp(out, ref, len)) {
fprintf(stderr, "CTR output mismatch@%d\n", i);
return 5;
}
}
/* perform small-chunk operations and compare to reference */
for (i = 1; i < lref / 2; i++) {
int delta;
b = BIO_new(BIO_f_cipher());
if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
return -1;
BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
memset(out, 0, sizeof(out));
for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
len += delta;
}
BIO_free_all(b);
if (len != lref || memcmp(out, ref, len)) {
fprintf(stderr, "CTR output mismatch@%d\n", i);
return 6;
}
}
return 0;
}
|