Be more explicit about RSAES-PKCS#1v1.5 error handling
[openssl.git] / test / afalgtest.c
1 /*
2  * Copyright 2016-2020 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 #endif
28
29
30 #ifndef OPENSSL_NO_AFALGENG
31 # include <linux/version.h>
32 # define K_MAJ   4
33 # define K_MIN1  1
34 # define K_MIN2  0
35 # if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)
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
47 static int test_afalg_aes_cbc(int keysize_idx)
48 {
49     EVP_CIPHER_CTX *ctx;
50     const EVP_CIPHER *cipher;
51     unsigned char ebuf[BUFFER_SIZE + 32];
52     unsigned char dbuf[BUFFER_SIZE + 32];
53     const unsigned char *enc_result = NULL;
54     int encl, encf, decl, decf;
55     int ret = 0;
56     static const unsigned char key[] =
57         "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06"
58         "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06";
59     static const unsigned char iv[] =
60         "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41";
61     /* input = "Single block msg\n" 17 Bytes*/
62     static const unsigned char in[BUFFER_SIZE] =
63         "\x53\x69\x6e\x67\x6c\x65\x20\x62\x6c\x6f\x63\x6b\x20\x6d\x73\x67"
64         "\x0a";
65     static const unsigned char encresult_128[BUFFER_SIZE] =
66         "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a"
67         "\x2d";
68     static const unsigned char encresult_192[BUFFER_SIZE] =
69         "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55"
70         "\xeb";
71     static const unsigned char encresult_256[BUFFER_SIZE] =
72         "\xa0\x76\x85\xfd\xc1\x65\x71\x9d\xc7\xe9\x13\x6e\xae\x55\x49\xb4"
73         "\x13";
74
75 #ifdef OSSL_SANITIZE_MEMORY
76     /*
77      * Initialise the encryption & decryption buffers to pacify the memory
78      * sanitiser.  The sanitiser doesn't know that this memory is modified
79      * by the engine, this tells it that all is good.
80      */
81     OPENSSL_cleanse(ebuf, sizeof(ebuf));
82     OPENSSL_cleanse(dbuf, sizeof(dbuf));
83 #endif
84
85     switch (keysize_idx) {
86         case 0:
87             cipher = EVP_aes_128_cbc();
88             enc_result = &encresult_128[0];
89             break;
90         case 1:
91             cipher = EVP_aes_192_cbc();
92             enc_result = &encresult_192[0];
93             break;
94         case 2:
95             cipher = EVP_aes_256_cbc();
96             enc_result = &encresult_256[0];
97             break;
98         default:
99             cipher = NULL;
100     }
101     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
102             return 0;
103
104     if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1))
105             || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE))
106             || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf + encl, &encf)))
107         goto end;
108     encl += encf;
109
110     if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE))
111         goto end;
112
113     if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
114             || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0))
115             || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl))
116             || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf + decl, &decf)))
117         goto end;
118     decl += decf;
119
120     if (!TEST_int_eq(decl, BUFFER_SIZE)
121             || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE))
122         goto end;
123
124     ret = 1;
125
126  end:
127     EVP_CIPHER_CTX_free(ctx);
128     return ret;
129 }
130 #endif
131
132 #ifndef OPENSSL_NO_ENGINE
133 int global_init(void)
134 {
135     ENGINE_load_builtin_engines();
136 # ifndef OPENSSL_NO_STATIC_ENGINE
137     OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL);
138 # endif
139     return 1;
140 }
141 #endif
142
143 int setup_tests(void)
144 {
145 #ifndef OPENSSL_NO_ENGINE
146     if ((e = ENGINE_by_id("afalg")) == NULL) {
147         /* Probably a platform env issue, not a test failure. */
148         TEST_info("Can't load AFALG engine");
149     } else {
150 # ifndef OPENSSL_NO_AFALGENG
151         ADD_ALL_TESTS(test_afalg_aes_cbc, 3);
152 # endif
153     }
154 #endif
155
156     return 1;
157 }
158
159 #ifndef OPENSSL_NO_ENGINE
160 void cleanup_tests(void)
161 {
162     ENGINE_free(e);
163 }
164 #endif