Add test/bio_enc_test.c.
[openssl.git] / test / bio_enc_test.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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
8  */
9 #include <stdio.h>
10 #include <string.h>
11 #include <openssl/evp.h>
12 #include <openssl/bio.h>
13
14 int main()
15 {
16     BIO *b;
17     static const unsigned char key[16] = { 0 };
18     static unsigned char inp[1024] = { 0 };
19     unsigned char out[1024], ref[1024];
20     int i, lref, len;
21
22     b = BIO_new(BIO_f_cipher());
23     if (!BIO_set_cipher(b, EVP_camellia_128_ctr(), key, NULL, 0))
24         return -1;
25     BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
26     lref = BIO_read(b, inp, sizeof(inp));
27     BIO_free_all(b);
28
29     /*
30      * Exercise CBC cipher
31      */
32
33     /* reference output for single-chunk operation */
34     b = BIO_new(BIO_f_cipher());
35     if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
36         return -1;
37     BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
38     lref = BIO_read(b, ref, sizeof(ref));
39     BIO_free_all(b);
40
41     /* perform split operations and compare to reference */
42     for (i = 1; i < lref; i++) {
43         b = BIO_new(BIO_f_cipher());
44         if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
45              return -1;
46         BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
47         memset(out, 0, sizeof(out));
48         out[i] = ~ref[i];
49         len = BIO_read(b, out, i);
50         /* check for overstep */
51         if (out[i] != (unsigned char)~ref[i]) {
52             fprintf(stderr, "CBC output overstep@%d\n", i);
53             return 1;
54         }
55         len += BIO_read(b, out + len, sizeof(out) - len);
56         BIO_free_all(b);
57
58         if (len != lref || memcmp(out, ref, len)) {
59             fprintf(stderr, "CBC output mismatch@%d\n", i);
60             return 2;
61         }
62     }
63
64     /* perform small-chunk operations and compare to reference */
65     for (i = 1; i < lref / 2; i++) {
66         int delta;
67
68         b = BIO_new(BIO_f_cipher());
69         if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
70              return -1;
71         BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
72         memset(out, 0, sizeof(out));
73         for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
74             len += delta;
75         }
76         BIO_free_all(b);
77
78         if (len != lref || memcmp(out, ref, len)) {
79             fprintf(stderr, "CBC output mismatch@%d\n", i);
80             return 3;
81         }
82     }
83
84     /*
85      * Exercise CTR cipher
86      */
87
88     /* reference output for single-chunk operation */
89     b = BIO_new(BIO_f_cipher());
90     if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
91          return -1;
92     BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
93     lref = BIO_read(b, ref, sizeof(ref));
94     BIO_free_all(b);
95
96     /* perform split operations and compare to reference */
97     for (i = 1; i < lref; i++) {
98         b = BIO_new(BIO_f_cipher());
99         if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
100              return -1;
101         BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
102         memset(out, 0, sizeof(out));
103         out[i] = ~ref[i];
104         len = BIO_read(b, out, i);
105         /* check for overstep */
106         if (out[i] != (unsigned char)~ref[i]) {
107             fprintf(stderr, "CTR output overstep@%d\n", i);
108             return 4;
109         }
110         len += BIO_read(b, out + len, sizeof(out) - len);
111         BIO_free_all(b);
112
113         if (len != lref || memcmp(out, ref, len)) {
114             fprintf(stderr, "CTR output mismatch@%d\n", i);
115             return 5;
116         }
117     }
118
119     /* perform small-chunk operations and compare to reference */
120     for (i = 1; i < lref / 2; i++) {
121         int delta;
122
123         b = BIO_new(BIO_f_cipher());
124         if (!BIO_set_cipher(b, EVP_aes_128_cbc(), key, NULL, 0))
125              return -1;
126         BIO_push(b, BIO_new_mem_buf(inp, sizeof(inp)));
127         memset(out, 0, sizeof(out));
128         for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
129             len += delta;
130         }
131         BIO_free_all(b);
132
133         if (len != lref || memcmp(out, ref, len)) {
134             fprintf(stderr, "CTR output mismatch@%d\n", i);
135             return 6;
136         }
137     }
138
139     return 0;
140 }