afalgtest.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <openssl/opensslconf.h>
  11. #include <string.h>
  12. #include <openssl/engine.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/rand.h>
  15. #include "testutil.h"
  16. /* Use a buffer size which is not aligned to block size */
  17. #define BUFFER_SIZE (8 * 1024) - 13
  18. #ifndef OPENSSL_NO_ENGINE
  19. static ENGINE *e;
  20. #endif
  21. #ifndef OPENSSL_NO_AFALGENG
  22. # include <linux/version.h>
  23. # define K_MAJ 4
  24. # define K_MIN1 1
  25. # define K_MIN2 0
  26. # if LINUX_VERSION_CODE <= KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)
  27. /*
  28. * If we get here then it looks like there is a mismatch between the linux
  29. * headers and the actual kernel version, so we have tried to compile with
  30. * afalg support, but then skipped it in e_afalg.c. As far as this test is
  31. * concerned we behave as if we had been configured without support
  32. */
  33. # define OPENSSL_NO_AFALGENG
  34. # endif
  35. #endif
  36. #ifndef OPENSSL_NO_AFALGENG
  37. static int test_afalg_aes_128_cbc(void)
  38. {
  39. EVP_CIPHER_CTX *ctx;
  40. const EVP_CIPHER *cipher = EVP_aes_128_cbc();
  41. unsigned char key[] = "\x5F\x4D\xCC\x3B\x5A\xA7\x65\xD6\
  42. \x1D\x83\x27\xDE\xB8\x82\xCF\x99";
  43. unsigned char iv[] = "\x2B\x95\x99\x0A\x91\x51\x37\x4A\
  44. \xBD\x8F\xF8\xC5\xA7\xA0\xFE\x08";
  45. unsigned char in[BUFFER_SIZE];
  46. unsigned char ebuf[BUFFER_SIZE + 32];
  47. unsigned char dbuf[BUFFER_SIZE + 32];
  48. int encl, encf, decl, decf;
  49. int ret = 0;
  50. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  51. return 0;
  52. RAND_bytes(in, BUFFER_SIZE);
  53. if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1))
  54. || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE))
  55. || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf+encl, &encf)))
  56. goto end;
  57. encl += encf;
  58. if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
  59. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0))
  60. || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl))
  61. || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf+decl, &decf)))
  62. goto end;
  63. decl += decf;
  64. if (!TEST_int_eq(decl, BUFFER_SIZE)
  65. || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE))
  66. goto end;
  67. ret = 1;
  68. end:
  69. EVP_CIPHER_CTX_free(ctx);
  70. return ret;
  71. }
  72. #endif
  73. int main(int argc, char **argv)
  74. {
  75. int ret = 0;
  76. #ifdef OPENSSL_NO_ENGINE
  77. setup_test();
  78. ret = run_tests(argv[0]);
  79. #else
  80. ENGINE_load_builtin_engines();
  81. # ifndef OPENSSL_NO_STATIC_ENGINE
  82. OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL);
  83. # endif
  84. setup_test();
  85. if ((e = ENGINE_by_id("afalg")) == NULL) {
  86. /* Probably a platform env issue, not a test failure. */
  87. TEST_info("Can't load AFALG engine");
  88. } else {
  89. # ifndef OPENSSL_NO_AFALGENG
  90. ADD_TEST(test_afalg_aes_128_cbc);
  91. # endif
  92. }
  93. ret = run_tests(argv[0]);
  94. ENGINE_free(e);
  95. #endif
  96. return finish_test(ret);
  97. }