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