1
0
mirror of https://github.com/SoftEtherVPN/SoftEtherVPN.git synced 2024-11-23 01:49:53 +03:00

Encrypt: reorder digest functions

This commit is contained in:
Davide Beatrici 2018-09-22 06:47:03 +02:00
parent 3f5f716357
commit 50b42070e2
2 changed files with 157 additions and 159 deletions

View File

@ -268,6 +268,91 @@ void Enc_tls1_PRF(unsigned char *label, int label_len, const unsigned char *sec,
Free(out2); Free(out2);
} }
// MD4 specific hash function
void HashMd4(void *dst, void *src, UINT size)
{
// Validate arguments
if (dst == NULL || (src == NULL && size != 0))
{
return;
}
MD4(src, size, dst);
}
// MD5 hash
void Md5(void *dst, void *src, UINT size)
{
// Validate arguments
if (dst == NULL || (src == NULL && size != 0))
{
return;
}
MD5(src, size, dst);
}
// SHA hash
void Sha(UINT sha_type, void *dst, void *src, UINT size)
{
// Validate arguments
if (dst == NULL || (src == NULL && size != 0))
{
return;
}
switch(sha_type) {
case SHA1_160:
SHA1(src, size, dst);
break;
case SHA2_256:
SHA256(src, size, dst);
break;
case SHA2_384:
SHA384(src, size, dst);
break;
case SHA2_512:
SHA512(src, size, dst);
break;
}
}
void Sha0(void *dst, void *src, UINT size)
{
// Validate arguments
if (dst == NULL || (src == NULL && size != 0))
{
return;
}
Internal_Sha0(dst, src, size);
}
void Sha1(void *dst, void *src, UINT size)
{
Sha(SHA1_160, dst, src, size);
}
void Sha2_256(void *dst, void *src, UINT size)
{
Sha(SHA2_256, dst, src, size);
}
void Sha2_384(void *dst, void *src, UINT size)
{
Sha(SHA2_384, dst, src, size);
}
void Sha2_512(void *dst, void *src, UINT size)
{
Sha(SHA2_512, dst, src, size);
}
void HashSha1(void *dst, void *src, UINT size)
{
Sha1(dst, src, size);
}
// Calculation of HMAC (MD5) // Calculation of HMAC (MD5)
UINT HMacMd5(void *dst, void *key, UINT key_size, void *src, UINT src_size) UINT HMacMd5(void *dst, void *key, UINT key_size, void *src, UINT src_size)
{ {
@ -280,65 +365,6 @@ UINT HMacSha1(void *dst, void *key, UINT key_size, void *src, UINT src_size)
return Internal_HMac(EVP_sha1(), dst, key, key_size, src, src_size); return Internal_HMac(EVP_sha1(), dst, key, key_size, src, src_size);
} }
// Calculate the hash/HMAC
UINT MdProcess(MD *md, void *dest, void *src, UINT size)
{
UINT len = 0;
// Validate arguments
if (md == NULL || md->IsNullMd || dest == NULL || (src == NULL && size != 0))
{
return 0;
}
if (md->IsHMac)
{
if (HMAC_Update(md->Ctx, src, size) == false)
{
Debug("MdProcess(): HMAC_Update() failed with error: %s\n", OpenSSL_Error());
return 0;
}
if (HMAC_Final(md->Ctx, dest, &len) == false)
{
Debug("MdProcess(): HMAC_Final() failed with error: %s\n", OpenSSL_Error());
}
}
else
{
if (EVP_DigestUpdate(md->Ctx, src, size) == false)
{
Debug("MdProcess(): EVP_DigestUpdate() failed with error: %s\n", OpenSSL_Error());
return 0;
}
if (EVP_DigestFinal_ex(md->Ctx, dest, &len) == false)
{
Debug("MdProcess(): EVP_DigestFinal_ex() failed with error: %s\n", OpenSSL_Error());
}
}
return len;
}
// Set the key to the message digest object
bool SetMdKey(MD *md, void *key, UINT key_size)
{
// Validate arguments
if (md == NULL || md->IsHMac == false || key == NULL || key_size == 0)
{
return false;
}
if (HMAC_Init_ex(md->Ctx, key, key_size, (const EVP_MD *)md->Md, NULL) == false)
{
Debug("SetMdKey(): HMAC_Init_ex() failed with error: %s\n", OpenSSL_Error());
return false;
}
return true;
}
// Creating a message digest object // Creating a message digest object
MD *NewMd(char *name) MD *NewMd(char *name)
{ {
@ -404,6 +430,65 @@ MD *NewMdEx(char *name, bool hmac)
return m; return m;
} }
// Set the key to the message digest object
bool SetMdKey(MD *md, void *key, UINT key_size)
{
// Validate arguments
if (md == NULL || md->IsHMac == false || key == NULL || key_size == 0)
{
return false;
}
if (HMAC_Init_ex(md->Ctx, key, key_size, (const EVP_MD *)md->Md, NULL) == false)
{
Debug("SetMdKey(): HMAC_Init_ex() failed with error: %s\n", OpenSSL_Error());
return false;
}
return true;
}
// Calculate the hash/HMAC
UINT MdProcess(MD *md, void *dest, void *src, UINT size)
{
UINT len = 0;
// Validate arguments
if (md == NULL || md->IsNullMd || dest == NULL || (src == NULL && size != 0))
{
return 0;
}
if (md->IsHMac)
{
if (HMAC_Update(md->Ctx, src, size) == false)
{
Debug("MdProcess(): HMAC_Update() failed with error: %s\n", OpenSSL_Error());
return 0;
}
if (HMAC_Final(md->Ctx, dest, &len) == false)
{
Debug("MdProcess(): HMAC_Final() failed with error: %s\n", OpenSSL_Error());
}
}
else
{
if (EVP_DigestUpdate(md->Ctx, src, size) == false)
{
Debug("MdProcess(): EVP_DigestUpdate() failed with error: %s\n", OpenSSL_Error());
return 0;
}
if (EVP_DigestFinal_ex(md->Ctx, dest, &len) == false)
{
Debug("MdProcess(): EVP_DigestFinal_ex() failed with error: %s\n", OpenSSL_Error());
}
}
return len;
}
// Release of the message digest object // Release of the message digest object
void FreeMd(MD *md) void FreeMd(MD *md)
{ {
@ -3717,18 +3802,6 @@ void InitCryptLibrary()
openssl_inited = true; openssl_inited = true;
} }
// MD4 specific hash function
void HashMd4(void *dst, void *src, UINT size)
{
// Validate arguments
if (dst == NULL || (src == NULL && size != 0))
{
return;
}
MD4(src, size, dst);
}
// Hash with the SHA-1 and convert it to UINT // Hash with the SHA-1 and convert it to UINT
UINT HashToUINT(void *data, UINT size) UINT HashToUINT(void *data, UINT size)
{ {
@ -3749,12 +3822,6 @@ UINT HashToUINT(void *data, UINT size)
return u; return u;
} }
// SHA-1 specific hash function
void HashSha1(void *dst, void *src, UINT size)
{
Sha1(dst, src, size);
}
// Creating a new CRYPT object // Creating a new CRYPT object
CRYPT *NewCrypt(void *key, UINT size) CRYPT *NewCrypt(void *key, UINT size)
{ {
@ -3787,74 +3854,6 @@ void Encrypt(CRYPT *c, void *dst, void *src, UINT size)
RC4(c->Rc4Key, size, src, dst); RC4(c->Rc4Key, size, src, dst);
} }
// SHA hash
void Sha(UINT sha_type, void *dst, void *src, UINT size)
{
// Validate arguments
if (dst == NULL || (src == NULL && size != 0))
{
return;
}
switch(sha_type) {
case SHA1_160:
SHA1(src, size, dst);
break;
case SHA2_256:
SHA256(src, size, dst);
break;
case SHA2_384:
SHA384(src, size, dst);
break;
case SHA2_512:
SHA512(src, size, dst);
break;
}
}
void Sha0(void *dst, void *src, UINT size)
{
// Validate arguments
if (dst == NULL || (src == NULL && size != 0))
{
return;
}
Internal_Sha0(dst, src, size);
}
void Sha1(void *dst, void *src, UINT size)
{
Sha(SHA1_160, dst, src, size);
}
void Sha2_256(void *dst, void *src, UINT size)
{
Sha(SHA2_256, dst, src, size);
}
void Sha2_384(void *dst, void *src, UINT size)
{
Sha(SHA2_384, dst, src, size);
}
void Sha2_512(void *dst, void *src, UINT size)
{
Sha(SHA2_512, dst, src, size);
}
// MD5 hash
void Md5(void *dst, void *src, UINT size)
{
// Validate arguments
if (dst == NULL || (src == NULL && size != 0))
{
return;
}
MD5(src, size, dst);
}
// 3DES encryption // 3DES encryption
void Des3Encrypt2(void *dest, void *src, UINT size, DES_KEY_VALUE *k1, DES_KEY_VALUE *k2, DES_KEY_VALUE *k3, void *ivec) void Des3Encrypt2(void *dest, void *src, UINT size, DES_KEY_VALUE *k1, DES_KEY_VALUE *k2, DES_KEY_VALUE *k3, void *ivec)
{ {

View File

@ -374,10 +374,6 @@ extern LOCK **ssl_lock_obj;
CRYPT *NewCrypt(void *key, UINT size); CRYPT *NewCrypt(void *key, UINT size);
void FreeCrypt(CRYPT *c); void FreeCrypt(CRYPT *c);
void Encrypt(CRYPT *c, void *dst, void *src, UINT size); void Encrypt(CRYPT *c, void *dst, void *src, UINT size);
void Hash(void *dst, void *src, UINT size, bool sha);
void HashSha1(void *dst, void *src, UINT size);
void HashMd4(void *dst, void *src, UINT size);
void HashMd4(void *dst, void *src, UINT size);
void InitCryptLibrary(); void InitCryptLibrary();
void Rand(void *buf, UINT size); void Rand(void *buf, UINT size);
UINT HashToUINT(void *data, UINT size); UINT HashToUINT(void *data, UINT size);
@ -491,13 +487,7 @@ void DesFreeKeyValue(DES_KEY_VALUE *v);
void Des3Encrypt2(void *dest, void *src, UINT size, DES_KEY_VALUE *k1, DES_KEY_VALUE *k2, DES_KEY_VALUE *k3, void *ivec); void Des3Encrypt2(void *dest, void *src, UINT size, DES_KEY_VALUE *k1, DES_KEY_VALUE *k2, DES_KEY_VALUE *k3, void *ivec);
void Des3Decrypt2(void *dest, void *src, UINT size, DES_KEY_VALUE *k1, DES_KEY_VALUE *k2, DES_KEY_VALUE *k3, void *ivec); void Des3Decrypt2(void *dest, void *src, UINT size, DES_KEY_VALUE *k1, DES_KEY_VALUE *k2, DES_KEY_VALUE *k3, void *ivec);
void Sha(UINT sha_type, void *dst, void *src, UINT size); void Sha(UINT sha_type, void *dst, void *src, UINT size);
void Sha0(void *dst, void *src, UINT size);
void Sha1(void *dst, void *src, UINT size);
void Sha2_256(void *dst, void *src, UINT size);
void Sha2_384(void *dst, void *src, UINT size);
void Sha2_512(void *dst, void *src, UINT size);
void Md5(void *dst, void *src, UINT size);
void DesEncrypt(void *dest, void *src, UINT size, DES_KEY_VALUE *k, void *ivec); void DesEncrypt(void *dest, void *src, UINT size, DES_KEY_VALUE *k, void *ivec);
void DesDecrypt(void *dest, void *src, UINT size, DES_KEY_VALUE *k, void *ivec); void DesDecrypt(void *dest, void *src, UINT size, DES_KEY_VALUE *k, void *ivec);
void DesEcbEncrypt(void *dst, void *src, void *key_7bytes); void DesEcbEncrypt(void *dst, void *src, void *key_7bytes);
@ -528,21 +518,30 @@ unsigned long OpenSSL_Id(void);
void FreeOpenSSLThreadState(); void FreeOpenSSLThreadState();
char *OpenSSL_Error(); char *OpenSSL_Error();
// Encryption/Decryption
CIPHER *NewCipher(char *name); CIPHER *NewCipher(char *name);
void FreeCipher(CIPHER *c); void FreeCipher(CIPHER *c);
void SetCipherKey(CIPHER *c, void *key, bool enc); void SetCipherKey(CIPHER *c, void *key, bool enc);
UINT CipherProcess(CIPHER *c, void *iv, void *dest, void *src, UINT size); UINT CipherProcess(CIPHER *c, void *iv, void *dest, void *src, UINT size);
// Hashing
MD *NewMd(char *name); MD *NewMd(char *name);
MD *NewMdEx(char *name, bool hmac); MD *NewMdEx(char *name, bool hmac);
void FreeMd(MD *md);
bool SetMdKey(MD *md, void *key, UINT key_size); bool SetMdKey(MD *md, void *key, UINT key_size);
UINT MdProcess(MD *md, void *dest, void *src, UINT size); UINT MdProcess(MD *md, void *dest, void *src, UINT size);
void Enc_tls1_PRF(unsigned char *label, int label_len, const unsigned char *sec, void FreeMd(MD *md);
int slen, unsigned char *out1, int olen); void HashMd4(void *dst, void *src, UINT size);
void HashSha1(void *dst, void *src, UINT size);
void Md5(void *dst, void *src, UINT size);
void Sha(UINT sha_type, void *dst, void *src, UINT size);
void Sha0(void *dst, void *src, UINT size);
void Sha1(void *dst, void *src, UINT size);
void Sha2_256(void *dst, void *src, UINT size);
void Sha2_384(void *dst, void *src, UINT size);
void Sha2_512(void *dst, void *src, UINT size);
UINT HMacSha1(void *dst, void *key, UINT key_size, void *data, UINT data_size); UINT HMacSha1(void *dst, void *key, UINT key_size, void *data, UINT data_size);
UINT HMacMd5(void *dst, void *key, UINT key_size, void *data, UINT data_size); UINT HMacMd5(void *dst, void *key, UINT key_size, void *data, UINT data_size);
void Enc_tls1_PRF(unsigned char *label, int label_len, const unsigned char *sec, int slen, unsigned char *out1, int olen);
int GetSslClientCertIndex(); int GetSslClientCertIndex();