@@ -21,6 +21,9 @@ void SharedUtil_WildcardMatch_Tests ( void );
2121void SharedUtil_Collection_Tests ( void );
2222void SharedUtil_String_Tests ( void );
2323void SharedUtil_Hash_Tests ( void );
24+ void SharedUtil_RSA_Tests ( void );
25+ void RSATestCrypt ( const SString& input, const SString& keyEncrypt, const SString& keyDecrypt );
26+
2427
2528// /////////////////////////////////////////////////////////////
2629//
@@ -38,6 +41,9 @@ void SharedUtil_Tests ( void )
3841 SharedUtil_Collection_Tests ();
3942 SharedUtil_String_Tests ();
4043 SharedUtil_Hash_Tests ();
44+ #ifndef MTA_CLIENT
45+ SharedUtil_RSA_Tests ();
46+ #endif
4147}
4248
4349
@@ -899,3 +905,51 @@ void SharedUtil_Hash_Tests ( void )
899905
900906 FileDelete ( szTempFilename );
901907}
908+
909+
910+ #ifndef MTA_CLIENT
911+ // /////////////////////////////////////////////////////////////
912+ //
913+ // SharedUtil_RSA_Tests
914+ //
915+ // Test behaviour of Raknet RSA code
916+ //
917+ // /////////////////////////////////////////////////////////////
918+ void SharedUtil_RSA_Tests ( void )
919+ {
920+ // Generate keys
921+ char privateKeyTemp[64 ];
922+ char publicKeyTemp[68 ];
923+ g_pRealNetServer->RSAGenerateKeys ( privateKeyTemp, sizeof ( privateKeyTemp ), publicKeyTemp, sizeof ( publicKeyTemp ) );
924+ SStringX privateKey ( privateKeyTemp, sizeof ( privateKeyTemp ) );
925+ SStringX publicKey ( publicKeyTemp, sizeof ( publicKeyTemp ) );
926+
927+ // Short data length
928+ SString input = " hello" ;
929+ RSATestCrypt ( input, publicKey, privateKey );
930+ RSATestCrypt ( input, privateKey, publicKey );
931+
932+ // Long data length
933+ while ( input.size () < 3000 )
934+ input += input + " goodbye" ;
935+ RSATestCrypt ( input, publicKey, privateKey );
936+ RSATestCrypt ( input, privateKey, publicKey );
937+ }
938+
939+ void RSATestCrypt ( const SString& input, const SString& keyEncrypt, const SString& keyDecrypt )
940+ {
941+ // Encrypt
942+ std::vector < char > encryptedBuffer;
943+ encryptedBuffer.resize ( input.size () + 100 );
944+ uint uiEncryptedSize = g_pRealNetServer->RSAEncryptData ( &input[0 ], input.size (), &keyEncrypt[0 ], keyEncrypt.size (), &encryptedBuffer[0 ], encryptedBuffer.size () );
945+
946+ // Decrypt
947+ std::vector < char > decryptedBuffer;
948+ decryptedBuffer.resize ( uiEncryptedSize );
949+ uint uiDecryptedSize = g_pRealNetServer->RSADecryptData ( &encryptedBuffer[0 ], uiEncryptedSize, &keyDecrypt[0 ], keyDecrypt.size (), &decryptedBuffer[0 ], decryptedBuffer.size () );
950+
951+ // Check decrypted is the same as original
952+ assert ( input.size () == uiDecryptedSize );
953+ assert ( memcmp ( &input[0 ], &decryptedBuffer[0 ], uiDecryptedSize ) == 0 );
954+ }
955+ #endif
0 commit comments