1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci/* We need to use some engine deprecated APIs */ 11e1051a39Sopenharmony_ci#define OPENSSL_SUPPRESS_DEPRECATED 12e1051a39Sopenharmony_ci 13e1051a39Sopenharmony_ci#include <stdio.h> 14e1051a39Sopenharmony_ci#include <openssl/opensslconf.h> 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_ci#include <string.h> 17e1051a39Sopenharmony_ci#include <openssl/engine.h> 18e1051a39Sopenharmony_ci#include <openssl/evp.h> 19e1051a39Sopenharmony_ci#include <openssl/rand.h> 20e1051a39Sopenharmony_ci#include "testutil.h" 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_ci/* Use a buffer size which is not aligned to block size */ 23e1051a39Sopenharmony_ci#define BUFFER_SIZE 17 24e1051a39Sopenharmony_ci 25e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE 26e1051a39Sopenharmony_cistatic ENGINE *e; 27e1051a39Sopenharmony_ci 28e1051a39Sopenharmony_cistatic int test_afalg_aes_cbc(int keysize_idx) 29e1051a39Sopenharmony_ci{ 30e1051a39Sopenharmony_ci EVP_CIPHER_CTX *ctx; 31e1051a39Sopenharmony_ci const EVP_CIPHER *cipher; 32e1051a39Sopenharmony_ci unsigned char ebuf[BUFFER_SIZE + 32]; 33e1051a39Sopenharmony_ci unsigned char dbuf[BUFFER_SIZE + 32]; 34e1051a39Sopenharmony_ci const unsigned char *enc_result = NULL; 35e1051a39Sopenharmony_ci int encl, encf, decl, decf; 36e1051a39Sopenharmony_ci int ret = 0; 37e1051a39Sopenharmony_ci static const unsigned char key[] = 38e1051a39Sopenharmony_ci "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06" 39e1051a39Sopenharmony_ci "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06"; 40e1051a39Sopenharmony_ci static const unsigned char iv[] = 41e1051a39Sopenharmony_ci "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41"; 42e1051a39Sopenharmony_ci /* input = "Single block msg\n" 17 Bytes*/ 43e1051a39Sopenharmony_ci static const unsigned char in[BUFFER_SIZE] = 44e1051a39Sopenharmony_ci "\x53\x69\x6e\x67\x6c\x65\x20\x62\x6c\x6f\x63\x6b\x20\x6d\x73\x67" 45e1051a39Sopenharmony_ci "\x0a"; 46e1051a39Sopenharmony_ci static const unsigned char encresult_128[BUFFER_SIZE] = 47e1051a39Sopenharmony_ci "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a" 48e1051a39Sopenharmony_ci "\x2d"; 49e1051a39Sopenharmony_ci static const unsigned char encresult_192[BUFFER_SIZE] = 50e1051a39Sopenharmony_ci "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55" 51e1051a39Sopenharmony_ci "\xeb"; 52e1051a39Sopenharmony_ci static const unsigned char encresult_256[BUFFER_SIZE] = 53e1051a39Sopenharmony_ci "\xa0\x76\x85\xfd\xc1\x65\x71\x9d\xc7\xe9\x13\x6e\xae\x55\x49\xb4" 54e1051a39Sopenharmony_ci "\x13"; 55e1051a39Sopenharmony_ci 56e1051a39Sopenharmony_ci#ifdef OSSL_SANITIZE_MEMORY 57e1051a39Sopenharmony_ci /* 58e1051a39Sopenharmony_ci * Initialise the encryption & decryption buffers to pacify the memory 59e1051a39Sopenharmony_ci * sanitiser. The sanitiser doesn't know that this memory is modified 60e1051a39Sopenharmony_ci * by the engine, this tells it that all is good. 61e1051a39Sopenharmony_ci */ 62e1051a39Sopenharmony_ci OPENSSL_cleanse(ebuf, sizeof(ebuf)); 63e1051a39Sopenharmony_ci OPENSSL_cleanse(dbuf, sizeof(dbuf)); 64e1051a39Sopenharmony_ci#endif 65e1051a39Sopenharmony_ci 66e1051a39Sopenharmony_ci switch (keysize_idx) { 67e1051a39Sopenharmony_ci case 0: 68e1051a39Sopenharmony_ci cipher = EVP_aes_128_cbc(); 69e1051a39Sopenharmony_ci enc_result = &encresult_128[0]; 70e1051a39Sopenharmony_ci break; 71e1051a39Sopenharmony_ci case 1: 72e1051a39Sopenharmony_ci cipher = EVP_aes_192_cbc(); 73e1051a39Sopenharmony_ci enc_result = &encresult_192[0]; 74e1051a39Sopenharmony_ci break; 75e1051a39Sopenharmony_ci case 2: 76e1051a39Sopenharmony_ci cipher = EVP_aes_256_cbc(); 77e1051a39Sopenharmony_ci enc_result = &encresult_256[0]; 78e1051a39Sopenharmony_ci break; 79e1051a39Sopenharmony_ci default: 80e1051a39Sopenharmony_ci cipher = NULL; 81e1051a39Sopenharmony_ci } 82e1051a39Sopenharmony_ci if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) 83e1051a39Sopenharmony_ci return 0; 84e1051a39Sopenharmony_ci 85e1051a39Sopenharmony_ci if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1)) 86e1051a39Sopenharmony_ci || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE)) 87e1051a39Sopenharmony_ci || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf + encl, &encf))) 88e1051a39Sopenharmony_ci goto end; 89e1051a39Sopenharmony_ci encl += encf; 90e1051a39Sopenharmony_ci 91e1051a39Sopenharmony_ci if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE)) 92e1051a39Sopenharmony_ci goto end; 93e1051a39Sopenharmony_ci 94e1051a39Sopenharmony_ci if (!TEST_true(EVP_CIPHER_CTX_reset(ctx)) 95e1051a39Sopenharmony_ci || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0)) 96e1051a39Sopenharmony_ci || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl)) 97e1051a39Sopenharmony_ci || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf + decl, &decf))) 98e1051a39Sopenharmony_ci goto end; 99e1051a39Sopenharmony_ci decl += decf; 100e1051a39Sopenharmony_ci 101e1051a39Sopenharmony_ci if (!TEST_int_eq(decl, BUFFER_SIZE) 102e1051a39Sopenharmony_ci || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE)) 103e1051a39Sopenharmony_ci goto end; 104e1051a39Sopenharmony_ci 105e1051a39Sopenharmony_ci ret = 1; 106e1051a39Sopenharmony_ci 107e1051a39Sopenharmony_ci end: 108e1051a39Sopenharmony_ci EVP_CIPHER_CTX_free(ctx); 109e1051a39Sopenharmony_ci return ret; 110e1051a39Sopenharmony_ci} 111e1051a39Sopenharmony_ci 112e1051a39Sopenharmony_cistatic int test_pr16743(void) 113e1051a39Sopenharmony_ci{ 114e1051a39Sopenharmony_ci int ret = 0; 115e1051a39Sopenharmony_ci const EVP_CIPHER * cipher; 116e1051a39Sopenharmony_ci EVP_CIPHER_CTX *ctx; 117e1051a39Sopenharmony_ci 118e1051a39Sopenharmony_ci if (!TEST_true(ENGINE_init(e))) 119e1051a39Sopenharmony_ci return 0; 120e1051a39Sopenharmony_ci cipher = ENGINE_get_cipher(e, NID_aes_128_cbc); 121e1051a39Sopenharmony_ci ctx = EVP_CIPHER_CTX_new(); 122e1051a39Sopenharmony_ci if (cipher != NULL && ctx != NULL) 123e1051a39Sopenharmony_ci ret = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL); 124e1051a39Sopenharmony_ci TEST_true(ret); 125e1051a39Sopenharmony_ci EVP_CIPHER_CTX_free(ctx); 126e1051a39Sopenharmony_ci ENGINE_finish(e); 127e1051a39Sopenharmony_ci return ret; 128e1051a39Sopenharmony_ci} 129e1051a39Sopenharmony_ci 130e1051a39Sopenharmony_ciint global_init(void) 131e1051a39Sopenharmony_ci{ 132e1051a39Sopenharmony_ci ENGINE_load_builtin_engines(); 133e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_STATIC_ENGINE 134e1051a39Sopenharmony_ci OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL); 135e1051a39Sopenharmony_ci# endif 136e1051a39Sopenharmony_ci return 1; 137e1051a39Sopenharmony_ci} 138e1051a39Sopenharmony_ci#endif 139e1051a39Sopenharmony_ci 140e1051a39Sopenharmony_ciint setup_tests(void) 141e1051a39Sopenharmony_ci{ 142e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE 143e1051a39Sopenharmony_ci if ((e = ENGINE_by_id("afalg")) == NULL) { 144e1051a39Sopenharmony_ci /* Probably a platform env issue, not a test failure. */ 145e1051a39Sopenharmony_ci TEST_info("Can't load AFALG engine"); 146e1051a39Sopenharmony_ci } else { 147e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_afalg_aes_cbc, 3); 148e1051a39Sopenharmony_ci ADD_TEST(test_pr16743); 149e1051a39Sopenharmony_ci } 150e1051a39Sopenharmony_ci#endif 151e1051a39Sopenharmony_ci 152e1051a39Sopenharmony_ci return 1; 153e1051a39Sopenharmony_ci} 154e1051a39Sopenharmony_ci 155e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE 156e1051a39Sopenharmony_civoid cleanup_tests(void) 157e1051a39Sopenharmony_ci{ 158e1051a39Sopenharmony_ci ENGINE_free(e); 159e1051a39Sopenharmony_ci} 160e1051a39Sopenharmony_ci#endif 161