Skip to content

Commit 0307add

Browse files
committed
Move 25519 crypto to a separate header.
Getting things closer to how they are organized in the Steam main branch so I can stop having these files diverge so much. Also tweaked defines.
1 parent 66e2a65 commit 0307add

15 files changed

+201
-151
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(MSVC_RUNTIME "dynamic")
2020
configure_msvc_runtime()
2121
print_default_msvc_flags()
2222

23+
add_definitions( -DVALVE_CRYPTO_ENABLE_25519 )
2324
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
2425
add_definitions(
2526
-D_CRT_SECURE_NO_WARNINGS

src/common/crypto.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,6 @@ namespace CCrypto
100100
const void *pAdditionalAuthenticationData, size_t cbAuthenticationData, // Optional additional authentication data. Not encrypted, but will be included in the tag, so it can be authenticated.
101101
size_t cbTag // Last N bytes in your buffer are assumed to be a tag, and will be checked
102102
);
103-
104-
//
105-
// Secure key exchange (curve25519 elliptic-curve Diffie-Hellman key exchange)
106-
//
107-
108-
// Generate a curve25519 key pair for Diffie-Hellman secure key exchange
109-
void GenerateKeyExchangeKeyPair( CECKeyExchangePublicKey *pPublicKey, CECKeyExchangePrivateKey *pPrivateKey );
110-
bool PerformKeyExchange( const CECKeyExchangePrivateKey &localPrivateKey, const CECKeyExchangePublicKey &remotePublicKey, SHA256Digest_t *pSharedSecretOut );
111-
112-
//
113-
// Signing and verification (ed25519 elliptic-curve signature scheme)
114-
//
115-
116-
// Generate an ed25519 key pair for public-key signature generation
117-
void GenerateSigningKeyPair( CECSigningPublicKey *pPublicKey, CECSigningPrivateKey *pPrivateKey );
118-
119-
// Legacy compatibility - use the key methods
120-
inline void GenerateSignature( const void *pData, size_t cbData, const CECSigningPrivateKey &privateKey, CryptoSignature_t *pSignatureOut ) { privateKey.GenerateSignature( pData, cbData, pSignatureOut ); }
121-
inline bool VerifySignature( const void *pData, size_t cbData, const CECSigningPublicKey &publicKey, const CryptoSignature_t &signature ) { return publicKey.VerifySignature( pData, cbData, signature ); }
122103

123104
bool HexEncode( const void *pubData, const uint32 cubData, char *pchEncodedData, uint32 cchEncodedData );
124105
bool HexDecode( const char *pchData, void *pubDecodedData, uint32 *pcubDecodedData );

src/common/crypto_25519.h

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
//========= Copyright Valve LLC, All rights reserved. =========================
2+
3+
#ifndef CRYPTO_25519_H
4+
#define CRYPTO_25519_H
5+
6+
#include "crypto_constants.h"
7+
#include "keypair.h"
8+
9+
class CEC25519KeyBase : public CCryptoKeyBase_RawBuffer
10+
{
11+
public:
12+
virtual ~CEC25519KeyBase();
13+
virtual bool IsValid() const override;
14+
virtual uint32 GetRawData( void *pData ) const override;
15+
virtual void Wipe() override;
16+
17+
void *evp_pkey() const { return m_evp_pkey; }
18+
protected:
19+
virtual bool SetRawData( const void *pData, size_t cbData ) override;
20+
inline CEC25519KeyBase( ECryptoKeyType keyType ) : CCryptoKeyBase_RawBuffer( keyType ), m_evp_pkey(nullptr) {}
21+
22+
// Actually EVP_PKEY*, but we don't want to include OpenSSL headers here,
23+
// especially since we might not actually be using OpenSSL for this at all!
24+
void *m_evp_pkey;
25+
};
26+
27+
//-----------------------------------------------------------------------------
28+
// Purpose: Common base for x25519 and ed25519 public keys on the 25519 curve
29+
// The raw data is 32 bytes
30+
//-----------------------------------------------------------------------------
31+
class CEC25519PublicKeyBase : public CEC25519KeyBase
32+
{
33+
public:
34+
virtual ~CEC25519PublicKeyBase();
35+
protected:
36+
CEC25519PublicKeyBase( ECryptoKeyType eType ) : CEC25519KeyBase( eType ) { }
37+
};
38+
39+
//-----------------------------------------------------------------------------
40+
// Purpose: Common base for x25519 and ed25519 private keys on the 25519 curve
41+
// The raw data is 32 bytes.
42+
// NOTE: An old version also stored the public key in the raw data.
43+
// We don't do that anymore.) If you want that, get the public
44+
// key data specifically
45+
//-----------------------------------------------------------------------------
46+
class CEC25519PrivateKeyBase : public CEC25519KeyBase
47+
{
48+
public:
49+
virtual ~CEC25519PrivateKeyBase();
50+
virtual void Wipe() override;
51+
bool GetPublicKey( CEC25519PublicKeyBase *pPublicKey ) const;
52+
bool MatchesPublicKey( const CEC25519PublicKeyBase &pPublicKey ) const;
53+
54+
const uint8 *GetPublicKeyRawData() const { return m_publicKey; }
55+
56+
protected:
57+
CEC25519PrivateKeyBase( ECryptoKeyType eType ) : CEC25519KeyBase( eType ) { }
58+
59+
// We keep a copy of the public key cached.
60+
// It is not considered part of the raw key data,
61+
// as was previously the case.)
62+
uint8 m_publicKey[32];
63+
64+
bool CachePublicKey();
65+
virtual bool SetRawData( const void *pData, size_t cbData ) override;
66+
};
67+
68+
69+
//-----------------------------------------------------------------------------
70+
// Purpose: Encapsulates an elliptic-curve signature private key (x25519)
71+
//-----------------------------------------------------------------------------
72+
class CECKeyExchangePrivateKey : public CEC25519PrivateKeyBase
73+
{
74+
public:
75+
CECKeyExchangePrivateKey() : CEC25519PrivateKeyBase( k_ECryptoKeyTypeKeyExchangePrivate ) { }
76+
virtual ~CECKeyExchangePrivateKey();
77+
};
78+
79+
80+
//-----------------------------------------------------------------------------
81+
// Purpose: Encapsulates an elliptic-curve key-exchange public key (curve25519)
82+
// Internally, this is stored as a 32-byte binary data blob
83+
//-----------------------------------------------------------------------------
84+
class CECKeyExchangePublicKey : public CEC25519PublicKeyBase
85+
{
86+
public:
87+
CECKeyExchangePublicKey() : CEC25519PublicKeyBase( k_ECryptoKeyTypeKeyExchangePublic ) { }
88+
virtual ~CECKeyExchangePublicKey();
89+
};
90+
91+
92+
//-----------------------------------------------------------------------------
93+
// Purpose: Encapsulates an elliptic-curve signature private key (ed25519)
94+
// Internally, this is stored as a 64-byte (public, private) pair
95+
//-----------------------------------------------------------------------------
96+
class CECSigningPrivateKey : public CEC25519PrivateKeyBase
97+
{
98+
public:
99+
CECSigningPrivateKey() : CEC25519PrivateKeyBase( k_ECryptoKeyTypeSigningPrivate ) { }
100+
101+
// Load from PEM
102+
virtual bool LoadFromAndWipeBuffer( void *pBuffer, size_t cBytes ) override;
103+
104+
// Purpose: Get key in PEM text format
105+
// Input: pchPEMData - Pointer to string buffer to store output in (or NULL to just calculate required size)
106+
// cubPEMData - Size of pchPEMData buffer
107+
// pcubPEMData - Pointer to number of bytes written to pchPEMData (including terminating nul), or
108+
// required size of pchPEMData if it is NULL or not big enough.
109+
bool GetAsPEM( char *pchPEMData, uint32 cubPEMData, uint32 *pcubPEMData ) const;
110+
111+
// Parses OpenSSH PEM block.
112+
// WARNING: DOES NOT WIPE INPUT.
113+
bool ParsePEM( const char *pBuffer, size_t cBytes );
114+
115+
// Generate an ed25519 public-key signature
116+
void GenerateSignature( const void *pData, size_t cbData, CryptoSignature_t *pSignatureOut ) const;
117+
};
118+
119+
//-----------------------------------------------------------------------------
120+
// Purpose: Encapsulates an elliptic-curve signature public key (x25519)
121+
// Internally, this is stored as a 32-byte binary data blob
122+
//-----------------------------------------------------------------------------
123+
class CECSigningPublicKey : public CEC25519PublicKeyBase
124+
{
125+
public:
126+
CECSigningPublicKey() : CEC25519PublicKeyBase( k_ECryptoKeyTypeSigningPublic ) { }
127+
128+
virtual bool LoadFromAndWipeBuffer( void *pBuffer, size_t cBytes ) override;
129+
130+
bool GetAsOpenSSHAuthorizedKeys( char *pchData, uint32 cubData, uint32 *pcubData, const char *pszComment = "" ) const;
131+
bool SetFromOpenSSHAuthorizedKeys( const char *pchData, size_t cbData );
132+
133+
bool VerifySignature( const void *pData, size_t cbData, const CryptoSignature_t &signature ) const;
134+
};
135+
136+
#ifdef VALVE_CRYPTO_ENABLE_25519
137+
138+
namespace CCrypto
139+
{
140+
141+
//
142+
// Secure key exchange (curve25519 elliptic-curve Diffie-Hellman key exchange)
143+
//
144+
145+
// Generate a X25519 key pair for Diffie-Hellman secure key exchange.
146+
// pPublicKey can be null. (Since the private key also has a copy of the public key.)
147+
void GenerateKeyExchangeKeyPair( CECKeyExchangePublicKey *pPublicKey, CECKeyExchangePrivateKey *pPrivateKey );
148+
149+
// Do Diffie-Hellman secure key exchange.
150+
// NOTE: this actually returns the SHA256 of the raw DH result. I don't know why.
151+
bool PerformKeyExchange( const CECKeyExchangePrivateKey &localPrivateKey, const CECKeyExchangePublicKey &remotePublicKey, SHA256Digest_t *pSharedSecretOut );
152+
153+
//
154+
// Signing and verification (ed25519 elliptic-curve signature scheme)
155+
//
156+
157+
// Generate an ed25519 key pair for public-key signature generation
158+
void GenerateSigningKeyPair( CECSigningPublicKey *pPublicKey, CECSigningPrivateKey *pPrivateKey );
159+
160+
// Legacy compatibility - use the key methods
161+
inline void GenerateSignature( const void *pData, size_t cbData, const CECSigningPrivateKey &privateKey, CryptoSignature_t *pSignatureOut ) { privateKey.GenerateSignature( pData, cbData, pSignatureOut ); }
162+
inline bool VerifySignature( const void *pData, size_t cbData, const CECSigningPublicKey &publicKey, const CryptoSignature_t &signature ) { return publicKey.VerifySignature( pData, cbData, signature ); }
163+
};
164+
165+
#endif // #ifdef VALVE_CRYPTO_ENABLE_25519
166+
167+
#endif // CRYPTO_H

src/common/crypto_25519_donna.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//========= Copyright Valve LLC, All rights reserved. ========================
2+
23
#include "crypto.h"
4+
#include "crypto_25519.h"
35
#include <tier0/dbg.h>
46

57
#ifdef VALVE_CRYPTO_25519_DONNA
@@ -232,5 +234,5 @@ bool CEC25519PrivateKeyBase::CachePublicKey()
232234
return true;
233235
}
234236

235-
#endif // #ifdef GNS_CRYPTO_25519_REF
237+
#endif // #ifdef VALVE_CRYPTO_25519_DONNA
236238

src/common/crypto_25519_openssl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//========= Copyright Valve LLC, All rights reserved. ========================
22
#include "crypto.h"
3+
#include "crypto_25519.h"
34
#include <tier0/dbg.h>
45

56
#ifdef STEAMNETWORKINGSOCKETS_CRYPTO_25519_OPENSSL

src/common/keypair.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "keypair.h"
88
#include "crypto.h"
9+
#include "crypto_25519.h"
910
#include <tier1/utlbuffer.h>
1011

1112
static const char k_szOpenSSHPrivatKeyPEMHeader[] = "-----BEGIN OPENSSH PRIVATE KEY-----";

0 commit comments

Comments
 (0)