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 #include <string.h> 31 #include <openssl/engine.h> 32 #include <openssl/evp.h> 33 #include <openssl/rand.h> 34 35 /* Use a buffer size which is not aligned to block size */ 36 #define BUFFER_SIZE (8 * 1024) - 13 37 38 static int test_afalg_aes_128_cbc(ENGINE *e) 39 { 40 EVP_CIPHER_CTX *ctx; 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"; 46 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; 52 53 ctx = EVP_CIPHER_CTX_new(); 54 if (ctx == NULL) { 55 fprintf(stderr, "%s() failed to allocate ctx\n", __func__); 56 return 0; 57 } 58 RAND_bytes(in, BUFFER_SIZE); 59 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__); 64 goto end; 65 } 66 encl += encf; 67 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__); 73 goto end; 74 } 75 decl += decf; 76 77 if ( decl != BUFFER_SIZE 78 || memcmp(dbuf, in, BUFFER_SIZE)) { 79 fprintf(stderr, "%s() failed Dec(Enc(P)) != P\n", __func__); 80 goto end; 81 } 82 83 status = 1; 84 85 end: 86 EVP_CIPHER_CTX_free(ctx); 87 return status; 88 } 89 90 int main(int argc, char **argv) 91 { 92 ENGINE *e; 93 94 CRYPTO_set_mem_debug(1); 95 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); 96 97 ENGINE_load_builtin_engines(); 98 99 # ifndef OPENSSL_NO_STATIC_ENGINE 100 OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL); 101 # endif 102 103 e = ENGINE_by_id("afalg"); 104 if (e == NULL) { 105 /* 106 * A failure to load is probably a platform environment problem so we 107 * don't treat this as an OpenSSL test failure, i.e. we return 0 108 */ 109 fprintf(stderr, 110 "AFALG Test: Failed to load AFALG Engine - skipping test\n"); 111 return 0; 112 } 113 114 if (test_afalg_aes_128_cbc(e) == 0) { 115 ENGINE_free(e); 116 return 1; 117 } 118 119 ENGINE_free(e); 120 printf("PASS\n"); 121 return 0; 122 } 123 124 #else /* OPENSSL_NO_AFALGENG */ 125 126 int main(int argc, char **argv) 127 { 128 fprintf(stderr, "AFALG not supported - skipping AFALG tests\n"); 129 printf("PASS\n"); 130 return 0; 131 } 132 133 #endif 134