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