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