From 37daaa2120960f70d58f670b0e3246a5754d6f21 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sat, 29 May 2021 16:20:48 +0300 Subject: [PATCH] GenerateK0AndCp sketch --- client/src/cipurse/cipursecrypto.c | 48 ++++++++++++++++++++++++++---- client/src/cipurse/cipursecrypto.h | 2 +- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/client/src/cipurse/cipursecrypto.c b/client/src/cipurse/cipursecrypto.c index 90d6f1722..996d43b1e 100644 --- a/client/src/cipurse/cipursecrypto.c +++ b/client/src/cipurse/cipursecrypto.c @@ -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); diff --git a/client/src/cipurse/cipursecrypto.h b/client/src/cipurse/cipursecrypto.h index f7b330280..c2f8c2de4 100644 --- a/client/src/cipurse/cipursecrypto.h +++ b/client/src/cipurse/cipursecrypto.h @@ -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];