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