2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/opensslconf.h>
13 #ifndef OPENSSL_NO_AFALGENG
14 # include <linux/version.h>
18 # if LINUX_VERSION_CODE <= KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)
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
25 # define OPENSSL_NO_AFALGENG
29 #ifndef OPENSSL_NO_AFALGENG
31 #include <openssl/engine.h>
32 #include <openssl/evp.h>
33 #include <openssl/rand.h>
35 /* Use a buffer size which is not aligned to block size */
36 #define BUFFER_SIZE (8 * 1024) - 13
38 static int test_afalg_aes_128_cbc(ENGINE
*e
)
41 const EVP_CIPHER
*cipher
= EVP_aes_128_cbc();
42 unsigned char key
[] = "\x5F\x4D\xCC\x3B\x5A\xA7\x65\xD6\
43 \x1D\x83\x27\xDE\xB8\x82\xCF\x99";
44 unsigned char iv
[] = "\x2B\x95\x99\x0A\x91\x51\x37\x4A\
45 \xBD\x8F\xF8\xC5\xA7\xA0\xFE\x08";
47 unsigned char in
[BUFFER_SIZE
];
48 unsigned char ebuf
[BUFFER_SIZE
+ 32];
49 unsigned char dbuf
[BUFFER_SIZE
+ 32];
50 int encl
, encf
, decl
, decf
;
51 unsigned int status
= 0;
53 ctx
= EVP_CIPHER_CTX_new();
55 fprintf(stderr
, "%s() failed to allocate ctx\n", __func__
);
58 RAND_bytes(in
, BUFFER_SIZE
);
60 if ( !EVP_CipherInit_ex(ctx
, cipher
, e
, key
, iv
, 1)
61 || !EVP_CipherUpdate(ctx
, ebuf
, &encl
, in
, BUFFER_SIZE
)
62 || !EVP_CipherFinal_ex(ctx
, ebuf
+encl
, &encf
)) {
63 fprintf(stderr
, "%s() failed encryption\n", __func__
);
68 if ( !EVP_CIPHER_CTX_reset(ctx
)
69 || !EVP_CipherInit_ex(ctx
, cipher
, e
, key
, iv
, 0)
70 || !EVP_CipherUpdate(ctx
, dbuf
, &decl
, ebuf
, encl
)
71 || !EVP_CipherFinal_ex(ctx
, dbuf
+decl
, &decf
)) {
72 fprintf(stderr
, "%s() failed decryption\n", __func__
);
77 if ( decl
!= BUFFER_SIZE
78 || memcmp(dbuf
, in
, BUFFER_SIZE
)) {
79 fprintf(stderr
, "%s() failed Dec(Enc(P)) != P\n", __func__
);
86 EVP_CIPHER_CTX_free(ctx
);
90 int main(int argc
, char **argv
)
94 CRYPTO_set_mem_debug(1);
95 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
97 ENGINE_load_builtin_engines();
99 # ifndef OPENSSL_NO_STATIC_ENGINE
100 OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG
, NULL
);
103 e
= ENGINE_by_id("afalg");
105 fprintf(stderr
, "AFALG Test: Failed to load AFALG Engine\n");
109 if (test_afalg_aes_128_cbc(e
) == 0) {
119 #else /* OPENSSL_NO_AFALGENG */
121 int main(int argc
, char **argv
)
123 fprintf(stderr
, "AFALG not supported - skipping AFALG tests\n");