Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 1 | /* |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 2 | * Copyright 2016-2017 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; |
| 27 | #endif |
| 28 | |
| 29 | |
Emilia Kasper | c91a0a8 | 2016-03-14 12:21:44 +0100 | [diff] [blame] | 30 | #ifndef OPENSSL_NO_AFALGENG |
Matt Caswell | 627537d | 2016-04-13 23:26:56 +0100 | [diff] [blame] | 31 | # include <linux/version.h> |
| 32 | # define K_MAJ 4 |
| 33 | # define K_MIN1 1 |
| 34 | # define K_MIN2 0 |
Baptiste Jonglez | 3ba7023 | 2017-10-30 11:38:09 +0100 | [diff] [blame] | 35 | # if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2) |
Matt Caswell | 627537d | 2016-04-13 23:26:56 +0100 | [diff] [blame] | 36 | /* |
| 37 | * If we get here then it looks like there is a mismatch between the linux |
| 38 | * headers and the actual kernel version, so we have tried to compile with |
| 39 | * afalg support, but then skipped it in e_afalg.c. As far as this test is |
| 40 | * concerned we behave as if we had been configured without support |
| 41 | */ |
| 42 | # define OPENSSL_NO_AFALGENG |
| 43 | # endif |
| 44 | #endif |
| 45 | |
| 46 | #ifndef OPENSSL_NO_AFALGENG |
JitendraLulla | 49ea0f0 | 2017-11-11 12:01:58 +0530 | [diff] [blame] | 47 | static int test_afalg_aes_cbc(int keysize_idx) |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 48 | { |
| 49 | EVP_CIPHER_CTX *ctx; |
JitendraLulla | 49ea0f0 | 2017-11-11 12:01:58 +0530 | [diff] [blame] | 50 | const EVP_CIPHER *cipher; |
| 51 | unsigned char key[] = "\x06\xa9\x21\x40\x36\xb8\xa1\x5b" |
| 52 | "\x51\x2e\x03\xd5\x34\x12\x00\x06" |
| 53 | "\x06\xa9\x21\x40\x36\xb8\xa1\x5b" |
| 54 | "\x51\x2e\x03\xd5\x34\x12\x00\x06"; |
| 55 | unsigned char iv[] = "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30" |
| 56 | "\xb4\x22\xda\x80\x2c\x9f\xac\x41"; |
| 57 | /* input = "Single block msg\n" 17Bytes*/ |
| 58 | unsigned char in[BUFFER_SIZE] = "\x53\x69\x6e\x67\x6c\x65\x20\x62" |
| 59 | "\x6c\x6f\x63\x6b\x20\x6d\x73\x67\x0a"; |
Matt Caswell | a193388 | 2016-03-07 12:36:20 +0000 | [diff] [blame] | 60 | unsigned char ebuf[BUFFER_SIZE + 32]; |
| 61 | unsigned char dbuf[BUFFER_SIZE + 32]; |
JitendraLulla | 49ea0f0 | 2017-11-11 12:01:58 +0530 | [diff] [blame] | 62 | unsigned char encresult_128[] = "\xe3\x53\x77\x9c\x10\x79\xae\xb8" |
| 63 | "\x27\x08\x94\x2d\xbe\x77\x18\x1a\x2d"; |
| 64 | unsigned char encresult_192[] = "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39" |
| 65 | "\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55\xeb"; |
| 66 | unsigned char encresult_256[] = "\xa0\x76\x85\xfd\xc1\x65\x71\x9d" |
| 67 | "\xc7\xe9\x13\x6e\xae\x55\x49\xb4\x13"; |
JitendraLulla | a3d7fd2 | 2017-11-15 06:03:07 +0530 | [diff] [blame] | 68 | unsigned char *enc_result = NULL; |
JitendraLulla | 49ea0f0 | 2017-11-11 12:01:58 +0530 | [diff] [blame] | 69 | |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 70 | int encl, encf, decl, decf; |
Rich Salz | 5292439 | 2017-04-14 20:32:20 -0400 | [diff] [blame] | 71 | int ret = 0; |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 72 | |
JitendraLulla | 49ea0f0 | 2017-11-11 12:01:58 +0530 | [diff] [blame] | 73 | switch (keysize_idx) { |
| 74 | case 0: |
| 75 | cipher = EVP_aes_128_cbc(); |
| 76 | enc_result = &encresult_128[0]; |
| 77 | break; |
| 78 | case 1: |
| 79 | cipher = EVP_aes_192_cbc(); |
| 80 | enc_result = &encresult_192[0]; |
| 81 | break; |
| 82 | case 2: |
| 83 | cipher = EVP_aes_256_cbc(); |
| 84 | enc_result = &encresult_256[0]; |
| 85 | break; |
| 86 | default: |
| 87 | cipher = NULL; |
| 88 | } |
Rich Salz | 5292439 | 2017-04-14 20:32:20 -0400 | [diff] [blame] | 89 | if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) |
| 90 | return 0; |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 91 | |
Rich Salz | 5292439 | 2017-04-14 20:32:20 -0400 | [diff] [blame] | 92 | if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1)) |
| 93 | || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE)) |
| 94 | || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf+encl, &encf))) |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 95 | goto end; |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 96 | encl += encf; |
| 97 | |
JitendraLulla | 49ea0f0 | 2017-11-11 12:01:58 +0530 | [diff] [blame] | 98 | if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE)) |
| 99 | goto end; |
| 100 | |
Rich Salz | 5292439 | 2017-04-14 20:32:20 -0400 | [diff] [blame] | 101 | if (!TEST_true(EVP_CIPHER_CTX_reset(ctx)) |
| 102 | || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0)) |
| 103 | || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl)) |
| 104 | || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf+decl, &decf))) |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 105 | goto end; |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 106 | decl += decf; |
| 107 | |
Rich Salz | 5292439 | 2017-04-14 20:32:20 -0400 | [diff] [blame] | 108 | if (!TEST_int_eq(decl, BUFFER_SIZE) |
| 109 | || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE)) |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 110 | goto end; |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 111 | |
Rich Salz | 5292439 | 2017-04-14 20:32:20 -0400 | [diff] [blame] | 112 | ret = 1; |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 113 | |
| 114 | end: |
| 115 | EVP_CIPHER_CTX_free(ctx); |
Rich Salz | 5292439 | 2017-04-14 20:32:20 -0400 | [diff] [blame] | 116 | return ret; |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 117 | } |
Rich Salz | 5c8e9d5 | 2017-04-16 09:17:39 -0400 | [diff] [blame] | 118 | #endif |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 119 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 120 | #ifndef OPENSSL_NO_ENGINE |
| 121 | int global_init(void) |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 122 | { |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 123 | ENGINE_load_builtin_engines(); |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 124 | # ifndef OPENSSL_NO_STATIC_ENGINE |
| 125 | OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL); |
| 126 | # endif |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 127 | return 1; |
| 128 | } |
| 129 | #endif |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 130 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 131 | int setup_tests(void) |
| 132 | { |
| 133 | #ifndef OPENSSL_NO_ENGINE |
Richard Levitte | c517ac4 | 2018-02-07 22:18:44 +0100 | [diff] [blame] | 134 | if ((e = ENGINE_by_id("afalg")) == NULL) { |
Rich Salz | 5292439 | 2017-04-14 20:32:20 -0400 | [diff] [blame] | 135 | /* Probably a platform env issue, not a test failure. */ |
Richard Levitte | c517ac4 | 2018-02-07 22:18:44 +0100 | [diff] [blame] | 136 | TEST_info("Can't load AFALG engine"); |
| 137 | } else { |
Richard Levitte | 227a1e3 | 2018-02-07 14:01:13 +0100 | [diff] [blame] | 138 | # ifndef OPENSSL_NO_AFALGENG |
Richard Levitte | c517ac4 | 2018-02-07 22:18:44 +0100 | [diff] [blame] | 139 | ADD_ALL_TESTS(test_afalg_aes_cbc, 3); |
Richard Levitte | 227a1e3 | 2018-02-07 14:01:13 +0100 | [diff] [blame] | 140 | # endif |
Richard Levitte | c517ac4 | 2018-02-07 22:18:44 +0100 | [diff] [blame] | 141 | } |
Rich Salz | 5c8e9d5 | 2017-04-16 09:17:39 -0400 | [diff] [blame] | 142 | #endif |
| 143 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 144 | return 1; |
clucey | d245844 | 2016-03-03 05:56:10 +0000 | [diff] [blame] | 145 | } |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 146 | |
| 147 | #ifndef OPENSSL_NO_ENGINE |
| 148 | void cleanup_tests(void) |
| 149 | { |
| 150 | ENGINE_free(e); |
| 151 | } |
| 152 | #endif |