changing {} style to match majority of previous style
This commit is contained in:
@@ -67,8 +67,7 @@ typedef struct {
|
||||
* is defined as
|
||||
* T (x 0 x 1 . . . . . . x 15 ) = x 0 ⊕ x 1 ⊕ x 5 ⊕ x 7 ⊕ x 10 ⊕ x 11 ⊕ x 14 ⊕ x 15 .
|
||||
**/
|
||||
bool T(State state)
|
||||
{
|
||||
bool T(State state) {
|
||||
bool x0 = state.t & 0x8000;
|
||||
bool x1 = state.t & 0x4000;
|
||||
bool x5 = state.t & 0x0400;
|
||||
@@ -83,8 +82,7 @@ bool T(State state)
|
||||
* Similarly, the feedback function for the bottom register B : F 8/2 → F 2 is defined as
|
||||
* B(x 0 x 1 . . . x 7 ) = x 1 ⊕ x 2 ⊕ x 3 ⊕ x 7 .
|
||||
**/
|
||||
bool B(State state)
|
||||
{
|
||||
bool B(State state) {
|
||||
bool x1 = state.b & 0x40;
|
||||
bool x2 = state.b & 0x20;
|
||||
bool x3 = state.b & 0x10;
|
||||
@@ -102,8 +100,7 @@ bool B(State state)
|
||||
* z 1 = (r 0 ∨ r 2 ) ⊕ (r 5 ∨ r 7 ) ⊕ r 1 ⊕ r 6 ⊕ x ⊕ y
|
||||
* z 2 = (r 3 ∧ r 5 ) ⊕ (r 4 ∧ r 6 ) ⊕ r 7 ⊕ x
|
||||
**/
|
||||
uint8_t _select(bool x, bool y, uint8_t r)
|
||||
{
|
||||
uint8_t _select(bool x, bool y, uint8_t r) {
|
||||
bool r0 = r >> 7 & 0x1;
|
||||
bool r1 = r >> 6 & 0x1;
|
||||
bool r2 = r >> 5 & 0x1;
|
||||
@@ -139,8 +136,7 @@ uint8_t _select(bool x, bool y, uint8_t r)
|
||||
* @param s - state
|
||||
* @param k - array containing 8 bytes
|
||||
**/
|
||||
State successor(uint8_t *k, State s, bool y)
|
||||
{
|
||||
State successor(uint8_t *k, State s, bool y) {
|
||||
bool r0 = s.r >> 7 & 0x1;
|
||||
bool r4 = s.r >> 3 & 0x1;
|
||||
bool r7 = s.r & 0x1;
|
||||
@@ -166,8 +162,7 @@ State successor(uint8_t *k, State s, bool y)
|
||||
* to multiple bit input x ∈ F n 2 which we define as
|
||||
* @param k - array containing 8 bytes
|
||||
**/
|
||||
State suc(uint8_t *k, State s, BitstreamIn *bitstream)
|
||||
{
|
||||
State suc(uint8_t *k, State s, BitstreamIn *bitstream) {
|
||||
if (bitsLeft(bitstream) == 0) {
|
||||
return s;
|
||||
}
|
||||
@@ -183,8 +178,7 @@ State suc(uint8_t *k, State s, BitstreamIn *bitstream)
|
||||
* output(k, s, x 0 . . . x n ) = output(s) · output(k, s ′ , x 1 . . . x n )
|
||||
* where s ′ = suc(k, s, x 0 ).
|
||||
**/
|
||||
void output(uint8_t *k, State s, BitstreamIn *in, BitstreamOut *out)
|
||||
{
|
||||
void output(uint8_t *k, State s, BitstreamIn *in, BitstreamOut *out) {
|
||||
if (bitsLeft(in) == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -200,8 +194,7 @@ void output(uint8_t *k, State s, BitstreamIn *in, BitstreamOut *out)
|
||||
* key k ∈ (F 82 ) 8 and outputs the initial cipher state s =< l, r, t, b >
|
||||
**/
|
||||
|
||||
State init(uint8_t *k)
|
||||
{
|
||||
State init(uint8_t *k) {
|
||||
State s = {
|
||||
((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
|
||||
((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
|
||||
@@ -210,16 +203,14 @@ State init(uint8_t *k)
|
||||
};
|
||||
return s;
|
||||
}
|
||||
void MAC(uint8_t *k, BitstreamIn input, BitstreamOut out)
|
||||
{
|
||||
void MAC(uint8_t *k, BitstreamIn input, BitstreamOut out) {
|
||||
uint8_t zeroes_32[] = {0, 0, 0, 0};
|
||||
BitstreamIn input_32_zeroes = {zeroes_32, sizeof(zeroes_32) * 8, 0};
|
||||
State initState = suc(k, init(k), &input);
|
||||
output(k, initState, &input_32_zeroes, &out);
|
||||
}
|
||||
|
||||
void doMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4])
|
||||
{
|
||||
void doMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4]) {
|
||||
uint8_t cc_nr[13] = { 0 };
|
||||
uint8_t div_key[8];
|
||||
//cc_nr=(uint8_t*) calloc(length+1, sizeof(uint8_t));
|
||||
@@ -238,8 +229,7 @@ void doMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4])
|
||||
//free(cc_nr);
|
||||
return;
|
||||
}
|
||||
void doMAC_N(uint8_t *address_data_p, uint8_t address_data_size, uint8_t *div_key_p, uint8_t mac[4])
|
||||
{
|
||||
void doMAC_N(uint8_t *address_data_p, uint8_t address_data_size, uint8_t *div_key_p, uint8_t mac[4]) {
|
||||
uint8_t *address_data;
|
||||
uint8_t div_key[8];
|
||||
address_data = (uint8_t *) calloc(address_data_size, sizeof(uint8_t));
|
||||
@@ -260,8 +250,7 @@ void doMAC_N(uint8_t *address_data_p, uint8_t address_data_size, uint8_t *div_ke
|
||||
}
|
||||
|
||||
#ifndef ON_DEVICE
|
||||
int testMAC()
|
||||
{
|
||||
int testMAC() {
|
||||
PrintAndLogDevice(SUCCESS, "Testing MAC calculation...");
|
||||
|
||||
//From the "dismantling.IClass" paper:
|
||||
|
||||
@@ -46,8 +46,7 @@
|
||||
* @param stream
|
||||
* @return
|
||||
*/
|
||||
bool headBit(BitstreamIn *stream)
|
||||
{
|
||||
bool headBit(BitstreamIn *stream) {
|
||||
int bytepos = stream->position >> 3; // divide by 8
|
||||
int bitpos = (stream->position++) & 7; // mask out 00000111
|
||||
return (*(stream->buffer + bytepos) >> (7 - bitpos)) & 1;
|
||||
@@ -57,8 +56,7 @@ bool headBit(BitstreamIn *stream)
|
||||
* @param stream
|
||||
* @return
|
||||
*/
|
||||
bool tailBit(BitstreamIn *stream)
|
||||
{
|
||||
bool tailBit(BitstreamIn *stream) {
|
||||
int bitpos = stream->numbits - 1 - (stream->position++);
|
||||
|
||||
int bytepos = bitpos >> 3;
|
||||
@@ -70,8 +68,7 @@ bool tailBit(BitstreamIn *stream)
|
||||
* @param stream
|
||||
* @param bit
|
||||
*/
|
||||
void pushBit(BitstreamOut *stream, bool bit)
|
||||
{
|
||||
void pushBit(BitstreamOut *stream, bool bit) {
|
||||
int bytepos = stream->position >> 3; // divide by 8
|
||||
int bitpos = stream->position & 7;
|
||||
*(stream->buffer + bytepos) |= (bit & 1) << (7 - bitpos);
|
||||
@@ -85,8 +82,7 @@ void pushBit(BitstreamOut *stream, bool bit)
|
||||
* @param stream
|
||||
* @param bits
|
||||
*/
|
||||
void push6bits(BitstreamOut *stream, uint8_t bits)
|
||||
{
|
||||
void push6bits(BitstreamOut *stream, uint8_t bits) {
|
||||
pushBit(stream, bits & 0x20);
|
||||
pushBit(stream, bits & 0x10);
|
||||
pushBit(stream, bits & 0x08);
|
||||
@@ -100,8 +96,7 @@ void push6bits(BitstreamOut *stream, uint8_t bits)
|
||||
* @param stream
|
||||
* @return number of bits left in stream
|
||||
*/
|
||||
int bitsLeft(BitstreamIn *stream)
|
||||
{
|
||||
int bitsLeft(BitstreamIn *stream) {
|
||||
return stream->numbits - stream->position;
|
||||
}
|
||||
/**
|
||||
@@ -109,21 +104,18 @@ int bitsLeft(BitstreamIn *stream)
|
||||
* @param stream
|
||||
* @return Number of bits stored in stream
|
||||
*/
|
||||
int numBits(BitstreamOut *stream)
|
||||
{
|
||||
int numBits(BitstreamOut *stream) {
|
||||
return stream->numbits;
|
||||
}
|
||||
|
||||
void x_num_to_bytes(uint64_t n, size_t len, uint8_t *dest)
|
||||
{
|
||||
void x_num_to_bytes(uint64_t n, size_t len, uint8_t *dest) {
|
||||
while (len--) {
|
||||
dest[len] = (uint8_t) n;
|
||||
n >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t x_bytes_to_num(uint8_t *src, size_t len)
|
||||
{
|
||||
uint64_t x_bytes_to_num(uint8_t *src, size_t len) {
|
||||
uint64_t num = 0;
|
||||
while (len--) {
|
||||
num = (num << 8) | (*src);
|
||||
@@ -131,30 +123,26 @@ uint64_t x_bytes_to_num(uint8_t *src, size_t len)
|
||||
}
|
||||
return num;
|
||||
}
|
||||
uint8_t reversebytes(uint8_t b)
|
||||
{
|
||||
uint8_t reversebytes(uint8_t b) {
|
||||
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
|
||||
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
|
||||
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
|
||||
return b;
|
||||
}
|
||||
void reverse_arraybytes(uint8_t *arr, size_t len)
|
||||
{
|
||||
void reverse_arraybytes(uint8_t *arr, size_t len) {
|
||||
uint8_t i;
|
||||
for (i = 0; i < len ; i++) {
|
||||
arr[i] = reversebytes(arr[i]);
|
||||
}
|
||||
}
|
||||
void reverse_arraycopy(uint8_t *arr, uint8_t *dest, size_t len)
|
||||
{
|
||||
void reverse_arraycopy(uint8_t *arr, uint8_t *dest, size_t len) {
|
||||
uint8_t i;
|
||||
for (i = 0; i < len ; i++) {
|
||||
dest[i] = reversebytes(arr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void printarr(char *name, uint8_t *arr, int len)
|
||||
{
|
||||
void printarr(char *name, uint8_t *arr, int len) {
|
||||
int cx, i;
|
||||
size_t outsize = 40 + strlen(name) + len * 5;
|
||||
char *output = calloc(outsize, sizeof(char));
|
||||
@@ -167,8 +155,7 @@ void printarr(char *name, uint8_t *arr, int len)
|
||||
free(output);
|
||||
}
|
||||
|
||||
void printvar(char *name, uint8_t *arr, int len)
|
||||
{
|
||||
void printvar(char *name, uint8_t *arr, int len) {
|
||||
int cx, i;
|
||||
size_t outsize = 40 + strlen(name) + len * 2;
|
||||
char *output = calloc(outsize, sizeof(char));
|
||||
@@ -181,8 +168,7 @@ void printvar(char *name, uint8_t *arr, int len)
|
||||
free(output);
|
||||
}
|
||||
|
||||
void printarr_human_readable(char *title, uint8_t *arr, int len)
|
||||
{
|
||||
void printarr_human_readable(char *title, uint8_t *arr, int len) {
|
||||
int cx, i;
|
||||
size_t outsize = 100 + strlen(title) + len * 4;
|
||||
char *output = calloc(outsize, sizeof(char));
|
||||
@@ -201,8 +187,7 @@ void printarr_human_readable(char *title, uint8_t *arr, int len)
|
||||
//-----------------------------
|
||||
|
||||
#ifndef ON_DEVICE
|
||||
int testBitStream()
|
||||
{
|
||||
int testBitStream() {
|
||||
uint8_t input [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
|
||||
uint8_t output [] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
BitstreamIn in = { input, sizeof(input) * 8, 0};
|
||||
@@ -227,8 +212,7 @@ int testBitStream()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int testReversedBitstream()
|
||||
{
|
||||
int testReversedBitstream() {
|
||||
uint8_t input [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
|
||||
uint8_t reverse [] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
uint8_t output [] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
@@ -259,8 +243,7 @@ int testReversedBitstream()
|
||||
}
|
||||
|
||||
|
||||
int testCipherUtils(void)
|
||||
{
|
||||
int testCipherUtils(void) {
|
||||
PrintAndLogDevice(INFO, "Testing some internals...");
|
||||
int retval = 0;
|
||||
retval |= testBitStream();
|
||||
|
||||
@@ -69,8 +69,7 @@
|
||||
* @param key
|
||||
* @param dest
|
||||
*/
|
||||
void permutekey(uint8_t key[8], uint8_t dest[8])
|
||||
{
|
||||
void permutekey(uint8_t key[8], uint8_t dest[8]) {
|
||||
int i;
|
||||
for (i = 0 ; i < 8 ; i++) {
|
||||
dest[i] = (((key[7] & (0x80 >> i)) >> (7 - i)) << 7) |
|
||||
@@ -89,8 +88,7 @@ void permutekey(uint8_t key[8], uint8_t dest[8])
|
||||
* @param key
|
||||
* @param dest
|
||||
*/
|
||||
void permutekey_rev(uint8_t key[8], uint8_t dest[8])
|
||||
{
|
||||
void permutekey_rev(uint8_t key[8], uint8_t dest[8]) {
|
||||
int i;
|
||||
for (i = 0 ; i < 8 ; i++) {
|
||||
dest[7 - i] = (((key[0] & (0x80 >> i)) >> (7 - i)) << 7) |
|
||||
@@ -110,8 +108,7 @@ void permutekey_rev(uint8_t key[8], uint8_t dest[8])
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
inline uint8_t rr(uint8_t val)
|
||||
{
|
||||
inline uint8_t rr(uint8_t val) {
|
||||
return val >> 1 | ((val & 1) << 7);
|
||||
}
|
||||
|
||||
@@ -121,8 +118,7 @@ inline uint8_t rr(uint8_t val)
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
inline uint8_t rl(uint8_t val)
|
||||
{
|
||||
inline uint8_t rl(uint8_t val) {
|
||||
return val << 1 | ((val & 0x80) >> 7);
|
||||
}
|
||||
|
||||
@@ -132,8 +128,7 @@ inline uint8_t rl(uint8_t val)
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
inline uint8_t swap(uint8_t val)
|
||||
{
|
||||
inline uint8_t swap(uint8_t val) {
|
||||
return ((val >> 4) & 0xFF) | ((val & 0xFF) << 4);
|
||||
}
|
||||
|
||||
@@ -143,8 +138,7 @@ inline uint8_t swap(uint8_t val)
|
||||
* @param csn the CSN used
|
||||
* @param k output
|
||||
*/
|
||||
void hash1(uint8_t csn[], uint8_t k[])
|
||||
{
|
||||
void hash1(uint8_t csn[], uint8_t k[]) {
|
||||
k[0] = csn[0] ^ csn[1] ^ csn[2] ^ csn[3] ^ csn[4] ^ csn[5] ^ csn[6] ^ csn[7];
|
||||
k[1] = csn[0] + csn[1] + csn[2] + csn[3] + csn[4] + csn[5] + csn[6] + csn[7];
|
||||
k[2] = rr(swap(csn[2] + k[1]));
|
||||
@@ -168,8 +162,7 @@ Definition 14. Define the rotate key function rk : (F 82 ) 8 × N → (F 82 ) 8
|
||||
rk(x [0] . . . x [7] , 0) = x [0] . . . x [7]
|
||||
rk(x [0] . . . x [7] , n + 1) = rk(rl(x [0] ) . . . rl(x [7] ), n)
|
||||
**/
|
||||
void rk(uint8_t *key, uint8_t n, uint8_t *outp_key)
|
||||
{
|
||||
void rk(uint8_t *key, uint8_t n, uint8_t *outp_key) {
|
||||
memcpy(outp_key, key, 8);
|
||||
uint8_t j;
|
||||
while (n-- > 0) {
|
||||
@@ -182,16 +175,14 @@ void rk(uint8_t *key, uint8_t n, uint8_t *outp_key)
|
||||
static mbedtls_des_context ctx_enc;
|
||||
static mbedtls_des_context ctx_dec;
|
||||
|
||||
void desdecrypt_iclass(uint8_t *iclass_key, uint8_t *input, uint8_t *output)
|
||||
{
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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);
|
||||
mbedtls_des_setkey_enc(&ctx_enc, key_std_format);
|
||||
@@ -204,8 +195,7 @@ void desencrypt_iclass(uint8_t *iclass_key, uint8_t *input, uint8_t *output)
|
||||
* @param hash1 hash1
|
||||
* @param key_sel output key_sel=h[hash1[i]]
|
||||
*/
|
||||
void hash2(uint8_t *key64, uint8_t *outp_keytable)
|
||||
{
|
||||
void hash2(uint8_t *key64, uint8_t *outp_keytable) {
|
||||
/**
|
||||
*Expected:
|
||||
* High Security Key Table
|
||||
@@ -276,8 +266,7 @@ void hash2(uint8_t *key64, uint8_t *outp_keytable)
|
||||
* @param i the number to read. Should be less than 127, or something is wrong...
|
||||
* @return
|
||||
*/
|
||||
int _readFromDump(uint8_t dump[], dumpdata *item, uint8_t i)
|
||||
{
|
||||
int _readFromDump(uint8_t dump[], dumpdata *item, uint8_t i) {
|
||||
size_t itemsize = sizeof(dumpdata);
|
||||
memcpy(item, dump + i * itemsize, itemsize);
|
||||
|
||||
@@ -301,8 +290,7 @@ int _readFromDump(uint8_t dump[], dumpdata *item, uint8_t i)
|
||||
* @param keytable where to write found values.
|
||||
* @return
|
||||
*/
|
||||
int bruteforceItem(dumpdata item, uint16_t keytable[])
|
||||
{
|
||||
int bruteforceItem(dumpdata item, uint16_t keytable[]) {
|
||||
int errors = 0;
|
||||
int found = false;
|
||||
uint8_t key_sel_p[8] = {0};
|
||||
@@ -443,8 +431,7 @@ int bruteforceItem(dumpdata item, uint16_t keytable[])
|
||||
* @param master_key where to put the master key
|
||||
* @return 0 for ok, 1 for failz
|
||||
*/
|
||||
int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[])
|
||||
{
|
||||
int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[]) {
|
||||
mbedtls_des_context ctx_e;
|
||||
|
||||
uint8_t z_0[8] = {0};
|
||||
@@ -502,8 +489,7 @@ int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[])
|
||||
* @param keytable
|
||||
* @return
|
||||
*/
|
||||
int bruteforceDump(uint8_t dump[], size_t dumpsize, uint16_t keytable[])
|
||||
{
|
||||
int bruteforceDump(uint8_t dump[], size_t dumpsize, uint16_t keytable[]) {
|
||||
uint8_t i;
|
||||
int errors = 0;
|
||||
size_t itemsize = sizeof(dumpdata);
|
||||
@@ -542,8 +528,7 @@ int bruteforceDump(uint8_t dump[], size_t dumpsize, uint16_t keytable[])
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
int bruteforceFile(const char *filename, uint16_t keytable[])
|
||||
{
|
||||
int bruteforceFile(const char *filename, uint16_t keytable[]) {
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
PrintAndLogDevice(WARNING, "Failed to read from file '%s'", filename);
|
||||
@@ -584,8 +569,7 @@ int bruteforceFile(const char *filename, uint16_t keytable[])
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
int bruteforceFileNoKeys(const char *filename)
|
||||
{
|
||||
int bruteforceFileNoKeys(const char *filename) {
|
||||
uint16_t keytable[128] = {0};
|
||||
return bruteforceFile(filename, keytable);
|
||||
}
|
||||
@@ -596,8 +580,7 @@ int bruteforceFileNoKeys(const char *filename)
|
||||
// ----------------------------------------------------------------------------
|
||||
// TEST CODE BELOW
|
||||
// ----------------------------------------------------------------------------
|
||||
int _testBruteforce()
|
||||
{
|
||||
int _testBruteforce() {
|
||||
int errors = 0;
|
||||
if (true) {
|
||||
// First test
|
||||
@@ -634,8 +617,7 @@ int _testBruteforce()
|
||||
return errors;
|
||||
}
|
||||
|
||||
int _test_iclass_key_permutation()
|
||||
{
|
||||
int _test_iclass_key_permutation() {
|
||||
uint8_t testcase[8] = {0x6c, 0x8d, 0x44, 0xf9, 0x2a, 0x2d, 0x01, 0xbf};
|
||||
uint8_t testcase_output[8] = {0};
|
||||
uint8_t testcase_output_correct[8] = {0x8a, 0x0d, 0xb9, 0x88, 0xbb, 0xa7, 0x90, 0xea};
|
||||
@@ -661,8 +643,7 @@ int _test_iclass_key_permutation()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _testHash1()
|
||||
{
|
||||
int _testHash1() {
|
||||
uint8_t expected[8] = {0x7E, 0x72, 0x2F, 0x40, 0x2D, 0x02, 0x51, 0x42};
|
||||
uint8_t csn[8] = {0x01, 0x02, 0x03, 0x04, 0xF7, 0xFF, 0x12, 0xE0};
|
||||
uint8_t k[8] = {0};
|
||||
@@ -677,8 +658,7 @@ int _testHash1()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int testElite()
|
||||
{
|
||||
int testElite() {
|
||||
PrintAndLogDevice(INFO, "Testing iClass Elite functinality...");
|
||||
PrintAndLogDevice(INFO, "Testing hash2");
|
||||
uint8_t k_cus[8] = {0x5B, 0x7C, 0x62, 0xC4, 0x91, 0xC1, 0x1B, 0x39};
|
||||
|
||||
@@ -45,8 +45,7 @@
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
int fileExists(const char *filename)
|
||||
{
|
||||
int fileExists(const char *filename) {
|
||||
|
||||
#ifdef _WIN32
|
||||
struct _stat st;
|
||||
@@ -58,8 +57,7 @@ int fileExists(const char *filename)
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen)
|
||||
{
|
||||
int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen) {
|
||||
int size = sizeof(char) * (strlen(preferredName) + strlen(suffix) + 10);
|
||||
char *fileName = calloc(size, sizeof(char));
|
||||
int num = 1;
|
||||
@@ -85,8 +83,7 @@ int saveFile(const char *preferredName, const char *suffix, const void *data, si
|
||||
return 0;
|
||||
}
|
||||
|
||||
int saveFileEML(const char *preferredName, const char *suffix, uint8_t *data, size_t datalen, size_t blocksize)
|
||||
{
|
||||
int saveFileEML(const char *preferredName, const char *suffix, uint8_t *data, size_t datalen, size_t blocksize) {
|
||||
|
||||
if (preferredName == NULL) return 1;
|
||||
if (suffix == NULL) return 1;
|
||||
@@ -140,8 +137,7 @@ out:
|
||||
return retval;
|
||||
}
|
||||
|
||||
int saveFileJSON(const char *preferredName, const char *suffix, JSONFileType ftype, uint8_t *data, size_t datalen)
|
||||
{
|
||||
int saveFileJSON(const char *preferredName, const char *suffix, JSONFileType ftype, uint8_t *data, size_t datalen) {
|
||||
if (preferredName == NULL) return 1;
|
||||
if (suffix == NULL) return 1;
|
||||
if (data == NULL) return 1;
|
||||
@@ -257,8 +253,7 @@ out:
|
||||
return retval;
|
||||
}
|
||||
|
||||
int loadFile(const char *preferredName, const char *suffix, void *data, size_t *datalen)
|
||||
{
|
||||
int loadFile(const char *preferredName, const char *suffix, void *data, size_t *datalen) {
|
||||
|
||||
if (preferredName == NULL) return 1;
|
||||
if (suffix == NULL) return 1;
|
||||
@@ -317,8 +312,7 @@ out:
|
||||
return retval;
|
||||
}
|
||||
|
||||
int loadFileEML(const char *preferredName, const char *suffix, void *data, size_t *datalen)
|
||||
{
|
||||
int loadFileEML(const char *preferredName, const char *suffix, void *data, size_t *datalen) {
|
||||
|
||||
if (preferredName == NULL) return 1;
|
||||
if (suffix == NULL) return 1;
|
||||
@@ -373,8 +367,7 @@ out:
|
||||
return retval;
|
||||
}
|
||||
|
||||
int loadFileJSON(const char *preferredName, const char *suffix, void *data, size_t maxdatalen, size_t *datalen)
|
||||
{
|
||||
int loadFileJSON(const char *preferredName, const char *suffix, void *data, size_t maxdatalen, size_t *datalen) {
|
||||
|
||||
if (preferredName == NULL) return 1;
|
||||
if (suffix == NULL) return 1;
|
||||
@@ -462,8 +455,7 @@ out:
|
||||
return retval;
|
||||
}
|
||||
|
||||
int loadFileDICTIONARY(const char *preferredName, const char *suffix, void *data, size_t *datalen, uint8_t keylen, uint16_t *keycnt)
|
||||
{
|
||||
int loadFileDICTIONARY(const char *preferredName, const char *suffix, void *data, size_t *datalen, uint8_t keylen, uint16_t *keycnt) {
|
||||
|
||||
if (preferredName == NULL) return 1;
|
||||
if (suffix == NULL) return 1;
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
#include <ctype.h>
|
||||
#include "elite_crack.h"
|
||||
|
||||
void calc_score(uint8_t *csn, uint8_t *k)
|
||||
{
|
||||
void calc_score(uint8_t *csn, uint8_t *k) {
|
||||
uint8_t score = 0 ;
|
||||
uint8_t i;
|
||||
uint8_t goodvals[16] = {0};
|
||||
@@ -54,8 +53,7 @@ void calc_score(uint8_t *csn, uint8_t *k)
|
||||
}
|
||||
}
|
||||
|
||||
void brute_hash1(void)
|
||||
{
|
||||
void brute_hash1(void) {
|
||||
uint16_t a, b, c, d;
|
||||
uint8_t csn[8] = {0, 0, 0, 0, 0xf7, 0xff, 0x12, 0xe0};
|
||||
uint8_t k[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
@@ -85,8 +85,7 @@ static int debug_print = 0;
|
||||
* @param n bitnumber
|
||||
* @return
|
||||
*/
|
||||
uint8_t getSixBitByte(uint64_t c, int n)
|
||||
{
|
||||
uint8_t getSixBitByte(uint64_t c, int n) {
|
||||
return (c >> (42 - 6 * n)) & 0x3F;
|
||||
}
|
||||
|
||||
@@ -96,8 +95,7 @@ uint8_t getSixBitByte(uint64_t c, int n)
|
||||
* @param z the value to place there
|
||||
* @param n bitnumber.
|
||||
*/
|
||||
void pushbackSixBitByte(uint64_t *c, uint8_t z, int n)
|
||||
{
|
||||
void pushbackSixBitByte(uint64_t *c, uint8_t z, int n) {
|
||||
//0x XXXX YYYY ZZZZ ZZZZ ZZZZ
|
||||
// ^z0 ^z7
|
||||
//z0: 1111 1100 0000 0000
|
||||
@@ -122,8 +120,7 @@ void pushbackSixBitByte(uint64_t *c, uint8_t z, int n)
|
||||
* @param c
|
||||
* @return
|
||||
*/
|
||||
uint64_t swapZvalues(uint64_t c)
|
||||
{
|
||||
uint64_t swapZvalues(uint64_t c) {
|
||||
uint64_t newz = 0;
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 0), 7);
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 1), 6);
|
||||
@@ -140,8 +137,7 @@ uint64_t swapZvalues(uint64_t c)
|
||||
/**
|
||||
* @return 4 six-bit bytes chunked into a uint64_t,as 00..00a0a1a2a3
|
||||
*/
|
||||
uint64_t ck(int i, int j, uint64_t z)
|
||||
{
|
||||
uint64_t ck(int i, int j, uint64_t z) {
|
||||
if (i == 1 && j == -1) {
|
||||
// ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
|
||||
return z;
|
||||
@@ -183,8 +179,7 @@ uint64_t ck(int i, int j, uint64_t z)
|
||||
otherwise.
|
||||
**/
|
||||
|
||||
uint64_t check(uint64_t z)
|
||||
{
|
||||
uint64_t check(uint64_t z) {
|
||||
//These 64 bits are divided as c = x, y, z [0] , . . . , z [7]
|
||||
|
||||
// ck(3, 2, z [0] . . . z [3] )
|
||||
@@ -202,8 +197,7 @@ uint64_t check(uint64_t z)
|
||||
|
||||
}
|
||||
|
||||
void permute(BitstreamIn *p_in, uint64_t z, int l, int r, BitstreamOut *out)
|
||||
{
|
||||
void permute(BitstreamIn *p_in, uint64_t z, int l, int r, BitstreamOut *out) {
|
||||
if (bitsLeft(p_in) == 0)
|
||||
return;
|
||||
|
||||
@@ -220,16 +214,14 @@ void permute(BitstreamIn *p_in, uint64_t z, int l, int r, BitstreamOut *out)
|
||||
permute(p_in, z, l, r + 1, out);
|
||||
}
|
||||
}
|
||||
void printbegin()
|
||||
{
|
||||
void printbegin() {
|
||||
if (debug_print < 2)
|
||||
return;
|
||||
|
||||
PrintAndLogDevice(NORMAL, " | x| y|z0|z1|z2|z3|z4|z5|z6|z7|");
|
||||
}
|
||||
|
||||
void printState(char *desc, uint64_t c)
|
||||
{
|
||||
void printState(char *desc, uint64_t c) {
|
||||
if (debug_print < 2)
|
||||
return;
|
||||
|
||||
@@ -254,8 +246,7 @@ void printState(char *desc, uint64_t c)
|
||||
* @param k this is where the diversified key is put (should be 8 bytes)
|
||||
* @return
|
||||
*/
|
||||
void hash0(uint64_t c, uint8_t k[8])
|
||||
{
|
||||
void hash0(uint64_t c, uint8_t k[8]) {
|
||||
c = swapZvalues(c);
|
||||
|
||||
printbegin();
|
||||
@@ -360,8 +351,7 @@ void hash0(uint64_t c, uint8_t k[8])
|
||||
* @param key
|
||||
* @param div_key
|
||||
*/
|
||||
void diversifyKey(uint8_t csn[8], uint8_t key[8], uint8_t div_key[8])
|
||||
{
|
||||
void diversifyKey(uint8_t csn[8], uint8_t key[8], uint8_t div_key[8]) {
|
||||
// Prepare the DES key
|
||||
mbedtls_des_setkey_enc(&ctx_enc, key);
|
||||
|
||||
@@ -377,8 +367,7 @@ void diversifyKey(uint8_t csn[8], uint8_t key[8], uint8_t div_key[8])
|
||||
hash0(crypt_csn, div_key);
|
||||
}
|
||||
|
||||
void testPermute()
|
||||
{
|
||||
void testPermute() {
|
||||
uint64_t x = 0;
|
||||
pushbackSixBitByte(&x, 0x00, 0);
|
||||
pushbackSixBitByte(&x, 0x01, 1);
|
||||
@@ -431,8 +420,7 @@ typedef struct {
|
||||
uint8_t div_key[8];
|
||||
} Testcase;
|
||||
|
||||
int testDES(Testcase testcase, mbedtls_des_context ctx_enc, mbedtls_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};
|
||||
@@ -468,8 +456,7 @@ int testDES(Testcase testcase, mbedtls_des_context ctx_enc, mbedtls_des_context
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
bool des_getParityBitFromKey(uint8_t key)
|
||||
{
|
||||
bool des_getParityBitFromKey(uint8_t key) {
|
||||
// The top 7 bits is used
|
||||
bool parity = ((key & 0x80) >> 7)
|
||||
^ ((key & 0x40) >> 6) ^ ((key & 0x20) >> 5)
|
||||
@@ -478,8 +465,7 @@ bool des_getParityBitFromKey(uint8_t key)
|
||||
return !parity;
|
||||
}
|
||||
|
||||
void des_checkParity(uint8_t *key)
|
||||
{
|
||||
void des_checkParity(uint8_t *key) {
|
||||
int i;
|
||||
int fails = 0;
|
||||
for (i = 0; i < 8; i++) {
|
||||
@@ -567,8 +553,7 @@ Testcase testcases[] = {
|
||||
{{0}, {0}, {0}}
|
||||
};
|
||||
|
||||
int testKeyDiversificationWithMasterkeyTestcases()
|
||||
{
|
||||
int testKeyDiversificationWithMasterkeyTestcases() {
|
||||
int i, error = 0;
|
||||
uint8_t empty[8] = {0};
|
||||
|
||||
@@ -584,13 +569,11 @@ int testKeyDiversificationWithMasterkeyTestcases()
|
||||
return error;
|
||||
}
|
||||
|
||||
void print64bits(char *name, uint64_t val)
|
||||
{
|
||||
void print64bits(char *name, uint64_t val) {
|
||||
printf("%s%08x%08x\n", name, (uint32_t)(val >> 32), (uint32_t)(val & 0xFFFFFFFF));
|
||||
}
|
||||
|
||||
uint64_t testCryptedCSN(uint64_t crypted_csn, uint64_t expected)
|
||||
{
|
||||
uint64_t testCryptedCSN(uint64_t crypted_csn, uint64_t expected) {
|
||||
int retval = 0;
|
||||
uint8_t result[8] = {0};
|
||||
if (debug_print) PrintAndLogDevice(DEBUG, "debug_print %d", debug_print);
|
||||
@@ -617,8 +600,7 @@ uint64_t testCryptedCSN(uint64_t crypted_csn, uint64_t expected)
|
||||
return retval;
|
||||
}
|
||||
|
||||
int testDES2(uint64_t csn, uint64_t expected)
|
||||
{
|
||||
int testDES2(uint64_t csn, uint64_t expected) {
|
||||
uint8_t result[8] = {0};
|
||||
uint8_t input[8] = {0};
|
||||
|
||||
@@ -644,8 +626,7 @@ int testDES2(uint64_t csn, uint64_t expected)
|
||||
* @brief doTestsWithKnownInputs
|
||||
* @return
|
||||
*/
|
||||
int doTestsWithKnownInputs()
|
||||
{
|
||||
int doTestsWithKnownInputs() {
|
||||
// KSel from http://www.proxmark.org/forum/viewtopic.php?pid=10977#p10977
|
||||
int errors = 0;
|
||||
PrintAndLogDevice(SUCCESS, "Testing DES encryption");
|
||||
@@ -673,8 +654,7 @@ int doTestsWithKnownInputs()
|
||||
return errors;
|
||||
}
|
||||
|
||||
static bool readKeyFile(uint8_t key[8])
|
||||
{
|
||||
static bool readKeyFile(uint8_t key[8]) {
|
||||
bool retval = false;
|
||||
|
||||
//Test a few variants
|
||||
@@ -704,8 +684,7 @@ static bool readKeyFile(uint8_t key[8])
|
||||
return retval;
|
||||
}
|
||||
|
||||
int doKeyTests(uint8_t debuglevel)
|
||||
{
|
||||
int doKeyTests(uint8_t debuglevel) {
|
||||
debug_print = debuglevel;
|
||||
|
||||
PrintAndLogDevice(INFO, "Checking if the master key is present (iclass_key.bin)...");
|
||||
|
||||
@@ -48,8 +48,7 @@
|
||||
#include "fileutils.h"
|
||||
#include "elite_crack.h"
|
||||
|
||||
int unitTests()
|
||||
{
|
||||
int unitTests() {
|
||||
int errors = testCipherUtils();
|
||||
errors += testMAC();
|
||||
errors += doKeyTests(0);
|
||||
@@ -59,8 +58,7 @@ int unitTests()
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
int showHelp()
|
||||
{
|
||||
int showHelp() {
|
||||
PrintAndLogDevice(NORMAL, "Usage: loclass [options]");
|
||||
PrintAndLogDevice(NORMAL, "Options:");
|
||||
PrintAndLogDevice(NORMAL, "-t Perform self-test");
|
||||
@@ -77,8 +75,7 @@ int showHelp()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int main(int argc, char **argv) {
|
||||
PrintAndLogDevice(NORMAL, "IClass Cipher version 1.2, Copyright (C) 2014 Martin Holst Swende\n");
|
||||
PrintAndLogDevice(NORMAL, "Comes with ABSOLUTELY NO WARRANTY");
|
||||
PrintAndLogDevice(NORMAL, "Released as GPLv2\n");
|
||||
|
||||
Reference in New Issue
Block a user