|
|
@ -74,15 +74,18 @@ int Utils::encryptThenMAC(const uint8_t* shared_secret, uint8_t* dest, const uin |
|
|
int Utils::MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len) { |
|
|
int Utils::MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len) { |
|
|
if (src_len <= CIPHER_MAC_SIZE) return 0; // invalid src bytes
|
|
|
if (src_len <= CIPHER_MAC_SIZE) return 0; // invalid src bytes
|
|
|
|
|
|
|
|
|
|
|
|
int enc_len = src_len - CIPHER_MAC_SIZE; |
|
|
|
|
|
if (enc_len % CIPHER_BLOCK_SIZE != 0) return 0; // reject non-block-aligned ciphertext
|
|
|
|
|
|
|
|
|
uint8_t hmac[CIPHER_MAC_SIZE]; |
|
|
uint8_t hmac[CIPHER_MAC_SIZE]; |
|
|
{ |
|
|
{ |
|
|
SHA256 sha; |
|
|
SHA256 sha; |
|
|
sha.resetHMAC(shared_secret, PUB_KEY_SIZE); |
|
|
sha.resetHMAC(shared_secret, PUB_KEY_SIZE); |
|
|
sha.update(src + CIPHER_MAC_SIZE, src_len - CIPHER_MAC_SIZE); |
|
|
sha.update(src + CIPHER_MAC_SIZE, enc_len); |
|
|
sha.finalizeHMAC(shared_secret, PUB_KEY_SIZE, hmac, CIPHER_MAC_SIZE); |
|
|
sha.finalizeHMAC(shared_secret, PUB_KEY_SIZE, hmac, CIPHER_MAC_SIZE); |
|
|
} |
|
|
} |
|
|
if (memcmp(hmac, src, CIPHER_MAC_SIZE) == 0) { |
|
|
if (memcmp(hmac, src, CIPHER_MAC_SIZE) == 0) { |
|
|
return decrypt(shared_secret, dest, src + CIPHER_MAC_SIZE, src_len - CIPHER_MAC_SIZE); |
|
|
return decrypt(shared_secret, dest, src + CIPHER_MAC_SIZE, enc_len); |
|
|
} |
|
|
} |
|
|
return 0; // invalid HMAC
|
|
|
return 0; // invalid HMAC
|
|
|
} |
|
|
} |
|
|
|