1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2012 Frederik Möllers
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Commands related to the German electronic Identification Card
9 //-----------------------------------------------------------------------------
19 #include "proxmark3.h"
21 #include "cmdparser.h"
25 static int CmdHelp(const char *Cmd
);
27 // Perform (part of) the PACE protocol
28 int CmdHFEPACollectPACENonces(const char *Cmd
)
30 // requested nonce size
32 // requested number of Nonces
34 // delay between requests
37 sscanf(Cmd
, "%u %u %u", &m
, &n
, &d
);
39 // values are expected to be > 0
43 PrintAndLog("Collecting %u %u-byte nonces", n
, m
);
44 PrintAndLog("Start: %" PRIu64
, msclock()/1000);
46 for (unsigned int i
= 0; i
< n
; i
++) {
48 UsbCommand c
= {CMD_EPA_PACE_COLLECT_NONCE
, {(int)m
, 0, 0}};
52 WaitForResponse(CMD_ACK
,&resp
);
54 // check if command failed
55 if (resp
.arg
[0] != 0) {
56 PrintAndLog("Error in step %d, Return code: %d",resp
.arg
[0],(int)resp
.arg
[1]);
58 size_t nonce_length
= resp
.arg
[1];
59 char *nonce
= (char *) malloc(2 * nonce_length
+ 1);
60 for(int j
= 0; j
< nonce_length
; j
++) {
61 sprintf(nonce
+ (2 * j
), "%02X", resp
.d
.asBytes
[j
]);
64 PrintAndLog("Length: %d, Nonce: %s", nonce_length
, nonce
);
71 PrintAndLog("End: %" PRIu64
, msclock()/1000);
76 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77 ////////////////////////////////The commands lie below here/////////////////////////////////////////////////////////////////////////////////////////
79 // perform the PACE protocol by replaying APDUs
80 int CmdHFEPAPACEReplay(const char *Cmd
)
82 // the 4 APDUs which are replayed + their lengths
83 uint8_t msesa_apdu
[41], gn_apdu
[8], map_apdu
[75];
84 uint8_t pka_apdu
[75], ma_apdu
[18], apdu_lengths
[5] = {0};
85 // pointers to the arrays to be able to iterate
86 uint8_t *apdus
[] = {msesa_apdu
, gn_apdu
, map_apdu
, pka_apdu
, ma_apdu
};
89 static const char *usage_msg
=
90 "Please specify 5 APDUs separated by spaces. "
91 "Example:\n preplay 0022C1A4 1068000000 1086000002 1234ABCDEF 1A2B3C4D";
96 int skip
= 0, skip_add
= 0, scan_return
= 0;
98 for (int i
= 0; i
< sizeof(apdu_lengths
); i
++) {
99 // scan to next space or end of string
100 while (Cmd
[skip
] != ' ' && Cmd
[skip
] != '\0') {
102 scan_return
= sscanf(Cmd
+ skip
, "%2X%n",
103 (unsigned int *) (apdus
[i
] + apdu_lengths
[i
]),
105 if (scan_return
< 1) {
106 PrintAndLog((char *)usage_msg
);
107 PrintAndLog("Not enough APDUs! Try again!");
115 if (Cmd
[skip
] == '\0') {
116 if (i
< sizeof(apdu_lengths
) - 1) {
118 PrintAndLog((char *)usage_msg
);
127 // transfer the APDUs to the Proxmark
129 usb_cmd
.cmd
= CMD_EPA_PACE_REPLAY
;
130 for (int i
= 0; i
< sizeof(apdu_lengths
); i
++) {
132 usb_cmd
.arg
[0] = i
+ 1;
133 // transfer the APDU in several parts if necessary
134 for (int j
= 0; j
* sizeof(usb_cmd
.d
.asBytes
) < apdu_lengths
[i
]; j
++) {
135 // offset into the APDU
136 usb_cmd
.arg
[1] = j
* sizeof(usb_cmd
.d
.asBytes
);
137 // amount of data in this packet
138 int packet_length
= apdu_lengths
[i
] - (j
* sizeof(usb_cmd
.d
.asBytes
));
139 if (packet_length
> sizeof(usb_cmd
.d
.asBytes
)) {
140 packet_length
= sizeof(usb_cmd
.d
.asBytes
);
142 usb_cmd
.arg
[2] = packet_length
;
144 memcpy(usb_cmd
.d
.asBytes
, // + (j * sizeof(usb_cmd.d.asBytes)),
145 apdus
[i
] + (j
* sizeof(usb_cmd
.d
.asBytes
)),
147 SendCommand(&usb_cmd
);
148 WaitForResponse(CMD_ACK
, &resp
);
149 if (resp
.arg
[0] != 0) {
150 PrintAndLog("Transfer of APDU #%d Part %d failed!", i
, j
);
156 // now perform the replay
158 SendCommand(&usb_cmd
);
159 WaitForResponse(CMD_ACK
, &resp
);
160 if (resp
.arg
[0] != 0) {
161 PrintAndLog("\nPACE replay failed in step %u!", (uint32_t)resp
.arg
[0]);
162 PrintAndLog("Measured times:");
163 PrintAndLog("MSE Set AT: %u us", resp
.d
.asDwords
[0]);
164 PrintAndLog("GA Get Nonce: %u us", resp
.d
.asDwords
[1]);
165 PrintAndLog("GA Map Nonce: %u us", resp
.d
.asDwords
[2]);
166 PrintAndLog("GA Perform Key Agreement: %u us", resp
.d
.asDwords
[3]);
167 PrintAndLog("GA Mutual Authenticate: %u us", resp
.d
.asDwords
[4]);
169 PrintAndLog("PACE replay successfull!");
170 PrintAndLog("MSE Set AT: %u us", resp
.d
.asDwords
[0]);
171 PrintAndLog("GA Get Nonce: %u us", resp
.d
.asDwords
[1]);
172 PrintAndLog("GA Map Nonce: %u us", resp
.d
.asDwords
[2]);
173 PrintAndLog("GA Perform Key Agreement: %u us", resp
.d
.asDwords
[3]);
174 PrintAndLog("GA Mutual Authenticate: %u us", resp
.d
.asDwords
[4]);
181 ////////////////////////////////The new commands lie above here/////////////////////////////////////////////////////////////////////////////////////
182 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
186 static const command_t CommandTable
[] =
188 {"help", CmdHelp
, 1, "This help"},
189 {"cnonces", CmdHFEPACollectPACENonces
, 0,
190 "<m> <n> <d> Acquire n>0 encrypted PACE nonces of size m>0 with d sec pauses"},
191 {"preplay", CmdHFEPAPACEReplay
, 0,
192 "<mse> <get> <map> <pka> <ma> Perform PACE protocol by replaying given APDUs"},
193 {NULL
, NULL
, 0, NULL
}
196 int CmdHelp(const char *Cmd
)
198 CmdsHelp(CommandTable
);
202 int CmdHFEPA(const char *Cmd
)
205 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
208 CmdsParse(CommandTable
, Cmd
);