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