aboutsummaryrefslogtreecommitdiff
path: root/include/device_proto.h
blob: fb1dd4e4b0960d19bd16c23a8947f2508b52c031 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
#ifndef DEVICE_PROTO_H
#define DEVICE_PROTO_H
#include <utility>
#include <type_traits>
#include <stdexcept>
#include <string>
#include <strings.h>
// a local version for compatibility with Windows
#include "inttypes.h"
#include "cxx_semantics.h"
#include "device.h"
#include "utils/crc32.h"

#define STICK20_UPDATE_MODE_VID 0x03EB
#define STICK20_UPDATE_MODE_PID 0x2FF1

#define PAYLOAD_SIZE 53
#define PWS_SLOT_COUNT 16
#define PWS_SLOTNAME_LENGTH 11
#define PWS_PASSWORD_LENGTH 20
#define PWS_LOGINNAME_LENGTH 32

#define PWS_SEND_PASSWORD     0
#define PWS_SEND_LOGINNAME    1
#define PWS_SEND_TAB          2
#define PWS_SEND_CR           3

namespace device {
	class Device;
	enum class CommError;
}

static inline uint32_t crc(const uint8_t *data, size_t size) {
	uint32_t crc = 0xffffffff;
	const uint32_t *pend = (const uint32_t *)(data + size);
	for (const uint32_t *p = (const uint32_t *)(data); p < pend; p++)
		crc = Crc32 (crc, *p);
	return crc;
}

namespace proto {
enum class CommandID : uint8_t {
	GET_STATUS                     = 0x00,
	WRITE_TO_SLOT                  = 0x01,
	READ_SLOT_NAME                 = 0x02,
	READ_SLOT                      = 0x03,
	GET_CODE                       = 0x04,
	WRITE_CONFIG                   = 0x05,
	ERASE_SLOT                     = 0x06,
	FIRST_AUTHENTICATE             = 0x07,
	AUTHORIZE                      = 0x08,
	GET_PASSWORD_RETRY_COUNT       = 0x09,
	CLEAR_WARNING                  = 0x0A,
	SET_TIME                       = 0x0B,
	TEST_COUNTER                   = 0x0C,
	TEST_TIME                      = 0x0D,
	USER_AUTHENTICATE              = 0x0E,
	GET_USER_PASSWORD_RETRY_COUNT  = 0x0F,
	USER_AUTHORIZE                 = 0x10,
	UNLOCK_USER_PASSWORD           = 0x11,
	LOCK_DEVICE                    = 0x12,
	FACTORY_RESET                  = 0x13,
	CHANGE_USER_PIN                = 0x14,
	CHANGE_ADMIN_PIN               = 0x15,

	ENABLE_CRYPTED_PARI = 0x20,
	DISABLE_CRYPTED_PARI,
	ENABLE_HIDDEN_CRYPTED_PARI,
	DISABLE_HIDDEN_CRYPTED_PARI,
	ENABLE_FIRMWARE_UPDATE,
	EXPORT_FIRMWARE_TO_FILE,
	GENERATE_NEW_KEYS,
	FILL_SD_CARD_WITH_RANDOM_CHARS,

	WRITE_STATUS_DATA,
	ENABLE_READONLY_UNCRYPTED_LUN,
	ENABLE_READWRITE_UNCRYPTED_LUN,

	SEND_PASSWORD_MATRIX,
	SEND_PASSWORD_MATRIX_PINDATA,
	SEND_PASSWORD_MATRIX_SETUP,

	GET_DEVICE_STATUS,
	SEND_DEVICE_STATUS,

	SEND_HIDDEN_VOLUME_PASSWORD,
	SEND_HIDDEN_VOLUME_SETUP,
	SEND_PASSWORD,
	SEND_NEW_PASSWORD,
	CLEAR_NEW_SD_CARD_FOUND,

	SEND_STARTUP,
	SEND_CLEAR_STICK_KEYS_NOT_INITIATED,
	SEND_LOCK_STICK_HARDWARE,

	PRODUCTION_TEST,
	SEND_DEBUG_DATA,

	CHANGE_UPDATE_PIN,

	GET_PW_SAFE_SLOT_STATUS			= 0x60,
	GET_PW_SAFE_SLOT_NAME      		= 0x61,
	GET_PW_SAFE_SLOT_PASSWORD  		= 0x62,
	GET_PW_SAFE_SLOT_LOGINNAME 		= 0x63,
	SET_PW_SAFE_SLOT_DATA_1    		= 0x64,
	SET_PW_SAFE_SLOT_DATA_2    		= 0x65,
	PW_SAFE_ERASE_SLOT         		= 0x66,
	PW_SAFE_ENABLE             		= 0x67,
	PW_SAFE_INIT_KEY           		= 0x68,
	PW_SAFE_SEND_DATA          		= 0x69,
	SD_CARD_HIGH_WATERMARK     		= 0x70,
	DETECT_SC_AES              		= 0x6a,
	NEW_AES_KEY                		= 0x6b
};

/*
 *	POD types for HID proto commands
 *	Instances are meant to be __packed.
 *
 *	TODO (future) support for Big Endian
 */

/*
 *	Every packet is a USB HID report (check USB spec)
 */
template <CommandID cmd_id, typename Payload>
struct HIDReport {
	uint8_t _zero;
	CommandID command_id; // uint8_t
	union {
		uint8_t _padding[HID_REPORT_SIZE - 6];
		Payload payload;
	} __packed;
	uint32_t crc;

	// POD types can't have non-default constructors
	// used in Transaction<>::run()
	void initialize() {
		bzero(this, sizeof *this);
		command_id = cmd_id;
	}

	uint32_t calculate_CRC() const {
		// w/o leading zero, a part of each HID packet
		// w/o 4-byte crc
		return ::crc((const uint8_t *)(this) + 1,
					(size_t)(HID_REPORT_SIZE - 5));
	}

	void update_CRC() {
		crc = calculate_CRC();
	}

	bool isCRCcorrect() const {
		return crc == calculate_CRC();
	}

	bool isValid() const {
		return !_zero && payload.isValid() && isCRCcorrect();
	}
} __packed;

/*
 *	Response payload (the parametrized type inside struct HIDReport)
 *
 *	command_id member in incoming HIDReport structure carries the command
 *	type last used.
 */
template <typename ResponsePayload>
struct DeviceResponse {
	uint8_t _zero;
	uint8_t device_status;
	uint8_t last_command_type;
	uint32_t last_command_crc;
	uint8_t	last_command_status;
	union {
		uint8_t _padding[HID_REPORT_SIZE - 12];
		ResponsePayload payload;
	} __packed;
	uint32_t crc;

	void initialize() {
		bzero(this, sizeof *this);
	}

	uint32_t calculate_CRC() const {
		// w/o leading zero, a part of each HID packet
		// w/o 4-byte crc
		return ::crc((const uint8_t *)(this) + 1,
					(size_t)(HID_REPORT_SIZE - 5));
	}

	void update_CRC() {
		crc = calculate_CRC();
	}

	bool isCRCcorrect() const {
		return crc == calculate_CRC();
	}

	bool isValid() const {
		return !_zero && payload.isValid() && isCRCcorrect();
	}
} __packed;

struct EmptyPayload {
	uint8_t _data[];

	bool isValid() const {
		return true;
	}
} __packed;

template<CommandID cmd_id, typename command_payload,
							typename response_payload>
class Transaction : semantics::non_constructible {
public:
	// Types declared in command class scope can't be reached from there.
	typedef command_payload CommandPayload;
	typedef response_payload ResponsePayload;

	typedef struct HIDReport<cmd_id, CommandPayload> OutgoingPacket;
	typedef struct DeviceResponse<ResponsePayload> ResponsePacket;

	static_assert(std::is_pod<OutgoingPacket>::value,
					"OutgoingPacket must be a POD type");
	static_assert(std::is_pod<ResponsePacket>::value,
					"ResponsePacket must be a POD type");
	static_assert(sizeof(OutgoingPacket) == HID_REPORT_SIZE,
					"OutgoingPacket type is not the right size");
	static_assert(sizeof(ResponsePacket) == HID_REPORT_SIZE,
					"ResponsePacket type is not the right size");

	static response_payload run(device::Device &dev,
			const command_payload &payload) {
		using namespace device;

		CommError status;
		OutgoingPacket outp;
		ResponsePacket resp;

		// POD types can't have non-default constructors
		outp.initialize();
		resp.initialize();

		outp.payload = payload;
		outp.update_CRC();

		if (!outp.isValid())
			throw std::runtime_error("Invalid outgoing packet");

		status = dev.send(&outp);
		if ((int)(status) < 0 && status != CommError::ERR_NO_ERROR) 
			throw std::runtime_error(
				std::string("Device error while sending command ") +
				std::to_string((int)(status)));

		status = dev.recv(&resp);
		if ((int)(status) < 0 && status != CommError::ERR_NO_ERROR) 
			throw std::runtime_error(
				std::string("Device error while executing command ") +
				std::to_string((int)(status)));

		if (!resp.isValid())
			throw std::runtime_error("Invalid incoming packet");

		// See: DeviceResponse
		return resp.payload;
	}

	static response_payload run(device::Device &dev) {
		command_payload empty_payload;
		return run(dev, empty_payload);
	}
};

/*
 *	Stick10 protocol definition
 */
namespace stick10 {
	namespace command {
		class GetSlotName : semantics::non_constructible {
		public:
			// reachable as a typedef in Transaction
			struct CommandPayload {
				uint8_t slot_number;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			struct ResponsePayload {
				uint8_t slot_name[15];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::READ_SLOT_NAME,
								struct CommandPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class EraseSlot : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			typedef Transaction<CommandID::ERASE_SLOT,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};

		class SetTime : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t		reset; // 0 - get time, 1 - set time
				uint64_t	time; // posix time

				bool isValid() const {
					return reset && reset != 1;
				}
			} __packed;

			typedef Transaction<CommandID::SET_TIME,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};

		// TODO duplicate TOTP
		class WriteToHOTPSlot : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;
				uint8_t slot_name[15];
				uint8_t slot_secret[20];
				uint8_t slot_config;
				uint8_t slot_token_id[13];
				uint8_t slot_counter[8];

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			typedef Transaction<CommandID::WRITE_TO_SLOT,
							struct CommandPayload,
							struct EmptyPayload> CommandTransaction;
		};

		class WriteToTOTPSlot : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;
				uint8_t slot_name[15];
				uint8_t slot_secret[20];
				uint8_t slot_config;
				uint8_t slot_token_id[13];
				uint16_t slot_interval;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			typedef Transaction<CommandID::WRITE_TO_SLOT,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};

		class GetCode : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;
				uint64_t challenge;
				uint64_t last_totp_time;
				uint8_t	last_interval;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			struct ResponsePayload {
				uint8_t code[18];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::GET_CODE,
								struct CommandPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class GetHOTP : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			typedef Transaction<CommandID::GET_CODE,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};

		class ReadSlot : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			struct ResponsePayload {
				uint8_t slot_name[15];
				uint8_t config;
				uint8_t token_id[13];
				uint64_t counter;

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::READ_SLOT,
								struct CommandPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class GetStatus : semantics::non_constructible {
		public:
			struct ResponsePayload {
				uint16_t firmware_version;
				uint8_t	card_serial[4];
				uint8_t	general_config[3];
				uint8_t	otp_password_config[2];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::GET_STATUS,
								struct EmptyPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class GetPasswordRetryCount : semantics::non_constructible {
		public:
			struct ResponsePayload {
				uint8_t password_retry_count;

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::GET_PASSWORD_RETRY_COUNT,
								struct EmptyPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class GetUserPasswordRetryCount : semantics::non_constructible {
		public:
			struct ResponsePayload {
				uint8_t password_retry_count;

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::GET_USER_PASSWORD_RETRY_COUNT,
								struct EmptyPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class GetPasswordSafeSlotStatus : semantics::non_constructible {
		public:
			struct ResponsePayload {
				uint8_t password_safe_status[PWS_SLOT_COUNT];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::GET_PW_SAFE_SLOT_STATUS,
								struct EmptyPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class GetPasswordSafeSlotName : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			struct ResponsePayload {
				uint8_t slot_name[PWS_SLOTNAME_LENGTH];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::GET_PW_SAFE_SLOT_NAME,
								struct CommandPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class GetPasswordSafeSlotPassword : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			struct ResponsePayload {
				uint8_t slot_password[PWS_PASSWORD_LENGTH];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::GET_PW_SAFE_SLOT_PASSWORD,
								struct CommandPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class GetPasswordSafeSlotLogin : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			struct ResponsePayload {
				uint8_t slot_login[PWS_LOGINNAME_LENGTH];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::GET_PW_SAFE_SLOT_LOGINNAME,
								struct CommandPayload,
								struct ResponsePayload> CommandTransaction;
		};

		class SetPasswordSafeSlotData : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;
				uint8_t slot_name[PWS_SLOTNAME_LENGTH];
				uint8_t slot_password[PWS_PASSWORD_LENGTH];

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			typedef Transaction<CommandID::SET_PW_SAFE_SLOT_DATA_1,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};

		class SetPasswordSafeSlotData2 : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;
				uint8_t slot_name[PWS_SLOTNAME_LENGTH];

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			typedef Transaction<CommandID::SET_PW_SAFE_SLOT_DATA_2,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};

		class ErasePasswordSafeSlot : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			typedef Transaction<CommandID::PW_SAFE_ERASE_SLOT,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};

		class EnablePasswordSafe : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::PW_SAFE_ENABLE,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};

		class PasswordSafeInitKey : semantics::non_constructible {
		public:
			typedef Transaction<CommandID::PW_SAFE_INIT_KEY,
								struct EmptyPayload,
								struct EmptyPayload> CommandTransaction;
		};

		// TODO naming screwed up, see above
		class PasswordSafeSendSlotViaHID: semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t slot_number;
				uint8_t slot_kind;

				bool isValid() const {
					return !(slot_number & 0xF0);
				}
			} __packed;

			typedef Transaction<CommandID::PW_SAFE_SEND_DATA,
								struct CommandPayload,
								struct EmptyPayload> CommandTransaction;
		};


		// TODO "Device::passwordSafeSendSlotDataViaHID"

		class WriteGeneralConfig : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t	config[5];
			} __packed;

			typedef Transaction<CommandID::WRITE_CONFIG,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class FirstAuthenticate : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t card_password[25];
				uint8_t temporary_password[25];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::FIRST_AUTHENTICATE,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class UserAuthenticate : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t card_password[25];
				uint8_t temporary_password[25];

				bool isValid() const {
					return true;
				}
			} __packed;

			typedef Transaction<CommandID::USER_AUTHENTICATE,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class Authorize : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t crc[4];
				uint8_t password[25];
			} __packed;

			typedef Transaction<CommandID::AUTHORIZE,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class UserAuthorize : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t crc[4];
				uint8_t password[25];
			} __packed;

			typedef Transaction<CommandID::USER_AUTHORIZE,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class UnlockUserPassword : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t admin_password[20]; // TODO
			} __packed;

			// TODO could we get the stick to return the retry count?

			typedef Transaction<CommandID::UNLOCK_USER_PASSWORD,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class ChangeUserPin : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t old_pin[25];
				uint8_t new_pin[25];
			} __packed;

			typedef Transaction<CommandID::CHANGE_USER_PIN,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		// TODO why is it needed?
		class IsAESSupported : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[20];
			} __packed;

			typedef Transaction<CommandID::DETECT_SC_AES,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class ChangeAdminPin : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t old_pin[25];
				uint8_t new_pin[25];
			} __packed;

			typedef Transaction<CommandID::CHANGE_ADMIN_PIN,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class LockDevice : semantics::non_constructible {
		public:
			typedef Transaction<CommandID::LOCK_DEVICE,
									struct EmptyPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class FactoryReset : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[20];
			} __packed;

			typedef Transaction<CommandID::FACTORY_RESET,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class BuildAESKey : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[20];
			} __packed;

			typedef Transaction<CommandID::NEW_AES_KEY,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};
	}
}

/*
*	STICK20 protocol command ids
*	a superset of STICK10
*/
namespace stick20 {
	namespace command {
		class EnableEncryptedPartition : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30]; // TODO check w/ firmware
			};

			typedef Transaction<CommandID::ENABLE_CRYPTED_PARI,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class DisableEncryptedPartition : semantics::non_constructible {
		public:
			typedef Transaction<CommandID::DISABLE_CRYPTED_PARI,
									struct EmptyPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class EnableHiddenEncryptedPartition : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30]; // TODO check w/ firmware
			};

			typedef Transaction<CommandID::ENABLE_HIDDEN_CRYPTED_PARI,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class DisableHiddenEncryptedPartition : semantics::non_constructible {
		public:
			typedef Transaction<CommandID::DISABLE_CRYPTED_PARI,
									struct EmptyPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class EnableFirmwareUpdate : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30]; // TODO check w/ firmware
			};

			typedef Transaction<CommandID::ENABLE_FIRMWARE_UPDATE,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class UpdatePassword : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t old_password[15];
				uint8_t new_password[15];
			};

			typedef Transaction<CommandID::CHANGE_UPDATE_PIN,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class ExportFirmware : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];
			};

			typedef Transaction<CommandID::EXPORT_FIRMWARE_TO_FILE,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class CreateNewKeys : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];
			};

			typedef Transaction<CommandID::GENERATE_NEW_KEYS,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class FillSDCardWithRandomChars : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t volume_flag;
				uint8_t password[30];
			};

			typedef Transaction<CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SetupHiddenVolume : semantics::non_constructible {
		public:
			typedef Transaction<CommandID::SEND_HIDDEN_VOLUME_SETUP,
									struct EmptyPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendPasswordMatrix : semantics::non_constructible {
		public:
			typedef Transaction<CommandID::SEND_PASSWORD_MATRIX,
									struct EmptyPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendPasswordMatrixPinData : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t pin_data[30]; // TODO how long actually can it be?
			};

			typedef Transaction<CommandID::SEND_PASSWORD_MATRIX_PINDATA,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendPasswordMatrixSetup : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t setup_data[30]; // TODO how long actually can it be?
			};

			typedef Transaction<CommandID::SEND_PASSWORD_MATRIX_SETUP,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class GetDeviceStatus : semantics::non_constructible {
		public:
			typedef Transaction<CommandID::GET_DEVICE_STATUS,
									struct EmptyPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendPassword : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];
			};

			typedef Transaction<CommandID::SEND_PASSWORD,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendNewPassword : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];
			};

			typedef Transaction<CommandID::SEND_NEW_PASSWORD,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		// TODO fix original nomenclature
		class SendSetReadonlyToUncryptedVolume : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];
			};

			typedef Transaction<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendSetReadwriteToUncryptedVolume : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];
			};

			typedef Transaction<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendClearNewSdCardFound : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];
			};

			typedef Transaction<CommandID::CLEAR_NEW_SD_CARD_FOUND,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendStartup : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint64_t localtime; // POSIX
			};

			typedef Transaction<CommandID::SEND_STARTUP,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class SendHiddenVolumeSetup : semantics::non_constructible {
		public:
			struct CommandPayload {
				// TODO HiddenVolumeSetup_tst type
			};

			typedef Transaction<CommandID::SEND_HIDDEN_VOLUME_SETUP,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class LockFirmware : semantics::non_constructible {
		public:
			struct CommandPayload {
				uint8_t password[30];
			};

			typedef Transaction<CommandID::SEND_LOCK_STICK_HARDWARE,
									struct CommandPayload,
									struct EmptyPayload> CommandTransaction;
		};

		class ProductionTest : semantics::non_constructible {
		public:
			typedef Transaction<CommandID::PRODUCTION_TEST,
									struct EmptyPayload,
									struct EmptyPayload> CommandTransaction;
		};
	}
}
}

#endif