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