X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/e5ad43c07eecd24792f0ca3f3468b10f447fafb3..dca558bf9e9b3709db27ad255167275972b06ae7:/armsrc/epa.c

diff --git a/armsrc/epa.c b/armsrc/epa.c
index 0e93a056..bec79e61 100644
--- a/armsrc/epa.c
+++ b/armsrc/epa.c
@@ -12,8 +12,8 @@
 //-----------------------------------------------------------------------------
 
 #include "iso14443a.h"
-
 #include "epa.h"
+#include "cmd.h"
 
 // Protocol and Parameter Selection Request
 // use regular (1x) speed in both directions
@@ -108,9 +108,9 @@ size_t EPA_Parse_CardAccess(uint8_t *data,
 		if (data[index] == 0x31 || data[index] == 0x30) {
 			// enter the set (skip tag + length)
 			index += 2;
-			// extended length
+			// check for extended length
 			if ((data[index - 1] & 0x80) != 0) {
-				index += (data[index] & 0x7F);
+				index += (data[index-1] & 0x7F);
 			}
 		}
 		// OID
@@ -185,6 +185,7 @@ int EPA_Read_CardAccess(uint8_t *buffer, size_t max_length)
 	    || response_apdu[rapdu_length - 4] != 0x90
 	    || response_apdu[rapdu_length - 3] != 0x00)
 	{
+		Dbprintf("epa - no select cardaccess");
 		return -1;
 	}
 	
@@ -196,6 +197,7 @@ int EPA_Read_CardAccess(uint8_t *buffer, size_t max_length)
 	    || response_apdu[rapdu_length - 4] != 0x90
 	    || response_apdu[rapdu_length - 3] != 0x00)
 	{
+		Dbprintf("epa - no read cardaccess");
 		return -1;
 	}
 	
@@ -211,24 +213,24 @@ int EPA_Read_CardAccess(uint8_t *buffer, size_t max_length)
 // Abort helper function for EPA_PACE_Collect_Nonce
 // sets relevant data in ack, sends the response
 //-----------------------------------------------------------------------------
-static void EPA_PACE_Collect_Nonce_Abort(UsbCommand *ack, uint8_t step, int func_return)
+static void EPA_PACE_Collect_Nonce_Abort(uint8_t step, int func_return)
 {
-	// step in which the failure occured
-	ack->arg[0] = step;
-	// last return code
-	ack->arg[1] = func_return;
+//	// step in which the failure occured
+//	ack->arg[0] = step;
+//	// last return code
+//	ack->arg[1] = func_return;
 
 	// power down the field
 	EPA_Finish();
 	
 	// send the USB packet
-	UsbSendPacket((void *)ack, sizeof(UsbCommand));
+  cmd_send(CMD_ACK,step,func_return,0,0,0);
 }
 
 //-----------------------------------------------------------------------------
 // Acquire one encrypted PACE nonce
 //-----------------------------------------------------------------------------
-void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
+void EPA_PACE_Collect_Nonce(UsbCommand *c)
 {
 	/*
 	 * ack layout:
@@ -242,16 +244,16 @@ void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
 	 */
 
 	// return value of a function
-	int func_return;
+	int func_return = 0;
 
-	// initialize ack with 0s
-	memset(ack->arg, 0, 12);
-	memset(ack->d.asBytes, 0, 48);
+//	// initialize ack with 0s
+//	memset(ack->arg, 0, 12);
+//	memset(ack->d.asBytes, 0, 48);
 	
 	// set up communication
 	func_return = EPA_Setup();
 	if (func_return != 0) {
-		EPA_PACE_Collect_Nonce_Abort(ack, 1, func_return);
+		EPA_PACE_Collect_Nonce_Abort(1, func_return);
 		return;
 	}
 
@@ -264,7 +266,7 @@ void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
 	int card_access_length = EPA_Read_CardAccess(card_access, 256);
 	// the response has to be at least this big to hold the OID
 	if (card_access_length < 18) {
-		EPA_PACE_Collect_Nonce_Abort(ack, 2, card_access_length);
+		EPA_PACE_Collect_Nonce_Abort(2, card_access_length);
 		return;
 	}
 
@@ -275,7 +277,7 @@ void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
 	                                   card_access_length,
 	                                   &pace_version_info);
 	if (func_return != 0 || pace_version_info.version == 0) {
-		EPA_PACE_Collect_Nonce_Abort(ack, 3, func_return);
+		EPA_PACE_Collect_Nonce_Abort(3, func_return);
 		return;
 	}
 	
@@ -290,17 +292,17 @@ void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
 	// check if the command succeeded
 	if (func_return < 0)
 	{
-		EPA_PACE_Collect_Nonce_Abort(ack, 4, func_return);
+		EPA_PACE_Collect_Nonce_Abort(4, func_return);
 		return;
 	}
+  
+  // all done, return
+	EPA_Finish();
 	
 	// save received information
-	ack->arg[1] = func_return;
-	memcpy(ack->d.asBytes, nonce, func_return);
-
-	// all done, return
-	EPA_Finish();
-	UsbSendPacket((void *)ack, sizeof(UsbCommand));
+//	ack->arg[1] = func_return;
+//	memcpy(ack->d.asBytes, nonce, func_return);
+  cmd_send(CMD_ACK,0,func_return,0,nonce,func_return);
 }
 
 //-----------------------------------------------------------------------------
@@ -414,25 +416,28 @@ int EPA_PACE_MSE_Set_AT(pace_version_info_t pace_version_info, uint8_t password)
 //-----------------------------------------------------------------------------
 int EPA_Setup()
 {
-	// return code
+
 	int return_code = 0;
-	// card UID
-	uint8_t uid[8];
-	// card select information
+	uint8_t uid[10];
+	uint8_t pps_response[3];
+	uint8_t pps_response_par[1];
 	iso14a_card_select_t card_select_info;
+
 	// power up the field
-	iso14443a_setup();
+	iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD);
 
+	iso14a_set_timeout(10500);
+	
 	// select the card
 	return_code = iso14443a_select_card(uid, &card_select_info, NULL);
 	if (return_code != 1) {
+		Dbprintf("Epa: Can't select card");
 		return 1;
 	}
 
 	// send the PPS request
-	ReaderTransmit((uint8_t *)pps, sizeof(pps));
-	uint8_t pps_response[3];
-	return_code = ReaderReceive(pps_response);
+	ReaderTransmit((uint8_t *)pps, sizeof(pps), NULL);
+	return_code = ReaderReceive(pps_response, pps_response_par);
 	if (return_code != 3 || pps_response[0] != 0xD0) {
 		return return_code == 0 ? 2 : return_code;
 	}