bio_enc_test.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2016-2017 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;
  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_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT)))
  46. return 0;
  47. BIO_push(b, BIO_new_mem_buf(inp, DATA_SIZE));
  48. lref = BIO_read(b, ref, sizeof(ref));
  49. BIO_free_all(b);
  50. /* perform split operations and compare to reference */
  51. for (i = 1; i < lref; i++) {
  52. b = BIO_new(BIO_f_cipher());
  53. if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) {
  54. TEST_info("Split encrypt failed @ operation %d", i);
  55. return 0;
  56. }
  57. BIO_push(b, BIO_new_mem_buf(inp, DATA_SIZE));
  58. memset(out, 0, sizeof(out));
  59. out[i] = ~ref[i];
  60. len = BIO_read(b, out, i);
  61. /* check for overstep */
  62. if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) {
  63. TEST_info("Encrypt overstep check failed @ operation %d", i);
  64. return 0;
  65. }
  66. len += BIO_read(b, out + len, sizeof(out) - len);
  67. BIO_free_all(b);
  68. if (!TEST_mem_eq(out, len, ref, lref)) {
  69. TEST_info("Encrypt compare failed @ operation %d", i);
  70. return 0;
  71. }
  72. }
  73. /* perform small-chunk operations and compare to reference */
  74. for (i = 1; i < lref / 2; i++) {
  75. int delta;
  76. b = BIO_new(BIO_f_cipher());
  77. if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) {
  78. TEST_info("Small chunk encrypt failed @ operation %d", i);
  79. return 0;
  80. }
  81. BIO_push(b, BIO_new_mem_buf(inp, DATA_SIZE));
  82. memset(out, 0, sizeof(out));
  83. for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
  84. len += delta;
  85. }
  86. BIO_free_all(b);
  87. if (!TEST_mem_eq(out, len, ref, lref)) {
  88. TEST_info("Small chunk encrypt compare failed @ operation %d", i);
  89. return 0;
  90. }
  91. }
  92. /* Decrypt tests */
  93. /* reference output for single-chunk operation */
  94. b = BIO_new(BIO_f_cipher());
  95. if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT)))
  96. return 0;
  97. /* Use original reference output as input */
  98. BIO_push(b, BIO_new_mem_buf(ref, lref));
  99. (void)BIO_flush(b);
  100. memset(out, 0, sizeof(out));
  101. len = BIO_read(b, out, sizeof(out));
  102. BIO_free_all(b);
  103. if (!TEST_mem_eq(inp, DATA_SIZE, out, len))
  104. return 0;
  105. /* perform split operations and compare to reference */
  106. for (i = 1; i < lref; i++) {
  107. b = BIO_new(BIO_f_cipher());
  108. if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) {
  109. TEST_info("Split decrypt failed @ operation %d", i);
  110. return 0;
  111. }
  112. BIO_push(b, BIO_new_mem_buf(ref, lref));
  113. memset(out, 0, sizeof(out));
  114. out[i] = ~ref[i];
  115. len = BIO_read(b, out, i);
  116. /* check for overstep */
  117. if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) {
  118. TEST_info("Decrypt overstep check failed @ operation %d", i);
  119. return 0;
  120. }
  121. len += BIO_read(b, out + len, sizeof(out) - len);
  122. BIO_free_all(b);
  123. if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) {
  124. TEST_info("Decrypt compare failed @ operation %d", i);
  125. return 0;
  126. }
  127. }
  128. /* perform small-chunk operations and compare to reference */
  129. for (i = 1; i < lref / 2; i++) {
  130. int delta;
  131. b = BIO_new(BIO_f_cipher());
  132. if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) {
  133. TEST_info("Small chunk decrypt failed @ operation %d", i);
  134. return 0;
  135. }
  136. BIO_push(b, BIO_new_mem_buf(ref, lref));
  137. memset(out, 0, sizeof(out));
  138. for (len = 0; (delta = BIO_read(b, out + len, i)); ) {
  139. len += delta;
  140. }
  141. BIO_free_all(b);
  142. if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) {
  143. TEST_info("Small chunk decrypt compare failed @ operation %d", i);
  144. return 0;
  145. }
  146. }
  147. return 1;
  148. }
  149. static int do_test_bio_cipher(const EVP_CIPHER* cipher, int idx)
  150. {
  151. switch(idx)
  152. {
  153. case 0:
  154. return do_bio_cipher(cipher, KEY, NULL);
  155. case 1:
  156. return do_bio_cipher(cipher, KEY, IV);
  157. }
  158. return 0;
  159. }
  160. static int test_bio_enc_aes_128_cbc(int idx)
  161. {
  162. return do_test_bio_cipher(EVP_aes_128_cbc(), idx);
  163. }
  164. static int test_bio_enc_aes_128_ctr(int idx)
  165. {
  166. return do_test_bio_cipher(EVP_aes_128_ctr(), idx);
  167. }
  168. static int test_bio_enc_aes_256_cfb(int idx)
  169. {
  170. return do_test_bio_cipher(EVP_aes_256_cfb(), idx);
  171. }
  172. static int test_bio_enc_aes_256_ofb(int idx)
  173. {
  174. return do_test_bio_cipher(EVP_aes_256_ofb(), idx);
  175. }
  176. # ifndef OPENSSL_NO_CHACHA
  177. static int test_bio_enc_chacha20(int idx)
  178. {
  179. return do_test_bio_cipher(EVP_chacha20(), idx);
  180. }
  181. # ifndef OPENSSL_NO_POLY1305
  182. static int test_bio_enc_chacha20_poly1305(int idx)
  183. {
  184. return do_test_bio_cipher(EVP_chacha20_poly1305(), idx);
  185. }
  186. # endif
  187. # endif
  188. int setup_tests(void)
  189. {
  190. ADD_ALL_TESTS(test_bio_enc_aes_128_cbc, 2);
  191. ADD_ALL_TESTS(test_bio_enc_aes_128_ctr, 2);
  192. ADD_ALL_TESTS(test_bio_enc_aes_256_cfb, 2);
  193. ADD_ALL_TESTS(test_bio_enc_aes_256_ofb, 2);
  194. # ifndef OPENSSL_NO_CHACHA
  195. ADD_ALL_TESTS(test_bio_enc_chacha20, 2);
  196. # ifndef OPENSSL_NO_POLY1305
  197. ADD_ALL_TESTS(test_bio_enc_chacha20_poly1305, 2);
  198. # endif
  199. # endif
  200. return 1;
  201. }