diff --git a/client/src/cipurse/cipursecrypto.c b/client/src/cipurse/cipursecrypto.c index 16917b7aa..90d6f1722 100644 --- a/client/src/cipurse/cipursecrypto.c +++ b/client/src/cipurse/cipursecrypto.c @@ -37,24 +37,24 @@ void CipurseCGetKVV(uint8_t *key, uint8_t *kvv) { memcpy(kvv, res, CIPURSE_KVV_LENGTH); } -void CipurseClearContext(CipurseContext *ctx) { +void CipurseCClearContext(CipurseContext *ctx) { if (ctx == NULL) return; memset(ctx, 0, sizeof(CipurseContext)); } -void CipurseSetKey(CipurseContext *ctx, uint8_t keyId, uint8_t *key) { +void CipurseCSetKey(CipurseContext *ctx, uint8_t keyId, uint8_t *key) { if (ctx == NULL) return; - CipurseClearContext(ctx); + CipurseCClearContext(ctx); ctx->keyId = keyId; memcpy(ctx->key, key, member_size(CipurseContext, key)); } -void CipurseSetRandomFromPICC(CipurseContext *ctx, uint8_t *random) { +void CipurseCSetRandomFromPICC(CipurseContext *ctx, uint8_t *random) { if (ctx == NULL) return; @@ -62,16 +62,25 @@ void CipurseSetRandomFromPICC(CipurseContext *ctx, uint8_t *random) { memcpy(ctx->rP, random + member_size(CipurseContext, RP), member_size(CipurseContext, rP)); } -void CipurseSetRandomHost(CipurseContext *ctx) { +void CipurseCSetRandomHost(CipurseContext *ctx) { memset(ctx->RT, 0x10, member_size(CipurseContext, RT)); memset(ctx->rT, 0x20, member_size(CipurseContext, rT)); } -void CipurseAuthenticateHost(CipurseContext *ctx) { +static void CipurseCFillAuthData(CipurseContext *ctx, uint8_t *authdata) { + memcpy(authdata, ctx->cP, member_size(CipurseContext, cP)); + memcpy(&authdata[member_size(CipurseContext, cP)], ctx->RT, member_size(CipurseContext, RT)); + memcpy(&authdata[member_size(CipurseContext, cP) + member_size(CipurseContext, RT)], ctx->rT, member_size(CipurseContext, rT)); +} + +void CipurseCAuthenticateHost(CipurseContext *ctx, uint8_t *authdata) { if (ctx == NULL) return; - CipurseSetRandomHost(ctx); + CipurseCSetRandomHost(ctx); CipurseCGenerateK0AndGetCp(ctx); - CipurseCGenerateCT(ctx->RT, ctx->CT); + CipurseCGenerateCT(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 04e3d27b5..f7b330280 100644 --- a/client/src/cipurse/cipursecrypto.h +++ b/client/src/cipurse/cipursecrypto.h @@ -44,12 +44,12 @@ typedef struct CipurseContextS { uint8_t frameKeyNext[CIPURSE_AES_KEY_LENGTH]; } CipurseContext; -void CipurseClearContext(CipurseContext *ctx); -void CipurseSetKey(CipurseContext *ctx, uint8_t keyId, uint8_t *key); -void CipurseSetRandomFromPICC(CipurseContext *ctx, uint8_t *random); -void CipurseSetRandomHost(CipurseContext *ctx); +void CipurseCClearContext(CipurseContext *ctx); +void CipurseCSetKey(CipurseContext *ctx, uint8_t keyId, uint8_t *key); +void CipurseCSetRandomFromPICC(CipurseContext *ctx, uint8_t *random); +void CipurseCSetRandomHost(CipurseContext *ctx); -void CipurseAuthenticateHost(CipurseContext *ctx); +void CipurseCAuthenticateHost(CipurseContext *ctx, uint8_t *authdata); void CipurseCGetKVV(uint8_t *key, uint8_t *kvv); diff --git a/client/src/cmdhfcipurse.c b/client/src/cmdhfcipurse.c index cac5ab03c..d693f2295 100644 --- a/client/src/cmdhfcipurse.c +++ b/client/src/cmdhfcipurse.c @@ -95,24 +95,35 @@ static int CmdHFCipurseAuth(const char *Cmd) { return PM3_ESOFT; } + uint8_t keyId = 1; uint8_t key[] = {0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73}; CipurseContext ctx = {0}; - CipurseSetKey(&ctx, 1, key); + CipurseCSetKey(&ctx, 1, key); uint8_t kvv[CIPURSE_KVV_LENGTH] = {0}; CipurseCGetKVV(key, kvv); PrintAndLogEx(INFO, "Key: %s KVV: %s", sprint_hex(key, CIPURSE_AES_KEY_LENGTH), sprint_hex_inrow(kvv, CIPURSE_KVV_LENGTH)); + // get RP, rP res = CIPURSEChallenge(buf, sizeof(buf), &len, &sw); if (res != 0 || len != 0x16) { PrintAndLogEx(ERR, "Cipurse get challenge error. Card returns 0x%04x.", sw); DropField(); return PM3_ESOFT; } - CipurseSetRandomFromPICC(&ctx, buf); - + CipurseCSetRandomFromPICC(&ctx, buf); + // make auth data + uint8_t authparams[16 + 16 + 6] = {0}; + CipurseCAuthenticateHost(&ctx, authparams); + // authenticate + res = CIPURSEMutalAuthenticate(keyId, authparams, sizeof(authparams), buf, sizeof(buf), &len, &sw); + if (res != 0 || sw != 0x9000 || len != 0x16) { + PrintAndLogEx(ERR, "Cipurse authentication error. Card returns 0x%04x.", sw); + DropField(); + return PM3_ESOFT; + } DropField(); return PM3_SUCCESS;