xref: /freebsd/crypto/openssl/test/afalgtest.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*
2  * Copyright 2016-2021 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[] =
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
65 
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     }
82     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
83             return 0;
84 
85     if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1))
86             || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE))
87             || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf + encl, &encf)))
88         goto end;
89     encl += encf;
90 
91     if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE))
92         goto end;
93 
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))
97             || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf + decl, &decf)))
98         goto end;
99     decl += decf;
100 
101     if (!TEST_int_eq(decl, BUFFER_SIZE)
102             || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE))
103         goto end;
104 
105     ret = 1;
106 
107  end:
108     EVP_CIPHER_CTX_free(ctx);
109     return ret;
110 }
111 
test_pr16743(void)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 
global_init(void)130 int global_init(void)
131 {
132     ENGINE_load_builtin_engines();
133 # ifndef OPENSSL_NO_STATIC_ENGINE
134     OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL);
135 # endif
136     return 1;
137 }
138 #endif
139 
setup_tests(void)140 int setup_tests(void)
141 {
142 #ifndef OPENSSL_NO_ENGINE
143     if ((e = ENGINE_by_id("afalg")) == NULL) {
144         /* Probably a platform env issue, not a test failure. */
145         TEST_info("Can't load AFALG engine");
146     } else {
147         ADD_ALL_TESTS(test_afalg_aes_cbc, 3);
148         ADD_TEST(test_pr16743);
149     }
150 #endif
151 
152     return 1;
153 }
154 
155 #ifndef OPENSSL_NO_ENGINE
cleanup_tests(void)156 void cleanup_tests(void)
157 {
158     ENGINE_free(e);
159 }
160 #endif
161