summaryrefslogtreecommitdiff
path: root/include/stick20_commands.h
blob: 5ee16de86326a403824cdda7b0be3b25904f4b44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#ifndef STICK20_COMMANDS_H
#define STICK20_COMMANDS_H

#include "inttypes.h"
#include "command.h"
#include <string>
#include <sstream>
#include "device_proto.h"


namespace nitrokey {
    namespace proto {

/*
*	STICK20 protocol command ids
*	a superset (almost) of STICK10
*/
#define print_to_ss(x) ( ss << " #x:\t" << (x) << std::endl );
        namespace stick20 {

            enum class PasswordKind : uint8_t {
                User = 'P',
                Admin = 'A'
            };

            class ChangeAdminUserPin20Current : Command<CommandID::STICK20_CMD_SEND_PASSWORD> {
            public:
                struct CommandPayload {
                    uint8_t kind;
                    uint8_t old_pin[20];

                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( kind );
                      ss << " old_pin:\t" << old_pin << std::endl;
                      return ss.str();
                    }

                    void set_kind(PasswordKind k) {
                      kind = (uint8_t) k;
                    }
                } __packed;

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };


            class ChangeAdminUserPin20New : Command<CommandID::STICK20_CMD_SEND_NEW_PASSWORD> {
            public:

                struct CommandPayload {
                    uint8_t kind;
                    uint8_t new_pin[20];

                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( kind );
                      ss << " new_pin:\t" << new_pin << std::endl;
                      return ss.str();
                    }

                    void set_kind(PasswordKind k) {
                      kind = (uint8_t) k;
                    }

                } __packed;

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };


            class UnlockUserPassword : Command<CommandID::UNLOCK_USER_PASSWORD> {
            public:
                struct CommandPayload {
                    uint8_t kind;
                    uint8_t user_new_password[20];

                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( kind );
                      ss << " user_new_password:\t" << user_new_password << std::endl;
                      return ss.str();
                    }

                    void set_kind(PasswordKind k) {
                      kind = (uint8_t) k;
                    }
                } __packed;

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class EnableEncryptedPartition : public PasswordCommand<CommandID::ENABLE_CRYPTED_PARI> {};
            class DisableEncryptedPartition : public PasswordCommand<CommandID::DISABLE_CRYPTED_PARI> {};

            class EnableHiddenEncryptedPartition : Command<CommandID::ENABLE_HIDDEN_CRYPTED_PARI> {
            public:
                struct CommandPayload {
                    uint8_t password[30];  // TODO check w/ firmware
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( password );
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class DisableHiddenEncryptedPartition : Command<CommandID::DISABLE_CRYPTED_PARI> {
            public:
                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class EnableFirmwareUpdate : Command<CommandID::ENABLE_FIRMWARE_UPDATE> {
            public:
                struct CommandPayload {
                    uint8_t password[30];  // TODO check w/ firmware
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( password );
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class UpdatePassword : Command<CommandID::CHANGE_UPDATE_PIN> {
            public:
                struct CommandPayload {
                    uint8_t old_password[15];
                    uint8_t new_password[15];
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( old_password );
                      print_to_ss( new_password );
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class ExportFirmware : Command<CommandID::EXPORT_FIRMWARE_TO_FILE> {
            public:
                struct CommandPayload {
                    uint8_t password[30];
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( password );
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class CreateNewKeys : Command<CommandID::GENERATE_NEW_KEYS> {
            public:
                struct CommandPayload {
                    uint8_t kind;
                    uint8_t admin_password[30]; //CS20_MAX_PASSWORD_LEN
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( kind );
                      ss << " admin_password:\t" << admin_password << std::endl;
                      return ss.str();
                    }

                    void setKindPrefixed() {
                      kind = 'P';
                    }
                } __packed;

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };


            class FillSDCardWithRandomChars : Command<CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS> {
            public:
                struct CommandPayload {
                    uint8_t volume_flag;
                    uint8_t password[30];
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( (int)volume_flag );
                      print_to_ss( password );
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class SetupHiddenVolume : Command<CommandID::SEND_HIDDEN_VOLUME_SETUP> {
            public:
                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

#define d(x) ss << " "#x":\t" << (int)x << std::endl;

            class GetDeviceStatus : Command<CommandID::GET_DEVICE_STATUS> {
            public:
                static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 20 + 1;
                static const int payload_absolute_begin = 8;
                static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin;

                struct ResponsePayload {
                    uint8_t _padding[padding_size]; //TODO confirm padding in Storage firmware
                    //data starts from 21st byte of packet -> 13th byte of payload
                    uint8_t command_counter;
                    uint8_t last_command;
                    uint8_t status;
                    uint8_t progress_bar_value;

                    bool isValid() const { return true; }

                    std::string dissect() const {
                      std::stringstream ss;
                      d(command_counter);
                      d(last_command);
                      d(status);
                      d(progress_bar_value);
                      ss << "_padding:\t"
                         << ::nitrokey::misc::hexdump((const char *) (_padding),
                                                      sizeof _padding);
                      return ss.str();
                    }
                } __packed;

                typedef Transaction<command_id(), struct EmptyPayload, struct ResponsePayload>
                    CommandTransaction;
            };

#undef d


// TODO fix original nomenclature
            class SendSetReadonlyToUncryptedVolume : Command<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN> {
            public:
                struct CommandPayload {
                    uint8_t password[30];
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( password );
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class SendSetReadwriteToUncryptedVolume : Command<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN> {
            public:
                struct CommandPayload {
                    uint8_t password[30];
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( password );
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class SendClearNewSdCardFound : Command<CommandID::CLEAR_NEW_SD_CARD_FOUND> {
            public:
                struct CommandPayload {
                    uint8_t password[30];
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( password );
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class SendStartup : Command<CommandID::SEND_STARTUP> {
            public:
                struct CommandPayload {
                    uint64_t localtime;  // POSIX
                    std::string dissect() const {
                      std::stringstream ss;
//                      ss << " admin_password:\t" << admin_password << std::endl;
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class SendHiddenVolumeSetup : Command<CommandID::SEND_HIDDEN_VOLUME_SETUP> {
            public:
                struct CommandPayload {
                    // TODO HiddenVolumeSetup_tst type
                    std::string dissect() const {
                      std::stringstream ss;
//                      ss << " admin_password:\t" << admin_password << std::endl;
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class LockFirmware : Command<CommandID::SEND_LOCK_STICK_HARDWARE> {
            public:
                struct CommandPayload {
                    uint8_t password[30];
                    std::string dissect() const {
                      std::stringstream ss;
//                      ss << " admin_password:\t" << admin_password << std::endl;
                      return ss.str();
                    }
                };

                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };

            class ProductionTest : Command<CommandID::PRODUCTION_TEST> {
            public:
                typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };
        }
    }
}

#undef print_to_ss

#endif