afalgtest.c 4.2 KB

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