aboutsummaryrefslogtreecommitdiff
path: root/include/stick20_commands.h
blob: d7d89cf21648c020032b6f8143733ee73d177b0c (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
#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 {

            class ChangeAdminUserPin20Current :
                public PasswordCommand<CommandID::SEND_PASSWORD, PasswordKind::Admin> {};
            class ChangeAdminUserPin20New :
                public PasswordCommand<CommandID::SEND_NEW_PASSWORD, PasswordKind::Admin> {};
            class UnlockUserPin :
                public PasswordCommand<CommandID::UNLOCK_USER_PASSWORD, PasswordKind::Admin> {};

            class EnableEncryptedPartition : public PasswordCommand<CommandID::ENABLE_CRYPTED_PARI> {};
            class DisableEncryptedPartition : public PasswordCommand<CommandID::DISABLE_CRYPTED_PARI> {};
            class EnableHiddenEncryptedPartition : public PasswordCommand<CommandID::ENABLE_HIDDEN_CRYPTED_PARI> {};
            class DisableHiddenEncryptedPartition : public PasswordCommand<CommandID::DISABLE_CRYPTED_PARI> {};
            class EnableFirmwareUpdate : public PasswordCommand<CommandID::ENABLE_FIRMWARE_UPDATE> {};

            class ChangeUpdatePassword : Command<CommandID::CHANGE_UPDATE_PIN> {
            public:
                struct CommandPayload {
                    uint8_t __gap;
                    uint8_t current_update_password[20];
                    uint8_t __gap2;
                    uint8_t new_update_password[20];
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( current_update_password );
                      print_to_ss( new_update_password );
                      return ss.str();
                    }
                };

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

            class ExportFirmware : public PasswordCommand<CommandID::EXPORT_FIRMWARE_TO_FILE> {};

            class CreateNewKeys :
                public PasswordCommand<CommandID::GENERATE_NEW_KEYS, PasswordKind::AdminPrefixed, 30> {};


            class FillSDCardWithRandomChars : Command<CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS> {
            public:
                enum class ChosenVolumes : uint8_t {
                    all_volumes = 0,
                    encrypted_volume = 1
                };

                struct CommandPayload {
                    uint8_t volume_flag;
                    uint8_t kind;
                    uint8_t admin_pin[20];

                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( (int) volume_flag );
                      print_to_ss( kind );
                      print_to_ss(admin_pin);
                      return ss.str();
                    }
                    void set_kind_user() {
                      kind = (uint8_t) 'P';
                    }
                    void set_defaults(){
                      set_kind_user();
                      volume_flag = static_cast<uint8_t>(ChosenVolumes::encrypted_volume);
                    }

                } __packed;

                typedef Transaction<Command<CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS>::command_id(),
                    struct CommandPayload, struct EmptyPayload>
                    CommandTransaction;
            };



            namespace DeviceConfigurationResponsePacket{
                static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 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];

                    uint8_t SendCounter_u8;
                    uint8_t SendDataType_u8;
                    uint8_t FollowBytesFlag_u8;
                    uint8_t SendSize_u8;

                    uint16_t MagicNumber_StickConfig_u16;
                    uint8_t ReadWriteFlagUncryptedVolume_u8;
                    uint8_t ReadWriteFlagCryptedVolume_u8;
                    uint8_t VersionInfo_au8[4];
                    uint8_t ReadWriteFlagHiddenVolume_u8;
                    uint8_t FirmwareLocked_u8;
                    uint8_t NewSDCardFound_u8;
                    uint8_t SDFillWithRandomChars_u8;
                    uint32_t ActiveSD_CardID_u32;
                    uint8_t VolumeActiceFlag_u8;
                    uint8_t NewSmartCardFound_u8;
                    uint8_t UserPwRetryCount;
                    uint8_t AdminPwRetryCount;
                    uint32_t ActiveSmartCardID_u32;
                    uint8_t StickKeysNotInitiated;

                    bool isValid() const { return true; }

                    std::string dissect() const {
                      std::stringstream ss;

                      print_to_ss((int) SendCounter_u8 );
                      print_to_ss((int) SendDataType_u8 );
                      print_to_ss((int) FollowBytesFlag_u8 );
                      print_to_ss((int) SendSize_u8 );

                      print_to_ss( MagicNumber_StickConfig_u16 );
                      print_to_ss((int) ReadWriteFlagUncryptedVolume_u8 );
                      print_to_ss((int) ReadWriteFlagCryptedVolume_u8 );
                      print_to_ss((int) VersionInfo_au8[1] );
                      print_to_ss((int) VersionInfo_au8[3] );
                      print_to_ss((int) ReadWriteFlagHiddenVolume_u8 );
                      print_to_ss((int) FirmwareLocked_u8 );
                      print_to_ss((int) NewSDCardFound_u8 );
                      print_to_ss((int) SDFillWithRandomChars_u8 );
                      print_to_ss( ActiveSD_CardID_u32 );
                      print_to_ss((int) VolumeActiceFlag_u8 );
                      print_to_ss((int) NewSmartCardFound_u8 );
                      print_to_ss((int) UserPwRetryCount );
                      print_to_ss((int) AdminPwRetryCount );
                      print_to_ss( ActiveSmartCardID_u32 );
                      print_to_ss((int) StickKeysNotInitiated );
                      ss << "_padding:" << std::endl
                         << ::nitrokey::misc::hexdump((const char *) (_padding),
                                                      sizeof _padding);
                      return ss.str();
                    }
                } __packed;
            }

            class SendStartup : Command<CommandID::SEND_STARTUP> {
            public:
                struct CommandPayload {
                    uint64_t localtime;  // POSIX seconds from epoch start, supports until year 2106
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss( localtime );
                      return ss.str();
                    }
                    void set_defaults(){
                      localtime =
                          std::chrono::duration_cast<std::chrono::seconds> (
                              std::chrono::system_clock::now().time_since_epoch()).count();
                    }
                }__packed;

                using ResponsePayload = DeviceConfigurationResponsePacket::ResponsePayload;

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


// TODO fix original nomenclature
            class SendSetReadonlyToUncryptedVolume : public PasswordCommand<CommandID::ENABLE_READONLY_UNCRYPTED_LUN> {};
            class SendSetReadwriteToUncryptedVolume : public PasswordCommand<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN> {};
            class SendClearNewSdCardFound : public PasswordCommand<CommandID::CLEAR_NEW_SD_CARD_FOUND> {};

            class GetDeviceStatus : Command<CommandID::GET_DEVICE_STATUS> {
            public:
                using ResponsePayload = DeviceConfigurationResponsePacket::ResponsePayload;

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

            class GetSDCardOccupancy : Command<CommandID::SD_CARD_HIGH_WATERMARK> {
            public:
                struct ResponsePayload {
                    uint8_t WriteLevelMin;
                    uint8_t WriteLevelMax;
                    uint8_t ReadLevelMin;
                    uint8_t ReadLevelMax;
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss((int) WriteLevelMin);
                      print_to_ss((int) WriteLevelMax);
                      print_to_ss((int) ReadLevelMin);
                      print_to_ss((int) ReadLevelMax);
                      return ss.str();
                    }
                } __packed;

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


            class SetupHiddenVolume : Command<CommandID::SEND_HIDDEN_VOLUME_SETUP> {
            public:
                constexpr static int MAX_HIDDEN_VOLUME_PASSWORD_SIZE = 20;
                struct CommandPayload {
                    uint8_t SlotNr_u8;
                    uint8_t StartBlockPercent_u8;
                    uint8_t EndBlockPercent_u8;
                    uint8_t HiddenVolumePassword_au8[MAX_HIDDEN_VOLUME_PASSWORD_SIZE];
                    std::string dissect() const {
                      std::stringstream ss;
                      print_to_ss((int) SlotNr_u8);
                      print_to_ss((int) StartBlockPercent_u8);
                      print_to_ss((int) EndBlockPercent_u8);
                      print_to_ss(HiddenVolumePassword_au8);
                      return ss.str();
                    }
                } __packed;

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


            class LockFirmware : public PasswordCommand<CommandID::SEND_LOCK_STICK_HARDWARE> {};

            class ProductionTest : Command<CommandID::PRODUCTION_TEST> {
            public:
                static constexpr int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1;
                static constexpr int payload_absolute_begin = 8;
                static constexpr int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin;

                struct ResponsePayload {
                    uint8_t _padding[padding_size];

                    uint8_t SendCounter_u8;
                    uint8_t SendDataType_u8;
                    uint8_t FollowBytesFlag_u8;
                    uint8_t SendSize_u8;

                    uint8_t FirmwareVersion_au8[2];        // 2 byte // 2
                    uint8_t FirmwareVersionInternal_u8;    // 1 byte // 3
                    uint8_t SD_Card_Size_u8;               // 1 byte // 4
                    uint32_t CPU_CardID_u32;                     // 4 byte // 8
                    uint32_t SmartCardID_u32;                    // 4 byte // 12
                    uint32_t SD_CardID_u32;                      // 4 byte // 16
                    uint8_t SC_UserPwRetryCount;           // User PIN retry count 1 byte  // 17
                    uint8_t SC_AdminPwRetryCount;          // Admin PIN retry count 1 byte // 18
                    uint8_t SD_Card_ManufacturingYear_u8;  // 1 byte // 19
                    uint8_t SD_Card_ManufacturingMonth_u8; // 1 byte // 20
                    uint16_t SD_Card_OEM_u16;              // 2 byte // 22
                    uint16_t SD_WriteSpeed_u16;            // in kbyte / sec 2 byte // 24
                    uint8_t SD_Card_Manufacturer_u8;       // 1 byte // 25

                    bool isValid() const { return true; }

                    std::string dissect() const {
                      std::stringstream ss;

                      print_to_ss((int) SendCounter_u8);
                      print_to_ss((int) SendDataType_u8);
                      print_to_ss((int) FollowBytesFlag_u8);
                      print_to_ss((int) SendSize_u8);

                      print_to_ss((int) FirmwareVersion_au8[0]);
                      print_to_ss((int) FirmwareVersion_au8[1]);
                      print_to_ss((int) FirmwareVersionInternal_u8);
                      print_to_ss((int) SD_Card_Size_u8);
                      print_to_ss( CPU_CardID_u32);
                      print_to_ss( SmartCardID_u32);
                      print_to_ss( SD_CardID_u32);
                      print_to_ss((int) SC_UserPwRetryCount);
                      print_to_ss((int) SC_AdminPwRetryCount);
                      print_to_ss((int) SD_Card_ManufacturingYear_u8);
                      print_to_ss((int) SD_Card_ManufacturingMonth_u8);
                      print_to_ss( SD_Card_OEM_u16);
                      print_to_ss( SD_WriteSpeed_u16);
                      print_to_ss((int) SD_Card_Manufacturer_u8);

                      ss << "_padding:" << std::endl
                         << ::nitrokey::misc::hexdump((const char *) (_padding),
                                                      sizeof _padding);
                      return ss.str();
                    }

                  } __packed;

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

#undef print_to_ss

#endif