2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
11 #include <openssl/evp.h>
12 #include <openssl/bio.h>
13 #include <openssl/rand.h>
20 #define DATA_SIZE 1024
22 #define BUF_SIZE (DATA_SIZE + MAX_IV)
24 static 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
31 static 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
38 static int do_bio_cipher(const EVP_CIPHER* cipher, const unsigned char* key,
39 const unsigned char* iv)
42 static unsigned char inp[BUF_SIZE] = { 0 };
43 unsigned char out[BUF_SIZE], ref[BUF_SIZE];
46 /* Fill buffer with non-zero data so that over steps can be detected */
47 if (!TEST_int_gt(RAND_bytes(inp, DATA_SIZE), 0))
52 /* reference output for single-chunk operation */
53 b = BIO_new(BIO_f_cipher());
56 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT)))
58 mem = BIO_new_mem_buf(inp, DATA_SIZE);
62 lref = BIO_read(b, ref, sizeof(ref));
65 /* perform split operations and compare to reference */
66 for (i = 1; i < lref; i++) {
67 b = BIO_new(BIO_f_cipher());
70 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) {
71 TEST_info("Split encrypt failed @ operation %d", i);
74 mem = BIO_new_mem_buf(inp, DATA_SIZE);
78 memset(out, 0, sizeof(out));
80 len = BIO_read(b, out, i);
81 /* check for overstep */
82 if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) {
83 TEST_info("Encrypt overstep check failed @ operation %d", i);
86 len += BIO_read(b, out + len, sizeof(out) - len);
89 if (!TEST_mem_eq(out, len, ref, lref)) {
90 TEST_info("Encrypt compare failed @ operation %d", i);
95 /* perform small-chunk operations and compare to reference */
96 for (i = 1; i < lref / 2; i++) {
99 b = BIO_new(BIO_f_cipher());
102 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) {
103 TEST_info("Small chunk encrypt failed @ operation %d", i);
106 mem = BIO_new_mem_buf(inp, DATA_SIZE);
110 memset(out, 0, sizeof(out));
111 for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
116 if (!TEST_mem_eq(out, len, ref, lref)) {
117 TEST_info("Small chunk encrypt compare failed @ operation %d", i);
124 /* reference output for single-chunk operation */
125 b = BIO_new(BIO_f_cipher());
128 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT)))
130 /* Use original reference output as input */
131 mem = BIO_new_mem_buf(ref, lref);
136 memset(out, 0, sizeof(out));
137 len = BIO_read(b, out, sizeof(out));
140 if (!TEST_mem_eq(inp, DATA_SIZE, out, len))
143 /* perform split operations and compare to reference */
144 for (i = 1; i < lref; i++) {
145 b = BIO_new(BIO_f_cipher());
148 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) {
149 TEST_info("Split decrypt failed @ operation %d", i);
152 mem = BIO_new_mem_buf(ref, lref);
156 memset(out, 0, sizeof(out));
158 len = BIO_read(b, out, i);
159 /* check for overstep */
160 if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) {
161 TEST_info("Decrypt overstep check failed @ operation %d", i);
164 len += BIO_read(b, out + len, sizeof(out) - len);
167 if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) {
168 TEST_info("Decrypt compare failed @ operation %d", i);
173 /* perform small-chunk operations and compare to reference */
174 for (i = 1; i < lref / 2; i++) {
177 b = BIO_new(BIO_f_cipher());
180 if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) {
181 TEST_info("Small chunk decrypt failed @ operation %d", i);
184 mem = BIO_new_mem_buf(ref, lref);
188 memset(out, 0, sizeof(out));
189 for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
194 if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) {
195 TEST_info("Small chunk decrypt compare failed @ operation %d", i);
207 static int do_test_bio_cipher(const EVP_CIPHER* cipher, int idx)
212 return do_bio_cipher(cipher, KEY, NULL);
214 return do_bio_cipher(cipher, KEY, IV);
219 static int test_bio_enc_aes_128_cbc(int idx)
221 return do_test_bio_cipher(EVP_aes_128_cbc(), idx);
224 static int test_bio_enc_aes_128_ctr(int idx)
226 return do_test_bio_cipher(EVP_aes_128_ctr(), idx);
229 static int test_bio_enc_aes_256_cfb(int idx)
231 return do_test_bio_cipher(EVP_aes_256_cfb(), idx);
234 static int test_bio_enc_aes_256_ofb(int idx)
236 return do_test_bio_cipher(EVP_aes_256_ofb(), idx);
239 # ifndef OPENSSL_NO_CHACHA
240 static int test_bio_enc_chacha20(int idx)
242 return do_test_bio_cipher(EVP_chacha20(), idx);
245 # ifndef OPENSSL_NO_POLY1305
246 static int test_bio_enc_chacha20_poly1305(int idx)
248 return do_test_bio_cipher(EVP_chacha20_poly1305(), idx);
253 int setup_tests(void)
255 ADD_ALL_TESTS(test_bio_enc_aes_128_cbc, 2);
256 ADD_ALL_TESTS(test_bio_enc_aes_128_ctr, 2);
257 ADD_ALL_TESTS(test_bio_enc_aes_256_cfb, 2);
258 ADD_ALL_TESTS(test_bio_enc_aes_256_ofb, 2);
259 # ifndef OPENSSL_NO_CHACHA
260 ADD_ALL_TESTS(test_bio_enc_chacha20, 2);
261 # ifndef OPENSSL_NO_POLY1305
262 ADD_ALL_TESTS(test_bio_enc_chacha20_poly1305, 2);