Harden packet, retry, and alert handling
This commit is contained in:
1 parent
b8f438d6ea
commit
e3f47b487b
20 files changed
+451
-143
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "Utils.h"
|
||||
#include <SHA256.h>
|
||||
|
||||
using namespace mesh;
|
||||
|
||||
@@ -73,6 +74,40 @@ TEST(UtilsFromHex, RejectsWrongLength) {
|
||||
EXPECT_FALSE(Utils::fromHex(output, sizeof(output), "001122"));
|
||||
}
|
||||
|
||||
TEST(UtilsCrypto, RejectsNonBlockAlignedCiphertext) {
|
||||
uint8_t key[PUB_KEY_SIZE] = {};
|
||||
uint8_t plaintext[MAX_PACKET_PAYLOAD] = {};
|
||||
uint8_t ciphertext[15] = {};
|
||||
|
||||
EXPECT_EQ(0, Utils::decrypt(key, plaintext, ciphertext, sizeof(ciphertext)));
|
||||
|
||||
uint8_t framed[CIPHER_MAC_SIZE + sizeof(ciphertext)] = {};
|
||||
for (size_t i = 0; i < sizeof(ciphertext); i++) {
|
||||
framed[CIPHER_MAC_SIZE + i] = (uint8_t)(i + 1);
|
||||
}
|
||||
SHA256 sha;
|
||||
sha.resetHMAC(key, sizeof(key));
|
||||
sha.update(framed + CIPHER_MAC_SIZE, sizeof(ciphertext));
|
||||
sha.finalizeHMAC(key, sizeof(key), framed, CIPHER_MAC_SIZE);
|
||||
|
||||
EXPECT_EQ(0, Utils::MACThenDecrypt(key, plaintext, framed, sizeof(framed)));
|
||||
}
|
||||
|
||||
TEST(UtilsCrypto, AcceptsBlockAlignedCiphertextWithValidMac) {
|
||||
uint8_t key[PUB_KEY_SIZE] = {};
|
||||
uint8_t plaintext[MAX_PACKET_PAYLOAD] = {};
|
||||
uint8_t framed[CIPHER_MAC_SIZE + CIPHER_BLOCK_SIZE] = {};
|
||||
for (size_t i = 0; i < CIPHER_BLOCK_SIZE; i++) {
|
||||
framed[CIPHER_MAC_SIZE + i] = (uint8_t)(i + 1);
|
||||
}
|
||||
SHA256 sha;
|
||||
sha.resetHMAC(key, sizeof(key));
|
||||
sha.update(framed + CIPHER_MAC_SIZE, CIPHER_BLOCK_SIZE);
|
||||
sha.finalizeHMAC(key, sizeof(key), framed, CIPHER_MAC_SIZE);
|
||||
|
||||
EXPECT_EQ(CIPHER_BLOCK_SIZE, Utils::MACThenDecrypt(key, plaintext, framed, sizeof(framed)));
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
|
||||
Reference in new issue
Block a user