Skip to content

Commit

Permalink
crypto: normalize indentation via editorconfig, NFC
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Noonan <[email protected]>
  • Loading branch information
Steven Noonan committed Sep 3, 2020
1 parent 0eedd61 commit e6a91da
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 244 deletions.
144 changes: 72 additions & 72 deletions src/common/crypto_25519_libsodium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,130 +19,130 @@ bool CEC25519KeyBase::IsValid() const

uint32 CEC25519KeyBase::GetRawData( void *pData ) const
{
return CCryptoKeyBase_RawBuffer::GetRawData( pData );
return CCryptoKeyBase_RawBuffer::GetRawData( pData );
}

void CEC25519KeyBase::Wipe()
{
CCryptoKeyBase_RawBuffer::Wipe();
CCryptoKeyBase_RawBuffer::Wipe();
}

bool CEC25519KeyBase::SetRawData( const void *pData, size_t cbData )
{
if ( cbData != 32 )
if ( cbData != 32 )
return false;
return CCryptoKeyBase_RawBuffer::SetRawData( pData, cbData );
}

bool CCrypto::PerformKeyExchange( const CECKeyExchangePrivateKey &localPrivateKey, const CECKeyExchangePublicKey &remotePublicKey, SHA256Digest_t *pSharedSecretOut )
{
Assert( localPrivateKey.IsValid() );
Assert( remotePublicKey.IsValid() );
Assert( localPrivateKey.IsValid() );
Assert( remotePublicKey.IsValid() );

if ( !localPrivateKey.IsValid() || !remotePublicKey.IsValid() )
{
// Fail securely - generate something that won't be the same on both sides!
GenerateRandomBlock( *pSharedSecretOut, sizeof( SHA256Digest_t ) );
return false;
}
if ( !localPrivateKey.IsValid() || !remotePublicKey.IsValid() )
{
// Fail securely - generate something that won't be the same on both sides!
GenerateRandomBlock( *pSharedSecretOut, sizeof( SHA256Digest_t ) );
return false;
}

uint8 bufSharedSecret[32];
uint8 bufLocalPrivate[32];
uint8 bufRemotePublic[32];
uint8 bufSharedSecret[32];
uint8 bufLocalPrivate[32];
uint8 bufRemotePublic[32];

localPrivateKey.GetRawData(bufLocalPrivate);
remotePublicKey.GetRawData(bufRemotePublic);
localPrivateKey.GetRawData(bufLocalPrivate);
remotePublicKey.GetRawData(bufRemotePublic);

const int nResult = crypto_scalarmult_curve25519(bufSharedSecret, bufLocalPrivate, bufRemotePublic);
const int nResult = crypto_scalarmult_curve25519(bufSharedSecret, bufLocalPrivate, bufRemotePublic);

SecureZeroMemory( bufLocalPrivate, 32 );
SecureZeroMemory( bufRemotePublic, 32 );
SecureZeroMemory( bufLocalPrivate, 32 );
SecureZeroMemory( bufRemotePublic, 32 );

if(nResult != 0)
{
return false;
}
if(nResult != 0)
{
return false;
}

GenerateSHA256Digest( bufSharedSecret, sizeof(bufSharedSecret), pSharedSecretOut );
SecureZeroMemory( bufSharedSecret, 32 );
GenerateSHA256Digest( bufSharedSecret, sizeof(bufSharedSecret), pSharedSecretOut );
SecureZeroMemory( bufSharedSecret, 32 );

return true;
return true;
}

void CECSigningPrivateKey::GenerateSignature( const void *pData, size_t cbData, CryptoSignature_t *pSignatureOut ) const
{
if ( !IsValid() )
if ( !IsValid() )
{
AssertMsg( false, "Key not initialized, cannot generate signature" );
sodium_memzero( pSignatureOut, sizeof( CryptoSignature_t ) );
sodium_memzero( pSignatureOut, sizeof( CryptoSignature_t ) );
return;
}

// libsodium secret key is concatenation of:
// seed (i.e. what everyone else calls the secret key)
// public key
// libsodium secret key is concatenation of:
// seed (i.e. what everyone else calls the secret key)
// public key

uint8 bufSodiumSecret[crypto_sign_ed25519_SECRETKEYBYTES];
uint8 bufSodiumSecret[crypto_sign_ed25519_SECRETKEYBYTES];

Assert( CCryptoKeyBase_RawBuffer::GetRawDataSize() == 32 );
Assert( sizeof(m_publicKey) == 32 );
Assert( crypto_sign_ed25519_SECRETKEYBYTES == 64 );
Assert( CCryptoKeyBase_RawBuffer::GetRawDataSize() == 32 );
Assert( sizeof(m_publicKey) == 32 );
Assert( crypto_sign_ed25519_SECRETKEYBYTES == 64 );

memcpy(bufSodiumSecret, CCryptoKeyBase_RawBuffer::GetRawDataPtr(), 32 );
memcpy(bufSodiumSecret + 32, m_publicKey, sizeof(m_publicKey));
memcpy(bufSodiumSecret, CCryptoKeyBase_RawBuffer::GetRawDataPtr(), 32 );
memcpy(bufSodiumSecret + 32, m_publicKey, sizeof(m_publicKey));

crypto_sign_ed25519_detached(*pSignatureOut, nullptr, static_cast<const unsigned char*>( pData ), cbData, bufSodiumSecret );
sodium_memzero(bufSodiumSecret, sizeof(bufSodiumSecret) );
crypto_sign_ed25519_detached(*pSignatureOut, nullptr, static_cast<const unsigned char*>( pData ), cbData, bufSodiumSecret );
sodium_memzero(bufSodiumSecret, sizeof(bufSodiumSecret) );
}

bool CECSigningPublicKey::VerifySignature( const void *pData, size_t cbData, const CryptoSignature_t &signature ) const
{
if ( !IsValid() )
if ( !IsValid() )
{
AssertMsg( false, "Key not initialized, cannot verify signature" );
return false;
}

return crypto_sign_ed25519_verify_detached( signature, static_cast<const unsigned char*>( pData ), cbData, CCryptoKeyBase_RawBuffer::GetRawDataPtr() ) == 0;
return crypto_sign_ed25519_verify_detached( signature, static_cast<const unsigned char*>( pData ), cbData, CCryptoKeyBase_RawBuffer::GetRawDataPtr() ) == 0;
}

bool CEC25519PrivateKeyBase::CachePublicKey()
{
// Need to convert the private key into a public key here
// then store in m_publicKey
if ( !IsValid() )
{
// then store in m_publicKey
if ( !IsValid() )
{
return false;
}
}

if ( m_eKeyType == k_ECryptoKeyTypeKeyExchangePrivate )
if ( m_eKeyType == k_ECryptoKeyTypeKeyExchangePrivate )
{
// Get public key from secret key
AssertMsg( sizeof(m_publicKey) == crypto_scalarmult_curve25519_bytes(), "Public key size mismatch." );
AssertMsg( CCryptoKeyBase_RawBuffer::GetRawDataSize() == crypto_scalarmult_curve25519_scalarbytes(), "Private key size mismatch." );

crypto_scalarmult_curve25519_base( m_publicKey, CCryptoKeyBase_RawBuffer::GetRawDataPtr() );
}
else if ( m_eKeyType == k_ECryptoKeyTypeSigningPrivate )
{
// Convert ed25519 private signing key to ed25519 public key
// Note that what everyone else calls the private key, libsodium calls the seed
AssertMsg( sizeof(m_publicKey) == crypto_sign_ed25519_publickeybytes(), "Public key size mismatch." );
AssertMsg( CCryptoKeyBase_RawBuffer::GetRawDataSize() == crypto_sign_ed25519_seedbytes(), "Private key size mismatch." );

unsigned char h[crypto_hash_sha512_BYTES];

crypto_sign_ed25519_seed_keypair( m_publicKey, h, static_cast<const unsigned char*>( CCryptoKeyBase_RawBuffer::GetRawDataPtr() ) );

sodium_memzero(h, sizeof(h));
}
else
{
Assert( false );
return false;
}

return true;
// Get public key from secret key
AssertMsg( sizeof(m_publicKey) == crypto_scalarmult_curve25519_bytes(), "Public key size mismatch." );
AssertMsg( CCryptoKeyBase_RawBuffer::GetRawDataSize() == crypto_scalarmult_curve25519_scalarbytes(), "Private key size mismatch." );

crypto_scalarmult_curve25519_base( m_publicKey, CCryptoKeyBase_RawBuffer::GetRawDataPtr() );
}
else if ( m_eKeyType == k_ECryptoKeyTypeSigningPrivate )
{
// Convert ed25519 private signing key to ed25519 public key
// Note that what everyone else calls the private key, libsodium calls the seed
AssertMsg( sizeof(m_publicKey) == crypto_sign_ed25519_publickeybytes(), "Public key size mismatch." );
AssertMsg( CCryptoKeyBase_RawBuffer::GetRawDataSize() == crypto_sign_ed25519_seedbytes(), "Private key size mismatch." );

unsigned char h[crypto_hash_sha512_BYTES];

crypto_sign_ed25519_seed_keypair( m_publicKey, h, static_cast<const unsigned char*>( CCryptoKeyBase_RawBuffer::GetRawDataPtr() ) );

sodium_memzero(h, sizeof(h));
}
else
{
Assert( false );
return false;
}

return true;
}

#endif
4 changes: 2 additions & 2 deletions src/common/crypto_25519_openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <openssl/evp.h>

#if OPENSSL_VERSION_NUMBER < 0x10101000
// https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_get_raw_private_key.html
#error "Raw access to 25519 keys requires OpenSSL 1.1.1"
// https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_get_raw_private_key.html
#error "Raw access to 25519 keys requires OpenSSL 1.1.1"
#endif

CEC25519KeyBase::~CEC25519KeyBase()
Expand Down
88 changes: 44 additions & 44 deletions src/common/crypto_bcrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ typedef struct _BCryptContext {
void CCrypto::Init()
{
BCryptOpenAlgorithmProvider(
&hAlgRandom,
BCRYPT_RNG_ALGORITHM,
nullptr,
0
);
&hAlgRandom,
BCRYPT_RNG_ALGORITHM,
nullptr,
0
);
AssertFatal( hAlgRandom != INVALID_HANDLE_VALUE );
BCryptOpenAlgorithmProvider(
&hAlgSHA256,
BCRYPT_SHA256_ALGORITHM,
nullptr,
0
);
&hAlgSHA256,
BCRYPT_SHA256_ALGORITHM,
nullptr,
0
);
AssertFatal( hAlgSHA256 != INVALID_HANDLE_VALUE );
BCryptOpenAlgorithmProvider(
&hAlgHMACSHA256,
BCRYPT_SHA256_ALGORITHM,
nullptr,
BCRYPT_ALG_HANDLE_HMAC_FLAG
);
&hAlgHMACSHA256,
BCRYPT_SHA256_ALGORITHM,
nullptr,
BCRYPT_ALG_HANDLE_HMAC_FLAG
);
AssertFatal( hAlgHMACSHA256 != INVALID_HANDLE_VALUE );
}

Expand Down Expand Up @@ -122,11 +122,11 @@ bool AES_GCM_CipherContext::InitCipher( const void *pKey, size_t cbKey, size_t c
}

bool AES_GCM_EncryptContext::Encrypt(
const void *pPlaintextData, size_t cbPlaintextData,
const void *pIV,
void *pEncryptedDataAndTag, uint32 *pcbEncryptedDataAndTag,
const void *pAdditionalAuthenticationData, size_t cbAuthenticationData // Optional additional authentication data. Not encrypted, but will be included in the tag, so it can be authenticated.
)
const void *pPlaintextData, size_t cbPlaintextData,
const void *pIV,
void *pEncryptedDataAndTag, uint32 *pcbEncryptedDataAndTag,
const void *pAdditionalAuthenticationData, size_t cbAuthenticationData // Optional additional authentication data. Not encrypted, but will be included in the tag, so it can be authenticated.
)
{
BCryptContext *ctx = (BCryptContext *)(this->m_ctx);
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO paddingInfo;
Expand All @@ -141,13 +141,13 @@ bool AES_GCM_EncryptContext::Encrypt(
paddingInfo.pbAuthData = cbAuthenticationData ? (PUCHAR)pAdditionalAuthenticationData : NULL;
ULONG ct_size;
NTSTATUS status = BCryptEncrypt(
ctx->hKey,
( PUCHAR )pPlaintextData, (ULONG)cbPlaintextData,
&paddingInfo,
NULL, 0,
( PUCHAR )pEncryptedDataAndTag, *pcbEncryptedDataAndTag,
&ct_size,
0 );
ctx->hKey,
( PUCHAR )pPlaintextData, (ULONG)cbPlaintextData,
&paddingInfo,
NULL, 0,
( PUCHAR )pEncryptedDataAndTag, *pcbEncryptedDataAndTag,
&ct_size,
0 );
AssertFatal( ( ct_size + m_cbTag ) < *pcbEncryptedDataAndTag );
memcpy( ( PUCHAR )( pEncryptedDataAndTag ) + ct_size, buffer, m_cbTag );
ct_size += m_cbTag;
Expand All @@ -156,11 +156,11 @@ bool AES_GCM_EncryptContext::Encrypt(
}

bool AES_GCM_DecryptContext::Decrypt(
const void *pEncryptedDataAndTag, size_t cbEncryptedDataAndTag,
const void *pIV,
void *pPlaintextData, uint32 *pcbPlaintextData,
const void *pAdditionalAuthenticationData, size_t cbAuthenticationData
)
const void *pEncryptedDataAndTag, size_t cbEncryptedDataAndTag,
const void *pIV,
void *pPlaintextData, uint32 *pcbPlaintextData,
const void *pAdditionalAuthenticationData, size_t cbAuthenticationData
)
{
BCryptContext *ctx = (BCryptContext *)(this->m_ctx);
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO paddingInfo;
Expand All @@ -178,13 +178,13 @@ bool AES_GCM_DecryptContext::Decrypt(
paddingInfo.pbAuthData = cbAuthenticationData ? (PUCHAR)pAdditionalAuthenticationData : NULL;
ULONG pt_size;
NTSTATUS status = BCryptDecrypt(
ctx->hKey,
( PUCHAR )pEncryptedDataAndTag, (ULONG)cbEncryptedDataAndTag,
&paddingInfo,
NULL, 0,
( PUCHAR )pPlaintextData, *pcbPlaintextData,
&pt_size,
0 );
ctx->hKey,
( PUCHAR )pEncryptedDataAndTag, (ULONG)cbEncryptedDataAndTag,
&paddingInfo,
NULL, 0,
( PUCHAR )pPlaintextData, *pcbPlaintextData,
&pt_size,
0 );
*pcbPlaintextData = pt_size;
return NT_SUCCESS(status);
}
Expand Down Expand Up @@ -232,11 +232,11 @@ void CCrypto::GenerateRandomBlock( void *pvDest, int cubDest )
AssertFatal( cubDest >= 0 );

NTSTATUS status = BCryptGenRandom(
hAlgRandom,
(PUCHAR)pvDest,
(ULONG)cubDest,
0
);
hAlgRandom,
(PUCHAR)pvDest,
(ULONG)cubDest,
0
);
AssertFatal( NT_SUCCESS( status) );
}

Expand Down
Loading

0 comments on commit e6a91da

Please sign in to comment.