afalgtest.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include <openssl/opensslconf.h>
  13. #include <string.h>
  14. #include <openssl/engine.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/rand.h>
  17. #include "testutil.h"
  18. /* Use a buffer size which is not aligned to block size */
  19. #define BUFFER_SIZE 17
  20. #ifndef OPENSSL_NO_ENGINE
  21. static ENGINE *e;
  22. #endif
  23. #ifndef OPENSSL_NO_AFALGENG
  24. # include <linux/version.h>
  25. # define K_MAJ 4
  26. # define K_MIN1 1
  27. # define K_MIN2 0
  28. # if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)
  29. /*
  30. * If we get here then it looks like there is a mismatch between the linux
  31. * headers and the actual kernel version, so we have tried to compile with
  32. * afalg support, but then skipped it in e_afalg.c. As far as this test is
  33. * concerned we behave as if we had been configured without support
  34. */
  35. # define OPENSSL_NO_AFALGENG
  36. # endif
  37. #endif
  38. #ifndef OPENSSL_NO_AFALGENG
  39. static int test_afalg_aes_cbc(int keysize_idx)
  40. {
  41. EVP_CIPHER_CTX *ctx;
  42. const EVP_CIPHER *cipher;
  43. unsigned char key[] = "\x06\xa9\x21\x40\x36\xb8\xa1\x5b"
  44. "\x51\x2e\x03\xd5\x34\x12\x00\x06"
  45. "\x06\xa9\x21\x40\x36\xb8\xa1\x5b"
  46. "\x51\x2e\x03\xd5\x34\x12\x00\x06";
  47. unsigned char iv[] = "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30"
  48. "\xb4\x22\xda\x80\x2c\x9f\xac\x41";
  49. /* input = "Single block msg\n" 17Bytes*/
  50. unsigned char in[BUFFER_SIZE] = "\x53\x69\x6e\x67\x6c\x65\x20\x62"
  51. "\x6c\x6f\x63\x6b\x20\x6d\x73\x67\x0a";
  52. unsigned char ebuf[BUFFER_SIZE + 32];
  53. unsigned char dbuf[BUFFER_SIZE + 32];
  54. unsigned char encresult_128[] = "\xe3\x53\x77\x9c\x10\x79\xae\xb8"
  55. "\x27\x08\x94\x2d\xbe\x77\x18\x1a\x2d";
  56. unsigned char encresult_192[] = "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39"
  57. "\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55\xeb";
  58. unsigned char encresult_256[] = "\xa0\x76\x85\xfd\xc1\x65\x71\x9d"
  59. "\xc7\xe9\x13\x6e\xae\x55\x49\xb4\x13";
  60. unsigned char *enc_result = NULL;
  61. int encl, encf, decl, decf;
  62. int ret = 0;
  63. switch (keysize_idx) {
  64. case 0:
  65. cipher = EVP_aes_128_cbc();
  66. enc_result = &encresult_128[0];
  67. break;
  68. case 1:
  69. cipher = EVP_aes_192_cbc();
  70. enc_result = &encresult_192[0];
  71. break;
  72. case 2:
  73. cipher = EVP_aes_256_cbc();
  74. enc_result = &encresult_256[0];
  75. break;
  76. default:
  77. cipher = NULL;
  78. }
  79. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  80. return 0;
  81. if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1))
  82. || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE))
  83. || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf+encl, &encf)))
  84. goto end;
  85. encl += encf;
  86. if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE))
  87. goto end;
  88. if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
  89. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0))
  90. || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl))
  91. || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf+decl, &decf)))
  92. goto end;
  93. decl += decf;
  94. if (!TEST_int_eq(decl, BUFFER_SIZE)
  95. || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE))
  96. goto end;
  97. ret = 1;
  98. end:
  99. EVP_CIPHER_CTX_free(ctx);
  100. return ret;
  101. }
  102. #endif
  103. #ifndef OPENSSL_NO_ENGINE
  104. int global_init(void)
  105. {
  106. ENGINE_load_builtin_engines();
  107. # ifndef OPENSSL_NO_STATIC_ENGINE
  108. OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL);
  109. # endif
  110. return 1;
  111. }
  112. #endif
  113. int setup_tests(void)
  114. {
  115. #ifndef OPENSSL_NO_ENGINE
  116. if ((e = ENGINE_by_id("afalg")) == NULL) {
  117. /* Probably a platform env issue, not a test failure. */
  118. TEST_info("Can't load AFALG engine");
  119. } else {
  120. # ifndef OPENSSL_NO_AFALGENG
  121. ADD_ALL_TESTS(test_afalg_aes_cbc, 3);
  122. # endif
  123. }
  124. #endif
  125. return 1;
  126. }
  127. #ifndef OPENSSL_NO_ENGINE
  128. void cleanup_tests(void)
  129. {
  130. ENGINE_free(e);
  131. }
  132. #endif