bio_enc_test.c 7.4 KB

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