Extensions to d2i_test.
[openssl.git] / test / afalgtest.c
index 1013fe0beb5b4cb7e47249a7bff8b63b2c4d6c45..80aa1e7550dadd0c609cb8c960d5ae24cc3431da 100644 (file)
  *
  */
 #include <stdio.h>
+#include <openssl/opensslconf.h>
+
+#ifndef OPENSSL_NO_AFALGENG
+# include <linux/version.h>
+# define K_MAJ   4
+# define K_MIN1  1
+# define K_MIN2  0
+# if LINUX_VERSION_CODE <= KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)
+/*
+ * If we get here then it looks like there is a mismatch between the linux
+ * headers and the actual kernel version, so we have tried to compile with
+ * afalg support, but then skipped it in e_afalg.c. As far as this test is
+ * concerned we behave as if we had been configured without support
+ */
+#  define OPENSSL_NO_AFALGENG
+# endif
+#endif
+
+#ifndef OPENSSL_NO_AFALGENG
+#include <string.h>
 #include <openssl/engine.h>
 #include <openssl/evp.h>
 #include <openssl/rand.h>
-#include <string.h>
 
-#ifndef OPENSSL_NO_AFALGENG
+/* Use a buffer size which is not aligned to block size */
+#define BUFFER_SIZE     (8 * 1024) - 13
+
 static int test_afalg_aes_128_cbc(ENGINE *e)
 {
     EVP_CIPHER_CTX *ctx;
@@ -66,11 +87,10 @@ static int test_afalg_aes_128_cbc(ENGINE *e)
                            \x1D\x83\x27\xDE\xB8\x82\xCF\x99";
     unsigned char iv[] = "\x2B\x95\x99\x0A\x91\x51\x37\x4A\
                           \xBD\x8F\xF8\xC5\xA7\xA0\xFE\x08";
-    /* Use a buffer which is not aligned to block size */
-    const int bufsz = (8 * 1024) - 13;
-    unsigned char in[bufsz];
-    unsigned char ebuf[bufsz + 32];
-    unsigned char dbuf[bufsz + 32];
+
+    unsigned char in[BUFFER_SIZE];
+    unsigned char ebuf[BUFFER_SIZE + 32];
+    unsigned char dbuf[BUFFER_SIZE + 32];
     int encl, encf, decl, decf;
     unsigned int status = 0;
 
@@ -79,11 +99,10 @@ static int test_afalg_aes_128_cbc(ENGINE *e)
         fprintf(stderr, "%s() failed to allocate ctx\n", __func__);
         return 0;
     }
-    EVP_CIPHER_CTX_init(ctx);
-    RAND_bytes(in, bufsz);
+    RAND_bytes(in, BUFFER_SIZE);
 
     if (       !EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1)
-            || !EVP_CipherUpdate(ctx, ebuf, &encl, in, bufsz)
+            || !EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE)
             || !EVP_CipherFinal_ex(ctx, ebuf+encl, &encf)) {
         fprintf(stderr, "%s() failed encryption\n", __func__);
         goto end;
@@ -99,8 +118,8 @@ static int test_afalg_aes_128_cbc(ENGINE *e)
     }
     decl += decf;
 
-    if (       decl != bufsz
-            || memcmp(dbuf, in, bufsz)) {
+    if (       decl != BUFFER_SIZE
+            || memcmp(dbuf, in, BUFFER_SIZE)) {
         fprintf(stderr, "%s() failed Dec(Enc(P)) != P\n", __func__);
         goto end;
     }
@@ -111,14 +130,9 @@ static int test_afalg_aes_128_cbc(ENGINE *e)
     EVP_CIPHER_CTX_free(ctx);
     return status;
 }
-#endif
 
 int main(int argc, char **argv)
 {
-#ifdef OPENSSL_NO_AFALGENG
-    fprintf(stderr, "AFALG not supported - skipping AFALG tests\n");
-#else
-
     ENGINE *e;
 
     CRYPTO_set_mem_debug(1);
@@ -142,7 +156,17 @@ int main(int argc, char **argv)
     }
 
     ENGINE_free(e);
-#endif
     printf("PASS\n");
     return 0;
 }
+
+#else  /* OPENSSL_NO_AFALGENG */
+
+int main(int argc, char **argv)
+{
+    fprintf(stderr, "AFALG not supported - skipping AFALG tests\n");
+    printf("PASS\n");
+    return 0;
+}
+
+#endif