VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.2/test/pbetest.c@ 101021

Last change on this file since 101021 was 101021, checked in by vboxsync, 15 months ago

openssl-3.1.2: Applied and adjusted our OpenSSL changes to 3.1.0. bugref:10519

File size: 4.0 KB
Line 
1/*
2 * Copyright 2021-2022 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#include <string.h>
11
12#include "testutil.h"
13
14#include <openssl/evp.h>
15#include <openssl/x509.h>
16#include <openssl/rc4.h>
17#include <openssl/md5.h>
18
19#if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 \
20 || !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
21static const char pbe_password[] = "MyVoiceIsMyPassport";
22
23static unsigned char pbe_salt[] = {
24 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
25};
26
27static const int pbe_iter = 1000;
28
29static unsigned char pbe_plaintext[] = {
30 0x57, 0x65, 0x20, 0x61, 0x72, 0x65, 0x20, 0x61,
31 0x6c, 0x6c, 0x20, 0x6d, 0x61, 0x64, 0x65, 0x20,
32 0x6f, 0x66, 0x20, 0x73, 0x74, 0x61, 0x72, 0x73,
33};
34#endif
35
36/* Expected output generated using OpenSSL 1.1.1 */
37
38#if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
39static const unsigned char pbe_ciphertext_rc4_md5[] = {
40 0x21, 0x90, 0xfa, 0xee, 0x95, 0x66, 0x59, 0x45,
41 0xfa, 0x1e, 0x9f, 0xe2, 0x25, 0xd2, 0xf9, 0x71,
42 0x94, 0xe4, 0x3d, 0xc9, 0x7c, 0xb0, 0x07, 0x23,
43};
44#endif
45
46#if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
47static const unsigned char pbe_ciphertext_des_sha1[] = {
48 0xce, 0x4b, 0xb0, 0x0a, 0x7b, 0x48, 0xd7, 0xe3,
49 0x9a, 0x9f, 0x46, 0xd6, 0x41, 0x42, 0x4b, 0x44,
50 0x36, 0x45, 0x5f, 0x60, 0x8f, 0x3c, 0xd0, 0x55,
51 0xd0, 0x8d, 0xa9, 0xab, 0x78, 0x5b, 0x63, 0xaf,
52};
53#endif
54
55#if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 \
56 || !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
57static int test_pkcs5_pbe(const EVP_CIPHER *cipher, const EVP_MD *md,
58 const unsigned char *exp, const int exp_len)
59{
60 int ret = 0;
61 EVP_CIPHER_CTX *ctx;
62 X509_ALGOR *algor = NULL;
63 int i, outlen;
64 unsigned char out[32];
65
66 ctx = EVP_CIPHER_CTX_new();
67 if (!TEST_ptr(ctx))
68 goto err;
69
70 algor = X509_ALGOR_new();
71 if (!TEST_ptr(algor))
72 goto err;
73
74 if (!TEST_true(PKCS5_pbe_set0_algor(algor, EVP_CIPHER_nid(cipher), pbe_iter,
75 pbe_salt, sizeof(pbe_salt)))
76 || !TEST_true(PKCS5_PBE_keyivgen(ctx, pbe_password, strlen(pbe_password),
77 algor->parameter, cipher, md, 1))
78 || !TEST_true(EVP_CipherUpdate(ctx, out, &i, pbe_plaintext,
79 sizeof(pbe_plaintext))))
80 goto err;
81 outlen = i;
82
83 if (!TEST_true(EVP_CipherFinal_ex(ctx, out + i, &i)))
84 goto err;
85 outlen += i;
86
87 if (!TEST_mem_eq(out, outlen, exp, exp_len))
88 goto err;
89
90 /* Decrypt */
91
92 if (!TEST_true(PKCS5_PBE_keyivgen(ctx, pbe_password, strlen(pbe_password),
93 algor->parameter, cipher, md, 0))
94 || !TEST_true(EVP_CipherUpdate(ctx, out, &i, exp, exp_len)))
95 goto err;
96
97 outlen = i;
98 if (!TEST_true(EVP_CipherFinal_ex(ctx, out + i, &i)))
99 goto err;
100
101 if (!TEST_mem_eq(out, outlen, pbe_plaintext, sizeof(pbe_plaintext)))
102 goto err;
103
104 ret = 1;
105err:
106 EVP_CIPHER_CTX_free(ctx);
107 X509_ALGOR_free(algor);
108 return ret;
109}
110#endif
111
112#if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
113static int test_pkcs5_pbe_rc4_md5(void)
114{
115 return test_pkcs5_pbe(EVP_rc4(), EVP_md5(), pbe_ciphertext_rc4_md5, sizeof(pbe_ciphertext_rc4_md5));
116}
117#endif
118
119#if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
120static int test_pkcs5_pbe_des_sha1(void)
121{
122 return test_pkcs5_pbe(EVP_des_cbc(), EVP_sha1(), pbe_ciphertext_des_sha1, sizeof(pbe_ciphertext_des_sha1));
123}
124#endif
125
126int setup_tests(void)
127{
128#if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
129 ADD_TEST(test_pkcs5_pbe_rc4_md5);
130#endif
131#if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
132 ADD_TEST(test_pkcs5_pbe_des_sha1);
133#endif
134
135 return 1;
136}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette