Encrypt packet. Key used: clientHashOrKadID != NULL -> clientHashOrKadID clientHashOrKadID == NULL && kad && receiverVerifyKey != 0 -> receiverVerifyKey else -> ASSERT
| 262 | // clientHashOrKadID == NULL && kad && receiverVerifyKey != 0 -> receiverVerifyKey |
| 263 | // else -> ASSERT |
| 264 | int CEncryptedDatagramSocket::EncryptSendClient(uint8_t **buf, int bufLen, const uint8_t *clientHashOrKadID, bool kad, uint32_t receiverVerifyKey, uint32_t senderVerifyKey) |
| 265 | { |
| 266 | wxASSERT(theApp->GetPublicIP() != 0 || kad); |
| 267 | wxASSERT(thePrefs::IsClientCryptLayerSupported()); |
| 268 | wxASSERT(clientHashOrKadID != NULL || receiverVerifyKey != 0); |
| 269 | wxASSERT((receiverVerifyKey == 0 && senderVerifyKey == 0) || kad); |
| 270 | |
| 271 | uint8_t padLen = 0; // padding disabled for UDP currently |
| 272 | const uint32_t cryptHeaderLen = padLen + CRYPT_HEADER_WITHOUTPADDING + (kad ? 8 : 0); |
| 273 | uint32_t cryptedLen = bufLen + cryptHeaderLen; |
| 274 | uint8_t *cryptedBuffer = new uint8_t[cryptedLen]; |
| 275 | bool kadRecvKeyUsed = false; |
| 276 | |
| 277 | uint16_t randomKeyPart = GetRandomUint16(); |
| 278 | CRC4EncryptableBuffer sendbuffer; |
| 279 | MD5Sum md5; |
| 280 | if (kad) { |
| 281 | if ((clientHashOrKadID == NULL || CMD4Hash(clientHashOrKadID).IsEmpty()) && receiverVerifyKey != 0) { |
| 282 | kadRecvKeyUsed = true; |
| 283 | uint8_t keyData[6]; |
| 284 | PokeUInt32(keyData, receiverVerifyKey); |
| 285 | PokeUInt16(keyData+4, randomKeyPart); |
| 286 | md5.Calculate(keyData, sizeof(keyData)); |
| 287 | //DEBUG_ONLY( DebugLog(_T("Creating obfuscated Kad packet encrypted by ReceiverKey (%u)"), nReceiverVerifyKey) ); |
| 288 | } |
| 289 | else if (clientHashOrKadID != NULL && !CMD4Hash(clientHashOrKadID).IsEmpty()) { |
| 290 | uint8_t keyData[18]; |
| 291 | md4cpy(keyData, clientHashOrKadID); |
| 292 | PokeUInt16(keyData+16, randomKeyPart); |
| 293 | md5.Calculate(keyData, sizeof(keyData)); |
| 294 | //DEBUG_ONLY( DebugLog(_T("Creating obfuscated Kad packet encrypted by Hash/NodeID %s"), md4str(pachClientHashOrKadID)) ); |
| 295 | } |
| 296 | else { |
| 297 | delete [] cryptedBuffer; |
| 298 | wxFAIL; |
| 299 | return bufLen; |
| 300 | } |
| 301 | } else { |
| 302 | uint8_t keyData[23]; |
| 303 | md4cpy(keyData, clientHashOrKadID); |
| 304 | PokeUInt32(keyData+16, theApp->GetPublicIP()); |
| 305 | PokeUInt16(keyData+21, randomKeyPart); |
| 306 | keyData[20] = MAGICVALUE_UDP; |
| 307 | md5.Calculate(keyData, sizeof(keyData)); |
| 308 | } |
| 309 | |
| 310 | sendbuffer.SetKey(md5, true); |
| 311 | |
| 312 | // create the semi random byte encryption header |
| 313 | uint8_t semiRandomNotProtocolMarker = 0; |
| 314 | int i; |
| 315 | for (i = 0; i < 128; i++) { |
| 316 | semiRandomNotProtocolMarker = GetRandomUint8(); |
| 317 | semiRandomNotProtocolMarker = kad ? (semiRandomNotProtocolMarker & 0xFE) : (semiRandomNotProtocolMarker | 0x01); // set the ed2k/kad marker bit |
| 318 | if (kad) { |
| 319 | // set the ed2k/kad and nodeid/recvkey markerbit |
| 320 | semiRandomNotProtocolMarker = kadRecvKeyUsed ? ((semiRandomNotProtocolMarker & 0xFE) | 0x02) : (semiRandomNotProtocolMarker & 0xFC); |
| 321 | } else { |
nothing calls this directly
no test coverage detected