GenerateK0AndCp sketch

This commit is contained in:
merlokk 2021-05-29 16:20:48 +03:00
parent fa85078822
commit 37daaa2120
2 changed files with 44 additions and 6 deletions

View file

@ -23,12 +23,50 @@
uint8_t AESData0[CIPURSE_AES_KEY_LENGTH] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static void CipurseCGenerateK0AndGetCp(CipurseContext *ctx) {
static void CipurseCGenerateK0AndCp(CipurseContext *ctx) {
/* // session key derivation function
// kP := NLM(EXT(kID), rP)
// k0 := AES(key=PAD2(kP) XOR PAD(rT),kID) XOR kID
var temp1 = CryptoUtils.extFunction(kid, CIPURSE_SECURITY_PARAM_N) ?: return null
val kp = CryptoUtils.computeNLM(rP, temp1) ?: return null
temp1 = CryptoUtils.pad2(kp) ?: return null
val temp2 = CryptoUtils.pad(rT) ?: return null
temp1 = temp1 xor temp2
// session key K0
k0 = AesECB.aesEncrypt(temp1, kid) ?: return null
k0 = k0 xor kid
// first frame key k1, function to calculate k1,
// k1 := AES(key = RP; k0 XOR RT) XOR (k0 XOR RT)
temp1 = k0 xor RT
val temp3: ByteArray = AesECB.aesEncrypt(RP, temp1) ?: return null
frameKeyi = temp3 xor temp1
Log.d(TAG, "frame key=${Utils.toHex(frameKeyi)}")
// function to caluclate cP := AES(key=k0, RP).
// terminal response
return AesECB.aesEncrypt(k0, RP)*/
uint8_t temp1[CIPURSE_AES_KEY_LENGTH] = {0};
uint8_t temp2[CIPURSE_AES_KEY_LENGTH] = {0};
// session key derivation function
// kP := NLM(EXT(kID), rP)
// k0 := AES(key=PAD2(kP) XOR PAD(rT),kID) XOR kID
// session key K0
// first frame key k1, function to calculate k1,
// k1 := AES(key = RP; k0 XOR RT) XOR (k0 XOR RT)
// function to caluclate cP := AES(key=k0, RP).
// terminal response
aes_encode(NULL, ctx->k0, ctx->RP, ctx->Cp, CIPURSE_AES_KEY_LENGTH);
}
static void CipurseCGenerateCT(uint8_t *RT, uint8_t *CT) {
static void CipurseCGenerateCT(uint8_t *k0, uint8_t *RT, uint8_t *CT) {
aes_encode(NULL, k0, RT, CT, CIPURSE_AES_KEY_LENGTH);
}
void CipurseCGetKVV(uint8_t *key, uint8_t *kvv) {
@ -78,8 +116,8 @@ void CipurseCAuthenticateHost(CipurseContext *ctx, uint8_t *authdata) {
return;
CipurseCSetRandomHost(ctx);
CipurseCGenerateK0AndGetCp(ctx);
CipurseCGenerateCT(ctx->RT, ctx->CT);
CipurseCGenerateK0AndCp(ctx);
CipurseCGenerateCT(ctx->k0, ctx->RT, ctx->CT);
if (authdata != NULL)
CipurseCFillAuthData(ctx, authdata);

View file

@ -36,7 +36,7 @@ typedef struct CipurseContextS {
uint8_t RT[16];
uint8_t rT[6];
uint8_t frameKey0[16];
uint8_t k0[16];
uint8_t cP[16];
uint8_t CT[16];