-
Notifications
You must be signed in to change notification settings - Fork 48
/
okcore.cpp
7581 lines (7265 loc) · 207 KB
/
okcore.cpp
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
/*
* Copyright (c) 2015-2022, CryptoTrust LLC.
* All rights reserved.
*
* Author : Tim Steiner <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by CryptoTrust LLC. for
* the OnlyKey Project (https://crp.to/ok)"
*
* 4. The names "OnlyKey" and "CryptoTrust" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
*
* 5. Products derived from this software may not be called "OnlyKey"
* nor may "OnlyKey" or "CryptoTrust" appear in their names without
* specific prior written permission. For written permission, please
* contact [email protected].
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by CryptoTrust LLC. for
* the OnlyKey Project (https://crp.to/ok)"
*
* 7. Redistributions in any form must be accompanied by information on
* how to obtain complete source code for this software and any
* accompanying software that uses this software. The source code
* must either be included in the distribution or be available for
* no more than the cost of distribution plus a nominal fee, and must
* be freely redistributable under reasonable conditions. For a
* binary file, complete source code means the source code for all
* modules it contains.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS
* ARE GRANTED BY THIS LICENSE. IF SOFTWARE RECIPIENT INSTITUTES PATENT
* LITIGATION AGAINST ANY ENTITY (INCLUDING A CROSS-CLAIM OR COUNTERCLAIM
* IN A LAWSUIT) ALLEGING THAT THIS SOFTWARE (INCLUDING COMBINATIONS OF THE
* SOFTWARE WITH OTHER SOFTWARE OR HARDWARE) INFRINGES SUCH SOFTWARE
* RECIPIENT'S PATENT(S), THEN SUCH SOFTWARE RECIPIENT'S RIGHTS GRANTED BY
* THIS LICENSE SHALL TERMINATE AS OF THE DATE SUCH LITIGATION IS FILED. IF
* ANY PROVISION OF THIS AGREEMENT IS INVALID OR UNENFORCEABLE UNDER
* APPLICABLE LAW, IT SHALL NOT AFFECT THE VALIDITY OR ENFORCEABILITY OF THE
* REMAINDER OF THE TERMS OF THIS AGREEMENT, AND WITHOUT FURTHER ACTION
* BY THE PARTIES HERETO, SUCH PROVISION SHALL BE REFORMED TO THE MINIMUM
* EXTENT NECESSARY TO MAKE SUCH PROVISION VALID AND ENFORCEABLE. ALL
* SOFTWARE RECIPIENT'S RIGHTS UNDER THIS AGREEMENT SHALL TERMINATE IF IT
* FAILS TO COMPLY WITH ANY OF THE MATERIAL TERMS OR CONDITIONS OF THIS
* AGREEMENT AND DOES NOT CURE SUCH FAILURE IN A REASONABLE PERIOD OF
* TIME AFTER BECOMING AWARE OF SUCH NONCOMPLIANCE. THIS SOFTWARE IS
* PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*************************************/
//Standard Libraries
/*************************************/
#include "sha256.h"
#include "string.h"
#include "EEPROM.h"
#include "SoftTimer.h"
#include "DelayRun.h"
#include "T3MacLib.h"
#include "password.h"
#include "Time.h"
#include "onlykey.h"
#include "flashkinetis.h"
#include "RNG.h"
#include "base64.h"
#include "Curve25519.h"
/*************************************/
//Color LED Libraries
/*************************************/
#ifdef OK_Color
#include "Adafruit_NeoPixel.h"
#define NEOPIN 10
// #define CORE_PIN10_CONFIG PORTC_PCR4
#define NUMPIXELS 2
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEOPIN, NEO_GRB + NEO_KHZ800);
#endif
uint8_t NEO_Color;
uint8_t NEO_Brightness[1];
int Profile_Offset = 0;
/*************************************/
//Additional Libraries to Load for STD firmware version
//These libraries will only be used if STD_VERSION is defined
/*************************************/
#ifdef STD_VERSION
#include "ctap_errors.h"
#include "yksim.h"
#include "uECC.h"
#include "ykcore.h"
#include "ctaphid.h"
#include "ok_extension.h"
#include "usb_dev.h"
#endif
#ifndef STD_VERSION
// Parts of some libraries required for international travel edition
// not including full libraries as those libraries include crypto
#define CTAPHID_BUFFER_SIZE 7609
#define CTAP2_ERR_NO_OPERATION_PENDING 0x2A
#define CTAP2_ERR_USER_ACTION_PENDING 0x23
#define CTAP2_ERR_DATA_READY 0xF6
#define CTAP2_ERR_DATA_WIPE 0xF7
#define OKSIGN_ERR_USER_ACTION_PENDING 0xF8
#define OKDECRYPT_ERR_USER_ACTION_PENDING 0xF9
uint16_t
yubikey_crc16 (const uint8_t * buf, size_t buf_size)
{
uint16_t m_crc = 0xffff;
while (buf_size--)
{
int i, j;
m_crc ^= (uint8_t) * buf++ & 0xFF;
for (i = 0; i < 8; i++)
{
j = m_crc & 1;
m_crc >>= 1;
if (j)
m_crc ^= 0x8408;
}
}
return m_crc;
}
#endif
/*************************************/
//Global assignments
/*************************************/
uint32_t unixTimeStamp;
int pin_set = 0;
uint8_t profilemode;
bool unlocked = false;
bool initialized = false;
bool configmode = false;
uint8_t TIMEOUT[1] = {30}; //Default 30 Min
uint8_t TYPESPEED[1] = {3}; //Default
uint8_t mod_keys_enabled = 0; //Default
extern uint8_t KeyboardLayout[1];
elapsedMillis idletimer;
uint8_t useinterface = 0;
uint8_t onlykeyhw = OK_HW_COLOR;
/*************************************/
//SoftTimer Tasks
/*************************************/
Task FadeinTask(15, increment);
Task FadeoutTask(10, decrement);
DelayRun Wipedata(5000, wipebuffersafter5sec); //5 second delay to wipe data after last message
DelayRun Usertimeout(20000, fadeoffafter20sec); //20 second delay to wait for user
DelayRun Endfade(2500, fadeendafter2sec); //delay to prevent inadvertent button press after challenge PIN
uint8_t fade = 0;
uint8_t isfade = 0;
#define THRESHOLD .5
/*************************************/
//Yubikey core assignments
/*************************************/
#ifdef STD_VERSION
yubikey_ctx_st ctx;
#endif
/*************************************/
//Password.cpp assignments
/*************************************/
Password password = Password((char *)"not used");
extern uint8_t profilekey[32];
extern uint8_t p1hash[32];
extern uint8_t sdhash[32];
extern uint8_t p2hash[32];
extern uint8_t nonce[32];
extern int integrityctr1;
extern int integrityctr2;
int initcheck;
/*************************************/
//Touch button assignments
/*************************************/
uint8_t BLINKPIN;
uint8_t TOUCHPIN1;
uint8_t TOUCHPIN2;
uint8_t TOUCHPIN3;
uint8_t TOUCHPIN4;
uint8_t TOUCHPIN5;
uint8_t TOUCHPIN6;
uint8_t ANALOGPIN1;
uint8_t ANALOGPIN2;
unsigned int touchread1;
unsigned int touchread2;
unsigned int touchread3; // OnlyKey Go Button #1
unsigned int touchread4;
unsigned int touchread5; // OnlyKey Go Button #2
unsigned int touchread6;
unsigned int touchread1ref;
unsigned int touchread2ref;
unsigned int touchread3ref;
unsigned int touchread4ref;
unsigned int touchread5ref;
unsigned int touchread6ref;
unsigned int sumofall;
int button_selected = 0;
uint8_t touchoffset;
/*************************************/
//HMCAC SHA1 Assignments
/*************************************/
uint8_t setBuffer[9] = {0};
uint8_t getBuffer[9] = {0, 2, 2, 3, 3, 3, 5, 0, 0};
uint8_t keyboard_buffer[KEYBOARD_BUFFER_SIZE] = {0};
uint8_t sess_counter = 3;
uint8_t may_block = 5;
/*************************************/
//ECC key assignments
/*************************************/
#ifdef STD_VERSION
extern uint8_t ecc_public_key[(MAX_ECC_KEY_SIZE * 2) + 1];
extern uint8_t ecc_private_key[MAX_ECC_KEY_SIZE];
/*************************************/
//RSA key assignments
/*************************************/
extern uint8_t rsa_private_key[MAX_RSA_KEY_SIZE];
extern uint8_t type;
/*************************************/
//FIDO2 assignments
/*************************************/
extern uint16_t attestation_key_size;
extern uint8_t attestation_key[33];
#endif
int large_buffer_len;
int large_buffer_offset;
int packet_buffer_offset = 0;
int large_resp_buffer_offset;
uint8_t ctap_buffer[CTAPHID_BUFFER_SIZE];
// Reuse ctap_buffer as it uses 7K of RAM
uint8_t *large_resp_buffer = ctap_buffer + CTAPHID_BUFFER_SIZE - LARGE_RESP_BUFFER_SIZE; // Last 1024 bytes used to store temp data
uint8_t *large_buffer = ctap_buffer + CTAPHID_BUFFER_SIZE - LARGE_RESP_BUFFER_SIZE - LARGE_BUFFER_SIZE; // Next 1024 bytes used to store temp data
uint8_t packet_buffer[PACKET_BUFFER_SIZE];
uint8_t packet_buffer_details[5];
uint8_t recv_buffer[64];
uint8_t resp_buffer[64];
int outputmode = 0;
uint8_t pending_operation;
/*************************************/
//Crypto Challenge assignments
/*************************************/
uint8_t Challenge_button1 = 0;
uint8_t Challenge_button2 = 0;
uint8_t Challenge_button3 = 0;
uint8_t CRYPTO_AUTH = 0;
uint8_t derived_key_challenge_mode = 0;
uint8_t stored_key_challenge_mode = 0;
/*************************************/
//RNG Assignments
/*************************************/
size_t length = 48; // First block should wait for the pool to fill up.
/*************************************/
uint8_t Duo_config[2];
// Main loop to receive data
void recvmsg(int n)
{
//Debug stored response
//Serial.println("Stored Response:");
//byteprint(large_resp_buffer, 64);
// FIDO2 operation processing
if (profilemode != NONENCRYPTEDPROFILE)
{
#ifdef STD_VERSION
if (pending_operation==CTAP2_ERR_OPERATION_PENDING) {
return;
}
#endif
}
// Debug for get and setBuffer
/*
if (setBuffer[7] >= 0x80)
{
Serial.println("setbuffer = ");
byteprint(setBuffer, 9);
Serial.println("getbuffer = ");
byteprint(getBuffer, 9);
byteprint(keyboard_buffer, KEYBOARD_BUFFER_SIZE);
}
*/
// This is for staging large response via keyboard, not currently used
//if (store_keyboard_response()) return;
if (!n)
{
n = RawHID.recv(recv_buffer, 0); // 0 timeout = do not wait
if (outputmode != RAW_USB && n) changeoutputmode(RAW_USB); //USB
}
//Integrity Check
if (integrityctr1 != integrityctr2)
{
unlocked = false;
CPU_RESTART();
return;
}
if (n > 0)
{
#ifdef DEBUG
Serial.print(F("\n\nReceived packet"));
byteprint(recv_buffer, 64);
#endif
if (configmode == true && recv_buffer[4] != OKCONNECT && recv_buffer[4] != OKWIPESLOT && recv_buffer[4] != OKSETSLOT && recv_buffer[4] != OKSETPRIV && recv_buffer[4] != OKRESTORE && recv_buffer[4] != OKFWUPDATE && recv_buffer[4] != OKWIPEPRIV && recv_buffer[4] != OKGETLABELS && recv_buffer[4] != OKPIN && recv_buffer[4] != OKPINSEC && recv_buffer[4] != OKPINSD)
{
#ifdef DEBUG
Serial.println("ERROR NOT SUPPORTED IN CONFIG MODE");
#endif
return;
}
switch (recv_buffer[4])
{
case OKPIN:
if (profilemode != NONENCRYPTEDPROFILE)
{
if (!initcheck || configmode == true) {
if (recv_buffer[5]==0xff) okcore_quick_setup(SETUP_MANUAL); // Received request to set PINs/passphrase
else set_primary_pin(recv_buffer, 0); // Received request to enter primary PIN on OnlyKey (not DUO)
}
}
else
{
if (!initcheck || configmode == true) set_secondary_pin(recv_buffer, 0);
}
return;
case OKPINSD:
if (!initcheck || configmode == true) set_sd_pin(recv_buffer, 0);
return;
case OKPINSEC:
if (!initcheck || configmode == true) set_secondary_pin(recv_buffer, 0);
return;
case OKCONNECT:
set_time(recv_buffer);
return;
case OKGETLABELS:
if (initialized == false && unlocked == true)
{
hidprint("Error OnlyKey must be initialized first");
return;
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2)
{
if (recv_buffer[5] == 'k')
get_key_labels(2);
else
get_slot_labels(2);
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKSETSLOT:
if (initialized == false && unlocked == true && integrityctr1 == integrityctr2)
{
if (recv_buffer[6] == 12 || recv_buffer[6] == 20)
{ //You can set wipemode and backupkeymode any time but they are set once settings
if (recv_buffer[0] != 0xBA)
set_slot(recv_buffer);
}
else
{
hidprint("Error OnlyKey must be initialized first");
}
return;
}
else if ((initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2) || (!initcheck && unlocked == true && initialized == true && integrityctr1 == integrityctr2))
{
if (recv_buffer[0] != 0xBA)
{
if (profilemode != NONENCRYPTEDPROFILE)
{
#ifdef STD_VERSION
if (mod_keys_enabled && configmode == false) {
hidprint("Error not in config mode");
return;
}
#endif
}
set_slot(recv_buffer);
}
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKWIPESLOT:
if (initialized == false && unlocked == true)
{
hidprint("Error OnlyKey must be initialized first");
return;
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2)
{
wipe_slot(recv_buffer);
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKSETPRIV:
if ((initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && configmode == true) || (initialized == true && unlocked == true && !initcheck)) //Only permit loading keys on first use and while in config mode
{
if (profilemode != NONENCRYPTEDPROFILE)
{
#ifdef STD_VERSION
if (recv_buffer[0] != 0xBA)
set_private(recv_buffer);
#endif
}
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && configmode == false)
{
hidprint("Error not in config mode");
}
else if (recv_buffer[6] > 0x80 && onlykeyhw == OK_HW_DUO && initialized == false) { // App Setup of backup key, no pin
memcpy(large_buffer, recv_buffer, 64);
set_built_in_pin();
set_private(large_buffer);
// Lock backup key since there is no PIN required
okeeprom_eeset_backupkeymode((uint8_t*)1);
memset(large_buffer, 0, 64);
initcheck = false;
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKWIPEPRIV:
if (initialized == false && unlocked == true)
{
hidprint("No PIN set, You must set a PIN first");
return;
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && configmode == true)
{
if (profilemode != NONENCRYPTEDPROFILE)
{
#ifdef STD_VERSION
wipe_private(recv_buffer, true);
#endif
}
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKSIGN:
if (initialized == false && unlocked == true)
{
hidprint("No PIN set, You must set a PIN first");
return;
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && !CRYPTO_AUTH)
{
if (profilemode != NONENCRYPTEDPROFILE)
{
#ifdef STD_VERSION
fadeon(213); //Purple
okcrypto_sign(recv_buffer);
#endif
}
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKDECRYPT:
if (initialized == false && unlocked == true)
{
hidprint("No PIN set, You must set a PIN first");
return;
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && !CRYPTO_AUTH)
{
if (profilemode != NONENCRYPTEDPROFILE)
{
#ifdef STD_VERSION
fadeon(128); //Turquoise
okcrypto_decrypt(recv_buffer);
#endif
}
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKGETPUBKEY:
if (initialized == false && unlocked == true)
{
hidprint("No PIN set, You must set a PIN first");
return;
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2)
{
if (profilemode != NONENCRYPTEDPROFILE)
{
#ifdef STD_VERSION
okcrypto_getpubkey(recv_buffer);
#endif
}
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKRESTORE:
if (initialized == false && unlocked == true)
{
hidprint("No PIN set, You must set a PIN first");
return;
}
else if ((initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && configmode == true) || (initialized == true && unlocked == true && !initcheck && integrityctr1 == integrityctr2)) //Only permit loading backup on first use and while in config mode
{
if (profilemode != NONENCRYPTEDPROFILE)
{
#ifdef STD_VERSION
RESTORE(recv_buffer);
#endif
}
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && configmode == false)
{
hidprint("Error not in config mode");
}
else
{
hidprint("Error device locked");
return;
}
return;
case OKFWUPDATE:
if ((initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && configmode == true) || (!initcheck && integrityctr1 == integrityctr2)) //Only permit loading firmware on first use and while in config mode
{
hidprint("SUCCESSFULL FW LOAD REQUEST, REBOOTING...");
eeprom_write_byte(0x00, 1); //Go to bootloader
eeprom_write_byte((unsigned char *)0x01, 1); //Firmware ready to load
delay(100);
CPU_RESTART();
}
else if (initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2 && configmode == false)
{
hidprint("Error not in config mode");
}
else
{
hidprint("Error device locked");
return;
}
return;
default:
if (profilemode != NONENCRYPTEDPROFILE && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2)
{
#ifdef STD_VERSION
extern uint8_t onlykeyhw;
if (onlykeyhw == OK_HW_DUO && initialized == false) {
// User attempting to use unconfigured OnlyKey for FIDO without first setting up with app
// Provision OnlyKey with no device PIN and lock backup
ctaphid_handle_packet(recv_buffer);
set_built_in_pin();
// Lock backup key since there is no PIN required
okeeprom_eeset_backupkeymode((uint8_t*)1);
}
if (initialized == true && unlocked == true) {
if (!useinterface) {
// Android bug, Android selects random interface for FIDO if there are
// multiple interfaces, doesn't even check if the interface has usage page 0xf1d0
delay(100);
useinterface=n;
n = RawHID.recv(recv_buffer, 0);
if (n) useinterface=n;
recv_fido_msg(recv_buffer);
} else if (useinterface == n) {
recv_fido_msg(recv_buffer);
} else {
// Fix for Android bug causes some OSes to select wrong interface if a user tries to do FIDO2 while OnlyKey is locked
useinterface = n;
recv_fido_msg(recv_buffer);
useinterface = 0;
}
}
#endif
}
return;
}
}
else
{
if (profilemode != NONENCRYPTEDPROFILE && initialized == true && unlocked == true && FTFL_FSEC == 0x44 && integrityctr1 == integrityctr2)
{
#ifdef STD_VERSION
fido_msg_timeout();
#endif
}
}
}
uint32_t getCounter()
{
unsigned int eeAddress = EEpos_U2Fcounter; //EEPROM address to start reading from
uint32_t counter;
EEPROM.get(eeAddress, counter);
return counter;
}
void setCounter(uint32_t counter)
{
unsigned int eeAddress = EEpos_U2Fcounter; //EEPROM address to start reading from
EEPROM.put(eeAddress, counter);
}
void okcore_quick_setup(uint8_t step)
{
#ifdef DEBUG
Serial.println("Keyboard-config OnlyKey");
#endif
uint8_t buffer[64] = {0};
KeyboardLayout[0] = 0;
update_keyboard_layout();
if (step == 0)
{ //Manual/Auto
keytype("*** WELCOME TO ONLYKEY QUICK SETUP ***");
keytype("RUN THIS SETUP FROM A TRUSTED COMPUTER AND CAREFULLY WRITE DOWN");
keytype("PINs AND PASSPHRASE. STORE IN A SECURE LOCATION SUCH AS A SAFE");
keytype("WHEN FINISHED DELETE THIS TEXT");
Keyboard.println();
if (onlykeyhw!=OK_HW_DUO) {
keytype("Three different PIN codes will be set up on your OnlyKey");
keytype("The first PIN unlocks your primary profile (i.e Personal Accounts)");
keytype("The second PIN unlocks an additional profile (i.e. Work Accounts)");
keytype("The third PIN is your self-destruct PIN use this to wipe/factory default device");
}
keytype("To choose the PINs yourself press 1 on OnlyKey, for random PINs press 2");
keytype("You have 20 seconds starting now...");
pin_set = 10;
fadeoffafter20();
//okcore_quick_setup(KEYBOARD_AUTO_PIN_SET);
return;
}
if (step == KEYBOARD_MANUAL_PIN_SET)
{ //Manual Set PIN
pin_set = 0;
keytype("You will now enter a PIN on the OnlyKey 6 button keypad");
keytype("Choose a PIN 7-10 digits long");
set_primary_pin(NULL, 1);
return;
}
else if (step == KEYBOARD_AUTO_PIN_SET)
{ //Auto Set PIN
keytype("Your OnlyKey will now be configured automatically with random PINs/Passphrase");
pin_set = 3;
generate_random_pin(buffer);
//memset(buffer, '1', 7);
set_primary_pin(buffer, KEYBOARD_AUTO_PIN_SET);
Keyboard.println();
keytype("YOUR ONLYKEY PIN IS:");
keytype((char *)buffer);
if (onlykeyhw!=OK_HW_DUO) {
pin_set = 9;
generate_random_pin(buffer);
//memset(buffer, '2', 7);
set_secondary_pin(buffer, KEYBOARD_AUTO_PIN_SET);
keytype("YOUR ONLYKEY SECOND PROFILE PIN IS:");
keytype((char *)buffer);
}
pin_set = 6;
generate_random_pin(buffer);
//memset(buffer, '3', 7);
set_sd_pin(buffer, KEYBOARD_AUTO_PIN_SET);
keytype("YOUR ONLYKEY SELF-DESTRUCT PIN IS:");
keytype((char *)buffer);
} else if (step == KEYBOARD_ONLYKEY_DUO_BACKUP) {
keytype("*** WELCOME TO ONLYKEY DUO QUICK SETUP ***");
keytype("RUN THIS SETUP FROM A TRUSTED COMPUTER AND CAREFULLY WRITE DOWN");
keytype("YOUR BACKUP PASSPHRASE. STORE IN A SECURE LOCATION SUCH AS A SAFE");
keytype("WHEN FINISHED DELETE THIS TEXT");
Keyboard.println();
} else if (step == KEYBOARD_ONLYKEY_DUO_NO_BACKUP) {
set_built_in_pin();
} else if (step == SETUP_MANUAL) {
changeoutputmode(RAW_USB); //USB
memcpy(buffer, recv_buffer, 64);
if (buffer[6]>='0') { // 16 max length
pin_set = 3;
//Serial.println("SETTING PRIMARY PIN");
//byteprint(buffer+6, 16);
set_primary_pin(buffer+6, SETUP_MANUAL);
}
if (onlykeyhw!=OK_HW_DUO) {
if (buffer[22]>='0') { // 16 max length
pin_set = 9;
//Serial.println("SETTING SEC PIN");
//byteprint(buffer+22, 16);
set_secondary_pin(buffer+22, SETUP_MANUAL);
}
}
if (buffer[38]>='0') { // 16 max length
//Serial.println("SETTING SD PIN");
//byteprint(buffer+38, 16);
pin_set = 6;
set_sd_pin(buffer+38, SETUP_MANUAL);
}
return;
}
// Set randomly generated backup passphrase
buffer[5] = RESERVED_KEY_DEFAULT_BACKUP;
buffer[6] = 0xA1;
SHA256_CTX hash;
sha256_init(&hash);
if (step != KEYBOARD_ONLYKEY_DUO_NO_BACKUP) {
keytype("YOUR ONLYKEY BACKUP PASSPHRASE IS:");
generate_random_passphrase(buffer + 7);
keytype((char *)(buffer + 7));
//memset(buffer, 'a', 32);
Keyboard.println();
sha256_update(&hash, buffer + 7, 27);
sha256_final(&hash, buffer + 7);
set_private(buffer); //set backup ECC key
if (onlykeyhw!=OK_HW_DUO) {
keytype("To start using OnlyKey enter your primary or secondary PIN on the OnlyKey 6 button keypad");
}
keytype("OnlyKey is ready for use as a security key (FIDO2/U2F) and for challenge-response");
keytype("For additional features such as password management install the OnlyKey desktop app");
keytype("https://onlykey.io/app ");
keytype("*** SETUP COMPLETE, DELETE THIS TEXT ***");
}
if (step == KEYBOARD_ONLYKEY_DUO_BACKUP || step == KEYBOARD_ONLYKEY_DUO_NO_BACKUP) {
// If KEYBOARD_ONLYKEY_DUO_BACKUP this disables changing backup key
// If KEYBOARD_ONLYKEY_DUO_NO_BACKUP this disables backup feature
okeeprom_eeset_backupkeymode((uint8_t*)1);
}
CPU_RESTART();
}
void generate_random_pin(uint8_t *buffer)
{
RNG2(buffer, 7);
for (int i = 0; i < 7; i++)
{
buffer[i] = ((buffer[i] % 6) + 1) + '0';
}
buffer[7] = 0;
}
// Generate a random 27 char alpha-numeric passphrase
// passphrase strength stronger than AES-128 key
// 256^16 = ~3E+38, 36^27 = ~1E+42
void generate_random_passphrase(uint8_t *buffer)
{
RNG2(buffer, 27);
byteprint(buffer, 27);
for (int i = 0; i < 27; i++)
{
buffer[i] = ((buffer[i] % 36) + 1) + 96; //Alpha
if (buffer[i] > 122) buffer[i] = buffer[i] - 75; //Numeric
}
buffer[27] = 0;
}
void set_built_in_pin() {
pin_set = 3;
// 19-35 bytes of chip ID used instead of primary PIN
set_primary_pin((uint8_t*)recv_buffer, KEYBOARD_AUTO_PIN_SET);
okeeprom_eeset_timeout(0); // No timeout as there is no PIN required
initcheck = okcore_flashget_noncehash ((uint8_t*)nonce, 32);
okcore_flashget_pinhashpublic ((uint8_t*)p1hash, 32); //store PIN hash
initialized = true;
if (password.profile1hashevaluate()) {
unlocked = true;
#ifdef STD_VERSION
U2Finit();
#endif
}
}
void set_primary_pin(uint8_t *buffer, uint8_t keyboard_mode)
{
#ifdef DEBUG
Serial.println("OKPIN MESSAGE RECEIVED");
#endif
if (pin_set > 3)
pin_set = 0;
switch (pin_set)
{
case 0:
password.reset();
#ifdef DEBUG
Serial.println("Enter PIN");
#endif
pin_set = 1;
if (keyboard_mode == KEYBOARD_MANUAL_PIN_SET)
{
keytype("You have 20 seconds to enter primary profile PIN, starting now");
fadeoffafter20();
}
else
hidprint("OnlyKey is ready, enter your PIN");
return;
case 1:
pin_set = 2;
if (strlen(password.guess) > 6 && strlen(password.guess) < 11)
{
#ifdef DEBUG
Serial.println("Storing PIN");
#endif
if (!keyboard_mode)
hidprint("Successful PIN entry");
else
keytype("Successful PIN entry");
static char passguess[10];
for (unsigned int i = 0; i <= strlen(password.guess); i++)
{
passguess[i] = password.guess[i];
}
password.set(passguess);
password.reset();
}
else
{
#ifdef DEBUG
Serial.println("Error PIN is not between 7 - 10 digits");
#endif
if (!keyboard_mode)
hidprint("Error PIN is not between 7 - 10 digits");
else
keytype("Error PIN is not between 7 - 10 digits");
password.reset();
pin_set = 0;
}
if (keyboard_mode == KEYBOARD_MANUAL_PIN_SET)
set_primary_pin(NULL, KEYBOARD_MANUAL_PIN_SET);
return;
case 2:
#ifdef DEBUG
Serial.println("Confirm PIN");
#endif
pin_set = 3;
if (keyboard_mode == KEYBOARD_MANUAL_PIN_SET)
{
keytype("You have 20 seconds to re-enter PIN, starting now");
fadeoffafter20();
}
else
hidprint("OnlyKey is ready, re-enter your PIN to confirm");
return;
case 3:
pin_set = 0;
if ((strlen(password.guess) >= 7 && strlen(password.guess) < 11) || keyboard_mode == KEYBOARD_AUTO_PIN_SET || keyboard_mode == SETUP_MANUAL)
{
if ((password.evaluate()) || keyboard_mode == KEYBOARD_AUTO_PIN_SET || keyboard_mode == SETUP_MANUAL)
{
#ifdef DEBUG
Serial.println("Both PINs Match");
#endif
//hidprint("Both PINs Match");
uint8_t temp[32];
uint8_t nonce2[32];
RNG2((uint8_t*)nonce2, 32); //Fill temp with random data
okeeprom_eeset_nonce2((uint8_t*)nonce2);
//Hash PIN and Nonce
SHA256_CTX pinhash;
sha256_init(&pinhash);
if (keyboard_mode == KEYBOARD_AUTO_PIN_SET) {
if (onlykeyhw==OK_HW_DUO) memcpy(password.guess, (ID+18), 16);
else memcpy(password.guess, buffer, 7);
} else if (keyboard_mode == SETUP_MANUAL) {
memcpy(password.guess, buffer, 16);
}
sha256_update(&pinhash, (uint8_t *)password.guess, strlen(password.guess)); //Add new PIN to hash
// Set new nonce if none is set
if (!initcheck) {
RNG2((uint8_t*)nonce, 32); //Fill temp with random data
okcore_flashset_noncehash((uint8_t*)nonce); //Store in flash
#ifdef DEBUG
Serial.println("Generating NONCE");
byteprint(nonce, 32);
#endif
}
sha256_update(&pinhash, nonce, 32); //Add nonce to hash
sha256_final(&pinhash, temp); //Create hash and store in temp
okcore_flashset_pinhashpublic((uint8_t*)temp);
#ifdef DEBUG
Serial.println();
Serial.println("Successfully set PIN");
#endif
if (keyboard_mode == KEYBOARD_MANUAL_PIN_SET)
{
keytype("Successfully set PIN");
if (onlykeyhw!=OK_HW_DUO) {
set_secondary_pin(NULL, KEYBOARD_MANUAL_PIN_SET);
} else {
set_sd_pin(NULL, KEYBOARD_MANUAL_PIN_SET);
}
}
else
hidprint("Successfully set PIN");
}
else
{
#ifdef DEBUG
Serial.println("Error PINs Don't Match");
#endif
if (!keyboard_mode)
hidprint("Error PINs Don't Match");
else
keytype("Error PINs Don't Match");
if (keyboard_mode == KEYBOARD_MANUAL_PIN_SET)
set_primary_pin(NULL, KEYBOARD_MANUAL_PIN_SET);
}
}
else
{
#ifdef DEBUG
Serial.println("Error PIN is not between 7 - 10 digits");
#endif
if (!keyboard_mode)
hidprint("Error PIN is not between 7 - 10 digits");
else
keytype("Error PIN is not between 7 - 10 digits");
if (keyboard_mode == KEYBOARD_MANUAL_PIN_SET)
set_primary_pin(NULL, KEYBOARD_MANUAL_PIN_SET);
}
password.reset();
blink(3);
return;
}
}
void set_sd_pin(uint8_t *buffer, uint8_t keyboard_mode)