Skip to content

Commit f38a152

Browse files
committed
First check in.
1 parent 4a79e52 commit f38a152

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+10522
-486
lines changed

armsrc/LCD.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// LCD code
77
//-----------------------------------------------------------------------------
88

9-
#include "proxmark3.h"
9+
#include "../include/proxmark3.h"
1010
#include "apps.h"
1111
#include "LCD.h"
1212
#include "fonts.h"

armsrc/Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@ APP_INCLUDES = apps.h
1010

1111
#remove one of the following defines and comment out the relevant line
1212
#in the next section to remove that particular feature from compilation
13-
APP_CFLAGS = -DWITH_LF -DWITH_ISO15693 -DWITH_ISO14443a -DWITH_ISO14443b -DWITH_ICLASS -DWITH_LEGICRF -DWITH_HITAG
13+
APP_CFLAGS = -DWITH_LF -DWITH_ISO15693 -DWITH_ISO14443a -DWITH_ISO14443b -DWITH_ICLASS -DWITH_LEGICRF -DWITH_HITAG -DWITH_CRC -fno-strict-aliasing
1414
#-DWITH_LCD
1515

1616
#SRC_LCD = fonts.c LCD.c
1717
SRC_LF = lfops.c hitag2.c
1818
SRC_ISO15693 = iso15693.c iso15693tools.c
1919
SRC_ISO14443a = epa.c iso14443a.c mifareutil.c mifarecmd.c mifaresniff.c
2020
SRC_ISO14443b = iso14443.c
21-
SRC_CRAPTO1 = crapto1.c crypto1.c
21+
SRC_CRAPTO1 = crapto1.c crypto1.c des.c aes.c
22+
SRC_CRC = iso14443crc.c crc.c crc16.c crc32.c
2223

2324
THUMBSRC = start.c \
2425
$(SRC_LCD) \
2526
$(SRC_ISO15693) \
2627
$(SRC_LF) \
27-
appmain.c printf.c \
28+
appmain.c \
29+
printf.c \
2830
util.c \
2931
string.c \
3032
usb_cdc.c \
@@ -33,14 +35,15 @@ THUMBSRC = start.c \
3335
# These are to be compiled in ARM mode
3436
ARMSRC = fpgaloader.c \
3537
legicrf.c \
36-
iso14443crc.c \
37-
crc16.c \
3838
$(SRC_ISO14443a) \
3939
$(SRC_ISO14443b) \
4040
$(SRC_CRAPTO1) \
41+
$(SRC_CRC) \
4142
legic_prng.c \
4243
iclass.c \
43-
crc.c
44+
mifaredesfire.c \
45+
desfire_crypto.c \
46+
desfire_key.c
4447

4548
# stdint.h provided locally until GCC 4.5 becomes C99 compliant
4649
APP_CFLAGS += -I.

armsrc/aes.c

Lines changed: 1168 additions & 0 deletions
Large diffs are not rendered by default.

armsrc/aes.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* AES Cryptographic Algorithm Header File. Include this header file in
3+
* your source which uses these given APIs. (This source is kept under
4+
* public domain)
5+
*/
6+
7+
// AES context structure
8+
typedef struct {
9+
unsigned int Ek[60];
10+
unsigned int Dk[60];
11+
unsigned int Iv[4];
12+
unsigned char Nr;
13+
unsigned char Mode;
14+
} AesCtx;
15+
16+
// key length in bytes
17+
#define KEY128 16
18+
#define KEY192 24
19+
#define KEY256 32
20+
// block size in bytes
21+
#define BLOCKSZ 16
22+
// mode
23+
#define EBC 0
24+
#define CBC 1
25+
26+
// AES API function prototype
27+
28+
int AesCtxIni(AesCtx *pCtx, unsigned char *pIV, unsigned char *pKey, unsigned int KeyLen, unsigned char Mode);
29+
int AesEncrypt(AesCtx *pCtx, unsigned char *pData, unsigned char *pCipher, unsigned int DataLen);
30+
int AesDecrypt(AesCtx *pCtx, unsigned char *pCipher, unsigned char *pData, unsigned int CipherLen);

armsrc/appmain.c

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
// executes.
1111
//-----------------------------------------------------------------------------
1212

13-
#include "usb_cdc.h"
14-
#include "cmd.h"
13+
#include "../common/usb_cdc.h"
14+
#include "../common/cmd.h"
1515

16-
#include "proxmark3.h"
16+
#include "../include/proxmark3.h"
1717
#include "apps.h"
1818
#include "util.h"
1919
#include "printf.h"
@@ -22,7 +22,7 @@
2222
#include <stdarg.h>
2323

2424
#include "legicrf.h"
25-
#include <hitag2.h>
25+
#include "../include/hitag2.h"
2626

2727
#ifdef WITH_LCD
2828
#include "LCD.h"
@@ -81,40 +81,12 @@ void DbpString(char *str)
8181
{
8282
byte_t len = strlen(str);
8383
cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(byte_t*)str,len);
84-
// /* this holds up stuff unless we're connected to usb */
85-
// if (!UsbConnected())
86-
// return;
87-
//
88-
// UsbCommand c;
89-
// c.cmd = CMD_DEBUG_PRINT_STRING;
90-
// c.arg[0] = strlen(str);
91-
// if(c.arg[0] > sizeof(c.d.asBytes)) {
92-
// c.arg[0] = sizeof(c.d.asBytes);
93-
// }
94-
// memcpy(c.d.asBytes, str, c.arg[0]);
95-
//
96-
// UsbSendPacket((uint8_t *)&c, sizeof(c));
97-
// // TODO fix USB so stupid things like this aren't req'd
98-
// SpinDelay(50);
9984
}
10085

10186
#if 0
10287
void DbpIntegers(int x1, int x2, int x3)
10388
{
10489
cmd_send(CMD_DEBUG_PRINT_INTEGERS,x1,x2,x3,0,0);
105-
// /* this holds up stuff unless we're connected to usb */
106-
// if (!UsbConnected())
107-
// return;
108-
//
109-
// UsbCommand c;
110-
// c.cmd = CMD_DEBUG_PRINT_INTEGERS;
111-
// c.arg[0] = x1;
112-
// c.arg[1] = x2;
113-
// c.arg[2] = x3;
114-
//
115-
// UsbSendPacket((uint8_t *)&c, sizeof(c));
116-
// // XXX
117-
// SpinDelay(50);
11890
}
11991
#endif
12092

@@ -199,8 +171,6 @@ void MeasureAntennaTuning(void)
199171
int i, adcval = 0, peak = 0, peakv = 0, peakf = 0; //ptr = 0
200172
int vLf125 = 0, vLf134 = 0, vHf = 0; // in mV
201173

202-
// UsbCommand c;
203-
204174
LED_B_ON();
205175
DbpString("Measuring antenna characteristics, please wait...");
206176
memset(dest,0,sizeof(FREE_BUFFER_SIZE));
@@ -692,7 +662,6 @@ void UsbPacketReceived(uint8_t *packet, int len)
692662
case CMD_PCF7931_READ: // Read PCF7931 tag
693663
ReadPCF7931();
694664
cmd_send(CMD_ACK,0,0,0,0,0);
695-
// UsbSendPacket((uint8_t*)&ack, sizeof(ack));
696665
break;
697666
case CMD_EM4X_READ_WORD:
698667
EM4xReadWord(c->arg[1], c->arg[2],c->d.asBytes[0]);
@@ -800,8 +769,17 @@ void UsbPacketReceived(uint8_t *packet, int len)
800769
case CMD_MIFAREU_READBL:
801770
MifareUReadBlock(c->arg[0],c->d.asBytes);
802771
break;
772+
case CMD_MIFAREUC_AUTH1:
773+
MifareUC_Auth1(c->arg[0],c->d.asBytes);
774+
break;
775+
case CMD_MIFAREUC_AUTH2:
776+
MifareUC_Auth2(c->arg[0],c->d.asBytes);
777+
break;
803778
case CMD_MIFAREU_READCARD:
804-
MifareUReadCard(c->arg[0],c->d.asBytes);
779+
MifareUReadCard(c->arg[0],c->arg[1],c->d.asBytes);
780+
break;
781+
case CMD_MIFAREUC_READCARD:
782+
MifareUReadCard(c->arg[0],c->arg[1],c->d.asBytes);
805783
break;
806784
case CMD_MIFARE_READSC:
807785
MifareReadSector(c->arg[0], c->arg[1], c->arg[2], c->d.asBytes);
@@ -854,6 +832,24 @@ void UsbPacketReceived(uint8_t *packet, int len)
854832
case CMD_MIFARE_SNIFFER:
855833
SniffMifare(c->arg[0]);
856834
break;
835+
836+
// mifare desfire
837+
case CMD_MIFARE_DESFIRE_READBL:
838+
break;
839+
case CMD_MIFARE_DESFIRE_WRITEBL:
840+
break;
841+
case CMD_MIFARE_DESFIRE_AUTH1:
842+
MifareDES_Auth1(c->arg[0], c->arg[1], c->arg[2], c->d.asBytes);
843+
break;
844+
case CMD_MIFARE_DESFIRE_AUTH2:
845+
MifareDES_Auth2(c->arg[0],c->d.asBytes);
846+
break;
847+
// case CMD_MIFARE_DES_READER:
848+
// ReaderMifareDES(c->arg[0], c->arg[1], c->d.asBytes);
849+
break;
850+
case CMD_MIFARE_DESFIRE_INFO:
851+
MifareDesfireGetInformation();
852+
break;
857853
#endif
858854

859855
#ifdef WITH_ICLASS
@@ -867,6 +863,9 @@ void UsbPacketReceived(uint8_t *packet, int len)
867863
case CMD_READER_ICLASS:
868864
ReaderIClass(c->arg[0]);
869865
break;
866+
case CMD_READER_ICLASS_REPLAY:
867+
ReaderIClass_Replay(c->arg[0], c->d.asBytes);
868+
break;
870869
#endif
871870

872871
case CMD_SIMULATE_TAG_HF_LISTEN:
@@ -896,18 +895,6 @@ void UsbPacketReceived(uint8_t *packet, int len)
896895
break;
897896

898897
case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
899-
// UsbCommand n;
900-
// if(c->cmd == CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K) {
901-
// n.cmd = CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K;
902-
// } else {
903-
// n.cmd = CMD_DOWNLOADED_RAW_BITS_TI_TYPE;
904-
// }
905-
// n.arg[0] = c->arg[0];
906-
// memcpy(n.d.asBytes, BigBuf+c->arg[0], 48); // 12*sizeof(uint32_t)
907-
// LED_B_ON();
908-
// usb_write((uint8_t *)&n, sizeof(n));
909-
// UsbSendPacket((uint8_t *)&n, sizeof(n));
910-
// LED_B_OFF();
911898

912899
LED_B_ON();
913900
for(size_t i=0; i<c->arg[1]; i += USB_CMD_DATA_SIZE) {
@@ -923,7 +910,6 @@ void UsbPacketReceived(uint8_t *packet, int len)
923910
uint8_t *b = (uint8_t *)BigBuf;
924911
memcpy(b+c->arg[0], c->d.asBytes, 48);
925912
//Dbprintf("copied 48 bytes to %i",b+c->arg[0]);
926-
// UsbSendPacket((uint8_t*)&ack, sizeof(ack));
927913
cmd_send(CMD_ACK,0,0,0,0,0);
928914
break;
929915
}
@@ -981,7 +967,6 @@ void UsbPacketReceived(uint8_t *packet, int len)
981967
case CMD_DEVICE_INFO: {
982968
uint32_t dev_info = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS;
983969
if(common_area.flags.bootrom_present) dev_info |= DEVICE_INFO_FLAG_BOOTROM_PRESENT;
984-
// UsbSendPacket((uint8_t*)&c, sizeof(c));
985970
cmd_send(CMD_DEVICE_INFO,dev_info,0,0,0,0);
986971
break;
987972
}
@@ -1010,7 +995,6 @@ void __attribute__((noreturn)) AppMain(void)
1010995

1011996
// Init USB device`
1012997
usb_enable();
1013-
// UsbStart();
1014998

1015999
// The FPGA gets its clock from us from PCK0 output, so set that up.
10161000
AT91C_BASE_PIOA->PIO_BSR = GPIO_PCK0;
@@ -1046,8 +1030,6 @@ void __attribute__((noreturn)) AppMain(void)
10461030
UsbPacketReceived(rx,rx_len);
10471031
}
10481032
}
1049-
// UsbPoll(FALSE);
1050-
10511033
WDT_HIT();
10521034

10531035
#ifdef WITH_LF

armsrc/apps.h

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,27 @@
1414

1515
#include <stdint.h>
1616
#include <stddef.h>
17-
#include "common.h"
18-
#include "hitag2.h"
19-
#include "mifare.h"
17+
#include <sys/types.h>
18+
19+
#include <stdlib.h>
20+
#include <string.h>
21+
#include <strings.h>
22+
23+
24+
#include "../include/common.h"
25+
#include "../include/hitag2.h"
26+
#include "../include/mifare.h"
27+
28+
//#include <openssl/des.h>
29+
//#include <openssl/aes.h>
30+
31+
//#include "des.h"
32+
//#include "aes.h"
33+
#include "../common/desfire.h"
34+
#include "../common/crc32.h"
35+
//#include "desfire_crypto.h"
36+
//#include "desfire_key.h"
37+
2038

2139
// The large multi-purpose buffer, typically used to hold A/D samples,
2240
// maybe processed in some way.
@@ -172,7 +190,9 @@ void ReaderMifare(bool first_try);
172190
int32_t dist_nt(uint32_t nt1, uint32_t nt2);
173191
void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *data);
174192
void MifareUReadBlock(uint8_t arg0,uint8_t *datain);
175-
void MifareUReadCard(uint8_t arg0,uint8_t *datain);
193+
void MifareUC_Auth1(uint8_t arg0, uint8_t *datain);
194+
void MifareUC_Auth2(uint32_t arg0, uint8_t *datain);
195+
void MifareUReadCard(uint8_t arg0,int Pages,uint8_t *datain);
176196
void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain);
177197
void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain);
178198
void MifareUWriteBlock(uint8_t arg0,uint8_t *datain);
@@ -188,6 +208,47 @@ void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai
188208
void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain); // Work with "magic Chinese" card
189209
void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain);
190210

211+
// mifaredesfire.h
212+
void MifareDesfireGetInformation();
213+
void MifareDES_Auth1(uint8_t arg0,uint8_t arg1,uint8_t arg2, uint8_t *datain);
214+
void MifareDES_Auth2(uint32_t arg0, uint8_t *datain);
215+
int mifare_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData);
216+
void ReaderMifareDES(uint32_t param, uint32_t param2, uint8_t * datain);
217+
int SendDesfireCommand(enum DESFIRE_CMD desfire_cmd, uint8_t *dataout, uint8_t fromscratch);
218+
uint8_t* CreateAPDU( uint8_t *datain, size_t len);
219+
void OnSuccess();
220+
void OnError();
221+
222+
// desfire_key.h
223+
desfirekey_t Desfire_des_key_new (const uint8_t value[8]);
224+
desfirekey_t Desfire_3des_key_new (const uint8_t value[16]);
225+
desfirekey_t Desfire_des_key_new_with_version (const uint8_t value[8]);
226+
desfirekey_t Desfire_3des_key_new_with_version (const uint8_t value[16]);
227+
desfirekey_t Desfire_3k3des_key_new (const uint8_t value[24]);
228+
desfirekey_t Desfire_3k3des_key_new_with_version (const uint8_t value[24]);
229+
desfirekey_t Desfire_aes_key_new (const uint8_t value[16]);
230+
desfirekey_t Desfire_aes_key_new_with_version (const uint8_t value[16], uint8_t version);
231+
uint8_t Desfire_key_get_version (desfirekey_t key);
232+
void Desfire_key_set_version (desfirekey_t key, uint8_t version);
233+
desfirekey_t Desfire_session_key_new (const uint8_t rnda[], const uint8_t rndb[], desfirekey_t authkey);
234+
235+
// desfire_crypto.h
236+
void *mifare_cryto_preprocess_data (desfiretag_t tag, void *data, size_t *nbytes, off_t offset, int communication_settings);
237+
void *mifare_cryto_postprocess_data (desfiretag_t tag, void *data, ssize_t *nbytes, int communication_settings);
238+
void mifare_cypher_single_block (desfirekey_t key, uint8_t *data, uint8_t *ivect, MifareCryptoDirection direction, MifareCryptoOperation operation, size_t block_size);
239+
void mifare_cypher_blocks_chained (desfiretag_t tag, desfirekey_t key, uint8_t *ivect, uint8_t *data, size_t data_size, MifareCryptoDirection direction, MifareCryptoOperation operation);
240+
size_t key_block_size (const desfirekey_t key);
241+
size_t padded_data_length (const size_t nbytes, const size_t block_size);
242+
size_t maced_data_length (const desfirekey_t key, const size_t nbytes);
243+
size_t enciphered_data_length (const desfiretag_t tag, const size_t nbytes, int communication_settings);
244+
void cmac_generate_subkeys (desfirekey_t key);
245+
void cmac (const desfirekey_t key, uint8_t *ivect, const uint8_t *data, size_t len, uint8_t *cmac);
246+
247+
248+
249+
250+
251+
191252
/// iso15693.h
192253
void RecordRawAdcSamplesIso15693(void);
193254
void AcquireRawAdcSamplesIso15693(void);
@@ -201,7 +262,9 @@ void SetDebugIso15693(uint32_t flag);
201262
void RAMFUNC SnoopIClass(void);
202263
void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain);
203264
void ReaderIClass(uint8_t arg0);
204-
//int doIClassSimulation(uint8_t csn[], int breakAfterMacReceived);
265+
void ReaderIClass_Replay(uint8_t arg0,uint8_t *MAC);
266+
void IClass_iso14443A_GetPublic(uint8_t arg0);
267+
205268
// hitag2.h
206269
void SnoopHitag(uint32_t type);
207270
void SimulateHitagTag(bool tag_mem_supplied, byte_t* data);

0 commit comments

Comments
 (0)