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