* derive_key() now uses the one HKDF operation to output 2 keys, one for AES cipher, one for HMAC

This commit is contained in:
Scott Powell committed 2026-11-13 19:35:31 +11:00
1 parent 8c5bdc15df
commit 5ab9f837f8
2 files changed
+54 -6

No files matched your search

+1
View File
@@ -16,6 +16,7 @@
// V1
#define CIPHER_MAC_SIZE 2
#define PATH_HASH_SIZE 1
#define NEW_CIPHER_MAC_SIZE 3
#define MAX_PACKET_PAYLOAD 184
#define MAX_GROUP_DATA_LENGTH (MAX_PACKET_PAYLOAD - CIPHER_BLOCK_SIZE - 3)
+53 -6
View File
@@ -175,26 +175,73 @@ int Utils::MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uin
return 0; // invalid HMAC
}
struct CompositeKey {
uint8_t cipher_key[CIPHER_KEY_SIZE];
uint8_t hmac_key[PUB_KEY_SIZE];
};
// Derive a key with HKDF-SHA256
static void derive_key(uint8_t* out, const uint8_t* key, size_t key_len, const uint8_t* iv, size_t iv_len) {
hkdf<SHA256>(out, key_len,
static void derive_key(CompositeKey& out, const uint8_t* key, size_t key_len, const uint8_t* iv, size_t iv_len) {
hkdf<SHA256>(&out, sizeof(out),
key, key_len, // IKM
nullptr, 0, // salt
iv, iv_len); // info
}
int Utils::encryptThenMAC(const uint8_t* shared_secret, const uint8_t* iv, int iv_len, uint8_t* dest, const uint8_t* src, int src_len) {
uint8_t derived[PUB_KEY_SIZE];
CompositeKey derived;
derive_key(derived, shared_secret, PUB_KEY_SIZE, iv, iv_len);
return Utils::encryptThenMAC(derived, dest, src, src_len);
int enc_len = encrypt(derived.cipher_key, dest + NEW_CIPHER_MAC_SIZE, src, src_len);
#ifdef USE_CC310_HW_CRYPTO
static CRYS_HMACUserContext_t hmac_ctx;
static CRYS_HASH_Result_t hmac_result;
CRYS_HMAC_Init(&hmac_ctx, CRYS_HASH_SHA256_mode, (uint8_t*)derived.hmac_key, PUB_KEY_SIZE);
CRYS_HMAC_Update(&hmac_ctx, dest + NEW_CIPHER_MAC_SIZE, enc_len);
CRYS_HMAC_Finish(&hmac_ctx, hmac_result);
memcpy(dest, hmac_result, NEW_CIPHER_MAC_SIZE);
#else
SHA256 sha;
sha.resetHMAC(derived.hmac_key, PUB_KEY_SIZE);
sha.update(dest + NEW_CIPHER_MAC_SIZE, enc_len);
sha.finalizeHMAC(derived.hmac_key, PUB_KEY_SIZE, dest, NEW_CIPHER_MAC_SIZE);
#endif
return NEW_CIPHER_MAC_SIZE + enc_len;
}
int Utils::MACThenDecrypt(const uint8_t* shared_secret, const uint8_t* iv, int iv_len, uint8_t* dest, const uint8_t* src, int src_len) {
uint8_t derived[PUB_KEY_SIZE];
CompositeKey derived;
derive_key(derived, shared_secret, PUB_KEY_SIZE, iv, iv_len);
return Utils::MACThenDecrypt(derived, dest, src, src_len);
if (src_len <= NEW_CIPHER_MAC_SIZE) return 0; // invalid src bytes
int enc_len = src_len - NEW_CIPHER_MAC_SIZE;
if (enc_len % CIPHER_BLOCK_SIZE != 0) return 0; // reject non-block-aligned ciphertext
uint8_t hmac[NEW_CIPHER_MAC_SIZE];
#ifdef USE_CC310_HW_CRYPTO
{
static CRYS_HMACUserContext_t hmac_ctx;
static CRYS_HASH_Result_t hmac_result;
CRYS_HMAC_Init(&hmac_ctx, CRYS_HASH_SHA256_mode, (uint8_t*)derived.hmac_key, PUB_KEY_SIZE);
CRYS_HMAC_Update(&hmac_ctx, (uint8_t*)(src + NEW_CIPHER_MAC_SIZE), src_len - NEW_CIPHER_MAC_SIZE);
CRYS_HMAC_Finish(&hmac_ctx, hmac_result);
memcpy(hmac, hmac_result, NEW_CIPHER_MAC_SIZE);
}
#else
{
SHA256 sha;
sha.resetHMAC(derived.hmac_key, PUB_KEY_SIZE);
sha.update(src + NEW_CIPHER_MAC_SIZE, enc_len);
sha.finalizeHMAC(derived.hmac_key, PUB_KEY_SIZE, hmac, NEW_CIPHER_MAC_SIZE);
}
#endif
if (memcmp(hmac, src, NEW_CIPHER_MAC_SIZE) == 0) {
return decrypt(derived.cipher_key, dest, src + NEW_CIPHER_MAC_SIZE, enc_len);
}
return 0; // invalid HMAC
}
static const char hex_chars[] = "0123456789ABCDEF";