Daniel Novak
18 hours ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
6 additions and
3 deletions
-
src/Utils.cpp
-
src/Utils.h
|
|
|
@ -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) { |
|
|
|
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]; |
|
|
|
{ |
|
|
|
SHA256 sha; |
|
|
|
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); |
|
|
|
} |
|
|
|
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
|
|
|
|
} |
|
|
|
|
|
|
|
@ -50,7 +50,7 @@ public: |
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief checks the MAC (in leading bytes of 'src'), then if valid, decrypts remaining bytes in src. |
|
|
|
* \returns zero if MAC is invalid, otherwise the length of decrypted bytes in 'dest' |
|
|
|
* \returns zero if MAC is invalid or ciphertext is not block-aligned, otherwise the length of decrypted bytes in 'dest' |
|
|
|
*/ |
|
|
|
static int MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len); |
|
|
|
|
|
|
|
|