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