TestLRPPlaintexts
This commit is contained in:
1 parent
99bc47c1ac
commit
442bbd38b5
3 files changed
+117
-1
No files matched your search
@@ -26,3 +26,62 @@
|
||||
#include "aes.h"
|
||||
#include "commonutil.h"
|
||||
|
||||
static uint8_t constAA[] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
|
||||
static uint8_t const55[] = {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
|
||||
|
||||
void LRPClearContext(LRPContext *ctx) {
|
||||
memset(ctx->key, 0, CRYPTO_AES128_KEY_SIZE);
|
||||
|
||||
ctx->useBitPadding = false;
|
||||
ctx->plaintextsCount = 0;
|
||||
memset(ctx->plaintexts, 0, LRP_MAX_PLAINTEXTS_SIZE * CRYPTO_AES128_KEY_SIZE);
|
||||
ctx->updatedKeysCount = 0;
|
||||
memset(ctx->updatedKeys, 0, LRP_MAX_UPDATED_KEYS_SIZE * CRYPTO_AES128_KEY_SIZE);
|
||||
ctx->useUpdatedKeyNum = 0;
|
||||
}
|
||||
|
||||
void LRPSetKey(LRPContext *ctx, uint8_t *key, size_t updatedKeyNum, bool useBitPadding) {
|
||||
LRPClearContext(ctx);
|
||||
|
||||
memcpy(ctx->key, key, CRYPTO_AES128_KEY_SIZE);
|
||||
|
||||
LRPGeneratePlaintexts(ctx, 16);
|
||||
LRPGenerateUpdatedKeys(ctx, 4);
|
||||
|
||||
ctx->useUpdatedKeyNum = updatedKeyNum;
|
||||
ctx->useBitPadding = useBitPadding;
|
||||
}
|
||||
|
||||
// https://www.nxp.com/docs/en/application-note/AN12304.pdf
|
||||
// Algorithm 1
|
||||
void LRPGeneratePlaintexts(LRPContext *ctx, size_t plaintextsCount) {
|
||||
if (plaintextsCount > LRP_MAX_PLAINTEXTS_SIZE)
|
||||
return;
|
||||
|
||||
uint8_t h[CRYPTO_AES128_KEY_SIZE] = {0};
|
||||
memcpy(h, ctx->key, CRYPTO_AES128_KEY_SIZE);
|
||||
|
||||
for (int i = 0; i < plaintextsCount; i++) {
|
||||
aes_encode(NULL, h, const55, h, CRYPTO_AES128_KEY_SIZE);
|
||||
aes_encode(NULL, h, constAA, ctx->plaintexts[i], CRYPTO_AES128_KEY_SIZE);
|
||||
}
|
||||
|
||||
ctx->plaintextsCount = plaintextsCount;
|
||||
}
|
||||
|
||||
// https://www.nxp.com/docs/en/application-note/AN12304.pdf
|
||||
// Algorithm 2
|
||||
void LRPGenerateUpdatedKeys(LRPContext *ctx, size_t updatedKeysCount) {
|
||||
if (updatedKeysCount > LRP_MAX_UPDATED_KEYS_SIZE)
|
||||
return;
|
||||
|
||||
uint8_t h[CRYPTO_AES128_KEY_SIZE] = {0};
|
||||
aes_encode(NULL, ctx->key, const55, h, CRYPTO_AES128_KEY_SIZE);
|
||||
|
||||
for (int i = 0; i < updatedKeysCount; i++) {
|
||||
aes_encode(NULL, h, constAA, ctx->plaintexts[i], CRYPTO_AES128_KEY_SIZE);
|
||||
aes_encode(NULL, h, const55, h, CRYPTO_AES128_KEY_SIZE);
|
||||
}
|
||||
|
||||
ctx->updatedKeysCount = updatedKeysCount;
|
||||
}
|
||||
Reference in new issue
Block a user