| 430 | } |
| 431 | |
| 432 | int CEncryptedDatagramSocket::EncryptSendServer(uint8_t** ppbyBuf, int nBufLen, uint32_t dwBaseKey) |
| 433 | { |
| 434 | wxASSERT( thePrefs::IsServerCryptLayerUDPEnabled() ); |
| 435 | wxASSERT( dwBaseKey != 0 ); |
| 436 | |
| 437 | uint16_t nRandomKeyPart = GetRandomUint16(); |
| 438 | |
| 439 | uint8_t achKeyData[7]; |
| 440 | PokeUInt32(achKeyData, dwBaseKey); |
| 441 | achKeyData[4] = MAGICVALUE_UDP_CLIENTSERVER; |
| 442 | PokeUInt16(achKeyData + 5, nRandomKeyPart); |
| 443 | MD5Sum md5(achKeyData, sizeof(achKeyData)); |
| 444 | CRC4EncryptableBuffer sendbuffer; |
| 445 | sendbuffer.SetKey(md5, true); |
| 446 | |
| 447 | // create the semi random byte encryption header |
| 448 | uint8_t bySemiRandomNotProtocolMarker = 0; |
| 449 | int i; |
| 450 | |
| 451 | for (i = 0; i < 128; i++) { |
| 452 | bySemiRandomNotProtocolMarker = GetRandomUint8(); |
| 453 | if (bySemiRandomNotProtocolMarker != OP_EDONKEYPROT) { // not allowed values |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | if (i >= 128) { |
| 459 | // either we have _real_ bad luck or the randomgenerator is a bit messed up |
| 460 | wxFAIL; |
| 461 | bySemiRandomNotProtocolMarker = 0x01; |
| 462 | } |
| 463 | |
| 464 | uint8_t byPadLen = 0; // padding disabled for UDP currently |
| 465 | uint32_t nCryptedLen = nBufLen + byPadLen + CRYPT_HEADER_WITHOUTPADDING; |
| 466 | uint8_t* pachCryptedBuffer = new uint8_t[nCryptedLen]; |
| 467 | |
| 468 | pachCryptedBuffer[0] = bySemiRandomNotProtocolMarker; |
| 469 | PokeUInt16(pachCryptedBuffer + 1, nRandomKeyPart); |
| 470 | |
| 471 | uint32_t dwMagicValue = ENDIAN_SWAP_32(MAGICVALUE_UDP_SYNC_SERVER); |
| 472 | sendbuffer.RC4Crypt((uint8_t*)&dwMagicValue, pachCryptedBuffer + 3, 4); |
| 473 | |
| 474 | sendbuffer.RC4Crypt((uint8_t*)&byPadLen, pachCryptedBuffer + 7, 1); |
| 475 | |
| 476 | for (int j = 0; j < byPadLen; j++){ |
| 477 | uint8_t byRand = (uint8_t)rand(); // they actually don't really need to be random, but it doesn't hurt either |
| 478 | sendbuffer.RC4Crypt((uint8_t*)&byRand, pachCryptedBuffer + CRYPT_HEADER_WITHOUTPADDING + j, 1); |
| 479 | } |
| 480 | sendbuffer.RC4Crypt(*ppbyBuf, pachCryptedBuffer + CRYPT_HEADER_WITHOUTPADDING + byPadLen, nBufLen); |
| 481 | delete[] *ppbyBuf; |
| 482 | *ppbyBuf = pachCryptedBuffer; |
| 483 | |
| 484 | theStats::AddUpOverheadCrypt(nCryptedLen - nBufLen); |
| 485 | return nCryptedLen; |
| 486 | } |
nothing calls this directly
no test coverage detected