]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/bio_enc_test.c
X509_VERIFY_PARAM_set_flags.pod: fix typos
[thirdparty/openssl.git] / test / bio_enc_test.c
CommitLineData
e6ed2b91 1/*
ad887416 2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
e6ed2b91 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
e6ed2b91
AP
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9#include <stdio.h>
10#include <string.h>
11#include <openssl/evp.h>
12#include <openssl/bio.h>
61884b81 13#include <openssl/rand.h>
e6ed2b91 14
dd94c37a
JS
15#include "testutil.h"
16
17#define ENCRYPT 1
18#define DECRYPT 0
19
20#define DATA_SIZE 1024
21#define MAX_IV 32
22#define BUF_SIZE (DATA_SIZE + MAX_IV)
23
24static const unsigned char KEY[] = {
25 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
26 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c,
27 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
28 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b
29};
30
31static const unsigned char IV[] = {
32 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
33 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
34 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
35 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
36};
37
38static int do_bio_cipher(const EVP_CIPHER* cipher, const unsigned char* key,
39 const unsigned char* iv)
e6ed2b91
AP
40{
41 BIO *b;
dd94c37a
JS
42 static unsigned char inp[BUF_SIZE] = { 0 };
43 unsigned char out[BUF_SIZE], ref[BUF_SIZE];
e6ed2b91
AP
44 int i, lref, len;
45
61884b81 46 /* Fill buffer with non-zero data so that over steps can be detected */
dd94c37a
JS
47 if (!TEST_int_gt(RAND_bytes(inp, DATA_SIZE), 0))
48 return 0;
e6ed2b91 49
dd94c37a 50 /* Encrypt tests */
e6ed2b91
AP
51
52 /* reference output for single-chunk operation */
53 b = BIO_new(BIO_f_cipher());
684326d3
PH
54 if (!TEST_ptr(b))
55 return 0;
dd94c37a
JS
56 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT)))
57 return 0;
58 BIO_push(b, BIO_new_mem_buf(inp, DATA_SIZE));
e6ed2b91
AP
59 lref = BIO_read(b, ref, sizeof(ref));
60 BIO_free_all(b);
61
62 /* perform split operations and compare to reference */
63 for (i = 1; i < lref; i++) {
64 b = BIO_new(BIO_f_cipher());
684326d3
PH
65 if (!TEST_ptr(b))
66 return 0;
dd94c37a
JS
67 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) {
68 TEST_info("Split encrypt failed @ operation %d", i);
69 return 0;
70 }
71 BIO_push(b, BIO_new_mem_buf(inp, DATA_SIZE));
e6ed2b91
AP
72 memset(out, 0, sizeof(out));
73 out[i] = ~ref[i];
74 len = BIO_read(b, out, i);
75 /* check for overstep */
dd94c37a
JS
76 if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) {
77 TEST_info("Encrypt overstep check failed @ operation %d", i);
78 return 0;
e6ed2b91
AP
79 }
80 len += BIO_read(b, out + len, sizeof(out) - len);
81 BIO_free_all(b);
82
dd94c37a
JS
83 if (!TEST_mem_eq(out, len, ref, lref)) {
84 TEST_info("Encrypt compare failed @ operation %d", i);
85 return 0;
e6ed2b91
AP
86 }
87 }
88
89 /* perform small-chunk operations and compare to reference */
90 for (i = 1; i < lref / 2; i++) {
91 int delta;
92
93 b = BIO_new(BIO_f_cipher());
684326d3
PH
94 if (!TEST_ptr(b))
95 return 0;
dd94c37a
JS
96 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) {
97 TEST_info("Small chunk encrypt failed @ operation %d", i);
98 return 0;
99 }
100 BIO_push(b, BIO_new_mem_buf(inp, DATA_SIZE));
e6ed2b91
AP
101 memset(out, 0, sizeof(out));
102 for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
103 len += delta;
104 }
105 BIO_free_all(b);
106
dd94c37a
JS
107 if (!TEST_mem_eq(out, len, ref, lref)) {
108 TEST_info("Small chunk encrypt compare failed @ operation %d", i);
109 return 0;
e6ed2b91
AP
110 }
111 }
112
dd94c37a 113 /* Decrypt tests */
e6ed2b91
AP
114
115 /* reference output for single-chunk operation */
116 b = BIO_new(BIO_f_cipher());
684326d3
PH
117 if (!TEST_ptr(b))
118 return 0;
dd94c37a
JS
119 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT)))
120 return 0;
121 /* Use original reference output as input */
122 BIO_push(b, BIO_new_mem_buf(ref, lref));
123 (void)BIO_flush(b);
124 memset(out, 0, sizeof(out));
125 len = BIO_read(b, out, sizeof(out));
e6ed2b91
AP
126 BIO_free_all(b);
127
dd94c37a
JS
128 if (!TEST_mem_eq(inp, DATA_SIZE, out, len))
129 return 0;
130
e6ed2b91
AP
131 /* perform split operations and compare to reference */
132 for (i = 1; i < lref; i++) {
133 b = BIO_new(BIO_f_cipher());
684326d3
PH
134 if (!TEST_ptr(b))
135 return 0;
dd94c37a
JS
136 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) {
137 TEST_info("Split decrypt failed @ operation %d", i);
138 return 0;
139 }
140 BIO_push(b, BIO_new_mem_buf(ref, lref));
e6ed2b91
AP
141 memset(out, 0, sizeof(out));
142 out[i] = ~ref[i];
143 len = BIO_read(b, out, i);
144 /* check for overstep */
dd94c37a
JS
145 if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) {
146 TEST_info("Decrypt overstep check failed @ operation %d", i);
147 return 0;
e6ed2b91
AP
148 }
149 len += BIO_read(b, out + len, sizeof(out) - len);
150 BIO_free_all(b);
151
dd94c37a
JS
152 if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) {
153 TEST_info("Decrypt compare failed @ operation %d", i);
154 return 0;
e6ed2b91
AP
155 }
156 }
157
158 /* perform small-chunk operations and compare to reference */
159 for (i = 1; i < lref / 2; i++) {
160 int delta;
161
162 b = BIO_new(BIO_f_cipher());
684326d3
PH
163 if (!TEST_ptr(b))
164 return 0;
dd94c37a
JS
165 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) {
166 TEST_info("Small chunk decrypt failed @ operation %d", i);
167 return 0;
168 }
169 BIO_push(b, BIO_new_mem_buf(ref, lref));
e6ed2b91
AP
170 memset(out, 0, sizeof(out));
171 for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
172 len += delta;
173 }
174 BIO_free_all(b);
175
dd94c37a
JS
176 if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) {
177 TEST_info("Small chunk decrypt compare failed @ operation %d", i);
178 return 0;
e6ed2b91
AP
179 }
180 }
181
dd94c37a
JS
182 return 1;
183}
184
185static int do_test_bio_cipher(const EVP_CIPHER* cipher, int idx)
186{
1287dabd 187 switch (idx)
dd94c37a
JS
188 {
189 case 0:
190 return do_bio_cipher(cipher, KEY, NULL);
191 case 1:
192 return do_bio_cipher(cipher, KEY, IV);
193 }
e6ed2b91
AP
194 return 0;
195}
dd94c37a
JS
196
197static int test_bio_enc_aes_128_cbc(int idx)
198{
199 return do_test_bio_cipher(EVP_aes_128_cbc(), idx);
200}
201
202static int test_bio_enc_aes_128_ctr(int idx)
203{
204 return do_test_bio_cipher(EVP_aes_128_ctr(), idx);
205}
206
207static int test_bio_enc_aes_256_cfb(int idx)
208{
209 return do_test_bio_cipher(EVP_aes_256_cfb(), idx);
210}
211
212static int test_bio_enc_aes_256_ofb(int idx)
213{
214 return do_test_bio_cipher(EVP_aes_256_ofb(), idx);
215}
216
0139ce7c 217# ifndef OPENSSL_NO_CHACHA
dd94c37a
JS
218static int test_bio_enc_chacha20(int idx)
219{
220 return do_test_bio_cipher(EVP_chacha20(), idx);
221}
222
0139ce7c 223# ifndef OPENSSL_NO_POLY1305
dd94c37a
JS
224static int test_bio_enc_chacha20_poly1305(int idx)
225{
226 return do_test_bio_cipher(EVP_chacha20_poly1305(), idx);
227}
0139ce7c
MC
228# endif
229# endif
dd94c37a 230
ad887416 231int setup_tests(void)
dd94c37a
JS
232{
233 ADD_ALL_TESTS(test_bio_enc_aes_128_cbc, 2);
234 ADD_ALL_TESTS(test_bio_enc_aes_128_ctr, 2);
235 ADD_ALL_TESTS(test_bio_enc_aes_256_cfb, 2);
236 ADD_ALL_TESTS(test_bio_enc_aes_256_ofb, 2);
237# ifndef OPENSSL_NO_CHACHA
238 ADD_ALL_TESTS(test_bio_enc_chacha20, 2);
239# ifndef OPENSSL_NO_POLY1305
240 ADD_ALL_TESTS(test_bio_enc_chacha20_poly1305, 2);
241# endif
242# endif
ad887416 243 return 1;
dd94c37a 244}