mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-20 13:23:25 -07:00
cleaning up iclass.c and optimized_cipher.c
* add iclass.h * reformatting * whitespace fixes * (no functional changes)
This commit is contained in:
parent
f98702bace
commit
17505ce2a7
5 changed files with 829 additions and 923 deletions
|
@ -24,6 +24,7 @@
|
||||||
#include "legicrfsim.h"
|
#include "legicrfsim.h"
|
||||||
#include "hitag2.h"
|
#include "hitag2.h"
|
||||||
#include "hitagS.h"
|
#include "hitagS.h"
|
||||||
|
#include "iclass.h"
|
||||||
#include "iso14443b.h"
|
#include "iso14443b.h"
|
||||||
#include "iso15693.h"
|
#include "iso15693.h"
|
||||||
#include "lfsampling.h"
|
#include "lfsampling.h"
|
||||||
|
|
|
@ -26,10 +26,6 @@ extern const uint8_t OddByteParity[256];
|
||||||
extern int rsamples; // = 0;
|
extern int rsamples; // = 0;
|
||||||
extern uint8_t trigger;
|
extern uint8_t trigger;
|
||||||
|
|
||||||
// This may be used (sparingly) to declare a function to be copied to
|
|
||||||
// and executed from RAM
|
|
||||||
#define RAMFUNC __attribute((long_call, section(".ramfunc")))
|
|
||||||
|
|
||||||
/// appmain.h
|
/// appmain.h
|
||||||
void ReadMem(int addr);
|
void ReadMem(int addr);
|
||||||
void __attribute__((noreturn)) AppMain(void);
|
void __attribute__((noreturn)) AppMain(void);
|
||||||
|
@ -144,21 +140,6 @@ void ReaderMifareDES(uint32_t param, uint32_t param2, uint8_t * datain);
|
||||||
int DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout);
|
int DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout);
|
||||||
size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout);
|
size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout);
|
||||||
|
|
||||||
|
|
||||||
/// iclass.h
|
|
||||||
void RAMFUNC SnoopIClass(void);
|
|
||||||
void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain);
|
|
||||||
void ReaderIClass(uint8_t arg0);
|
|
||||||
void ReaderIClass_Replay(uint8_t arg0,uint8_t *MAC);
|
|
||||||
void IClass_iso14443A_GetPublic(uint8_t arg0);
|
|
||||||
void iClass_Authentication(uint8_t *MAC);
|
|
||||||
void iClass_WriteBlock(uint8_t blockNo, uint8_t *data);
|
|
||||||
void iClass_ReadBlk(uint8_t blockNo);
|
|
||||||
bool iClass_ReadBlock(uint8_t blockNo, uint8_t *readdata);
|
|
||||||
void iClass_Dump(uint8_t blockno, uint8_t numblks);
|
|
||||||
void iClass_Clone(uint8_t startblock, uint8_t endblock, uint8_t *data);
|
|
||||||
void iClass_ReadCheck(uint8_t blockNo, uint8_t keyType);
|
|
||||||
|
|
||||||
// cmd.h
|
// cmd.h
|
||||||
bool cmd_receive(UsbCommand* cmd);
|
bool cmd_receive(UsbCommand* cmd);
|
||||||
bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len);
|
bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len);
|
||||||
|
|
916
armsrc/iclass.c
916
armsrc/iclass.c
File diff suppressed because it is too large
Load diff
|
@ -100,9 +100,7 @@ uint8_t xopt__select(bool x, bool y, uint8_t r)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void opt_successor(const uint8_t* k, State *s, bool y, State* successor)
|
void opt_successor(const uint8_t *k, State *s, bool y, State *successor) {
|
||||||
{
|
|
||||||
|
|
||||||
uint8_t Tt = 1 & opt_T(s);
|
uint8_t Tt = 1 & opt_T(s);
|
||||||
|
|
||||||
successor->t = (s->t >> 1);
|
successor->t = (s->t >> 1);
|
||||||
|
@ -111,83 +109,72 @@ void opt_successor(const uint8_t* k, State *s, bool y, State* successor)
|
||||||
successor->b = s->b >> 1;
|
successor->b = s->b >> 1;
|
||||||
successor->b |= (opt_B(s) ^ (s->r & 0x1)) << 7;
|
successor->b |= (opt_B(s) ^ (s->r & 0x1)) << 7;
|
||||||
|
|
||||||
successor->r = (k[opt__select(Tt,y,s->r)] ^ successor->b) + s->l ;
|
successor->r = (k[opt__select(Tt, y, s->r)] ^ successor->b) + s->l ;
|
||||||
successor->l = successor->r+s->r;
|
successor->l = successor->r + s->r;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void opt_suc(const uint8_t* k,State* s, uint8_t *in, uint8_t length, bool add32Zeroes)
|
void opt_suc(const uint8_t *k, State *s, uint8_t *in, uint8_t length, bool add32Zeroes) {
|
||||||
{
|
|
||||||
State x2;
|
State x2;
|
||||||
int i;
|
for (int i = 0; i < length; i++) {
|
||||||
uint8_t head = 0;
|
uint8_t head;
|
||||||
for(i =0 ; i < length ; i++)
|
|
||||||
{
|
|
||||||
head = 1 & (in[i] >> 7);
|
head = 1 & (in[i] >> 7);
|
||||||
opt_successor(k,s,head,&x2);
|
opt_successor(k, s, head, &x2);
|
||||||
|
|
||||||
head = 1 & (in[i] >> 6);
|
head = 1 & (in[i] >> 6);
|
||||||
opt_successor(k,&x2,head,s);
|
opt_successor(k, &x2, head, s);
|
||||||
|
|
||||||
head = 1 & (in[i] >> 5);
|
head = 1 & (in[i] >> 5);
|
||||||
opt_successor(k,s,head,&x2);
|
opt_successor(k, s, head, &x2);
|
||||||
|
|
||||||
head = 1 & (in[i] >> 4);
|
head = 1 & (in[i] >> 4);
|
||||||
opt_successor(k,&x2,head,s);
|
opt_successor(k, &x2, head, s);
|
||||||
|
|
||||||
head = 1 & (in[i] >> 3);
|
head = 1 & (in[i] >> 3);
|
||||||
opt_successor(k,s,head,&x2);
|
opt_successor(k, s, head, &x2);
|
||||||
|
|
||||||
head = 1 & (in[i] >> 2);
|
head = 1 & (in[i] >> 2);
|
||||||
opt_successor(k,&x2,head,s);
|
opt_successor(k, &x2, head, s);
|
||||||
|
|
||||||
head = 1 & (in[i] >> 1);
|
head = 1 & (in[i] >> 1);
|
||||||
opt_successor(k,s,head,&x2);
|
opt_successor(k, s, head, &x2);
|
||||||
|
|
||||||
head = 1 & in[i];
|
head = 1 & in[i];
|
||||||
opt_successor(k,&x2,head,s);
|
opt_successor(k, &x2, head, s);
|
||||||
|
|
||||||
}
|
}
|
||||||
//For tag MAC, an additional 32 zeroes
|
//For tag MAC, an additional 32 zeroes
|
||||||
if(add32Zeroes)
|
if (add32Zeroes) {
|
||||||
for(i =0 ; i < 16 ; i++)
|
for(int i = 0; i < 16; i++) {
|
||||||
{
|
opt_successor(k, s, 0, &x2);
|
||||||
opt_successor(k,s,0,&x2);
|
opt_successor(k, &x2, 0, s);
|
||||||
opt_successor(k,&x2,0,s);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void opt_output(const uint8_t* k,State* s, uint8_t *buffer)
|
void opt_output(const uint8_t *k, State *s, uint8_t *buffer) {
|
||||||
{
|
State temp = {0, 0, 0, 0};
|
||||||
uint8_t times = 0;
|
for (uint8_t times = 0; times < 4; times++) {
|
||||||
uint8_t bout = 0;
|
uint8_t bout = 0;
|
||||||
State temp = {0,0,0,0};
|
|
||||||
for( ; times < 4 ; times++)
|
|
||||||
{
|
|
||||||
bout =0;
|
|
||||||
bout |= (s->r & 0x4) << 5;
|
bout |= (s->r & 0x4) << 5;
|
||||||
opt_successor(k,s,0,&temp);
|
opt_successor(k, s, 0, &temp);
|
||||||
bout |= (temp.r & 0x4) << 4;
|
bout |= (temp.r & 0x4) << 4;
|
||||||
opt_successor(k,&temp,0,s);
|
opt_successor(k, &temp, 0, s);
|
||||||
bout |= (s->r & 0x4) << 3;
|
bout |= (s->r & 0x4) << 3;
|
||||||
opt_successor(k,s,0,&temp);
|
opt_successor(k, s, 0, &temp);
|
||||||
bout |= (temp.r & 0x4) << 2;
|
bout |= (temp.r & 0x4) << 2;
|
||||||
opt_successor(k,&temp,0,s);
|
opt_successor(k, &temp, 0, s);
|
||||||
bout |= (s->r & 0x4) << 1;
|
bout |= (s->r & 0x4) << 1;
|
||||||
opt_successor(k,s,0,&temp);
|
opt_successor(k, s, 0, &temp);
|
||||||
bout |= (temp.r & 0x4) ;
|
bout |= (temp.r & 0x4) ;
|
||||||
opt_successor(k,&temp,0,s);
|
opt_successor(k, &temp, 0, s);
|
||||||
bout |= (s->r & 0x4) >> 1;
|
bout |= (s->r & 0x4) >> 1;
|
||||||
opt_successor(k,s,0,&temp);
|
opt_successor(k, s, 0, &temp);
|
||||||
bout |= (temp.r & 0x4) >> 2;
|
bout |= (temp.r & 0x4) >> 2;
|
||||||
opt_successor(k,&temp,0,s);
|
opt_successor(k, &temp, 0, s);
|
||||||
buffer[times] = bout;
|
buffer[times] = bout;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void opt_MAC(uint8_t* k, uint8_t* input, uint8_t* out)
|
void opt_MAC(uint8_t *k, uint8_t *input, uint8_t *out) {
|
||||||
{
|
|
||||||
State _init = {
|
State _init = {
|
||||||
((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
|
((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
|
||||||
((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
|
((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
|
||||||
|
@ -195,52 +182,51 @@ void opt_MAC(uint8_t* k, uint8_t* input, uint8_t* out)
|
||||||
0xE012 // t
|
0xE012 // t
|
||||||
};
|
};
|
||||||
|
|
||||||
opt_suc(k,&_init,input,12, false);
|
opt_suc(k, &_init, input, 12, false);
|
||||||
//printf("\noutp ");
|
//printf("\noutp ");
|
||||||
opt_output(k,&_init, out);
|
opt_output(k, &_init, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t rev_byte(uint8_t b) {
|
uint8_t rev_byte(uint8_t b) {
|
||||||
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
|
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
|
||||||
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
|
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
|
||||||
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
|
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
void opt_reverse_arraybytecpy(uint8_t* dest, uint8_t *src, size_t len)
|
|
||||||
{
|
void opt_reverse_arraybytecpy(uint8_t *dest, uint8_t *src, size_t len) {
|
||||||
uint8_t i;
|
for (size_t i = 0; i < len; i++) {
|
||||||
for( i =0; i< len ; i++)
|
|
||||||
dest[i] = rev_byte(src[i]);
|
dest[i] = rev_byte(src[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4])
|
void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4]) {
|
||||||
{
|
|
||||||
static uint8_t cc_nr[12];
|
static uint8_t cc_nr[12];
|
||||||
|
opt_reverse_arraybytecpy(cc_nr, cc_nr_p, 12);
|
||||||
opt_reverse_arraybytecpy(cc_nr, cc_nr_p,12);
|
uint8_t dest[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||||
uint8_t dest []= {0,0,0,0,0,0,0,0};
|
opt_MAC(div_key_p, cc_nr, dest);
|
||||||
opt_MAC(div_key_p,cc_nr, dest);
|
|
||||||
//The output MAC must also be reversed
|
//The output MAC must also be reversed
|
||||||
opt_reverse_arraybytecpy(mac, dest,4);
|
opt_reverse_arraybytecpy(mac, dest, 4);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4])
|
|
||||||
{
|
void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4]) {
|
||||||
static uint8_t cc_nr[8+4+4];
|
static uint8_t cc_nr[8+4+4];
|
||||||
opt_reverse_arraybytecpy(cc_nr, cc_p,12);
|
opt_reverse_arraybytecpy(cc_nr, cc_p, 12);
|
||||||
State _init = {
|
State _init = {
|
||||||
((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
|
((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
|
||||||
((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
|
((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
|
||||||
0x4c, // b
|
0x4c, // b
|
||||||
0xE012 // t
|
0xE012 // t
|
||||||
};
|
};
|
||||||
opt_suc(div_key_p,&_init,cc_nr, 12,true);
|
opt_suc(div_key_p, &_init,cc_nr, 12, true);
|
||||||
uint8_t dest []= {0,0,0,0};
|
uint8_t dest[] = {0, 0, 0, 0};
|
||||||
opt_output(div_key_p,&_init, dest);
|
opt_output(div_key_p, &_init, dest);
|
||||||
//The output MAC must also be reversed
|
//The output MAC must also be reversed
|
||||||
opt_reverse_arraybytecpy(mac, dest,4);
|
opt_reverse_arraybytecpy(mac, dest, 4);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The tag MAC can be divided (both can, but no point in dividing the reader mac) into
|
* The tag MAC can be divided (both can, but no point in dividing the reader mac) into
|
||||||
* two functions, since the first 8 bytes are known, we can pre-calculate the state
|
* two functions, since the first 8 bytes are known, we can pre-calculate the state
|
||||||
|
@ -249,19 +235,19 @@ void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4])
|
||||||
* @param div_key_p
|
* @param div_key_p
|
||||||
* @return the cipher state
|
* @return the cipher state
|
||||||
*/
|
*/
|
||||||
State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p)
|
State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p) {
|
||||||
{
|
|
||||||
static uint8_t cc_nr[8];
|
static uint8_t cc_nr[8];
|
||||||
opt_reverse_arraybytecpy(cc_nr, cc_p,8);
|
opt_reverse_arraybytecpy(cc_nr, cc_p, 8);
|
||||||
State _init = {
|
State _init = {
|
||||||
((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
|
((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
|
||||||
((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
|
((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
|
||||||
0x4c, // b
|
0x4c, // b
|
||||||
0xE012 // t
|
0xE012 // t
|
||||||
};
|
};
|
||||||
opt_suc(div_key_p,&_init,cc_nr, 8,false);
|
opt_suc(div_key_p, &_init, cc_nr, 8, false);
|
||||||
return _init;
|
return _init;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The second part of the tag MAC calculation, since the CC is already calculated into the state,
|
* The second part of the tag MAC calculation, since the CC is already calculated into the state,
|
||||||
* this function is fed only the NR, and internally feeds the remaining 32 0-bits to generate the tag
|
* this function is fed only the NR, and internally feeds the remaining 32 0-bits to generate the tag
|
||||||
|
@ -271,15 +257,14 @@ State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p)
|
||||||
* @param mac - where to store the MAC
|
* @param mac - where to store the MAC
|
||||||
* @param div_key_p - the key to use
|
* @param div_key_p - the key to use
|
||||||
*/
|
*/
|
||||||
void opt_doTagMAC_2(State _init, uint8_t* nr, uint8_t mac[4], const uint8_t* div_key_p)
|
void opt_doTagMAC_2(State _init, uint8_t *nr, uint8_t mac[4], const uint8_t *div_key_p) {
|
||||||
{
|
static uint8_t _nr[4];
|
||||||
static uint8_t _nr [4];
|
|
||||||
opt_reverse_arraybytecpy(_nr, nr, 4);
|
opt_reverse_arraybytecpy(_nr, nr, 4);
|
||||||
opt_suc(div_key_p,&_init,_nr, 4, true);
|
opt_suc(div_key_p, &_init, _nr, 4, true);
|
||||||
//opt_suc(div_key_p,&_init,nr, 4, false);
|
//opt_suc(div_key_p, &_init,nr, 4, false);
|
||||||
uint8_t dest []= {0,0,0,0};
|
uint8_t dest[] = {0, 0, 0, 0};
|
||||||
opt_output(div_key_p,&_init, dest);
|
opt_output(div_key_p, &_init, dest);
|
||||||
//The output MAC must also be reversed
|
//The output MAC must also be reversed
|
||||||
opt_reverse_arraybytecpy(mac, dest,4);
|
opt_reverse_arraybytecpy(mac, dest, 4);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,8 +35,9 @@
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef OPTIMIZED_CIPHER_H
|
#ifndef OPTIMIZED_CIPHER_H__
|
||||||
#define OPTIMIZED_CIPHER_H
|
#define OPTIMIZED_CIPHER_H__
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,6 +58,7 @@ typedef struct {
|
||||||
/** The reader MAC is MAC(key, CC * NR )
|
/** The reader MAC is MAC(key, CC * NR )
|
||||||
**/
|
**/
|
||||||
void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4]);
|
void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The tag MAC is MAC(key, CC * NR * 32x0))
|
* The tag MAC is MAC(key, CC * NR * 32x0))
|
||||||
*/
|
*/
|
||||||
|
@ -71,6 +73,7 @@ void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4]);
|
||||||
* @return the cipher state
|
* @return the cipher state
|
||||||
*/
|
*/
|
||||||
State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p);
|
State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The second part of the tag MAC calculation, since the CC is already calculated into the state,
|
* The second part of the tag MAC calculation, since the CC is already calculated into the state,
|
||||||
* this function is fed only the NR, and internally feeds the remaining 32 0-bits to generate the tag
|
* this function is fed only the NR, and internally feeds the remaining 32 0-bits to generate the tag
|
||||||
|
@ -80,6 +83,6 @@ State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p);
|
||||||
* @param mac - where to store the MAC
|
* @param mac - where to store the MAC
|
||||||
* @param div_key_p - the key to use
|
* @param div_key_p - the key to use
|
||||||
*/
|
*/
|
||||||
void opt_doTagMAC_2(State _init, uint8_t* nr, uint8_t mac[4], const uint8_t* div_key_p);
|
void opt_doTagMAC_2(State _init, uint8_t *nr, uint8_t mac[4], const uint8_t *div_key_p);
|
||||||
|
|
||||||
#endif // OPTIMIZED_CIPHER_H
|
#endif // OPTIMIZED_CIPHER_H__
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue