]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfepa.c
FIX: @Aczid suggested this to fix a Coverity scan warning.
[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
1a07fd51 39 PrintAndLog("Collecting %u %"hhu"-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);
7f6ccd39 61 free(nonce);
5acd09bd 62 }
69f8a37b 63 if (i < n - 1) {
64 sleep(d);
65 }
5acd09bd 66 }
67 PrintAndLog("End: %u", time(NULL));
5acd09bd 68 return 1;
69}
70
3bb07d96
FM
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
dfc5ee88 84 static const char *usage_msg =
3bb07d96
FM
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
5acd09bd 179// UI-related stuff
180
7144c99b 181static const command_t CommandTable[] = {
5acd09bd 182 {"help", CmdHelp, 1, "This help"},
69f8a37b 183 {"cnonces", CmdHFEPACollectPACENonces, 0,
184 "<m> <n> <d> Acquire n>0 encrypted PACE nonces of size m>0 with d sec pauses"},
3bb07d96
FM
185 {"preplay", CmdHFEPAPACEReplay, 0,
186 "<mse> <get> <map> <pka> <ma> Perform PACE protocol by replaying given APDUs"},
5acd09bd 187 {NULL, NULL, 0, NULL}
188};
189
7144c99b 190int CmdHelp(const char *Cmd) {
5acd09bd 191 CmdsHelp(CommandTable);
192 return 0;
193}
194
7144c99b 195int CmdHFEPA(const char *Cmd) {
5acd09bd 196 // flush
28415b5d 197 clearCommandBuffer();
198 //WaitForResponseTimeout(CMD_ACK,NULL,100);
5acd09bd 199 // parse
28415b5d 200 CmdsParse(CommandTable, Cmd);
201 return 0;
3bb07d96 202}
Impressum, Datenschutz