2015-10-08 23:26:37 +03:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
*
|
|
|
|
* Copyright 2015 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
#include <winpr/crypto.h>
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
#include <openssl/aes.h>
|
|
|
|
#include <openssl/rc4.h>
|
|
|
|
#include <openssl/des.h>
|
2015-10-09 19:15:31 +03:00
|
|
|
#include <openssl/evp.h>
|
2015-10-08 23:26:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_MBEDTLS
|
2015-10-09 22:23:15 +03:00
|
|
|
#include <mbedtls/md.h>
|
2015-10-08 23:26:37 +03:00
|
|
|
#include <mbedtls/aes.h>
|
|
|
|
#include <mbedtls/arc4.h>
|
|
|
|
#include <mbedtls/des.h>
|
2015-10-09 19:15:31 +03:00
|
|
|
#include <mbedtls/cipher.h>
|
2015-10-08 23:26:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RC4
|
|
|
|
*/
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_RC4_Init(WINPR_RC4_CTX* ctx, const BYTE* key, size_t keylen)
|
2015-10-08 23:26:37 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
RC4_set_key((RC4_KEY*) ctx, keylen, key);
|
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_ARC4_C)
|
|
|
|
mbedtls_arc4_init((mbedtls_arc4_context*) ctx);
|
|
|
|
mbedtls_arc4_setup((mbedtls_arc4_context*) ctx, key, keylen);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
return TRUE;
|
2015-10-08 23:26:37 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_RC4_Update(WINPR_RC4_CTX* ctx, size_t length, const BYTE* input, BYTE* output)
|
2015-10-08 23:26:37 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
RC4((RC4_KEY*) ctx, length, input, output);
|
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_ARC4_C)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (mbedtls_arc4_crypt((mbedtls_arc4_context*) ctx, length, input, output) != 0)
|
|
|
|
return FALSE;
|
2015-10-08 23:26:37 +03:00
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2015-10-08 23:26:37 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_RC4_Final(WINPR_RC4_CTX* ctx)
|
2015-10-08 23:26:37 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
|
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_ARC4_C)
|
|
|
|
mbedtls_arc4_free((mbedtls_arc4_context*) ctx);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
return TRUE;
|
2015-10-08 23:26:37 +03:00
|
|
|
}
|
2015-10-09 19:15:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic Cipher API
|
|
|
|
*/
|
|
|
|
|
2015-10-09 22:23:15 +03:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
extern const EVP_MD* winpr_openssl_get_evp_md(int md);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_MBEDTLS
|
|
|
|
extern mbedtls_md_type_t winpr_mbedtls_get_md_type(int md);
|
|
|
|
#endif
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
const EVP_CIPHER* winpr_openssl_get_evp_cipher(int cipher)
|
|
|
|
{
|
|
|
|
const EVP_CIPHER* evp = NULL;
|
|
|
|
|
2015-10-13 16:13:52 +03:00
|
|
|
OpenSSL_add_all_ciphers();
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
switch (cipher)
|
|
|
|
{
|
|
|
|
case WINPR_CIPHER_NULL:
|
|
|
|
evp = EVP_enc_null();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_ECB:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-128-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_ECB:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-192-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_ECB:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-256-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_CBC:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-128-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_CBC:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-192-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_CBC:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-256-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_CFB128:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-128-cfb128");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_CFB128:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-192-cfb128");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_CFB128:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-256-cfb128");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_CTR:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-128-ctr");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_CTR:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-192-ctr");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_CTR:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-256-ctr");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_GCM:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-128-gcm");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_GCM:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-192-gcm");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_GCM:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-256-gcm");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_CCM:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-128-ccm");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_CCM:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-192-ccm");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_CCM:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("aes-256-ccm");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_ECB:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-128-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_ECB:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-192-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_ECB:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-256-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_CBC:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-128-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_CBC:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-192-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_CBC:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-256-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_CFB128:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-128-cfb128");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_CFB128:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-192-cfb128");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_CFB128:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-256-cfb128");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_CTR:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-128-ctr");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_CAMELLIA_192_CTR:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-192-ctr");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_CAMELLIA_256_CTR:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-256-ctr");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_CAMELLIA_128_GCM:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-128-gcm");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_CAMELLIA_192_GCM:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-192-gcm");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_CAMELLIA_256_GCM:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-256-gcm");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_CAMELLIA_128_CCM:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-128-ccm");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_CAMELLIA_192_CCM:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-192-gcm");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_CAMELLIA_256_CCM:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("camellia-256-gcm");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_ECB:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("des-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_CBC:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("des-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_EDE_ECB:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("des-ede-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_EDE_CBC:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("des-ede-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_EDE3_ECB:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("des-ede3-ecb");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_EDE3_CBC:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("des-ede3-cbc");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_ARC4_128:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("rc4");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_BLOWFISH_ECB:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("blowfish-ecb");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_BLOWFISH_CBC:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("blowfish-cbc");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_BLOWFISH_CFB64:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("blowfish-cfb64");
|
|
|
|
break;
|
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
case WINPR_CIPHER_BLOWFISH_CTR:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_cipherbyname("blowfish-ctr");
|
2015-10-09 19:15:31 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return evp;
|
|
|
|
}
|
|
|
|
#elif defined(WITH_MBEDTLS)
|
|
|
|
mbedtls_cipher_type_t winpr_mbedtls_get_cipher_type(int cipher)
|
|
|
|
{
|
|
|
|
mbedtls_cipher_type_t type = MBEDTLS_CIPHER_NONE;
|
|
|
|
|
|
|
|
switch (cipher)
|
|
|
|
{
|
|
|
|
case WINPR_CIPHER_NONE:
|
|
|
|
type = MBEDTLS_CIPHER_NONE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_NULL:
|
|
|
|
type = MBEDTLS_CIPHER_NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_AES_192_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_AES_256_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_AES_192_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_AES_256_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_CFB128:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_CFB128;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_CFB128:
|
|
|
|
type = MBEDTLS_CIPHER_AES_192_CFB128;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_CFB128:
|
|
|
|
type = MBEDTLS_CIPHER_AES_256_CFB128;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_CTR:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_CTR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_CTR:
|
|
|
|
type = MBEDTLS_CIPHER_AES_192_CTR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_CTR:
|
|
|
|
type = MBEDTLS_CIPHER_AES_256_CTR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_GCM:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_GCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_GCM:
|
|
|
|
type = MBEDTLS_CIPHER_AES_192_GCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_GCM:
|
|
|
|
type = MBEDTLS_CIPHER_AES_256_GCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_128_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_192_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_256_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_128_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_192_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_256_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_CFB128:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_128_CFB128;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_CFB128:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_192_CFB128;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_CFB128:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_256_CFB128;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_CTR:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_128_CTR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_CTR:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_192_CTR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_CTR:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_256_CTR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_GCM:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_128_GCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_GCM:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_192_GCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_GCM:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_256_GCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_DES_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_DES_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_EDE_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_DES_EDE_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_EDE_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_DES_EDE_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_EDE3_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_DES_EDE3_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_DES_EDE3_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_DES_EDE3_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_BLOWFISH_ECB:
|
|
|
|
type = MBEDTLS_CIPHER_BLOWFISH_ECB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_BLOWFISH_CBC:
|
|
|
|
type = MBEDTLS_CIPHER_BLOWFISH_CBC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_BLOWFISH_CFB64:
|
|
|
|
type = MBEDTLS_CIPHER_BLOWFISH_CFB64;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_BLOWFISH_CTR:
|
|
|
|
type = MBEDTLS_CIPHER_BLOWFISH_CTR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_ARC4_128:
|
|
|
|
type = MBEDTLS_CIPHER_ARC4_128;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_128_CCM:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_CCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_192_CCM:
|
|
|
|
type = MBEDTLS_CIPHER_AES_192_CCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_AES_256_CCM:
|
|
|
|
type = MBEDTLS_CIPHER_AES_256_CCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_128_CCM:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_128_CCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_192_CCM:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_192_CCM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_CIPHER_CAMELLIA_256_CCM:
|
|
|
|
type = MBEDTLS_CIPHER_CAMELLIA_256_CCM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-28 01:28:49 +03:00
|
|
|
BOOL winpr_Cipher_New(WINPR_CIPHER_CTX** cctx, int cipher, int op, const BYTE* key, const BYTE* iv)
|
2015-10-09 19:15:31 +03:00
|
|
|
{
|
2016-02-28 01:28:49 +03:00
|
|
|
WINPR_CIPHER_CTX* ctx;
|
2015-10-09 19:15:31 +03:00
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
int operation;
|
|
|
|
const EVP_CIPHER* evp;
|
2016-02-28 01:28:49 +03:00
|
|
|
EVP_CIPHER_CTX* octx;
|
|
|
|
#elif defined(WITH_MBEDTLS)
|
|
|
|
int key_bitlen;
|
|
|
|
mbedtls_operation_t operation;
|
|
|
|
mbedtls_cipher_type_t cipher_type;
|
|
|
|
const mbedtls_cipher_info_t* cipher_info;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ctx = calloc(1, sizeof(WINPR_CIPHER_CTX));
|
|
|
|
if (!ctx)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
octx = (EVP_CIPHER_CTX*)ctx;
|
2015-10-09 19:15:31 +03:00
|
|
|
evp = winpr_openssl_get_evp_cipher(cipher);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (!evp)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
operation = (op == WINPR_ENCRYPT) ? 1 : 0;
|
2016-02-28 01:28:49 +03:00
|
|
|
EVP_CIPHER_CTX_init(octx);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-28 01:28:49 +03:00
|
|
|
if (EVP_CipherInit_ex(octx, evp, NULL, key, iv, operation) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2016-02-28 01:28:49 +03:00
|
|
|
|
|
|
|
EVP_CIPHER_CTX_set_padding(octx, 0);
|
2015-10-09 19:15:31 +03:00
|
|
|
#elif defined(WITH_MBEDTLS)
|
|
|
|
cipher_type = winpr_mbedtls_get_cipher_type(cipher);
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type(cipher_type);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (!cipher_info)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
operation = (op == WINPR_ENCRYPT) ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT;
|
|
|
|
mbedtls_cipher_init((mbedtls_cipher_context_t*) ctx);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (mbedtls_cipher_setup((mbedtls_cipher_context_t*) ctx, cipher_info) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
key_bitlen = mbedtls_cipher_get_key_bitlen((mbedtls_cipher_context_t*) ctx);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (mbedtls_cipher_setkey((mbedtls_cipher_context_t*) ctx, key, key_bitlen, operation) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-09 19:15:31 +03:00
|
|
|
#endif
|
2016-02-28 01:28:49 +03:00
|
|
|
|
|
|
|
*cctx = ctx;
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-09 19:15:31 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_Cipher_Update(WINPR_CIPHER_CTX* ctx, const BYTE* input, size_t ilen, BYTE* output, size_t* olen)
|
2015-10-09 19:15:31 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
int outl = (int) *olen;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (EVP_CipherUpdate((EVP_CIPHER_CTX*) ctx, output, &outl, input, ilen) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
*olen = (size_t) outl;
|
|
|
|
#elif defined(WITH_MBEDTLS)
|
2015-10-13 16:43:26 +03:00
|
|
|
if (mbedtls_cipher_update((mbedtls_cipher_context_t*) ctx, input, ilen, output, olen) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-09 19:15:31 +03:00
|
|
|
#endif
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-09 19:15:31 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_Cipher_Final(WINPR_CIPHER_CTX* ctx, BYTE* output, size_t* olen)
|
2015-10-09 19:15:31 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
int outl = (int) *olen;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (EVP_CipherFinal_ex((EVP_CIPHER_CTX*) ctx, output, &outl) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-09 19:15:31 +03:00
|
|
|
*olen = (size_t) outl;
|
|
|
|
#elif defined(WITH_MBEDTLS)
|
2015-10-13 16:43:26 +03:00
|
|
|
if (mbedtls_cipher_finish((mbedtls_cipher_context_t*) ctx, output, olen) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2016-02-28 01:28:49 +03:00
|
|
|
#endif
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-28 01:28:49 +03:00
|
|
|
void winpr_Cipher_Free(WINPR_CIPHER_CTX* ctx)
|
|
|
|
{
|
|
|
|
if (!ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
EVP_CIPHER_CTX_cleanup((EVP_CIPHER_CTX*) ctx);
|
|
|
|
#elif defined(WITH_MBEDTLS)
|
2015-10-09 19:15:31 +03:00
|
|
|
mbedtls_cipher_free((mbedtls_cipher_context_t*) ctx);
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-09 22:23:15 +03:00
|
|
|
|
2016-02-28 01:28:49 +03:00
|
|
|
|
2015-10-09 22:23:15 +03:00
|
|
|
/**
|
|
|
|
* Key Generation
|
|
|
|
*/
|
|
|
|
|
|
|
|
int winpr_openssl_BytesToKey(int cipher, int md, const BYTE* salt, const BYTE* data, int datal, int count, BYTE* key, BYTE* iv)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Key and IV generation compatible with OpenSSL EVP_BytesToKey():
|
|
|
|
* https://www.openssl.org/docs/manmaster/crypto/EVP_BytesToKey.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
const EVP_MD* evp_md;
|
|
|
|
const EVP_CIPHER* evp_cipher;
|
|
|
|
evp_md = winpr_openssl_get_evp_md(md);
|
|
|
|
evp_cipher = winpr_openssl_get_evp_cipher(cipher);
|
|
|
|
return EVP_BytesToKey(evp_cipher, evp_md, salt, data, datal, count, key, iv);
|
|
|
|
#elif defined(WITH_MBEDTLS)
|
|
|
|
int rv = 0;
|
|
|
|
BYTE md_buf[64];
|
|
|
|
int niv, nkey, addmd = 0;
|
|
|
|
unsigned int mds = 0, i;
|
|
|
|
mbedtls_md_context_t ctx;
|
|
|
|
const mbedtls_md_info_t* md_info;
|
|
|
|
mbedtls_cipher_type_t cipher_type;
|
|
|
|
const mbedtls_cipher_info_t* cipher_info;
|
|
|
|
|
|
|
|
mbedtls_md_type_t md_type = winpr_mbedtls_get_md_type(md);
|
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
|
|
|
|
|
|
|
cipher_type = winpr_mbedtls_get_cipher_type(cipher);
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type(cipher_type);
|
|
|
|
|
|
|
|
nkey = cipher_info->key_bitlen / 8;
|
|
|
|
niv = cipher_info->iv_size;
|
|
|
|
|
|
|
|
if ((nkey > 64) || (niv > 64))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return nkey;
|
|
|
|
|
|
|
|
mbedtls_md_init(&ctx);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (mbedtls_md_setup(&ctx, md_info, 0) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (mbedtls_md_starts(&ctx) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (addmd++)
|
|
|
|
{
|
2015-10-09 22:57:41 +03:00
|
|
|
if (mbedtls_md_update(&ctx, md_buf, mds) != 0)
|
2015-10-09 22:23:15 +03:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbedtls_md_update(&ctx, data, datal) != 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (salt)
|
|
|
|
{
|
|
|
|
if (mbedtls_md_update(&ctx, salt, 8) != 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-10-09 22:57:41 +03:00
|
|
|
if (mbedtls_md_finish(&ctx, md_buf) != 0)
|
2015-10-09 22:23:15 +03:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
mds = mbedtls_md_get_size(md_info);
|
|
|
|
|
|
|
|
for (i = 1; i < (unsigned int) count; i++)
|
|
|
|
{
|
|
|
|
if (mbedtls_md_starts(&ctx) != 0)
|
|
|
|
goto err;
|
2015-10-09 22:57:41 +03:00
|
|
|
if (mbedtls_md_update(&ctx, md_buf, mds) != 0)
|
2015-10-09 22:23:15 +03:00
|
|
|
goto err;
|
2015-10-09 22:57:41 +03:00
|
|
|
if (mbedtls_md_finish(&ctx, md_buf) != 0)
|
2015-10-09 22:23:15 +03:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
if (nkey)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (nkey == 0)
|
|
|
|
break;
|
|
|
|
if (i == mds)
|
|
|
|
break;
|
|
|
|
if (key)
|
|
|
|
*(key++) = md_buf[i];
|
|
|
|
nkey--;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (niv && (i != mds))
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (niv == 0)
|
|
|
|
break;
|
|
|
|
if (i == mds)
|
|
|
|
break;
|
|
|
|
if (iv)
|
|
|
|
*(iv++) = md_buf[i];
|
|
|
|
niv--;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nkey == 0) && (niv == 0))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = cipher_info->key_bitlen / 8;
|
|
|
|
err:
|
|
|
|
mbedtls_md_free(&ctx);
|
2015-10-09 22:57:41 +03:00
|
|
|
SecureZeroMemory(md_buf, 64);
|
2015-10-09 22:23:15 +03:00
|
|
|
return rv;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|