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