xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/afalgtest.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1c7da899bSchristos /*
221497c5cSchristos  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3c7da899bSchristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5c7da899bSchristos  * this file except in compliance with the License.  You can obtain a copy
6c7da899bSchristos  * in the file LICENSE in the source distribution or at
7c7da899bSchristos  * https://www.openssl.org/source/license.html
8c7da899bSchristos  */
9c7da899bSchristos 
10*b0d17251Schristos /* We need to use some engine deprecated APIs */
11*b0d17251Schristos #define OPENSSL_SUPPRESS_DEPRECATED
12*b0d17251Schristos 
13c7da899bSchristos #include <stdio.h>
14c7da899bSchristos #include <openssl/opensslconf.h>
15c7da899bSchristos 
1613d40330Schristos #include <string.h>
1713d40330Schristos #include <openssl/engine.h>
1813d40330Schristos #include <openssl/evp.h>
1913d40330Schristos #include <openssl/rand.h>
2013d40330Schristos #include "testutil.h"
2113d40330Schristos 
2213d40330Schristos /* Use a buffer size which is not aligned to block size */
2313d40330Schristos #define BUFFER_SIZE     17
2413d40330Schristos 
2513d40330Schristos #ifndef OPENSSL_NO_ENGINE
2613d40330Schristos static ENGINE *e;
2713d40330Schristos 
test_afalg_aes_cbc(int keysize_idx)2813d40330Schristos static int test_afalg_aes_cbc(int keysize_idx)
29c7da899bSchristos {
30c7da899bSchristos     EVP_CIPHER_CTX *ctx;
3113d40330Schristos     const EVP_CIPHER *cipher;
32c7da899bSchristos     unsigned char ebuf[BUFFER_SIZE + 32];
33c7da899bSchristos     unsigned char dbuf[BUFFER_SIZE + 32];
34*b0d17251Schristos     const unsigned char *enc_result = NULL;
35c7da899bSchristos     int encl, encf, decl, decf;
3613d40330Schristos     int ret = 0;
37*b0d17251Schristos     static const unsigned char key[] =
38*b0d17251Schristos         "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06"
39*b0d17251Schristos         "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06";
40*b0d17251Schristos     static const unsigned char iv[] =
41*b0d17251Schristos         "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41";
42*b0d17251Schristos     /* input = "Single block msg\n" 17 Bytes*/
43*b0d17251Schristos     static const unsigned char in[BUFFER_SIZE] =
44*b0d17251Schristos         "\x53\x69\x6e\x67\x6c\x65\x20\x62\x6c\x6f\x63\x6b\x20\x6d\x73\x67"
45*b0d17251Schristos         "\x0a";
46*b0d17251Schristos     static const unsigned char encresult_128[BUFFER_SIZE] =
47*b0d17251Schristos         "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a"
48*b0d17251Schristos         "\x2d";
49*b0d17251Schristos     static const unsigned char encresult_192[BUFFER_SIZE] =
50*b0d17251Schristos         "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55"
51*b0d17251Schristos         "\xeb";
52*b0d17251Schristos     static const unsigned char encresult_256[BUFFER_SIZE] =
53*b0d17251Schristos         "\xa0\x76\x85\xfd\xc1\x65\x71\x9d\xc7\xe9\x13\x6e\xae\x55\x49\xb4"
54*b0d17251Schristos         "\x13";
55*b0d17251Schristos 
56*b0d17251Schristos #ifdef OSSL_SANITIZE_MEMORY
57*b0d17251Schristos     /*
58*b0d17251Schristos      * Initialise the encryption & decryption buffers to pacify the memory
59*b0d17251Schristos      * sanitiser.  The sanitiser doesn't know that this memory is modified
60*b0d17251Schristos      * by the engine, this tells it that all is good.
61*b0d17251Schristos      */
62*b0d17251Schristos     OPENSSL_cleanse(ebuf, sizeof(ebuf));
63*b0d17251Schristos     OPENSSL_cleanse(dbuf, sizeof(dbuf));
64*b0d17251Schristos #endif
65c7da899bSchristos 
6613d40330Schristos     switch (keysize_idx) {
6713d40330Schristos         case 0:
6813d40330Schristos             cipher = EVP_aes_128_cbc();
6913d40330Schristos             enc_result = &encresult_128[0];
7013d40330Schristos             break;
7113d40330Schristos         case 1:
7213d40330Schristos             cipher = EVP_aes_192_cbc();
7313d40330Schristos             enc_result = &encresult_192[0];
7413d40330Schristos             break;
7513d40330Schristos         case 2:
7613d40330Schristos             cipher = EVP_aes_256_cbc();
7713d40330Schristos             enc_result = &encresult_256[0];
7813d40330Schristos             break;
7913d40330Schristos         default:
8013d40330Schristos             cipher = NULL;
8113d40330Schristos     }
8213d40330Schristos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
83c7da899bSchristos             return 0;
84c7da899bSchristos 
8513d40330Schristos     if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1))
8613d40330Schristos             || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE))
8713d40330Schristos             || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf + encl, &encf)))
88c7da899bSchristos         goto end;
89c7da899bSchristos     encl += encf;
90c7da899bSchristos 
9113d40330Schristos     if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE))
92c7da899bSchristos         goto end;
9313d40330Schristos 
9413d40330Schristos     if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
9513d40330Schristos             || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0))
9613d40330Schristos             || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl))
9713d40330Schristos             || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf + decl, &decf)))
9813d40330Schristos         goto end;
99c7da899bSchristos     decl += decf;
100c7da899bSchristos 
10113d40330Schristos     if (!TEST_int_eq(decl, BUFFER_SIZE)
10213d40330Schristos             || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE))
103c7da899bSchristos         goto end;
104c7da899bSchristos 
10513d40330Schristos     ret = 1;
106c7da899bSchristos 
107c7da899bSchristos  end:
108c7da899bSchristos     EVP_CIPHER_CTX_free(ctx);
10913d40330Schristos     return ret;
110c7da899bSchristos }
111c7da899bSchristos 
test_pr16743(void)11221497c5cSchristos static int test_pr16743(void)
11321497c5cSchristos {
11421497c5cSchristos     int ret = 0;
11521497c5cSchristos     const EVP_CIPHER * cipher;
11621497c5cSchristos     EVP_CIPHER_CTX *ctx;
11721497c5cSchristos 
11821497c5cSchristos     if (!TEST_true(ENGINE_init(e)))
11921497c5cSchristos         return 0;
12021497c5cSchristos     cipher = ENGINE_get_cipher(e, NID_aes_128_cbc);
12121497c5cSchristos     ctx = EVP_CIPHER_CTX_new();
12221497c5cSchristos     if (cipher != NULL && ctx != NULL)
12321497c5cSchristos         ret = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL);
12421497c5cSchristos     TEST_true(ret);
12521497c5cSchristos     EVP_CIPHER_CTX_free(ctx);
12621497c5cSchristos     ENGINE_finish(e);
12721497c5cSchristos     return ret;
12821497c5cSchristos }
12921497c5cSchristos 
global_init(void)13013d40330Schristos int global_init(void)
131c7da899bSchristos {
132c7da899bSchristos     ENGINE_load_builtin_engines();
133c7da899bSchristos # ifndef OPENSSL_NO_STATIC_ENGINE
134c7da899bSchristos     OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL);
135c7da899bSchristos # endif
13613d40330Schristos     return 1;
137c7da899bSchristos }
13813d40330Schristos #endif
139c7da899bSchristos 
setup_tests(void)14013d40330Schristos int setup_tests(void)
14113d40330Schristos {
14213d40330Schristos #ifndef OPENSSL_NO_ENGINE
14313d40330Schristos     if ((e = ENGINE_by_id("afalg")) == NULL) {
14413d40330Schristos         /* Probably a platform env issue, not a test failure. */
14513d40330Schristos         TEST_info("Can't load AFALG engine");
14613d40330Schristos     } else {
14713d40330Schristos         ADD_ALL_TESTS(test_afalg_aes_cbc, 3);
14821497c5cSchristos         ADD_TEST(test_pr16743);
14913d40330Schristos     }
15013d40330Schristos #endif
15113d40330Schristos 
152c7da899bSchristos     return 1;
153c7da899bSchristos }
154c7da899bSchristos 
15513d40330Schristos #ifndef OPENSSL_NO_ENGINE
cleanup_tests(void)15613d40330Schristos void cleanup_tests(void)
157c7da899bSchristos {
15813d40330Schristos     ENGINE_free(e);
159c7da899bSchristos }
160c7da899bSchristos #endif
161