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