xref: /freebsd/crypto/openssl/test/pbetest.c (revision e0c4386e)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <string.h>
11*e0c4386eSCy Schubert 
12*e0c4386eSCy Schubert #include "testutil.h"
13*e0c4386eSCy Schubert 
14*e0c4386eSCy Schubert #include <openssl/evp.h>
15*e0c4386eSCy Schubert #include <openssl/x509.h>
16*e0c4386eSCy Schubert #include <openssl/rc4.h>
17*e0c4386eSCy Schubert #include <openssl/md5.h>
18*e0c4386eSCy Schubert #include <openssl/configuration.h>
19*e0c4386eSCy Schubert #include <openssl/provider.h>
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 \
22*e0c4386eSCy Schubert     || !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
23*e0c4386eSCy Schubert static const char pbe_password[] = "MyVoiceIsMyPassport";
24*e0c4386eSCy Schubert 
25*e0c4386eSCy Schubert static unsigned char pbe_salt[] = {
26*e0c4386eSCy Schubert     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
27*e0c4386eSCy Schubert };
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert static const int pbe_iter = 1000;
30*e0c4386eSCy Schubert 
31*e0c4386eSCy Schubert static unsigned char pbe_plaintext[] = {
32*e0c4386eSCy Schubert     0x57, 0x65, 0x20, 0x61, 0x72, 0x65, 0x20, 0x61,
33*e0c4386eSCy Schubert     0x6c, 0x6c, 0x20, 0x6d, 0x61, 0x64, 0x65, 0x20,
34*e0c4386eSCy Schubert     0x6f, 0x66, 0x20, 0x73, 0x74, 0x61, 0x72, 0x73,
35*e0c4386eSCy Schubert };
36*e0c4386eSCy Schubert #endif
37*e0c4386eSCy Schubert 
38*e0c4386eSCy Schubert /* Expected output generated using OpenSSL 1.1.1 */
39*e0c4386eSCy Schubert 
40*e0c4386eSCy Schubert #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
41*e0c4386eSCy Schubert static const unsigned char pbe_ciphertext_rc4_md5[] = {
42*e0c4386eSCy Schubert     0x21, 0x90, 0xfa, 0xee, 0x95, 0x66, 0x59, 0x45,
43*e0c4386eSCy Schubert     0xfa, 0x1e, 0x9f, 0xe2, 0x25, 0xd2, 0xf9, 0x71,
44*e0c4386eSCy Schubert     0x94, 0xe4, 0x3d, 0xc9, 0x7c, 0xb0, 0x07, 0x23,
45*e0c4386eSCy Schubert };
46*e0c4386eSCy Schubert #endif
47*e0c4386eSCy Schubert 
48*e0c4386eSCy Schubert #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
49*e0c4386eSCy Schubert static const unsigned char pbe_ciphertext_des_sha1[] = {
50*e0c4386eSCy Schubert     0xce, 0x4b, 0xb0, 0x0a, 0x7b, 0x48, 0xd7, 0xe3,
51*e0c4386eSCy Schubert     0x9a, 0x9f, 0x46, 0xd6, 0x41, 0x42, 0x4b, 0x44,
52*e0c4386eSCy Schubert     0x36, 0x45, 0x5f, 0x60, 0x8f, 0x3c, 0xd0, 0x55,
53*e0c4386eSCy Schubert     0xd0, 0x8d, 0xa9, 0xab, 0x78, 0x5b, 0x63, 0xaf,
54*e0c4386eSCy Schubert };
55*e0c4386eSCy Schubert #endif
56*e0c4386eSCy Schubert 
57*e0c4386eSCy Schubert #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 \
58*e0c4386eSCy Schubert     || !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
test_pkcs5_pbe(const EVP_CIPHER * cipher,const EVP_MD * md,const unsigned char * exp,const int exp_len)59*e0c4386eSCy Schubert static int test_pkcs5_pbe(const EVP_CIPHER *cipher, const EVP_MD *md,
60*e0c4386eSCy Schubert                           const unsigned char *exp, const int exp_len)
61*e0c4386eSCy Schubert {
62*e0c4386eSCy Schubert     int ret = 0;
63*e0c4386eSCy Schubert     EVP_CIPHER_CTX *ctx;
64*e0c4386eSCy Schubert     X509_ALGOR *algor = NULL;
65*e0c4386eSCy Schubert     int i, outlen;
66*e0c4386eSCy Schubert     unsigned char out[32];
67*e0c4386eSCy Schubert 
68*e0c4386eSCy Schubert     ctx = EVP_CIPHER_CTX_new();
69*e0c4386eSCy Schubert     if (!TEST_ptr(ctx))
70*e0c4386eSCy Schubert         goto err;
71*e0c4386eSCy Schubert 
72*e0c4386eSCy Schubert     algor = X509_ALGOR_new();
73*e0c4386eSCy Schubert     if (!TEST_ptr(algor))
74*e0c4386eSCy Schubert         goto err;
75*e0c4386eSCy Schubert 
76*e0c4386eSCy Schubert     if (!TEST_true(PKCS5_pbe_set0_algor(algor, EVP_CIPHER_nid(cipher), pbe_iter,
77*e0c4386eSCy Schubert                                         pbe_salt, sizeof(pbe_salt)))
78*e0c4386eSCy Schubert         || !TEST_true(PKCS5_PBE_keyivgen(ctx, pbe_password, strlen(pbe_password),
79*e0c4386eSCy Schubert                                           algor->parameter, cipher, md, 1))
80*e0c4386eSCy Schubert         || !TEST_true(EVP_CipherUpdate(ctx, out, &i, pbe_plaintext,
81*e0c4386eSCy Schubert                                        sizeof(pbe_plaintext))))
82*e0c4386eSCy Schubert         goto err;
83*e0c4386eSCy Schubert     outlen = i;
84*e0c4386eSCy Schubert 
85*e0c4386eSCy Schubert     if (!TEST_true(EVP_CipherFinal_ex(ctx, out + i, &i)))
86*e0c4386eSCy Schubert         goto err;
87*e0c4386eSCy Schubert     outlen += i;
88*e0c4386eSCy Schubert 
89*e0c4386eSCy Schubert     if (!TEST_mem_eq(out, outlen, exp, exp_len))
90*e0c4386eSCy Schubert         goto err;
91*e0c4386eSCy Schubert 
92*e0c4386eSCy Schubert     /* Decrypt */
93*e0c4386eSCy Schubert 
94*e0c4386eSCy Schubert     if (!TEST_true(PKCS5_PBE_keyivgen(ctx, pbe_password, strlen(pbe_password),
95*e0c4386eSCy Schubert                                           algor->parameter, cipher, md, 0))
96*e0c4386eSCy Schubert         || !TEST_true(EVP_CipherUpdate(ctx, out, &i, exp, exp_len)))
97*e0c4386eSCy Schubert         goto err;
98*e0c4386eSCy Schubert 
99*e0c4386eSCy Schubert     outlen = i;
100*e0c4386eSCy Schubert     if (!TEST_true(EVP_CipherFinal_ex(ctx, out + i, &i)))
101*e0c4386eSCy Schubert         goto err;
102*e0c4386eSCy Schubert 
103*e0c4386eSCy Schubert     if (!TEST_mem_eq(out, outlen, pbe_plaintext, sizeof(pbe_plaintext)))
104*e0c4386eSCy Schubert         goto err;
105*e0c4386eSCy Schubert 
106*e0c4386eSCy Schubert     ret = 1;
107*e0c4386eSCy Schubert err:
108*e0c4386eSCy Schubert     EVP_CIPHER_CTX_free(ctx);
109*e0c4386eSCy Schubert     X509_ALGOR_free(algor);
110*e0c4386eSCy Schubert     return ret;
111*e0c4386eSCy Schubert }
112*e0c4386eSCy Schubert #endif
113*e0c4386eSCy Schubert 
114*e0c4386eSCy Schubert #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
test_pkcs5_pbe_rc4_md5(void)115*e0c4386eSCy Schubert static int test_pkcs5_pbe_rc4_md5(void)
116*e0c4386eSCy Schubert {
117*e0c4386eSCy Schubert     return test_pkcs5_pbe(EVP_rc4(), EVP_md5(), pbe_ciphertext_rc4_md5, sizeof(pbe_ciphertext_rc4_md5));
118*e0c4386eSCy Schubert }
119*e0c4386eSCy Schubert #endif
120*e0c4386eSCy Schubert 
121*e0c4386eSCy Schubert #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
test_pkcs5_pbe_des_sha1(void)122*e0c4386eSCy Schubert static int test_pkcs5_pbe_des_sha1(void)
123*e0c4386eSCy Schubert {
124*e0c4386eSCy Schubert     return test_pkcs5_pbe(EVP_des_cbc(), EVP_sha1(), pbe_ciphertext_des_sha1, sizeof(pbe_ciphertext_des_sha1));
125*e0c4386eSCy Schubert }
126*e0c4386eSCy Schubert #endif
127*e0c4386eSCy Schubert 
128*e0c4386eSCy Schubert #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
129*e0c4386eSCy Schubert /*
130*e0c4386eSCy Schubert  * For configurations where we are not autoloading configuration, we need
131*e0c4386eSCy Schubert  * to access the legacy provider.  The easiest way is to load both the
132*e0c4386eSCy Schubert  * legacy and default providers directly and unload them on termination.
133*e0c4386eSCy Schubert  */
134*e0c4386eSCy Schubert static OSSL_PROVIDER *legacy, *dflt;
135*e0c4386eSCy Schubert #endif
136*e0c4386eSCy Schubert 
setup_tests(void)137*e0c4386eSCy Schubert int setup_tests(void)
138*e0c4386eSCy Schubert {
139*e0c4386eSCy Schubert #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
140*e0c4386eSCy Schubert     /* Load required providers if not done via configuration */
141*e0c4386eSCy Schubert     legacy = OSSL_PROVIDER_load(NULL, "legacy");
142*e0c4386eSCy Schubert     dflt = OSSL_PROVIDER_load(NULL, "default");
143*e0c4386eSCy Schubert     if (!TEST_ptr(legacy) || !TEST_ptr(dflt)) {
144*e0c4386eSCy Schubert         cleanup_tests();
145*e0c4386eSCy Schubert         return -1;
146*e0c4386eSCy Schubert     }
147*e0c4386eSCy Schubert #endif
148*e0c4386eSCy Schubert 
149*e0c4386eSCy Schubert #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
150*e0c4386eSCy Schubert     ADD_TEST(test_pkcs5_pbe_rc4_md5);
151*e0c4386eSCy Schubert #endif
152*e0c4386eSCy Schubert #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
153*e0c4386eSCy Schubert     ADD_TEST(test_pkcs5_pbe_des_sha1);
154*e0c4386eSCy Schubert #endif
155*e0c4386eSCy Schubert 
156*e0c4386eSCy Schubert     return 1;
157*e0c4386eSCy Schubert }
158*e0c4386eSCy Schubert 
159*e0c4386eSCy Schubert #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
cleanup_tests(void)160*e0c4386eSCy Schubert void cleanup_tests(void)
161*e0c4386eSCy Schubert {
162*e0c4386eSCy Schubert     /* Dispose of providers */
163*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(legacy);
164*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(dflt);
165*e0c4386eSCy Schubert     legacy = dflt = NULL;
166*e0c4386eSCy Schubert }
167*e0c4386eSCy Schubert #endif
168