Merge pull request #53 from merlokk/des

get rid of polarssl's Des
This commit is contained in:
RFID Research Group
2018-11-14 13:48:33 +01:00
committed by GitHub
11 changed files with 39 additions and 3514 deletions

View File

@@ -108,7 +108,6 @@ CMDSRCS = crapto1/crapto1.c \
mfkey.c \
tea.c \
fido/additional_ca.c \
polarssl/des.c \
crypto/libpcrypto.c\
crypto/asn1utils.c\
cliparser/argtable3.c\

View File

@@ -766,8 +766,8 @@ int CmdHFiClassDecrypt(const char *Cmd) {
hdr->csn[4],hdr->csn[5],hdr->csn[6],hdr->csn[7]);
// tripledes
des3_context ctx = { DES_DECRYPT ,{ 0 } };
des3_set2key_dec( &ctx, key);
mbedtls_des3_context ctx = { 0 };
mbedtls_des3_set2key_dec( &ctx, key);
uint8_t enc_dump[8] = {0};
uint8_t empty[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
@@ -778,7 +778,7 @@ int CmdHFiClassDecrypt(const char *Cmd) {
// block 7 or higher, and not empty 0xFF
if(blocknum > 6 && memcmp(enc_dump, empty, 8) != 0 ) {
des3_crypt_ecb(&ctx, enc_dump, decrypted + idx );
mbedtls_des3_crypt_ecb(&ctx, enc_dump, decrypted + idx );
}
}
@@ -797,10 +797,10 @@ static int iClassEncryptBlkData(uint8_t *blkData) {
PrintAndLogEx(SUCCESS, "decryption file found");
uint8_t encryptedData[16];
uint8_t *encrypted = encryptedData;
des3_context ctx = { DES_DECRYPT ,{ 0 } };
des3_set2key_enc( &ctx, key);
mbedtls_des3_context ctx = { 0 };
mbedtls_des3_set2key_enc( &ctx, key);
des3_crypt_ecb(&ctx, blkData,encrypted);
mbedtls_des3_crypt_ecb(&ctx, blkData,encrypted);
memcpy(blkData,encrypted,8);
return 1;
}

View File

@@ -22,7 +22,7 @@
#include "common.h"
#include "util.h"
#include "comms.h"
#include "des.h"
#include "mbedtls/des.h"
#include "loclass/cipherutils.h"
#include "loclass/cipher.h"
#include "loclass/ikeys.h"

View File

@@ -2441,11 +2441,11 @@ int CmdHF14AMfuGenDiverseKeys(const char *Cmd){
mix[6] = block ^ uid[2];
mix[7] = uid[3];
des3_context ctx = { 0x00 };
des3_set2key_enc(&ctx, masterkey);
mbedtls_des3_context ctx = { 0x00 };
mbedtls_des3_set2key_enc(&ctx, masterkey);
des3_crypt_cbc(&ctx // des3_context
, DES_ENCRYPT // int mode
mbedtls_des3_crypt_cbc(&ctx // des3_context
, MBEDTLS_DES_ENCRYPT // int mode
, sizeof(mix) // length
, iv // iv[8]
, mix // input
@@ -2478,10 +2478,10 @@ int CmdHF14AMfuGenDiverseKeys(const char *Cmd){
memcpy(dmkey+16, dkeyA, 8);
memset(iv, 0x00, 8);
des3_set3key_enc(&ctx, dmkey);
mbedtls_des3_set3key_enc(&ctx, dmkey);
des3_crypt_cbc(&ctx // des3_context
, DES_ENCRYPT // int mode
mbedtls_des3_crypt_cbc(&ctx // des3_context
, MBEDTLS_DES_ENCRYPT // int mode
, sizeof(newpwd) // length
, iv // iv[8]
, zeros // input

View File

@@ -3,7 +3,7 @@
#include <stdint.h>
#include <stdio.h>
#include "des.h"
#include "mbedtls/des.h"
#include "cmdhfmf.h"
#include "cmdhf14a.h"
#include "mifare.h"

View File

@@ -45,7 +45,7 @@
#include "ikeys.h"
#include "elite_crack.h"
#include "fileutils.h"
#include "des.h"
#include "mbedtls/des.h"
#include "util_posix.h"
/**
@@ -172,21 +172,21 @@ void rk(uint8_t *key, uint8_t n, uint8_t *outp_key) {
return;
}
static des_context ctx_enc = {DES_ENCRYPT,{0}};
static des_context ctx_dec = {DES_DECRYPT,{0}};
static mbedtls_des_context ctx_enc = {0};
static mbedtls_des_context ctx_dec = {0};
void desdecrypt_iclass(uint8_t *iclass_key, uint8_t *input, uint8_t *output) {
uint8_t key_std_format[8] = {0};
permutekey_rev(iclass_key, key_std_format);
des_setkey_dec( &ctx_dec, key_std_format);
des_crypt_ecb(&ctx_dec,input,output);
mbedtls_des_setkey_dec( &ctx_dec, key_std_format);
mbedtls_des_crypt_ecb(&ctx_dec,input,output);
}
void desencrypt_iclass(uint8_t *iclass_key, uint8_t *input, uint8_t *output) {
uint8_t key_std_format[8] = {0};
permutekey_rev(iclass_key, key_std_format);
des_setkey_enc( &ctx_enc, key_std_format);
des_crypt_ecb(&ctx_enc,input,output);
mbedtls_des_setkey_enc( &ctx_enc, key_std_format);
mbedtls_des_crypt_ecb(&ctx_enc,input,output);
}
/**
@@ -431,7 +431,7 @@ int bruteforceItem(dumpdata item, uint16_t keytable[]) {
* @return 0 for ok, 1 for failz
*/
int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[] ){
des_context ctx_e = {DES_ENCRYPT,{0}};
mbedtls_des_context ctx_e = {0};
uint8_t z_0[8] = {0};
uint8_t y_0[8] = {0};
@@ -450,8 +450,8 @@ int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[] ){
permutekey_rev(z_0, z_0_rev);
// ~K_cus = DESenc(z[0], y[0])
des_setkey_enc( &ctx_e, z_0_rev );
des_crypt_ecb(&ctx_e, y_0, key64_negated);
mbedtls_des_setkey_enc( &ctx_e, z_0_rev );
mbedtls_des_crypt_ecb(&ctx_e, y_0, key64_negated);
int i;
for (i = 0; i < 8 ; i++)
@@ -462,8 +462,8 @@ int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[] ){
uint8_t key64_stdformat[8] = {0};
permutekey_rev(key64, key64_stdformat);
des_setkey_enc( &ctx_e, key64_stdformat );
des_crypt_ecb(&ctx_e, key64_negated, result);
mbedtls_des_setkey_enc( &ctx_e, key64_stdformat );
mbedtls_des_crypt_ecb(&ctx_e, key64_negated, result);
PrintAndLogDevice(NORMAL, "\n"); PrintAndLogDevice(SUCCESS, "-- High security custom key (Kcus) --");
printvar("[+] Standard format ", key64_stdformat, 8);
printvar("[+] iClass format ", key64, 8);

View File

@@ -65,12 +65,12 @@ From "Dismantling iclass":
#include <inttypes.h>
#include "fileutils.h"
#include "cipherutils.h"
#include "des.h"
#include "mbedtls/des.h"
uint8_t pi[35] = {0x0F,0x17,0x1B,0x1D,0x1E,0x27,0x2B,0x2D,0x2E,0x33,0x35,0x39,0x36,0x3A,0x3C,0x47,0x4B,0x4D,0x4E,0x53,0x55,0x56,0x59,0x5A,0x5C,0x63,0x65,0x66,0x69,0x6A,0x6C,0x71,0x72,0x74,0x78};
static des_context ctx_enc = {DES_ENCRYPT,{0}};
static des_context ctx_dec = {DES_DECRYPT,{0}};
static mbedtls_des_context ctx_enc = {0};
static mbedtls_des_context ctx_dec = {0};
static int debug_print = 0;
@@ -370,12 +370,12 @@ void hash0(uint64_t c, uint8_t k[8])
void diversifyKey(uint8_t csn[8], uint8_t key[8], uint8_t div_key[8])
{
// Prepare the DES key
des_setkey_enc( &ctx_enc, key);
mbedtls_des_setkey_enc( &ctx_enc, key);
uint8_t crypted_csn[8] = {0};
// Calculate DES(CSN, KEY)
des_crypt_ecb(&ctx_enc,csn, crypted_csn);
mbedtls_des_crypt_ecb(&ctx_enc,csn, crypted_csn);
//Calculate HASH0(DES))
uint64_t crypt_csn = x_bytes_to_num(crypted_csn, 8);
@@ -437,13 +437,13 @@ typedef struct
uint8_t div_key[8];
} Testcase;
int testDES(Testcase testcase, des_context ctx_enc, des_context ctx_dec)
int testDES(Testcase testcase, mbedtls_des_context ctx_enc, mbedtls_des_context ctx_dec)
{
uint8_t des_encrypted_csn[8] = {0};
uint8_t decrypted[8] = {0};
uint8_t div_key[8] = {0};
int retval = des_crypt_ecb(&ctx_enc,testcase.uid,des_encrypted_csn);
retval |= des_crypt_ecb(&ctx_dec,des_encrypted_csn,decrypted);
int retval = mbedtls_des_crypt_ecb(&ctx_enc,testcase.uid,des_encrypted_csn);
retval |= mbedtls_des_crypt_ecb(&ctx_dec,des_encrypted_csn,decrypted);
if(memcmp(testcase.uid,decrypted,8) != 0)
{
@@ -629,7 +629,7 @@ int testDES2(uint64_t csn, uint64_t expected) {
print64bits(" csn ", csn);
x_num_to_bytes(csn, 8,input);
des_crypt_ecb(&ctx_enc,input, result);
mbedtls_des_crypt_ecb(&ctx_enc,input, result);
uint64_t crypt_csn = x_bytes_to_num(result, 8);
print64bits(" {csn} ", crypt_csn );
@@ -654,7 +654,7 @@ int doTestsWithKnownInputs() {
PrintAndLogDevice(SUCCESS, "Testing DES encryption");
uint8_t key[8] = {0x6c,0x8d,0x44,0xf9,0x2a,0x2d,0x01,0xbf};
des_setkey_enc( &ctx_enc, key);
mbedtls_des_setkey_enc( &ctx_enc, key);
testDES2(0xbbbbaaaabbbbeeee,0xd6ad3ca619659e6b);
PrintAndLogDevice(SUCCESS, "Testing hashing algorithm");
@@ -712,8 +712,8 @@ int doKeyTests(uint8_t debuglevel) {
PrintAndLogDevice(SUCCESS, "Key present");
PrintAndLogDevice(SUCCESS, "Checking key parity...");
des_checkParity(key);
des_setkey_enc( &ctx_enc, key);
des_setkey_dec( &ctx_dec, key);
mbedtls_des_setkey_enc( &ctx_enc, key);
mbedtls_des_setkey_dec( &ctx_dec, key);
// Test hashing functions
PrintAndLogDevice(SUCCESS, "The following tests require the correct 8-byte master key");
testKeyDiversificationWithMasterkeyTestcases();