]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmdhfepa.c
Merge branch 'master' into topaz
[proxmark3-svn] / client / cmdhfepa.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------
2// Copyright (C) 2012 Frederik Möllers
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// Commands related to the German electronic Identification Card
9//-----------------------------------------------------------------------------
10
11#include "util.h"
12
13#include "proxmark3.h"
14#include "ui.h"
15#include "cmdparser.h"
16#include "common.h"
17#include "cmdmain.h"
18#include "sleep.h"
19#include "cmdhfepa.h"
20
21static int CmdHelp(const char *Cmd);
22
23// Perform (part of) the PACE protocol
24int CmdHFEPACollectPACENonces(const char *Cmd)
25{
26 // requested nonce size
27 unsigned int m = 0;
28 // requested number of Nonces
29 unsigned int n = 0;
30 // delay between requests
31 unsigned int d = 0;
32
33 sscanf(Cmd, "%u %u %u", &m, &n, &d);
34
35 // values are expected to be > 0
36 m = m > 0 ? m : 1;
37 n = n > 0 ? n : 1;
38
39 PrintAndLog("Collecting %u %"hhu"-byte nonces", n, m);
40 PrintAndLog("Start: %u", time(NULL));
41 // repeat n times
42 for (unsigned int i = 0; i < n; i++) {
43 // execute PACE
44 UsbCommand c = {CMD_EPA_PACE_COLLECT_NONCE, {(int)m, 0, 0}};
45 SendCommand(&c);
46 UsbCommand resp;
47
48 WaitForResponse(CMD_ACK,&resp);
49
50 // check if command failed
51 if (resp.arg[0] != 0) {
52 PrintAndLog("Error in step %d, Return code: %d",resp.arg[0],(int)resp.arg[1]);
53 } else {
54 size_t nonce_length = resp.arg[1];
55 char *nonce = (char *) malloc(2 * nonce_length + 1);
56 for(int j = 0; j < nonce_length; j++) {
57 sprintf(nonce + (2 * j), "%02X", resp.d.asBytes[j]);
58 }
59 // print nonce
60 PrintAndLog("Length: %d, Nonce: %s", nonce_length, nonce);
61 }
62 if (i < n - 1) {
63 sleep(d);
64 }
65 }
66 PrintAndLog("End: %u", time(NULL));
67
68 return 1;
69}
70
71////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72////////////////////////////////The commands lie below here/////////////////////////////////////////////////////////////////////////////////////////
73
74// perform the PACE protocol by replaying APDUs
75int CmdHFEPAPACEReplay(const char *Cmd)
76{
77 // the 4 APDUs which are replayed + their lengths
78 uint8_t msesa_apdu[41], gn_apdu[8], map_apdu[75];
79 uint8_t pka_apdu[75], ma_apdu[18], apdu_lengths[5] = {0};
80 // pointers to the arrays to be able to iterate
81 uint8_t *apdus[] = {msesa_apdu, gn_apdu, map_apdu, pka_apdu, ma_apdu};
82
83 // usage message
84 static const char const *usage_msg =
85 "Please specify 5 APDUs separated by spaces. "
86 "Example:\n preplay 0022C1A4 1068000000 1086000002 1234ABCDEF 1A2B3C4D";
87
88 // Proxmark response
89 UsbCommand resp;
90
91 int skip = 0, skip_add = 0, scan_return = 0;
92 // for each APDU
93 for (int i = 0; i < sizeof(apdu_lengths); i++) {
94 // scan to next space or end of string
95 while (Cmd[skip] != ' ' && Cmd[skip] != '\0') {
96 // convert
97 scan_return = sscanf(Cmd + skip, "%2X%n",
98 (unsigned int *) (apdus[i] + apdu_lengths[i]),
99 &skip_add);
100 if (scan_return < 1) {
101 PrintAndLog((char *)usage_msg);
102 PrintAndLog("Not enough APDUs! Try again!");
103 return 0;
104 }
105 skip += skip_add;
106 apdu_lengths[i]++;
107 }
108
109 // break on EOF
110 if (Cmd[skip] == '\0') {
111 if (i < sizeof(apdu_lengths) - 1) {
112
113 PrintAndLog((char *)usage_msg);
114 return 0;
115 }
116 break;
117 }
118 // skip the space
119 skip++;
120 }
121
122 // transfer the APDUs to the Proxmark
123 UsbCommand usb_cmd;
124 usb_cmd.cmd = CMD_EPA_PACE_REPLAY;
125 for (int i = 0; i < sizeof(apdu_lengths); i++) {
126 // APDU number
127 usb_cmd.arg[0] = i + 1;
128 // transfer the APDU in several parts if necessary
129 for (int j = 0; j * sizeof(usb_cmd.d.asBytes) < apdu_lengths[i]; j++) {
130 // offset into the APDU
131 usb_cmd.arg[1] = j * sizeof(usb_cmd.d.asBytes);
132 // amount of data in this packet
133 int packet_length = apdu_lengths[i] - (j * sizeof(usb_cmd.d.asBytes));
134 if (packet_length > sizeof(usb_cmd.d.asBytes)) {
135 packet_length = sizeof(usb_cmd.d.asBytes);
136 }
137 usb_cmd.arg[2] = packet_length;
138
139 memcpy(usb_cmd.d.asBytes, // + (j * sizeof(usb_cmd.d.asBytes)),
140 apdus[i] + (j * sizeof(usb_cmd.d.asBytes)),
141 packet_length);
142 SendCommand(&usb_cmd);
143 WaitForResponse(CMD_ACK, &resp);
144 if (resp.arg[0] != 0) {
145 PrintAndLog("Transfer of APDU #%d Part %d failed!", i, j);
146 return 0;
147 }
148 }
149 }
150
151 // now perform the replay
152 usb_cmd.arg[0] = 0;
153 SendCommand(&usb_cmd);
154 WaitForResponse(CMD_ACK, &resp);
155 if (resp.arg[0] != 0) {
156 PrintAndLog("\nPACE replay failed in step %u!", (uint32_t)resp.arg[0]);
157 PrintAndLog("Measured times:");
158 PrintAndLog("MSE Set AT: %u us", resp.d.asDwords[0]);
159 PrintAndLog("GA Get Nonce: %u us", resp.d.asDwords[1]);
160 PrintAndLog("GA Map Nonce: %u us", resp.d.asDwords[2]);
161 PrintAndLog("GA Perform Key Agreement: %u us", resp.d.asDwords[3]);
162 PrintAndLog("GA Mutual Authenticate: %u us", resp.d.asDwords[4]);
163 } else {
164 PrintAndLog("PACE replay successfull!");
165 PrintAndLog("MSE Set AT: %u us", resp.d.asDwords[0]);
166 PrintAndLog("GA Get Nonce: %u us", resp.d.asDwords[1]);
167 PrintAndLog("GA Map Nonce: %u us", resp.d.asDwords[2]);
168 PrintAndLog("GA Perform Key Agreement: %u us", resp.d.asDwords[3]);
169 PrintAndLog("GA Mutual Authenticate: %u us", resp.d.asDwords[4]);
170 }
171
172
173 return 1;
174}
175
176////////////////////////////////The new commands lie above here/////////////////////////////////////////////////////////////////////////////////////
177////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178
179// UI-related stuff
180
181static const command_t CommandTable[] =
182{
183 {"help", CmdHelp, 1, "This help"},
184 {"cnonces", CmdHFEPACollectPACENonces, 0,
185 "<m> <n> <d> Acquire n>0 encrypted PACE nonces of size m>0 with d sec pauses"},
186 {"preplay", CmdHFEPAPACEReplay, 0,
187 "<mse> <get> <map> <pka> <ma> Perform PACE protocol by replaying given APDUs"},
188 {NULL, NULL, 0, NULL}
189};
190
191int CmdHelp(const char *Cmd)
192{
193 CmdsHelp(CommandTable);
194 return 0;
195}
196
197int CmdHFEPA(const char *Cmd)
198{
199 // flush
200 WaitForResponseTimeout(CMD_ACK,NULL,100);
201
202 // parse
203 CmdsParse(CommandTable, Cmd);
204 return 0;
205}
Impressum, Datenschutz