game-networking-sockets-sys 0.2.0

Rust bindings for Valve GameNetworkingSockets library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//========= Copyright Valve LLC, All rights reserved. ========================

// Note: not using precompiled headers! This file is included directly by
// several different projects and may include Crypto++ headers depending
// on compile-time defines, which in turn pulls in other odd dependencies

#include "keypair.h"
#include "crypto.h"
#include "crypto_25519.h"
#include <tier1/utlbuffer.h>

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

static bool BCheckAndEatBytes( CUtlBuffer &buf, const void *data, int sz )
{
	if ( buf.GetBytesRemaining() < sz )
		return false;
	if ( V_memcmp( buf.PeekGet(), data, sz ) != 0 )
		return false;
	buf.SeekGet( CUtlBuffer::SEEK_CURRENT, sz );
	return true;
}

static bool BOpenSSHGetUInt32( CUtlBuffer &buf, uint32 &result )
{
	uint32 temp;
	if ( !buf.Get( &temp, 4 ) )
		return false;
	result = BigDWord( temp );
	return true;
}

static void OpenSSHWriteUInt32( CUtlBuffer &buf, uint32 data )
{
	data = BigDWord( data );
	buf.Put( &data, sizeof(data) );
}

static const uint32 k_nBinarySSHEd25519KeyTypeIDLen = 15;

static bool BOpenSSHBinaryEd25519CheckAndEatKeyType( CUtlBuffer &buf )
{
	return BCheckAndEatBytes( buf, "\x00\x00\x00\x0bssh-ed25519", k_nBinarySSHEd25519KeyTypeIDLen );
}

static void OpenSSHBinaryEd25519WriteKeyType( CUtlBuffer &buf )
{
	buf.Put( "\x00\x00\x00\x0bssh-ed25519", k_nBinarySSHEd25519KeyTypeIDLen );
}

static bool BOpenSSHBinaryReadFixedSizeKey( CUtlBuffer &buf, void *pOut, uint32 cbExpectedSize )
{
	uint32 cbSize = 0;
	if ( !BOpenSSHGetUInt32( buf, cbSize ) )
		return false;
	if ( cbSize != cbExpectedSize || buf.GetBytesRemaining() < (int)cbExpectedSize )
		return false;
	V_memcpy( pOut, buf.PeekGet(), cbExpectedSize );
	buf.SeekGet( CUtlBuffer::SEEK_CURRENT, cbExpectedSize );
	return true;
}

static void OpenSSHBinaryWriteFixedSizeKey( CUtlBuffer &buf, const void *pData, uint32 cbSize )
{
	OpenSSHWriteUInt32( buf, cbSize );
	buf.Put( pData, cbSize );
}

static bool BParseOpenSSHBinaryEd25519Private( CUtlBuffer &buf, uint8 *pPrivateThenPublicKey )
{

	// OpenSSH source sshkey.c, sshkey_private_to_blob2():

	//	if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
	//	    (r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
	//	    (r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
	//	    (r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
	//	    (r = sshbuf_put_u32(encoded, 1)) != 0 ||	/* number of keys */
	//	    (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
	//	    (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
	//		goto out;

	if ( !BCheckAndEatBytes( buf, "openssh-key-v1", 15 ) )
		return false;

	// Encrypted keys not supported
	if ( !BCheckAndEatBytes( buf, "\x00\x00\x00\x04none\x00\x00\x00\x04none\x00\x00\x00\x00", 20 ) )
	{
		AssertMsg( false, "Tried to use encrypted OpenSSH private key" );
		return false;
	}

	// File should only contain a single key
	if ( !BCheckAndEatBytes( buf, "\x00\x00\x00\x01", 4 ) )
		return false;

	// Public key.  It's actually stored in the file 3 times.
	uint8 arbPubKey1[ 32 ];
	{
		// Size of public key
		uint32 cbEncodedPubKey;
		if ( !BOpenSSHGetUInt32( buf, cbEncodedPubKey ) )
			return false;
		if ( buf.GetBytesRemaining() < (int)cbEncodedPubKey )
			return false;

		// Parse public key
		CUtlBuffer bufPubKey( buf.PeekGet(), cbEncodedPubKey, CUtlBuffer::READ_ONLY );
		bufPubKey.SeekPut( CUtlBuffer::SEEK_HEAD, cbEncodedPubKey );
		buf.SeekGet( CUtlBuffer::SEEK_CURRENT, cbEncodedPubKey );

		if ( !BOpenSSHBinaryEd25519CheckAndEatKeyType( bufPubKey ) )
			return false;
		if ( !BOpenSSHBinaryReadFixedSizeKey( bufPubKey, arbPubKey1, sizeof(arbPubKey1) ) )
			return false;
	}

	// Private key
	{
		uint32 cbEncodedPrivKey;
		if ( !BOpenSSHGetUInt32( buf, cbEncodedPrivKey ) )
			return false;
		if ( buf.GetBytesRemaining() < (int)cbEncodedPrivKey ) // This should actually be the last thing, but if there's extra stuff, I guess we don't care.
			return false;

		// OpenSSH source sshkey.c, to_blob_buf()

		CUtlBuffer bufPrivKey( buf.PeekGet(), cbEncodedPrivKey, CUtlBuffer::READ_ONLY );
		bufPrivKey.SeekPut( CUtlBuffer::SEEK_HEAD, cbEncodedPrivKey );

		// Consume check bytes (used for encrypted keys)

		//	/* Random check bytes */
		//	check = arc4random();
		//	if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
		//	    (r = sshbuf_put_u32(encrypted, check)) != 0)
		//		goto out;

		uint32 check1, check2;
		if ( !BOpenSSHGetUInt32( bufPrivKey, check1 ) || !BOpenSSHGetUInt32( bufPrivKey, check2 ) || check1 != check2 )
			return false;

		// Key type
		if ( !BOpenSSHBinaryEd25519CheckAndEatKeyType( bufPrivKey ) )
			return false;

		// Public key...again.  One would think that having this large,
		// known know plaintext (TWICE!) is not wise if the key is encrypted
		// with a password....but oh well.
		uint8 arbPubKey2[ 32 ];
		if ( !BOpenSSHBinaryReadFixedSizeKey( bufPrivKey, arbPubKey2, sizeof(arbPubKey2) ) )
			return false;
		if ( V_memcmp( arbPubKey1, arbPubKey2, sizeof(arbPubKey1) ) != 0 )
			return false;

		// And now the entire secret key
		if ( !BOpenSSHBinaryReadFixedSizeKey( bufPrivKey, pPrivateThenPublicKey, 64 ) )
			return false;

		// The "secret" actually consists of the real secret key
		// followed by the public key.  Check that this third
		// copy of the public key matches the other two.
		if ( V_memcmp( arbPubKey1, pPrivateThenPublicKey+32, sizeof(arbPubKey1) ) != 0 )
			return false;

		// Comment and padding comes after this, but we don't care
	}

	return true;
}

static int OpenSSHBinaryBeginSubBlock( CUtlBuffer &buf )
{
	int nSaveTell = buf.TellPut();
	buf.SeekPut( CUtlBuffer::SEEK_CURRENT, sizeof(uint32) );
	return nSaveTell;
}

static void OpenSSHBinaryEndSubBlock( CUtlBuffer &buf, int nSaveTell )
{
	int nBytesWritten = buf.TellPut() - nSaveTell - sizeof(uint32);
	Assert( nBytesWritten >= 0 );
	*(uint32 *)( (uint8 *)buf.Base() + nSaveTell ) = BigDWord( uint32(nBytesWritten) );
}

static void OpenSSHBinaryWriteEd25519Private( CUtlBuffer &buf, const uint8 *pPrivKey, const uint8 *pPubKey )
{
	// Make sure we don't realloc, so that if we wipe afterwards we don't
	// leave key material lying around.
	buf.EnsureCapacity( 2048 );

	buf.Put( "openssh-key-v1", 15 );
	buf.Put( "\x00\x00\x00\x04none\x00\x00\x00\x04none\x00\x00\x00\x00", 20 );
	buf.Put( "\x00\x00\x00\x01", 4 );

	// Public key.  It's actually stored in the file 3 times.
	{
		// Size of public key
		int nSaveTell = OpenSSHBinaryBeginSubBlock( buf );
		OpenSSHBinaryEd25519WriteKeyType( buf );
		OpenSSHBinaryWriteFixedSizeKey( buf, pPubKey, 32 );
		OpenSSHBinaryEndSubBlock( buf, nSaveTell );
	}

	// Private key
	{
		int nSaveTell = OpenSSHBinaryBeginSubBlock( buf );

		// Check bytes.  Since we aren't encrypting, it's not useful for
		// these to be random.
		OpenSSHWriteUInt32( buf, 0x12345678 );
		OpenSSHWriteUInt32( buf, 0x12345678 );

		// Key type
		OpenSSHBinaryEd25519WriteKeyType( buf );

		// Public key...again.
		OpenSSHBinaryWriteFixedSizeKey( buf, pPubKey, 32 );

		// And now the entire "secret" key.  But this is actually
		// the private key followed by the public
		OpenSSHWriteUInt32( buf, 64 );
		buf.Put( pPrivKey, 32 );
		buf.Put( pPubKey, 32 );

		// Comment and padding comes after this.
		// Should we write it anything?

		OpenSSHBinaryEndSubBlock( buf, nSaveTell );
	}
}

static bool BParseOpenSSHBinaryEd25519Public( CUtlBuffer &buf, uint8 *pKey )
{
	if ( !BOpenSSHBinaryEd25519CheckAndEatKeyType( buf ) )
		return false;

	if ( !BOpenSSHBinaryReadFixedSizeKey( buf, pKey, 32 ) )
		return false;

	// If there's extra stuff, we don't care
	return true;
}

static void OpenSSHBinaryEd25519WritePublic( CUtlBuffer &buf, const uint8 *pKey )
{
	buf.EnsureCapacity( 128 );
	OpenSSHBinaryEd25519WriteKeyType( buf );
	OpenSSHBinaryWriteFixedSizeKey( buf, pKey, 32 );
}

static bool GetBinaryDataAsPEM( char *pchPEMData, uint32_t cubPEMData, uint32_t *pcubPEMData, const void *pBinaryData, uint32 cbBinaryData, const char *pchPrefix, const char *pchSuffix )
{
	uint32_t uRequiredBytes = V_strlen( pchPrefix ) + 2 + V_strlen( pchSuffix ) + 2 + CCrypto::Base64EncodeMaxOutput( cbBinaryData, "\r\n" );
	if ( pcubPEMData )
		*pcubPEMData = uRequiredBytes;

	if ( pchPEMData )
	{
		if ( cubPEMData < uRequiredBytes )
			return false;

		V_strncpy( pchPEMData, pchPrefix, cubPEMData );
		V_strncat( pchPEMData, "\r\n", cubPEMData );
		uint32_t uRemainingBytes = cubPEMData - V_strlen( pchPEMData );
	
		if ( !CCrypto::Base64Encode( pBinaryData, cbBinaryData, pchPEMData + V_strlen( pchPEMData ), &uRemainingBytes, "\r\n" ) )
			return false;

		V_strncat( pchPEMData, pchSuffix, cubPEMData );
		V_strncat( pchPEMData, "\r\n", cubPEMData );
		if ( pcubPEMData )
			*pcubPEMData = V_strlen( pchPEMData ) + 1;
	}

	return true;
}

CCryptoKeyBase::~CCryptoKeyBase() {}

bool CCryptoKeyBase::GetRawDataAsStdString( std::string *pString ) const
{
	pString->clear();
	uint32 cbSize = GetRawData(nullptr);
	if ( cbSize == 0 )
		return false;
	void *tmp = alloca( cbSize );
	if ( GetRawData( tmp ) != cbSize )
	{
		Assert( false );
		return false;
	}
	pString->assign( (const char *)tmp, cbSize );
	SecureZeroMemory( tmp, cbSize );
	return true;
}

bool CCryptoKeyBase::SetRawDataAndWipeInput( void *pData, size_t cbData )
{
	bool bResult = SetRawDataWithoutWipingInput( pData, cbData );
	SecureZeroMemory( pData, cbData );
	return bResult;
}

bool CCryptoKeyBase::SetRawDataWithoutWipingInput( const void *pData, size_t cbData )
{
	Wipe();
	return SetRawData( pData, cbData ); // Call type-specific function
}

bool CCryptoKeyBase::SetFromHexEncodedString( const char *pchEncodedKey )
{
	Wipe();

	uint32 cubKey = V_strlen( pchEncodedKey ) / 2 + 1;
	void *buf = alloca( cubKey );

	if ( !CCrypto::HexDecode( pchEncodedKey, buf, &cubKey ) )
	{
		SecureZeroMemory( buf, cubKey );
		return false;
	}

	return SetRawDataAndWipeInput( buf, cubKey );
}


//-----------------------------------------------------------------------------
// Purpose: Initialize a key object from a base-64 encoded string of the raw key bytes
// Input:	pchEncodedKey -		Pointer to the base-64 encoded key string
// Output:  true if successful, false if initialization failed
//-----------------------------------------------------------------------------
bool CCryptoKeyBase::SetFromBase64EncodedString( const char *pchEncodedKey )
{
	Wipe();
	const int cchEncodedKey = V_strlen( pchEncodedKey );
	uint32 cubKey = cchEncodedKey * 3 / 4 + 1;

	void *buf = alloca( cubKey );
	if ( !CCrypto::Base64Decode( pchEncodedKey, cchEncodedKey, buf, &cubKey ) )
	{
		SecureZeroMemory( buf, cubKey );
		return false;
	}

	return SetRawDataAndWipeInput( buf, cubKey );
}

//-----------------------------------------------------------------------------
// Purpose: Compare two keys for equality
// Output:  true if the keys are identical
//-----------------------------------------------------------------------------
bool CCryptoKeyBase::operator==( const CCryptoKeyBase &rhs ) const
{
	if ( m_eKeyType != rhs.m_eKeyType ) return false;
	uint32 cbRawData = GetRawData(nullptr);
	if ( cbRawData != rhs.GetRawData(nullptr) ) return false;

	CAutoWipeBuffer bufLHS( cbRawData );
	CAutoWipeBuffer bufRHS( cbRawData );
	DbgVerify( GetRawData( bufLHS.Base() ) == cbRawData );
	DbgVerify( rhs.GetRawData( bufRHS.Base() ) == cbRawData );

	return memcmp( bufLHS.Base(), bufRHS.Base(), cbRawData ) == 0;
}


//-----------------------------------------------------------------------------
// Purpose: Return true if our raw data matches the the specified buffer
//-----------------------------------------------------------------------------
bool CCryptoKeyBase::BMatchesRawData( const void *pData, size_t cbData ) const
{
	uint32 cbMyRawData = GetRawData(nullptr);
	if ( cbMyRawData != cbData ) return false;

	CAutoWipeBuffer bufMyRawData( cbMyRawData );
	DbgVerify( GetRawData( bufMyRawData.Base() ) == cbMyRawData );

	return memcmp( bufMyRawData.Base(), pData, cbData ) == 0;
}

void CCryptoKeyBase::CopyFrom( const CCryptoKeyBase &x )
{
	Assert( m_eKeyType == x.m_eKeyType );
	Wipe();

	uint32 cbData = x.GetRawData( nullptr );
	if ( cbData == 0 )
		return;
	void *tmp = alloca( cbData );
	VerifyFatal( x.GetRawData( tmp ) == cbData );
	VerifyFatal( SetRawDataAndWipeInput( tmp, cbData ) );
}

bool CCryptoKeyBase::LoadFromAndWipeBuffer( void *pBuffer, size_t cBytes )
{
	AssertMsg1( false, "Key type %d doesn't know how to load from buffer", m_eKeyType );
	Wipe();
	SecureZeroMemory( pBuffer, cBytes );
	return false;
}

//-----------------------------------------------------------------------------
// CCryptoKeyBase_RawBuffer
//-----------------------------------------------------------------------------

CCryptoKeyBase_RawBuffer::~CCryptoKeyBase_RawBuffer()
{
	// Note that we don't call virtual Wipe() here.  We're in a
	// destructor, so it would just call our own Wipe(),
	// anyway, but that relies on a relatively subtle aspect
	// ot C++ destructor semantics, and this is more clear.
	InternalWipeRawDataBuffer();
}

bool CCryptoKeyBase_RawBuffer::IsValid() const
{
	return m_pData != nullptr && m_cbData > 0;
}

uint32 CCryptoKeyBase_RawBuffer::GetRawData( void *pData ) const
{
	if ( pData )
		memcpy( pData, m_pData, m_cbData );
	return m_cbData;
}

bool CCryptoKeyBase_RawBuffer::SetRawData( const void *pData, size_t cbData )
{
	return InternalSetRawDataBuffer( pData, cbData );
}

bool CCryptoKeyBase_RawBuffer::InternalSetRawDataBuffer( const void *pData, size_t cbData )
{
	InternalWipeRawDataBuffer();
	m_pData = (uint8*)malloc( cbData );
	if ( !m_pData )
		return false;
	memcpy( m_pData, pData, cbData );
	m_cbData = (uint32)cbData;
	return true;
}

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

void CCryptoKeyBase_RawBuffer::InternalWipeRawDataBuffer()
{
	if ( m_pData )
	{
		SecureZeroMemory( m_pData, m_cbData );
		free( m_pData );
		m_pData = nullptr;
	}
	m_cbData = 0;
}

bool CCryptoKeyBase_RawBuffer::EnsureRawDataPtrAvailable()
{
	if ( !m_pData )
	{
		uint32 cbData = GetRawData(nullptr);
		if ( cbData == 0 )
			return false;
		m_pData = (uint8*)malloc( cbData );
		if ( !m_pData )
			return false;
		m_cbData = cbData;
		if ( GetRawData( m_pData ) != cbData )
		{
			InternalWipeRawDataBuffer();
			return false;
		}
	}
	return true;
}

//-----------------------------------------------------------------------------
// CEC25519PrivateKeyBase
//-----------------------------------------------------------------------------

CEC25519PrivateKeyBase::~CEC25519PrivateKeyBase()
{
	Wipe();
}

void CEC25519PrivateKeyBase::Wipe()
{
	CEC25519KeyBase::Wipe();

	// A public key is not sensitive, by definition, but let's zero it anyway
	SecureZeroMemory( m_publicKey, sizeof(m_publicKey) );
}

bool CEC25519PrivateKeyBase::GetPublicKey( CEC25519PublicKeyBase *pPublicKey ) const
{
	pPublicKey->Wipe();

	if ( m_eKeyType == k_ECryptoKeyTypeKeyExchangePrivate )
	{
		Assert( pPublicKey->GetKeyType() == k_ECryptoKeyTypeKeyExchangePublic );
		if ( pPublicKey->GetKeyType() != k_ECryptoKeyTypeKeyExchangePublic )
			return false;
	}
	else if ( m_eKeyType == k_ECryptoKeyTypeSigningPrivate )
	{
		Assert( pPublicKey->GetKeyType() == k_ECryptoKeyTypeSigningPublic );
		if ( pPublicKey->GetKeyType() != k_ECryptoKeyTypeSigningPublic )
			return false;
	}
	else
	{
		Assert( false ); // impossible, we must be one or the other if valid
		return false;
	}

	return pPublicKey->SetRawDataWithoutWipingInput( m_publicKey, 32 );
}

bool CEC25519PrivateKeyBase::MatchesPublicKey( const CEC25519PublicKeyBase &publicKey ) const
{
	if ( m_eKeyType == k_ECryptoKeyTypeKeyExchangePrivate && publicKey.GetKeyType() != k_ECryptoKeyTypeKeyExchangePublic )
		return false;
	if ( m_eKeyType == k_ECryptoKeyTypeSigningPrivate && publicKey.GetKeyType() != k_ECryptoKeyTypeSigningPublic )
		return false;
	if ( !IsValid() || !publicKey.IsValid() )
		return false;

	uint8 pubKey2[32];
	DbgVerify( publicKey.GetRawData( pubKey2 ) == 32 );

	return memcmp( GetPublicKeyRawData(), pubKey2, 32 ) == 0;
}

bool CEC25519PrivateKeyBase::SetRawData( const void *pData, size_t cbData )
{
	if ( !CEC25519KeyBase::SetRawData( pData, cbData ) )
		return false;
	if ( CachePublicKey() )
		return true;
	Wipe();
	return false;
}

CEC25519PublicKeyBase::~CEC25519PublicKeyBase() {}
CECKeyExchangePrivateKey::~CECKeyExchangePrivateKey() {}
CECKeyExchangePublicKey::~CECKeyExchangePublicKey() {}

//-----------------------------------------------------------------------------
// CECSigningPrivateKey
//-----------------------------------------------------------------------------

bool CECSigningPrivateKey::GetAsPEM( char *pchPEMData, uint32_t cubPEMData, uint32_t *pcubPEMData ) const
{
	if ( !IsValid() )
		return false;
	uint8 privateKey[ 32 ];
	VerifyFatal( GetRawData( privateKey ) == 32 );

	CAutoWipeBuffer bufTemp;
	OpenSSHBinaryWriteEd25519Private( bufTemp, privateKey, GetPublicKeyRawData() );
	SecureZeroMemory( privateKey, sizeof(privateKey) );

	return GetBinaryDataAsPEM( pchPEMData, cubPEMData, pcubPEMData, bufTemp.Base(), bufTemp.TellPut(), k_szOpenSSHPrivatKeyPEMHeader, k_szOpenSSHPrivatKeyPEMFooter ); 
}

bool CECSigningPrivateKey::LoadFromAndWipeBuffer( void *pBuffer, size_t cBytes )
{
	bool bResult = ParsePEM( (const char *)pBuffer, cBytes );
	SecureZeroMemory( pBuffer, cBytes );
	return bResult;
}

bool CECSigningPrivateKey::ParsePEM( const char *pBuffer, size_t cBytes )
{
	Wipe();

	CAutoWipeBuffer buf;
	if ( !CCrypto::DecodePEMBody( pBuffer, (uint32)cBytes, buf, "OPENSSH PRIVATE KEY" ) )
		return false;
	uint8 privateThenPublic[64];
	if ( !BParseOpenSSHBinaryEd25519Private( buf, privateThenPublic ) )
		return false;

	if ( !SetRawDataAndWipeInput( privateThenPublic, 32 ) )
		return false;

	// Check that the public key matches the private one.
	// (And also that all of our code works.)
	if ( V_memcmp( m_publicKey, privateThenPublic+32, 32 ) == 0 )
		return true;

	AssertMsg( false, "Ed25519 key public doesn't match private!" );
	Wipe();
	return false;
}

//-----------------------------------------------------------------------------
// CECSigningPublicKey
//-----------------------------------------------------------------------------

bool CECSigningPublicKey::GetAsOpenSSHAuthorizedKeys( char *pchData, uint32 cubData, uint32 *pcubData, const char *pszComment ) const
{
	if ( !IsValid() )
		return false;

	int cchComment = pszComment ? V_strlen( pszComment ) : 0;

	uint8 publicKey[ 32 ];
	VerifyFatal( GetRawData( publicKey ) == 32 );

	CUtlBuffer bufBinary;
	OpenSSHBinaryEd25519WritePublic( bufBinary, publicKey );

	static const char pchPrefix[] = "ssh-ed25519 ";

	uint32_t uRequiredBytes =
		V_strlen( pchPrefix )
		+ CCrypto::Base64EncodeMaxOutput( bufBinary.TellPut(), "" )
		+ ( cchComment > 0 ? 1 : 0 ) // space
		+ cchComment
		+ 1; // '\0'
	if ( pcubData )
		*pcubData = uRequiredBytes;

	if ( pchData )
	{
		if ( cubData < uRequiredBytes )
			return false;

		V_strncpy( pchData, pchPrefix, cubData );
		uint32_t uRemainingBytes = cubData - V_strlen( pchData );
	
		if ( !CCrypto::Base64Encode( (const uint8 *)bufBinary.Base(), bufBinary.TellPut(), pchData + V_strlen( pchData ), &uRemainingBytes, "" ) )
			return false;

		if ( pszComment )
		{
			V_strncat( pchData, " ", cubData );
			V_strncat( pchData, pszComment, cubData );
		}
		if ( pcubData )
			*pcubData = V_strlen( pchData ) + 1;
	}

	return true;
}

bool CECSigningPublicKey::LoadFromAndWipeBuffer( void *pBuffer, size_t cBytes )
{
	bool bResult = SetFromOpenSSHAuthorizedKeys( (const char *)pBuffer, cBytes );
	SecureZeroMemory( pBuffer, cBytes );
	return bResult;
}

bool CECSigningPublicKey::SetFromOpenSSHAuthorizedKeys( const char *pchData, size_t cbData )
{
	Wipe();

	// Gah, we need to make a copy to '\0'-terminate it, to make parsing below easier
	CAutoWipeBuffer bufText( int( cbData + 8 ) );
	bufText.Put( pchData, int(cbData) );
	bufText.PutChar(0);
	pchData = (const char *)bufText.Base();

	int idxStart = -1, idxEnd = -1;
	sscanf( pchData, "ssh-ed25519 %nAAAA%*s%n", &idxStart, &idxEnd );
	if ( idxStart <= 0 || idxEnd <= idxStart )
		return false;

	CAutoWipeBuffer bufBinary;
	if ( !CCrypto::DecodeBase64ToBuf( pchData + idxStart, idxEnd-idxStart, bufBinary ) )
		return false;

	uint8 pubKey[ 32 ];
	if ( !BParseOpenSSHBinaryEd25519Public( bufBinary, pubKey ) )
		return false;
	return SetRawDataAndWipeInput( pubKey, 32 );
 }

void CCrypto::GenerateKeyExchangeKeyPair( CECKeyExchangePublicKey *pPublicKey, CECKeyExchangePrivateKey *pPrivateKey )
{
	uint8 rgubSecretData[32];
	GenerateRandomBlock( rgubSecretData, 32 );
	VerifyFatal( pPrivateKey->SetRawDataAndWipeInput( rgubSecretData, 32 ) );
	if ( pPublicKey )
	{
		VerifyFatal( pPrivateKey->GetPublicKey( pPublicKey ) );
	}
}

void CCrypto::GenerateSigningKeyPair( CECSigningPublicKey *pPublicKey, CECSigningPrivateKey *pPrivateKey )
{
	uint8 rgubSecretData[32];
	GenerateRandomBlock( rgubSecretData, 32 );
	VerifyFatal( pPrivateKey->SetRawDataAndWipeInput( rgubSecretData, 32 ) );
	if ( pPublicKey )
	{
		VerifyFatal( pPrivateKey->GetPublicKey( pPublicKey ) );
	}
}