afalgtest.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2016-2021 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. static int test_afalg_aes_cbc(int keysize_idx)
  23. {
  24. EVP_CIPHER_CTX *ctx;
  25. const EVP_CIPHER *cipher;
  26. unsigned char ebuf[BUFFER_SIZE + 32];
  27. unsigned char dbuf[BUFFER_SIZE + 32];
  28. const unsigned char *enc_result = NULL;
  29. int encl, encf, decl, decf;
  30. int ret = 0;
  31. static const unsigned char key[] =
  32. "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06"
  33. "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06";
  34. static const unsigned char iv[] =
  35. "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41";
  36. /* input = "Single block msg\n" 17 Bytes*/
  37. static const unsigned char in[BUFFER_SIZE] =
  38. "\x53\x69\x6e\x67\x6c\x65\x20\x62\x6c\x6f\x63\x6b\x20\x6d\x73\x67"
  39. "\x0a";
  40. static const unsigned char encresult_128[BUFFER_SIZE] =
  41. "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a"
  42. "\x2d";
  43. static const unsigned char encresult_192[BUFFER_SIZE] =
  44. "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55"
  45. "\xeb";
  46. static const unsigned char encresult_256[BUFFER_SIZE] =
  47. "\xa0\x76\x85\xfd\xc1\x65\x71\x9d\xc7\xe9\x13\x6e\xae\x55\x49\xb4"
  48. "\x13";
  49. #ifdef OSSL_SANITIZE_MEMORY
  50. /*
  51. * Initialise the encryption & decryption buffers to pacify the memory
  52. * sanitiser. The sanitiser doesn't know that this memory is modified
  53. * by the engine, this tells it that all is good.
  54. */
  55. OPENSSL_cleanse(ebuf, sizeof(ebuf));
  56. OPENSSL_cleanse(dbuf, sizeof(dbuf));
  57. #endif
  58. switch (keysize_idx) {
  59. case 0:
  60. cipher = EVP_aes_128_cbc();
  61. enc_result = &encresult_128[0];
  62. break;
  63. case 1:
  64. cipher = EVP_aes_192_cbc();
  65. enc_result = &encresult_192[0];
  66. break;
  67. case 2:
  68. cipher = EVP_aes_256_cbc();
  69. enc_result = &encresult_256[0];
  70. break;
  71. default:
  72. cipher = NULL;
  73. }
  74. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  75. return 0;
  76. if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1))
  77. || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE))
  78. || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf + encl, &encf)))
  79. goto end;
  80. encl += encf;
  81. if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE))
  82. goto end;
  83. if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
  84. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0))
  85. || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl))
  86. || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf + decl, &decf)))
  87. goto end;
  88. decl += decf;
  89. if (!TEST_int_eq(decl, BUFFER_SIZE)
  90. || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE))
  91. goto end;
  92. ret = 1;
  93. end:
  94. EVP_CIPHER_CTX_free(ctx);
  95. return ret;
  96. }
  97. static int test_pr16743(void)
  98. {
  99. int ret = 0;
  100. const EVP_CIPHER * cipher;
  101. EVP_CIPHER_CTX *ctx;
  102. if (!TEST_true(ENGINE_init(e)))
  103. return 0;
  104. cipher = ENGINE_get_cipher(e, NID_aes_128_cbc);
  105. ctx = EVP_CIPHER_CTX_new();
  106. if (cipher != NULL && ctx != NULL)
  107. ret = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL);
  108. TEST_true(ret);
  109. EVP_CIPHER_CTX_free(ctx);
  110. ENGINE_finish(e);
  111. return ret;
  112. }
  113. int global_init(void)
  114. {
  115. ENGINE_load_builtin_engines();
  116. # ifndef OPENSSL_NO_STATIC_ENGINE
  117. OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL);
  118. # endif
  119. return 1;
  120. }
  121. #endif
  122. int setup_tests(void)
  123. {
  124. #ifndef OPENSSL_NO_ENGINE
  125. if ((e = ENGINE_by_id("afalg")) == NULL) {
  126. /* Probably a platform env issue, not a test failure. */
  127. TEST_info("Can't load AFALG engine");
  128. } else {
  129. ADD_ALL_TESTS(test_afalg_aes_cbc, 3);
  130. ADD_TEST(test_pr16743);
  131. }
  132. #endif
  133. return 1;
  134. }
  135. #ifndef OPENSSL_NO_ENGINE
  136. void cleanup_tests(void)
  137. {
  138. ENGINE_free(e);
  139. }
  140. #endif