0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 12:12:15 +02:00

Simplified CipherInfo and DigestInfo by not storing

the CryptoAlgs::Type in the object.

For AppleCrypto, delegate name functionality to
CipherAlgs::name().
This commit is contained in:
James Yonan 2014-10-21 23:03:55 -06:00
parent 05c2a1a56a
commit a15b680dbd
7 changed files with 160 additions and 51 deletions

View File

@ -46,23 +46,18 @@ namespace openvpn {
class CipherInfo
{
public:
CipherInfo(const char *name,
CipherInfo(CryptoAlgs::Type type,
const size_t key_size,
const size_t iv_length,
const size_t block_size,
const CCAlgorithm algorithm)
: name_(name),
: type_(type),
key_size_(key_size),
iv_length_(iv_length),
block_size_(block_size),
algorithm_(algorithm) {}
bool name_match(const char *name) const
{
return string::strcasecmp(name, name_) == 0;
}
const char *name() const { return name_; }
CryptoAlgs::Type type() const { return type_; }
size_t key_length() const { return key_size_; }
size_t iv_length() const { return iv_length_; }
size_t block_size() const { return block_size_; }
@ -70,26 +65,32 @@ namespace openvpn {
CCAlgorithm algorithm() const { return algorithm_; }
private:
const char *name_;
CryptoAlgs::Type type_;
size_t key_size_;
size_t iv_length_;
size_t block_size_;
CCAlgorithm algorithm_;
};
const CipherInfo aes128("AES-128-CBC", kCCKeySizeAES128, kCCBlockSizeAES128, // CONST GLOBAL
const CipherInfo aes128(CryptoAlgs::AES_128_CBC, // CONST GLOBAL
kCCKeySizeAES128, kCCBlockSizeAES128,
kCCBlockSizeAES128, kCCAlgorithmAES128);
const CipherInfo aes192("AES-192-CBC", kCCKeySizeAES192, kCCBlockSizeAES128, // CONST GLOBAL
const CipherInfo aes192(CryptoAlgs::AES_192_CBC, // CONST GLOBAL
kCCKeySizeAES192, kCCBlockSizeAES128,
kCCBlockSizeAES128, kCCAlgorithmAES128);
const CipherInfo aes256("AES-256-CBC", kCCKeySizeAES256, kCCBlockSizeAES128, // CONST GLOBAL
const CipherInfo aes256(CryptoAlgs::AES_256_CBC, // CONST GLOBAL
kCCKeySizeAES256, kCCBlockSizeAES128,
kCCBlockSizeAES128, kCCAlgorithmAES128);
const CipherInfo des3("DES-EDE3-CBC", kCCKeySize3DES, kCCBlockSize3DES, // CONST GLOBAL
const CipherInfo des3(CryptoAlgs::DES_EDE3_CBC, // CONST GLOBAL
kCCKeySize3DES, kCCBlockSize3DES,
kCCBlockSize3DES, kCCAlgorithm3DES);
const CipherInfo des("DES-CBC", kCCKeySizeDES, kCCBlockSizeDES, // CONST GLOBAL
const CipherInfo des(CryptoAlgs::DES_CBC, // CONST GLOBAL
kCCKeySizeDES, kCCBlockSizeDES,
kCCBlockSizeDES, kCCAlgorithmDES);
#ifdef OPENVPN_PLATFORM_IPHONE
const CipherInfo bf("BF-CBC", 16, kCCBlockSizeBlowfish, // CONST GLOBAL
const CipherInfo bf(CryptoAlgs::BF_CBC, // CONST GLOBAL
16, kCCBlockSizeBlowfish,
kCCBlockSizeBlowfish, kCCAlgorithmBlowfish);
#endif
@ -110,7 +111,7 @@ namespace openvpn {
Cipher(const CryptoAlgs::Type alg)
{
switch (type_ = alg)
switch (alg)
{
case CryptoAlgs::NONE:
reset();
@ -140,9 +141,17 @@ namespace openvpn {
}
}
CryptoAlgs::Type type() const
{
if (cipher_)
return cipher_->type();
else
return CryptoAlgs::NONE;
}
std::string name() const
{
return CryptoAlgs::name(type_);
return CryptoAlgs::name(type());
}
size_t key_length() const
@ -175,7 +184,6 @@ namespace openvpn {
void reset()
{
cipher_ = NULL;
type_ = CryptoAlgs::NONE;
}
const CipherInfo *get() const
@ -193,7 +201,6 @@ namespace openvpn {
}
const CipherInfo *cipher_;
CryptoAlgs::Type type_;
};
class CipherContext : boost::noncopyable

View File

@ -61,9 +61,9 @@
#define OPENVPN_DIGEST_ALG_DECLARE(TYPE) const DigestAlgorithm##TYPE alg_##TYPE;
#define OPENVPN_DIGEST_INFO_DECLARE(TYPE) const DigestInfo info_##TYPE(#TYPE, CC_##TYPE##_DIGEST_LENGTH, &alg_##TYPE, kCCHmacAlg##TYPE)
#define OPENVPN_DIGEST_INFO_DECLARE(TYPE) const DigestInfo info_##TYPE(CryptoAlgs::TYPE, CC_##TYPE##_DIGEST_LENGTH, &alg_##TYPE, kCCHmacAlg##TYPE)
#define OPENVPN_DIGEST_INFO_DECLARE_NO_HMAC(TYPE) const DigestInfo info_##TYPE(#TYPE, CC_##TYPE##_DIGEST_LENGTH, &alg_##TYPE, DigestInfo::NO_HMAC_ALG)
#define OPENVPN_DIGEST_INFO_DECLARE_NO_HMAC(TYPE) const DigestInfo info_##TYPE(CryptoAlgs::TYPE, CC_##TYPE##_DIGEST_LENGTH, &alg_##TYPE, DigestInfo::NO_HMAC_ALG)
namespace openvpn {
namespace AppleCrypto {
@ -104,28 +104,24 @@ namespace openvpn {
NO_HMAC_ALG = -1
};
DigestInfo(const char *name,
DigestInfo(CryptoAlgs::Type type,
const int md_size,
const DigestAlgorithm* digest_alg,
const CCHmacAlgorithm hmac_alg)
: name_(name),
: type_(type),
md_size_(md_size),
digest_alg_(digest_alg),
hmac_alg_(hmac_alg) {}
bool name_match(const char *name) const
{
return string::strcasecmp(name, name_) == 0;
}
CryptoAlgs::Type type() const { return type_; }
std::string name() const { return CryptoAlgs::name(type_); }
size_t size() const { return md_size_; }
const char *name() const { return name_; }
const DigestAlgorithm* digest_alg() const { return digest_alg_; }
CCHmacAlgorithm hmac_alg() const { return hmac_alg_; }
private:
const char *name_;
CryptoAlgs::Type type_;
int md_size_;
const DigestAlgorithm* digest_alg_;
CCHmacAlgorithm hmac_alg_;
@ -170,7 +166,7 @@ namespace openvpn {
Digest(const CryptoAlgs::Type alg)
{
switch (type_ = alg)
switch (alg)
{
case CryptoAlgs::NONE:
reset();
@ -201,6 +197,14 @@ namespace openvpn {
}
}
CryptoAlgs::Type type() const
{
if (digest_)
return digest_->type();
else
return CryptoAlgs::NONE;
}
// convenience methods for common digests
static Digest md4() { return Digest(CryptoAlgs::MD4); }
static Digest md5() { return Digest(CryptoAlgs::MD5); }
@ -208,7 +212,7 @@ namespace openvpn {
std::string name() const
{
return CryptoAlgs::name(type_);
return CryptoAlgs::name(type());
}
size_t size() const
@ -223,7 +227,6 @@ namespace openvpn {
void reset()
{
digest_ = NULL;
type_ = CryptoAlgs::NONE;
}
const DigestInfo *get() const
@ -241,7 +244,6 @@ namespace openvpn {
}
const DigestInfo *digest_;
CryptoAlgs::Type type_;
};
class DigestContext : boost::noncopyable

View File

@ -57,7 +57,7 @@ namespace openvpn {
Cipher(const CryptoAlgs::Type alg)
{
switch (type_ = alg)
switch (alg)
{
case CryptoAlgs::NONE:
reset();
@ -85,9 +85,35 @@ namespace openvpn {
}
}
CryptoAlgs::Type type() const
{
if (cipher_)
{
switch (cipher_->nid)
{
case NID_aes_128_cbc:
return CryptoAlgs::AES_128_CBC;
case NID_aes_192_cbc:
return CryptoAlgs::AES_192_CBC;
case NID_aes_256_cbc:
return CryptoAlgs::AES_256_CBC;
case NID_des_cbc:
return CryptoAlgs::DES_CBC;
case NID_des_ede3_cbc:
return CryptoAlgs::DES_EDE3_CBC;
case NID_bf_cbc:
return CryptoAlgs::BF_CBC;
default:
OPENVPN_THROW(openssl_cipher, "unknown type");
}
}
else
return CryptoAlgs::NONE;
}
std::string name() const
{
return CryptoAlgs::name(type_);
return CryptoAlgs::name(type());
}
size_t key_length() const
@ -119,7 +145,6 @@ namespace openvpn {
void reset()
{
cipher_ = NULL;
type_ = CryptoAlgs::NONE;
}
const EVP_CIPHER *get() const
@ -137,7 +162,6 @@ namespace openvpn {
}
const EVP_CIPHER *cipher_;
CryptoAlgs::Type type_;
};
class CipherContext : boost::noncopyable

View File

@ -61,7 +61,7 @@ namespace openvpn {
Digest(const CryptoAlgs::Type alg)
{
switch (type_ = alg)
switch (alg)
{
case CryptoAlgs::NONE:
reset();
@ -92,6 +92,34 @@ namespace openvpn {
}
}
CryptoAlgs::Type type() const
{
if (digest_)
{
switch (digest_->type)
{
case NID_md4:
return CryptoAlgs::MD4;
case NID_md5:
return CryptoAlgs::MD5;
case NID_sha1:
return CryptoAlgs::SHA1;
case NID_sha224:
return CryptoAlgs::SHA224;
case NID_sha256:
return CryptoAlgs::SHA256;
case NID_sha384:
return CryptoAlgs::SHA384;
case NID_sha512:
return CryptoAlgs::SHA512;
default:
OPENVPN_THROW(openssl_digest, "unknown type");
}
}
else
return CryptoAlgs::NONE;
}
// convenience methods for common digests
static Digest md4() { return Digest(CryptoAlgs::MD4); }
static Digest md5() { return Digest(CryptoAlgs::MD5); }
@ -99,7 +127,7 @@ namespace openvpn {
std::string name() const
{
return CryptoAlgs::name(type_);
return CryptoAlgs::name(type());
}
size_t size() const
@ -114,7 +142,6 @@ namespace openvpn {
void reset()
{
digest_ = NULL;
type_ = CryptoAlgs::NONE;
}
const EVP_MD *get() const
@ -132,7 +159,6 @@ namespace openvpn {
}
const EVP_MD *digest_;
CryptoAlgs::Type type_;
};
class DigestContext : boost::noncopyable

View File

@ -56,7 +56,7 @@ namespace openvpn {
Cipher(const CryptoAlgs::Type alg)
{
switch (type_ = alg)
switch (alg)
{
case CryptoAlgs::NONE:
reset();
@ -84,9 +84,35 @@ namespace openvpn {
}
}
CryptoAlgs::Type type() const
{
if (cipher_)
{
switch (cipher_->type)
{
case POLARSSL_CIPHER_AES_128_CBC:
return CryptoAlgs::AES_128_CBC;
case POLARSSL_CIPHER_AES_192_CBC:
return CryptoAlgs::AES_192_CBC;
case POLARSSL_CIPHER_AES_256_CBC:
return CryptoAlgs::AES_256_CBC;
case POLARSSL_CIPHER_DES_CBC:
return CryptoAlgs::DES_CBC;
case POLARSSL_CIPHER_DES_EDE3_CBC:
return CryptoAlgs::DES_EDE3_CBC;
case POLARSSL_CIPHER_BLOWFISH_CBC:
return CryptoAlgs::BF_CBC;
default:
OPENVPN_THROW(polarssl_cipher, "unknown type");
}
}
else
return CryptoAlgs::NONE;
}
std::string name() const
{
return CryptoAlgs::name(type_);
return CryptoAlgs::name(type());
}
size_t key_length() const
@ -119,7 +145,6 @@ namespace openvpn {
void reset()
{
cipher_ = NULL;
type_ = CryptoAlgs::NONE;
}
const cipher_info_t *get() const
@ -137,7 +162,6 @@ namespace openvpn {
}
const cipher_info_t *cipher_;
CryptoAlgs::Type type_;
};
class CipherContext : boost::noncopyable

View File

@ -56,7 +56,7 @@ namespace openvpn {
Digest(const CryptoAlgs::Type alg)
{
switch (type_ = alg)
switch (alg)
{
case CryptoAlgs::NONE:
reset();
@ -87,6 +87,34 @@ namespace openvpn {
}
}
CryptoAlgs::Type type() const
{
if (digest_)
{
switch (md_get_type(digest_))
{
case POLARSSL_MD_MD4:
return CryptoAlgs::MD4;
case POLARSSL_MD_MD5:
return CryptoAlgs::MD5;
case POLARSSL_MD_SHA1:
return CryptoAlgs::SHA1;
case POLARSSL_MD_SHA224:
return CryptoAlgs::SHA224;
case POLARSSL_MD_SHA256:
return CryptoAlgs::SHA256;
case POLARSSL_MD_SHA384:
return CryptoAlgs::SHA384;
case POLARSSL_MD_SHA512:
return CryptoAlgs::SHA512;
default:
OPENVPN_THROW(polarssl_digest, "unknown type");
}
}
else
return CryptoAlgs::NONE;
}
// convenience methods for common digests
static Digest md4() { return Digest(CryptoAlgs::MD4); }
static Digest md5() { return Digest(CryptoAlgs::MD5); }
@ -94,7 +122,7 @@ namespace openvpn {
std::string name() const
{
return CryptoAlgs::name(type_);
return CryptoAlgs::name(type());
}
size_t size() const
@ -109,7 +137,6 @@ namespace openvpn {
void reset()
{
digest_ = NULL;
type_ = CryptoAlgs::NONE;
}
const md_info_t *get() const
@ -127,7 +154,6 @@ namespace openvpn {
}
const md_info_t *digest_;
CryptoAlgs::Type type_;
};
class DigestContext : boost::noncopyable

View File

@ -10,7 +10,7 @@ Build on Mac:
GCC_EXTRA="-ferror-limit=4 -std=c++11" STRIP=1 PSSL=1 MINI=1 SNAP=1 LZ4=1 build cli
With OpenSSL:
GCC_EXTRA="-ferror-limit=4" STRIP=1 OSSL=1 SNAP=1 LZ4=1 build cli
GCC_EXTRA="-ferror-limit=4" STRIP=1 OSSL=1 OPENSSL_SYS=1 SNAP=1 LZ4=1 build cli
With PolarSSL/AppleCrypto hybrid:
GCC_EXTRA="-ferror-limit=4" STRIP=1 HYBRID=1 SNAP=1 LZ4=1 build cli