]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/bio_enc_test.c
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/evp.h>
12 #include <openssl/bio.h>
13 #include <openssl/rand.h>
18 static const unsigned char key
[16] = { 0 };
19 static unsigned char inp
[1024] = { 0 };
20 unsigned char out
[1024], ref
[1024];
23 /* Fill buffer with non-zero data so that over steps can be detected */
24 if (RAND_bytes(inp
, sizeof(inp
)) <= 0)
31 /* reference output for single-chunk operation */
32 b
= BIO_new(BIO_f_cipher());
33 if (!BIO_set_cipher(b
, EVP_aes_128_cbc(), key
, NULL
, 0))
35 BIO_push(b
, BIO_new_mem_buf(inp
, sizeof(inp
)));
36 lref
= BIO_read(b
, ref
, sizeof(ref
));
39 /* perform split operations and compare to reference */
40 for (i
= 1; i
< lref
; i
++) {
41 b
= BIO_new(BIO_f_cipher());
42 if (!BIO_set_cipher(b
, EVP_aes_128_cbc(), key
, NULL
, 0))
44 BIO_push(b
, BIO_new_mem_buf(inp
, sizeof(inp
)));
45 memset(out
, 0, sizeof(out
));
47 len
= BIO_read(b
, out
, i
);
48 /* check for overstep */
49 if (out
[i
] != (unsigned char)~ref
[i
]) {
50 fprintf(stderr
, "CBC output overstep@%d\n", i
);
53 len
+= BIO_read(b
, out
+ len
, sizeof(out
) - len
);
56 if (len
!= lref
|| memcmp(out
, ref
, len
)) {
57 fprintf(stderr
, "CBC output mismatch@%d\n", i
);
62 /* perform small-chunk operations and compare to reference */
63 for (i
= 1; i
< lref
/ 2; i
++) {
66 b
= BIO_new(BIO_f_cipher());
67 if (!BIO_set_cipher(b
, EVP_aes_128_cbc(), key
, NULL
, 0))
69 BIO_push(b
, BIO_new_mem_buf(inp
, sizeof(inp
)));
70 memset(out
, 0, sizeof(out
));
71 for (len
= 0; (delta
= BIO_read(b
, out
+ len
, i
)); ) {
76 if (len
!= lref
|| memcmp(out
, ref
, len
)) {
77 fprintf(stderr
, "CBC output mismatch@%d\n", i
);
86 /* reference output for single-chunk operation */
87 b
= BIO_new(BIO_f_cipher());
88 if (!BIO_set_cipher(b
, EVP_aes_128_ctr(), key
, NULL
, 0))
90 BIO_push(b
, BIO_new_mem_buf(inp
, sizeof(inp
)));
91 lref
= BIO_read(b
, ref
, sizeof(ref
));
94 /* perform split operations and compare to reference */
95 for (i
= 1; i
< lref
; i
++) {
96 b
= BIO_new(BIO_f_cipher());
97 if (!BIO_set_cipher(b
, EVP_aes_128_ctr(), key
, NULL
, 0))
99 BIO_push(b
, BIO_new_mem_buf(inp
, sizeof(inp
)));
100 memset(out
, 0, sizeof(out
));
102 len
= BIO_read(b
, out
, i
);
103 /* check for overstep */
104 if (out
[i
] != (unsigned char)~ref
[i
]) {
105 fprintf(stderr
, "CTR output overstep@%d\n", i
);
108 len
+= BIO_read(b
, out
+ len
, sizeof(out
) - len
);
111 if (len
!= lref
|| memcmp(out
, ref
, len
)) {
112 fprintf(stderr
, "CTR output mismatch@%d\n", i
);
117 /* perform small-chunk operations and compare to reference */
118 for (i
= 1; i
< lref
/ 2; i
++) {
121 b
= BIO_new(BIO_f_cipher());
122 if (!BIO_set_cipher(b
, EVP_aes_128_ctr(), key
, NULL
, 0))
124 BIO_push(b
, BIO_new_mem_buf(inp
, sizeof(inp
)));
125 memset(out
, 0, sizeof(out
));
126 for (len
= 0; (delta
= BIO_read(b
, out
+ len
, i
)); ) {
131 if (len
!= lref
|| memcmp(out
, ref
, len
)) {
132 fprintf(stderr
, "CTR output mismatch@%d\n", i
);