2013-01-29 04:07:54 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
2013-03-05 07:32:27 +01:00
|
|
|
* Copyright (c) 2007-2013, Valient Gough
|
2013-01-29 04:07:54 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cipher/openssl.h"
|
2013-03-05 07:39:51 +01:00
|
|
|
#include "base/config.h"
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-03-05 07:32:27 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <ctime>
|
2013-01-29 04:07:54 +01:00
|
|
|
#include <pthread.h>
|
2013-03-05 07:32:27 +01:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2013-03-05 07:39:51 +01:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-29 04:07:54 +01:00
|
|
|
#define NO_DES
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
#include <openssl/engine.h>
|
|
|
|
#endif
|
|
|
|
|
2013-03-05 07:32:27 +01:00
|
|
|
#include <openssl/blowfish.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/hmac.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
#include "base/Error.h"
|
|
|
|
#include "base/i18n.h"
|
|
|
|
#include "base/Mutex.h"
|
|
|
|
#include "base/Range.h"
|
|
|
|
|
|
|
|
#include "cipher/BlockCipher.h"
|
|
|
|
#include "cipher/MAC.h"
|
|
|
|
#include "cipher/MemoryPool.h"
|
|
|
|
#include "cipher/PBKDF.h"
|
|
|
|
#include "cipher/StreamCipher.h"
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
namespace encfs {
|
|
|
|
|
2013-03-05 07:32:27 +01:00
|
|
|
const int MAX_KEYLENGTH = 64; // in bytes (256 bit)
|
|
|
|
const int MAX_IVLENGTH = 16;
|
|
|
|
const int KEY_CHECKSUM_BYTES = 4;
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
inline int MIN(int a, int b)
|
|
|
|
{
|
|
|
|
return (a < b) ? a : b;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Base for {Block,Stream}Cipher implementation.
|
|
|
|
class OpenSSLCipher : public BlockCipher {
|
|
|
|
public:
|
|
|
|
OpenSSLCipher() {
|
2013-03-05 07:36:32 +01:00
|
|
|
EVP_CIPHER_CTX_init( &enc );
|
|
|
|
EVP_CIPHER_CTX_init( &dec );
|
2013-03-05 07:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~OpenSSLCipher() {
|
|
|
|
EVP_CIPHER_CTX_cleanup( &enc );
|
|
|
|
EVP_CIPHER_CTX_cleanup( &dec );
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
bool rekey(const EVP_CIPHER *cipher, const CipherKey &key) {
|
|
|
|
VLOG(1) << "setting key length " << key.size();
|
2013-03-05 07:32:27 +01:00
|
|
|
EVP_EncryptInit_ex( &enc, cipher, NULL, NULL, NULL);
|
2013-03-05 07:36:32 +01:00
|
|
|
EVP_CIPHER_CTX_set_key_length( &enc, key.size() );
|
2013-03-05 07:32:27 +01:00
|
|
|
EVP_CIPHER_CTX_set_padding( &enc, 0 );
|
2013-03-05 07:36:32 +01:00
|
|
|
EVP_EncryptInit_ex( &enc, NULL, NULL, key.data(), NULL);
|
2013-03-05 07:32:27 +01:00
|
|
|
|
|
|
|
EVP_DecryptInit_ex( &dec, cipher, NULL, NULL, NULL);
|
2013-03-05 07:36:32 +01:00
|
|
|
EVP_CIPHER_CTX_set_key_length( &dec, key.size() );
|
2013-03-05 07:32:27 +01:00
|
|
|
EVP_CIPHER_CTX_set_padding( &dec, 0 );
|
2013-03-05 07:36:32 +01:00
|
|
|
EVP_DecryptInit_ex( &dec, NULL, NULL, key.data(), NULL);
|
2013-03-05 07:32:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
static bool randomize(CipherKey *key) {
|
|
|
|
int result = RAND_bytes( key->data(), key->size() );
|
|
|
|
if(result != 1)
|
|
|
|
{
|
|
|
|
char errStr[120]; // specs require string at least 120 bytes long..
|
|
|
|
unsigned long errVal = 0;
|
|
|
|
if((errVal = ERR_get_error()) != 0)
|
|
|
|
LOG(ERROR) << "openssl error: " << ERR_error_string( errVal, errStr );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-05 07:39:51 +01:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(key->data(), key->size());
|
|
|
|
#endif
|
2013-03-05 07:36:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pseudoRandomize(byte *out, int length) {
|
2013-03-05 07:39:51 +01:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
if (VALGRIND_CHECK_MEM_IS_ADDRESSABLE(out, length) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-05 07:36:32 +01:00
|
|
|
int result = RAND_pseudo_bytes( out, length );
|
2013-03-05 07:32:27 +01:00
|
|
|
if(result != 1)
|
|
|
|
{
|
|
|
|
char errStr[120]; // specs require string at least 120 bytes long..
|
|
|
|
unsigned long errVal = 0;
|
|
|
|
if((errVal = ERR_get_error()) != 0)
|
|
|
|
LOG(ERROR) << "openssl error: " << ERR_error_string( errVal, errStr );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rekey with random key.
|
|
|
|
bool rekey(const EVP_CIPHER *cipher, int keyLength) {
|
2013-03-05 07:36:32 +01:00
|
|
|
CipherKey key(keyLength);
|
2013-03-05 07:32:27 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
if (!randomize(&key))
|
2013-03-05 07:32:27 +01:00
|
|
|
return false;
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
return rekey(cipher, key);
|
2013-03-05 07:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual int blockSize() const {
|
2013-03-05 07:39:51 +01:00
|
|
|
int len = EVP_CIPHER_CTX_block_size(&enc);
|
|
|
|
// Some versions of OpenSSL report 1 for block size os AES_XTS..
|
|
|
|
if (len == 1)
|
|
|
|
len = EVP_CIPHER_CTX_key_length(&enc);
|
|
|
|
return len;
|
2013-03-05 07:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool encrypt(const byte *ivec, const byte *in,
|
|
|
|
byte *out, int size) {
|
|
|
|
int dstLen = 0, tmpLen = 0;
|
2013-03-05 07:39:51 +01:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
int ivLen = EVP_CIPHER_CTX_iv_length(&enc);
|
|
|
|
if (VALGRIND_CHECK_MEM_IS_DEFINED(ivec, ivLen) != 0) {
|
|
|
|
LOG(ERROR) << "expected iv of length " << ivLen;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (VALGRIND_CHECK_MEM_IS_ADDRESSABLE(out, size) != 0) {
|
|
|
|
LOG(ERROR) << "expected output length " << size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-05 07:32:27 +01:00
|
|
|
EVP_EncryptInit_ex( &enc, NULL, NULL, NULL, ivec);
|
|
|
|
EVP_EncryptUpdate( &enc, out, &dstLen, in, size);
|
|
|
|
EVP_EncryptFinal_ex( &enc, out+dstLen, &tmpLen );
|
|
|
|
dstLen += tmpLen;
|
|
|
|
|
|
|
|
if (dstLen != size) {
|
|
|
|
LOG(ERROR) << "encoding " << size
|
|
|
|
<< " bytes, got back " << dstLen << " (" << tmpLen << " in final_ex)";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool decrypt(const byte *ivec, const byte *in,
|
|
|
|
byte *out, int size) {
|
|
|
|
int dstLen = 0, tmpLen = 0;
|
2013-03-05 07:39:51 +01:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
int ivLen = EVP_CIPHER_CTX_iv_length(&enc);
|
|
|
|
if (VALGRIND_CHECK_MEM_IS_DEFINED(ivec, ivLen) != 0) {
|
|
|
|
LOG(ERROR) << "expected iv of length " << ivLen;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (VALGRIND_CHECK_MEM_IS_ADDRESSABLE(out, size) != 0) {
|
|
|
|
LOG(ERROR) << "expected output length " << size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-05 07:32:27 +01:00
|
|
|
EVP_DecryptInit_ex( &dec, NULL, NULL, NULL, ivec);
|
|
|
|
EVP_DecryptUpdate( &dec, out, &dstLen, in, size );
|
|
|
|
EVP_DecryptFinal_ex( &dec, out+dstLen, &tmpLen );
|
|
|
|
dstLen += tmpLen;
|
|
|
|
|
|
|
|
if (dstLen != size) {
|
|
|
|
LOG(ERROR) << "decoding " << size
|
|
|
|
<< " bytes, got back " << dstLen << " (" << tmpLen << " in final_ex)";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EVP_CIPHER_CTX enc;
|
|
|
|
EVP_CIPHER_CTX dec;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_EVP_BF)
|
|
|
|
static Range BfKeyRange(128,256,32);
|
|
|
|
class BfCbcBlockCipher : public OpenSSLCipher {
|
|
|
|
public:
|
|
|
|
BfCbcBlockCipher() {}
|
|
|
|
virtual ~BfCbcBlockCipher() {}
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
virtual bool setKey(const CipherKey &key) {
|
|
|
|
if (BfKeyRange.allowed(key.size() * 8))
|
|
|
|
return rekey(EVP_bf_cbc(), key);
|
2013-03-05 07:32:27 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Properties GetProperties() {
|
|
|
|
Properties props;
|
|
|
|
props.keySize = BfKeyRange;
|
|
|
|
props.cipher = "Blowfish";
|
|
|
|
props.mode = "CBC";
|
|
|
|
props.library = "OpenSSL";
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_CLASS(BfCbcBlockCipher, BlockCipher);
|
|
|
|
|
|
|
|
class BfCfbStreamCipher : public OpenSSLCipher {
|
|
|
|
public:
|
|
|
|
BfCfbStreamCipher() {}
|
|
|
|
virtual ~BfCfbStreamCipher() {}
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
virtual bool setKey(const CipherKey &key) {
|
|
|
|
return BfKeyRange.allowed(key.size() * 8) && rekey(EVP_bf_cfb(), key);
|
2013-03-05 07:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Properties GetProperties() {
|
|
|
|
Properties props;
|
|
|
|
props.keySize = BfKeyRange;
|
|
|
|
props.cipher = "Blowfish";
|
|
|
|
props.mode = "CFB";
|
|
|
|
props.library = "OpenSSL";
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_CLASS(BfCfbStreamCipher, StreamCipher);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_EVP_AES)
|
|
|
|
static Range AesKeyRange(128,256,64);
|
|
|
|
class AesCbcBlockCipher : public OpenSSLCipher {
|
|
|
|
public:
|
|
|
|
AesCbcBlockCipher() {}
|
|
|
|
virtual ~AesCbcBlockCipher() {}
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
virtual bool setKey(const CipherKey& key) {
|
|
|
|
const EVP_CIPHER *cipher = getCipher(key.size());
|
|
|
|
return (cipher != NULL) && rekey(cipher, key);
|
2013-03-05 07:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_CIPHER *getCipher(int keyLength) {
|
|
|
|
switch(keyLength * 8)
|
|
|
|
{
|
|
|
|
case 128: return EVP_aes_128_cbc();
|
|
|
|
case 192: return EVP_aes_192_cbc();
|
|
|
|
case 256: return EVP_aes_256_cbc();
|
|
|
|
default:
|
2013-03-05 07:36:32 +01:00
|
|
|
LOG(INFO) << "Unsupported key length: " << keyLength;
|
2013-03-05 07:32:27 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Properties GetProperties() {
|
|
|
|
Properties props;
|
|
|
|
props.keySize = AesKeyRange;
|
|
|
|
props.cipher = "AES";
|
|
|
|
props.mode = "CBC";
|
|
|
|
props.library = "OpenSSL";
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_CLASS(AesCbcBlockCipher, BlockCipher);
|
2013-03-05 07:36:32 +01:00
|
|
|
|
|
|
|
class AesCfbStreamCipher : public OpenSSLCipher {
|
|
|
|
public:
|
|
|
|
AesCfbStreamCipher() {}
|
|
|
|
virtual ~AesCfbStreamCipher() {}
|
|
|
|
|
|
|
|
virtual bool setKey(const CipherKey& key) {
|
|
|
|
const EVP_CIPHER *cipher = getCipher(key.size());
|
|
|
|
return (cipher != NULL) && rekey(cipher, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_CIPHER *getCipher(int keyLength) {
|
|
|
|
switch(keyLength * 8)
|
|
|
|
{
|
|
|
|
case 128: return EVP_aes_128_cfb();
|
|
|
|
case 192: return EVP_aes_192_cfb();
|
|
|
|
case 256: return EVP_aes_256_cfb();
|
|
|
|
default:
|
|
|
|
LOG(INFO) << "Unsupported key length: " << keyLength;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Properties GetProperties() {
|
|
|
|
Properties props;
|
|
|
|
props.keySize = AesKeyRange;
|
|
|
|
props.cipher = "AES";
|
|
|
|
props.mode = "CFB";
|
|
|
|
props.library = "OpenSSL";
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_CLASS(AesCfbStreamCipher, StreamCipher);
|
2013-03-05 07:32:27 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_EVP_AES_XTS)
|
|
|
|
static Range AesXtsKeyRange(128,256,128);
|
|
|
|
class AesXtsBlockCipher : public OpenSSLCipher {
|
|
|
|
public:
|
|
|
|
AesXtsBlockCipher() {}
|
|
|
|
virtual ~AesXtsBlockCipher() {}
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
virtual bool setKey(const CipherKey &key) {
|
|
|
|
const EVP_CIPHER *cipher = getCipher(key.size());
|
|
|
|
return (cipher != NULL) && rekey(cipher, key);
|
2013-03-05 07:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_CIPHER *getCipher(int keyLength) {
|
|
|
|
switch(keyLength * 8)
|
|
|
|
{
|
|
|
|
case 128: return EVP_aes_128_xts();
|
|
|
|
case 256: return EVP_aes_256_xts();
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Properties GetProperties() {
|
|
|
|
Properties props;
|
|
|
|
props.keySize = AesXtsKeyRange;
|
|
|
|
props.cipher = "AES";
|
|
|
|
props.mode = "XTS";
|
|
|
|
props.library = "OpenSSL";
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_CLASS(AesXtsBlockCipher, BlockCipher);
|
|
|
|
#endif
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
class Sha1HMac : public MAC {
|
2013-03-05 07:32:27 +01:00
|
|
|
public:
|
2013-03-05 07:36:32 +01:00
|
|
|
Sha1HMac() {
|
|
|
|
HMAC_CTX_init(&ctx);
|
|
|
|
}
|
2013-03-05 07:32:27 +01:00
|
|
|
virtual ~Sha1HMac() {
|
|
|
|
HMAC_CTX_cleanup(&ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int outputSize() const {
|
|
|
|
return 20; // 160 bit.
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
virtual bool setKey(const CipherKey &key) {
|
|
|
|
HMAC_Init_ex(&ctx, key.data(), key.size(), EVP_sha1(), 0);
|
2013-03-05 07:32:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:39:51 +01:00
|
|
|
virtual void init() {
|
2013-03-05 07:32:27 +01:00
|
|
|
HMAC_Init_ex(&ctx, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool update(const byte *in, int length) {
|
|
|
|
HMAC_Update(&ctx, in, length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool write(byte *out) {
|
2013-03-05 07:36:32 +01:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
if (VALGRIND_CHECK_MEM_IS_ADDRESSABLE(out, 20) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-05 07:32:27 +01:00
|
|
|
unsigned int outSize = 0;
|
|
|
|
HMAC_Final(&ctx, (unsigned char *)out, &outSize);
|
|
|
|
CHECK_EQ(outputSize(), outSize) << "Invalid HMAC output size";
|
2013-03-05 07:39:51 +01:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(out, outSize);
|
|
|
|
#endif
|
2013-03-05 07:32:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Properties GetProperties() {
|
|
|
|
Properties props;
|
|
|
|
props.blockSize = 20;
|
|
|
|
props.hashFunction = "SHA-1";
|
|
|
|
props.mode = "HMAC";
|
|
|
|
props.library = "OpenSSL";
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
HMAC_CTX ctx;
|
|
|
|
};
|
2013-03-05 07:36:32 +01:00
|
|
|
REGISTER_CLASS(Sha1HMac, MAC);
|
2013-03-05 07:32:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PbkdfPkcs5HmacSha1 : public PBKDF {
|
|
|
|
public:
|
|
|
|
PbkdfPkcs5HmacSha1() {}
|
|
|
|
virtual ~PbkdfPkcs5HmacSha1() {}
|
|
|
|
|
|
|
|
virtual bool makeKey(const char *password, int passwordLength,
|
|
|
|
const byte *salt, int saltLength,
|
|
|
|
int numIterations,
|
2013-03-05 07:36:32 +01:00
|
|
|
CipherKey *outKey) {
|
2013-03-05 07:32:27 +01:00
|
|
|
return PKCS5_PBKDF2_HMAC_SHA1(
|
|
|
|
password, passwordLength,
|
|
|
|
const_cast<byte *>(salt), saltLength,
|
2013-03-05 07:36:32 +01:00
|
|
|
numIterations, outKey->size(), outKey->data()) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual CipherKey randomKey(int length) {
|
|
|
|
CipherKey key(length);
|
|
|
|
if (!OpenSSLCipher::randomize(&key))
|
|
|
|
key.reset();
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool pseudoRandom(byte *out, int length) {
|
|
|
|
return OpenSSLCipher::pseudoRandomize(out, length);
|
2013-03-05 07:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Properties GetProperties() {
|
|
|
|
Properties props;
|
2013-03-06 09:02:23 +01:00
|
|
|
props.mode = NAME_PBKDF2_HMAC_SHA1;
|
2013-03-05 07:32:27 +01:00
|
|
|
props.library = "OpenSSL";
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_CLASS(PbkdfPkcs5HmacSha1, PBKDF);
|
|
|
|
|
|
|
|
|
2013-01-29 04:07:54 +01:00
|
|
|
unsigned long pthreads_thread_id()
|
|
|
|
{
|
|
|
|
return (unsigned long)pthread_self();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pthread_mutex_t *crypto_locks = NULL;
|
|
|
|
void pthreads_locking_callback( int mode, int n,
|
|
|
|
const char *caller_file, int caller_line )
|
|
|
|
{
|
|
|
|
(void)caller_file;
|
|
|
|
(void)caller_line;
|
|
|
|
|
|
|
|
if(!crypto_locks)
|
|
|
|
{
|
|
|
|
VLOG(1) << "Allocating " << CRYPTO_num_locks() << " locks for OpenSSL";
|
|
|
|
crypto_locks = new pthread_mutex_t[ CRYPTO_num_locks() ];
|
|
|
|
for(int i=0; i<CRYPTO_num_locks(); ++i)
|
|
|
|
pthread_mutex_init( crypto_locks+i, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mode & CRYPTO_LOCK)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock( crypto_locks + n );
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock( crypto_locks + n );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pthreads_locking_cleanup()
|
|
|
|
{
|
|
|
|
if(crypto_locks)
|
|
|
|
{
|
|
|
|
for(int i=0; i<CRYPTO_num_locks(); ++i)
|
|
|
|
pthread_mutex_destroy( crypto_locks+i );
|
|
|
|
delete[] crypto_locks;
|
|
|
|
crypto_locks = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:32:27 +01:00
|
|
|
void OpenSSL::init(bool threaded)
|
2013-01-29 04:07:54 +01:00
|
|
|
{
|
|
|
|
// initialize the SSL library
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
|
|
|
|
unsigned int randSeed = 0;
|
|
|
|
RAND_bytes( (unsigned char*)&randSeed, sizeof(randSeed) );
|
|
|
|
srand( randSeed );
|
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
/* Load all bundled ENGINEs into memory and make them visible */
|
|
|
|
ENGINE_load_builtin_engines();
|
|
|
|
/* Register all of them for every algorithm they collectively implement */
|
|
|
|
ENGINE_register_all_complete();
|
|
|
|
#endif // NO_ENGINE
|
|
|
|
|
|
|
|
if(threaded)
|
|
|
|
{
|
|
|
|
// provide locking functions to OpenSSL since we'll be running with
|
|
|
|
// threads accessing openssl in parallel.
|
|
|
|
CRYPTO_set_id_callback( pthreads_thread_id );
|
|
|
|
CRYPTO_set_locking_callback( pthreads_locking_callback );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:32:27 +01:00
|
|
|
void OpenSSL::shutdown(bool threaded)
|
2013-01-29 04:07:54 +01:00
|
|
|
{
|
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
ENGINE_cleanup();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(threaded)
|
|
|
|
pthreads_locking_cleanup();
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:32:27 +01:00
|
|
|
void OpenSSL::registerCiphers()
|
|
|
|
{
|
|
|
|
// Nothing required.. Just need to reference this code block to get static
|
|
|
|
// initializers.
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
} // namespace encfs
|