| 268 | |
| 269 | |
| 270 | bool DecryptBuffer |
| 271 | ( |
| 272 | uint8 *e_buffer, |
| 273 | uint8 e_length, |
| 274 | Driver *driver, |
| 275 | uint8 const _sendingNode, |
| 276 | uint8 const _receivingNode, |
| 277 | uint8 const m_nonce[8], |
| 278 | uint8* m_buffer |
| 279 | ) |
| 280 | { |
| 281 | PrintHex("Raw", e_buffer, e_length); |
| 282 | |
| 283 | if (e_length < 19) { |
| 284 | Log::Write(LogLevel_Warning, _sendingNode, "Received a Encrypted Message that is too Short. Dropping it"); |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | uint8 iv[17]; |
| 290 | createIVFromPacket_inbound(&e_buffer[2], m_nonce, iv); /* first 8 bytes of Packet are the Random Value generated by the Device |
| 291 | * 2nd 8 bytes of the IV are our nonce we sent previously |
| 292 | */ |
| 293 | memset(&m_buffer[0], 0, 32); |
| 294 | uint32 encryptedpacketsize = e_length - 8 - 8 - 2 - 2; |
| 295 | |
| 296 | /* if the Encrypted Packet Size is less than 3, there is probably a issue, drop it. */ |
| 297 | if (encryptedpacketsize < 3) { |
| 298 | Log::Write(LogLevel_Warning, _sendingNode, "Encrypted Packet Size is Less than 3 Bytes. Dropping"); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | uint8 encyptedpacket[32]; |
| 304 | |
| 305 | for (uint32 i = 0; i < 32; i++) { |
| 306 | if (i >= encryptedpacketsize) { |
| 307 | /* pad the remaining fields */ |
| 308 | encyptedpacket[i] = 0; |
| 309 | } else { |
| 310 | encyptedpacket[i] = e_buffer[10+i]; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | #ifdef DEBUG |
| 316 | Log::Write(LogLevel_Debug, _sendingNode, "Encrypted Packet Sizes: %d (Total) %d (Payload)", e_length, encryptedpacketsize); |
| 317 | PrintHex("IV", iv, 16); |
| 318 | PrintHex("Encrypted", encyptedpacket, 16); |
| 319 | /* Mac Starts after Encrypted Packet. */ |
| 320 | PrintHex("Auth", &e_buffer[11+encryptedpacketsize], 8); |
| 321 | #endif |
| 322 | aes_mode_reset(driver->GetEncKey()); |
| 323 | #if 0 |
| 324 | uint8_t iv[16] = { 0x81, 0x42, 0xd1, 0x51, 0xf1, 0x59, 0x3d, 0x70, 0xd5, 0xe3, 0x6c, 0xcb, 0x02, 0xd0, 0x3f, 0x5c, /* */ }; |
| 325 | uint8_t pck[] = { 0x25, 0x68, 0x06, 0xc5, 0xb3, 0xee, 0x2c, 0x17, 0x26, 0x7e, 0xf0, 0x84, 0xd4, 0xc3, 0xba, 0xed, 0xe5, 0xb9, 0x55}; |
| 326 | if (aes_ofb_decrypt(pck, decryptpacket, 19, iv, this->EncryptKey) == EXIT_FAILURE) { |
| 327 | Log::Write(LogLevel_Warning, GetNodeId(), "Failed to Decrypt Packet"); |
no test coverage detected