MCPcopy Create free account
hub / github.com/ValveSoftware/GameNetworkingSockets / TestEllipticCrypto

Function TestEllipticCrypto

tests/test_crypto.cpp:417–536  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

415}
416
417void TestEllipticCrypto()
418{
419 // test vectors from curve25519 reference impl
420 const char rgchAlicePriv[] = "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a";
421 const char rgchAlicePub[] = "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a";
422 const char rgchBobPriv[] = "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb";
423 const char rgchBobPub[] = "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f";
424 const char rgchExpectSharedPreHash[] = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742";
425
426 uint8 buf[64];
427 CECKeyExchangePrivateKey alicePriv, bobPriv;
428 CECKeyExchangePublicKey alicePub, bobPub;
429 alicePriv.SetFromHexEncodedString( rgchAlicePriv );
430 alicePub.SetFromHexEncodedString( rgchAlicePub );
431 CHECK( alicePriv.MatchesPublicKey( alicePub ) );
432
433 bobPriv.SetFromHexEncodedString( rgchBobPriv );
434 bobPub.SetFromHexEncodedString( rgchBobPub );
435 CHECK( bobPriv.MatchesPublicKey( bobPub ) );
436
437 V_hextobinary( rgchExpectSharedPreHash, 64, buf, 32 );
438 SHA256Digest_t expectedResult;
439 CCrypto::GenerateSHA256Digest( buf, 32, &expectedResult );
440
441 SHA256Digest_t aliceSharedSecret = {0};
442 SHA256Digest_t bobSharedSecret = {1};
443 CHECK( CCrypto::PerformKeyExchange( alicePriv, bobPub, &aliceSharedSecret ) );
444 CHECK( CCrypto::PerformKeyExchange( bobPriv, alicePub, &bobSharedSecret ) );
445
446 CHECK( V_memcmp( aliceSharedSecret, bobSharedSecret, sizeof(SHA256Digest_t) ) == 0 );
447 CHECK( V_memcmp( expectedResult, aliceSharedSecret, sizeof(SHA256Digest_t) ) == 0 );
448 CHECK( V_memcmp( expectedResult, bobSharedSecret, sizeof(SHA256Digest_t) ) == 0 );
449
450 // test key extraction and comparison operations
451 CECKeyExchangePublicKey testPubFromPriv;
452 alicePriv.GetPublicKey( &testPubFromPriv );
453 CHECK( testPubFromPriv == alicePub );
454 CHECK( testPubFromPriv != bobPub );
455 CHECK( alicePriv.MatchesPublicKey( testPubFromPriv ) );
456 CHECK( !bobPriv.MatchesPublicKey( testPubFromPriv ) );
457
458 // test key exchange with random keys
459 alicePriv.Wipe();
460 alicePub.Wipe();
461 bobPriv.Wipe();
462 bobPub.Wipe();
463 memset( aliceSharedSecret, 0, sizeof( aliceSharedSecret ) );
464 memset( bobSharedSecret, 0xFF, sizeof( bobSharedSecret ) );
465 CCrypto::GenerateKeyExchangeKeyPair( &alicePub, &alicePriv );
466 CCrypto::GenerateKeyExchangeKeyPair( &bobPub, &bobPriv );
467 // alice and bob send each other only their public keys.
468 CHECK( CCrypto::PerformKeyExchange( alicePriv, bobPub, &aliceSharedSecret ) );
469 CHECK( CCrypto::PerformKeyExchange( bobPriv, alicePub, &bobSharedSecret ) );
470 // alice and bob should have computed the same shared secret.
471 CHECK( V_memcmp( aliceSharedSecret, bobSharedSecret, sizeof( bobSharedSecret ) ) == 0 );
472
473
474 // test vectors from ed25519 reference impl

Callers 1

mainFunction · 0.85

Calls 8

V_hextobinaryFunction · 0.85
V_memcmpFunction · 0.85
GenerateSignatureFunction · 0.85
VerifySignatureFunction · 0.85
MatchesPublicKeyMethod · 0.80
GetPublicKeyMethod · 0.80
WipeMethod · 0.45

Tested by

no test coverage detected