-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathokcrypto.cpp
1712 lines (1588 loc) · 51.4 KB
/
okcrypto.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 <t@crp.to>
*
* 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
* admin@crp.to.
*
* 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 admin@crp.to.
*
* 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.
*/
#include <cstring>
#include "Arduino.h"
#include "onlykey.h"
#ifdef STD_VERSION
#include <SoftTimer.h>
#include "T3MacLib.h"
#include <RNG.h>
#include <AES.h>
#include <CBC.h>
#include <GCM.h>
#include <ChaCha.h>
#include <Crypto.h>
#include "sha1.h"
#include "sha512.h"
#include "yubikey.h"
#include "device.h"
#include "okcrypto.h"
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
#include "memory_buffer_alloc.h"
#endif
/*************************************/
//RSA assignments
/*************************************/
uint8_t rsa_publicN[MAX_RSA_KEY_SIZE];
uint8_t rsa_private_key[MAX_RSA_KEY_SIZE];
/*************************************/
//ECC Authentication assignments
/*************************************/
uint8_t ecc_public_key[(MAX_ECC_KEY_SIZE*2)+1];
uint8_t ecc_private_key[MAX_ECC_KEY_SIZE];
/*************************************/
//HMACSHA1 assignments
/*************************************/
extern uint8_t keyboard_buffer[KEYBOARD_BUFFER_SIZE];
/*************************************/
extern uint8_t Challenge_button1;
extern uint8_t Challenge_button2;
extern uint8_t Challenge_button3;
extern uint8_t CRYPTO_AUTH;
uint8_t type;
extern int large_buffer_offset;
extern uint8_t resp_buffer[64];
extern uint8_t* large_buffer;
extern uint8_t recv_buffer[64];
extern int large_buffer_len;
extern uint8_t profilekey[32];
extern uint8_t packet_buffer_details[5];
extern uint8_t* large_resp_buffer;
extern uint8_t outputmode;
extern uint8_t pending_operation;
extern uint8_t transit_key[32];
void okcrypto_sign (uint8_t *buffer) {
uECC_set_rng(&RNG2);
uint8_t features = 0;
#ifdef DEBUG
Serial.println();
Serial.println("OKSIGN MESSAGE RECEIVED");
#endif
if (buffer[5] < 101) { //Slot 101-132 are for ECC, 1-4 are for RSA
features = okcore_flashget_RSA ((int)buffer[5]);
if (type == 0) {
hidprint("Error no key set in this slot");
fadeoff(0);
return;
}
#ifdef DEBUG
Serial.print(features, BIN);
#endif
if (is_bit_set(features, 6)) {
okcrypto_rsasign(buffer);
} else {
#ifdef DEBUG
Serial.print("Error key not set as signature key");
#endif
hidprint("Error key not set as signature key");
fadeoff(0);
}
return;
}
else if (buffer[5] > 200 && buffer[5] < 205) { // SSH/GPG Derive Key
okcrypto_ecdsa_eddsa(buffer);
} else {
if (buffer[5] > 100 && buffer[5] < 117) { // Keys 117 - 132 reserved
features = okcore_flashget_ECC ((int)buffer[5]);
}
if (type == 0) {
hidprint("Error no key set in this slot");
fadeoff(0);
return;
}
#ifdef DEBUG
Serial.print(features, BIN);
#endif
if (is_bit_set(features, 6)) {
okcrypto_ecdsa_eddsa(buffer);
} else {
#ifdef DEBUG
Serial.print("Error key not set as signature key");
#endif
hidprint("Error key not set as signature key");
fadeoff(0);
return;
}
}
}
void okcrypto_getpubkey (uint8_t *buffer) {
#ifdef DEBUG
Serial.println();
Serial.println("OKGETPUBKEY MESSAGE RECEIVED");
#endif
if (buffer[5] < 5 && !buffer[6]) { //Slot 101-132 are for ECC, 1-4 are for RSA
if (okcore_flashget_RSA ((int)buffer[5])) okcrypto_getrsapubkey(buffer);
} else if (buffer[5] < 117) { //128-132 are reserved
if (okcore_flashget_ECC ((int)buffer[5])) okcrypto_geteccpubkey(buffer);
} else if (buffer[5] == RESERVED_KEY_DERIVATION && buffer[6] <= KEYTYPE_CURVE25519) { // Generate key using provided data, return public
okcrypto_derive_key(buffer[6], buffer+7, NULL);
send_transport_response(ecc_public_key, 64, false, false);
}
}
void okcrypto_decrypt (uint8_t *buffer){
uECC_set_rng(&RNG2);
uint8_t features = 0;
#ifdef DEBUG
Serial.println();
Serial.println("OKDECRYPT MESSAGE RECEIVED");
#endif
if (buffer[5] < 101) { //Slot 101-132 are for ECC, 1-4 are for RSA
features = okcore_flashget_RSA (buffer[5]);
if (type == 0) {
hidprint("Error no key set in this slot");
fadeoff(0);
return;
}
if (is_bit_set(features, 5)) {
okcrypto_rsadecrypt(buffer);
} else {
#ifdef DEBUG
Serial.print("Error key not set as decryption key");
#endif
hidprint("Error key not set as decryption key");
fadeoff(0);
return;
}
} else if (buffer[5] > 200 && buffer[5] < 205) { // SSH/GPG Derive Key
okcrypto_ecdh(buffer);
} else {
if (buffer[5] > 100 && buffer[5] < 117) { // Keys 117 - 132 reserved
features = okcore_flashget_ECC ((int)buffer[5]);
}
if (type == 0) {
hidprint("Error no key set in this slot");
fadeoff(0);
return;
}
if (is_bit_set(features, 5)) {
okcrypto_ecdh(buffer);
} else {
#ifdef DEBUG
Serial.print("Error key not set as decryption key");
#endif
hidprint("Error key not set as decryption key");
fadeoff(0);
return;
}
}
}
void okcrypto_generate_random_key (uint8_t *buffer) {
uECC_set_rng(&RNG2);
//uint8_t backupslot;
//uint8_t temp[64];
#ifdef DEBUG
Serial.println();
Serial.println("GENERATE KEY MESSAGE RECEIVED");
#endif
if (buffer[5] > 100) { //Slot 101-132 are for ECC, 1-4 are for RSA
if ((buffer[6] & 0x0F) == 1) {
crypto_box_keypair(ecc_public_key, buffer+7); //Curve25519
} else if ((buffer[6] & 0x0F) == 2) {
const struct uECC_Curve_t * curve = uECC_secp256r1(); //P-256
uECC_make_key(ecc_public_key, buffer+7, curve);
} else if ((buffer[6] & 0x0F) == 3) {
const struct uECC_Curve_t * curve = uECC_secp256k1();
uECC_make_key(ecc_public_key, buffer+7, curve);
}
memset(ecc_public_key, 0, sizeof(ecc_public_key));
}
return;
}
void okcrypto_getrsapubkey (uint8_t *buffer) {
#ifdef DEBUG
byteprint(rsa_publicN, (type*128));
#endif
send_transport_response(rsa_publicN, (type*128), true, true);
}
void okcrypto_rsasign (uint8_t *buffer) {
uint8_t rsa_signature[(type*128)];
uint8_t rsa_signaturetemp[64];
char code[6];
if(!CRYPTO_AUTH) {
process_packets (buffer, 0, 0);
pending_operation=OKSIGN_ERR_USER_ACTION_PENDING;
}
else if (CRYPTO_AUTH == 4) {
if (large_buffer_offset != 28 && large_buffer_offset != 32 && large_buffer_offset != 48 && large_buffer_offset != 64) {
hidprint("Error with RSA data to sign invalid size");
#ifdef DEBUG
Serial.println("Error with RSA data to sign invalid size");
Serial.println(large_buffer_offset);
#endif
fadeoff(1);
large_buffer_offset = 0;
memset(large_buffer, 0, LARGE_BUFFER_SIZE); //wipe buffer
return;
}
okcore_aes_gcm_decrypt(large_buffer, packet_buffer_details[0], packet_buffer_details[1], profilekey, large_buffer_offset);
#ifdef DEBUG
Serial.println();
Serial.printf("RSA data to sign size=%d", large_buffer_offset);
Serial.println();
byteprint(large_buffer, large_buffer_offset);
#endif
pending_operation=CTAP2_ERR_OPERATION_PENDING;
memcpy(rsa_signaturetemp, large_buffer, large_buffer_offset);
memset(large_buffer, 0, LARGE_BUFFER_SIZE);
if (rsa_sign (large_buffer_offset, rsa_signaturetemp, rsa_signature) == 0) {
pending_operation=CTAP2_ERR_DATA_READY;
#ifdef DEBUG
Serial.print("Signature = ");
byteprint(rsa_signature, sizeof(rsa_signature));
#endif
outputmode=packet_buffer_details[2]; // Outputmode set at start of operation
if (outputmode == WEBAUTHN) {
send_transport_response(rsa_signature, (type*128), true, true);
}
else {
send_transport_response(rsa_signature, (type*128), false, false);
wipetasks();
}
} else {
pending_operation=0;
hidprint("Error with RSA signing");
}
fadeoff(85);
memset(rsa_signature, 0, sizeof(rsa_signature));
if (outputmode != WEBAUTHN) memset(large_resp_buffer, 0, LARGE_RESP_BUFFER_SIZE);
return;
} else {
#ifdef DEBUG
Serial.println("Waiting for challenge buttons to be pressed");
#endif
pending_operation=CTAP2_ERR_USER_ACTION_PENDING;
}
}
void okcrypto_rsadecrypt (uint8_t *buffer) {
unsigned int plaintext_len = 0;
if(!CRYPTO_AUTH) {
process_packets (buffer, 0, 0);
pending_operation=OKDECRYPT_ERR_USER_ACTION_PENDING;
}
else if (CRYPTO_AUTH == 4) {
if (large_buffer_offset != (type*128)) {
hidprint("Error with RSA data to decrypt invalid size");
#ifdef DEBUG
Serial.println("Error with RSA data to decrypt invalid size");
Serial.println(large_buffer_offset);
#endif
fadeoff(1);
large_buffer_offset = 0;
memset(large_buffer, 0, LARGE_BUFFER_SIZE); //wipe buffer
return;
}
okcore_aes_gcm_decrypt(large_buffer, packet_buffer_details[0], packet_buffer_details[1], profilekey, large_buffer_offset);
#ifdef DEBUG
Serial.println();
Serial.printf("RSA ciphertext blob size=%d", large_buffer_offset);
Serial.println();
byteprint(large_buffer, large_buffer_offset);
#endif
// decrypt ciphertext in large_buffer to temp_buffer
pending_operation=CTAP2_ERR_OPERATION_PENDING;
uint8_t rsa_decrypttemp[(type*128)];
memcpy(rsa_decrypttemp, large_buffer, large_buffer_offset);
memset(large_buffer, 0, LARGE_BUFFER_SIZE);
if (rsa_decrypt (&plaintext_len, rsa_decrypttemp, large_resp_buffer) == 0) {
pending_operation=CTAP2_ERR_DATA_READY;
#ifdef DEBUG
Serial.println();
Serial.print("Plaintext len = ");
Serial.println(plaintext_len);
Serial.print("Plaintext = ");
byteprint(large_resp_buffer, plaintext_len);
Serial.println();
#endif
outputmode=packet_buffer_details[2]; // Outputmode set at start of operation
if (outputmode == WEBAUTHN) {
send_transport_response(large_resp_buffer, plaintext_len, true, true);
}
else {
send_transport_response(large_resp_buffer, plaintext_len, false, false);
wipetasks();
}
} else {
pending_operation=0;
hidprint("Error with RSA decryption");
}
fadeoff(85);
if (outputmode != WEBAUTHN) memset(large_resp_buffer, 0, LARGE_RESP_BUFFER_SIZE);
return;
} else {
#ifdef DEBUG
Serial.println("Waiting for challenge buttons to be pressed");
#endif
pending_operation=CTAP2_ERR_USER_ACTION_PENDING;
}
}
void okcrypto_geteccpubkey (uint8_t *buffer) {
uint8_t pubkeylen = 64;
okcore_flashget_ECC (buffer[5]);
if (type==KEYTYPE_NACL && buffer[6]==KEYTYPE_CURVE25519) {
type = KEYTYPE_CURVE25519;
okcrypto_compute_pubkey();
}
#ifdef DEBUG
Serial.println("okcrypto_geteccpubkey MESSAGE RECEIVED");
byteprint(ecc_public_key, sizeof(ecc_public_key));
#endif
if (type==KEYTYPE_CURVE25519 || type==KEYTYPE_ED25519) pubkeylen = 32;
send_transport_response(ecc_public_key, pubkeylen, true, true);
memset(ecc_public_key, 0, MAX_ECC_KEY_SIZE*2); //wipe buffer
memset(ecc_private_key, 0, MAX_ECC_KEY_SIZE); //wipe buffer
}
void okcrypto_derive_key (uint8_t ktype, uint8_t *data, uint8_t slot) {
if (!slot) { //SHA256 KDF used for SSH and challenge-response
okcore_flashget_ECC (RESERVED_KEY_DERIVATION); //Default Key stored in ECC slot 32
memset(ecc_public_key, 0, sizeof(ecc_public_key));
SHA256_CTX ekey;
sha256_init(&ekey);
sha256_update(&ekey, ecc_private_key, 32); //Add default key to ekey
sha256_update(&ekey, data, 32); //Add provided data to ekey
sha256_final(&ekey, ecc_private_key); //Create hash and store
#ifdef DEBUG
Serial.println();
Serial.println("Agent derivation private key");
byteprint(ecc_private_key,32);
#endif
} else if (slot==RESERVED_KEY_WEB_DERIVATION) { //HMAC SHA256 KDF used for web requests
okcore_flashget_ECC (slot);
#ifdef DEBUG
Serial.println();
Serial.println("Web derivation key");
byteprint(ecc_private_key,32);
Serial.println("Other data");
byteprint(data,33);
#endif
// HKDF Reference: RFC5869 - https://tools.ietf.org/html/rfc5869
okcrypto_hkdf(data, ecc_private_key, ecc_private_key, 32);
#ifdef DEBUG
Serial.println();
Serial.println("HKDF Key");
byteprint(ecc_private_key,32);
#endif
}
type=ktype;
okcrypto_compute_pubkey();
}
void okcrypto_ecdsa_eddsa(uint8_t *buffer)
{
uint8_t ecc_signature[64];
uint8_t hash[64];
uint8_t len = 0;
#ifdef DEBUG
Serial.println();
Serial.println("OKECDSA_EDDSA SIGN MESSAGE RECEIVED");
#endif
if (!CRYPTO_AUTH) {
process_packets (buffer, 0, 0);
pending_operation=OKSIGN_ERR_USER_ACTION_PENDING;
}
else if (CRYPTO_AUTH == 4) {
//if (outputmode == RAW_USB && !derived_key_challenge_mode && !stored_key_challenge_mod) {
//}
okcore_aes_gcm_decrypt(large_buffer, packet_buffer_details[0], packet_buffer_details[1], profilekey, large_buffer_offset);
#ifdef DEBUG
Serial.println();
Serial.print("ECC blob size of ");
Serial.println(large_buffer_offset);
byteprint(large_buffer, large_buffer_offset);
#endif
uint8_t tmp[32 + 32 + 64];
SHA256_HashContext ectx = {{&init_SHA256, &update_SHA256, &finish_SHA256, 64, 32, tmp}};
if (buffer[5] > 200) {
if (buffer[5] == 201) {
//Used by SSH, old version used 132, new version uses 201 for type 1
okcrypto_derive_key(1, large_buffer+(large_buffer_offset-32), NULL);
}
else if (buffer[5] == 202) {
okcrypto_derive_key(2, large_buffer+(large_buffer_offset-32), NULL);
}
else if (buffer[5] == 203) {
okcrypto_derive_key(3, large_buffer+(large_buffer_offset-32), NULL);
} else if (buffer[5] == 211) {
okcrypto_derive_key(1, large_buffer+(large_buffer_offset-32), RESERVED_KEY_WEB_DERIVATION);
}
else if (buffer[5] == 212) {
okcrypto_derive_key(2, large_buffer+(large_buffer_offset-32), RESERVED_KEY_WEB_DERIVATION);
}
else if (buffer[5] == 213) {
okcrypto_derive_key(3, large_buffer+(large_buffer_offset-32), RESERVED_KEY_WEB_DERIVATION);
}
large_buffer_offset = large_buffer_offset - 32;
}
else if (buffer[5] >= 101 && buffer[5] <= 116) {
// Not using derived key, using stored key
okcore_flashget_ECC (buffer[5]); //Default Key stored in ECC slot 32
} else {
return;
}
if (large_buffer_offset == 32 || large_buffer_offset == 64) { // Hash and sign data if larger than 32 bytes, if 32 bytes sign data
memcpy(hash, large_buffer, large_buffer_offset);
} else {
SHA256_CTX msghash;
sha256_init(&msghash);
sha256_update(&msghash, large_buffer, large_buffer_offset);
sha256_final(&msghash, hash); //Create hash and store
if (type!=0x01) large_buffer_offset = 32;
}
#ifdef DEBUG
Serial.println("Signature Hash ");
byteprint(hash, large_buffer_offset);
Serial.print("Type");
Serial.println(type);
Serial.println("Private ");
byteprint(ecc_private_key, sizeof(ecc_private_key));
#endif
pending_operation=CTAP2_ERR_OPERATION_PENDING;
if (type==0x01) Ed25519::sign(ecc_signature, ecc_private_key, ecc_public_key, large_buffer, large_buffer_offset);
else if (type==0x02) {
const struct uECC_Curve_t * curve = uECC_secp256r1(); //P-256
if (!uECC_sign_deterministic(ecc_private_key,
hash,
large_buffer_offset,
&ectx.uECC,
ecc_signature,
curve)) {
#ifdef DEBUG
Serial.println("Signature Failed ");
#endif
}
}
else if (type==0x03) {
const struct uECC_Curve_t * curve = uECC_secp256k1();
if (!uECC_sign_deterministic(ecc_private_key,
hash,
large_buffer_offset,
&ectx.uECC,
ecc_signature,
curve)) {
#ifdef DEBUG
Serial.println("Signature Failed ");
#endif
}
}
#ifdef DEBUG
Serial.print("Signature=");
byteprint(ecc_signature, 64);
#endif
pending_operation=CTAP2_ERR_DATA_READY;
outputmode=packet_buffer_details[2]; // Outputmode set at start of operation
if (outputmode == WEBAUTHN) {
send_transport_response (ecc_signature, 64, true, true);
}
else {
send_transport_response (ecc_signature, 64, true, true);
wipetasks();
}
// Stop the fade in
fadeoff(85);
memset(large_buffer, 0, LARGE_BUFFER_SIZE);
memset(ecc_public_key, 0, sizeof(ecc_public_key)); //wipe buffer
memset(ecc_private_key, 0, sizeof(ecc_private_key)); //wipe buffer
return;
} else {
#ifdef DEBUG
Serial.println("Waiting for challenge buttons to be pressed");
#endif
pending_operation=CTAP2_ERR_USER_ACTION_PENDING;
}
}
void okcrypto_ecdh(uint8_t *buffer) {
uint8_t temp[65] = {0};
uint8_t resplen = 64;
#ifdef DEBUG
Serial.println();
Serial.println("OKECDH MESSAGE RECEIVED");
#endif
if(!CRYPTO_AUTH) {
process_packets (buffer, 0, 0);
pending_operation=OKDECRYPT_ERR_USER_ACTION_PENDING;
}
else if (CRYPTO_AUTH == 4) {
okcore_aes_gcm_decrypt(large_buffer, packet_buffer_details[0], packet_buffer_details[1], profilekey, large_buffer_offset);
#ifdef DEBUG
Serial.println();
Serial.print("ECC blob size of ");
Serial.println(large_buffer_offset);
byteprint(large_buffer, large_buffer_offset);
#endif
if (buffer[5] > 201) {
if (buffer[5] == 202) {
okcrypto_derive_key(2, large_buffer+(large_buffer_offset-32), NULL);
}
else if (buffer[5] == 203) {
okcrypto_derive_key(3, large_buffer+(large_buffer_offset-32), NULL);
}
else if (buffer[5] == 204) {
okcrypto_derive_key(4, large_buffer+(large_buffer_offset-32), NULL);
} else if (buffer[5] == 212) {
okcrypto_derive_key(2, large_buffer+(large_buffer_offset-32), RESERVED_KEY_WEB_DERIVATION);
}
else if (buffer[5] == 213) {
okcrypto_derive_key(3, large_buffer+(large_buffer_offset-32), RESERVED_KEY_WEB_DERIVATION);
}
else if (buffer[5] == 214) {
okcrypto_derive_key(4, large_buffer+(large_buffer_offset-32), RESERVED_KEY_WEB_DERIVATION);
}
large_buffer_offset = large_buffer_offset - 32; //Remove derivation data hash
}
else if (buffer[5] >= 101 && buffer[5] <= 116) {
// Not using derived key, using stored key
okcore_flashget_ECC (buffer[5]);
} else {
return;
}
if (type==2 || type==3) type+=100; // Different shared secret method required, multiply points and return x and y
if (type==1) {
type=4; // Use Curve25519 scalar multiply
resplen = 32;
okcrypto_compute_pubkey();
}
if (large_buffer_offset == 33 || large_buffer_offset == 65) { // Remove public key first byte 0x04 or 0x40 for Trezor agent
large_buffer_offset--;
memcpy(ecc_public_key, large_buffer+1, large_buffer_offset);
} else if (large_buffer_offset == 32 || large_buffer_offset == 64) {
memcpy(ecc_public_key, large_buffer, large_buffer_offset);
}
pending_operation=CTAP2_ERR_USER_ACTION_PENDING;
// Use ecc_private_key and provided pubkey to generate shared secret
if (large_buffer_offset == 64 || large_buffer_offset == 32) { // Public key sizes
if (okcrypto_shared_secret (ecc_public_key, temp)) {
//Error
}
}
#ifdef DEBUG
Serial.println("Input public");
byteprint(ecc_public_key, large_buffer_offset);
Serial.println("Private");
byteprint(ecc_private_key, sizeof(ecc_private_key));
Serial.print("Type");
Serial.println(type);
Serial.print("Shared Secret =");
byteprint(temp, large_buffer_offset);
#endif
pending_operation=CTAP2_ERR_DATA_READY;
outputmode=packet_buffer_details[2]; // Outputmode set at start of operation
if (outputmode == WEBAUTHN) {
send_transport_response (temp, resplen, true, true);
}
else {
send_transport_response (temp, resplen, true, true);
wipetasks();
}
// Stop the fade in
fadeoff(85);
memset(large_buffer, 0, LARGE_BUFFER_SIZE);
memset(ecc_public_key, 0, sizeof(ecc_public_key)); //wipe buffer
memset(ecc_private_key, 0, sizeof(ecc_private_key)); //wipe buffer
return;
} else {
#ifdef DEBUG
Serial.println("Waiting for challenge buttons to be pressed");
#endif
}
}
void okcrypto_hmacsha1 () {
uint8_t temp[32];
uint8_t inputlen;
uint8_t slot = keyboard_buffer[64];
uint16_t crc;
extern uint8_t setBuffer[9];
uint8_t *ptr;
#ifdef DEBUG
Serial.println();
Serial.println("GENERATE HMACSHA1 MESSAGE RECEIVED");
Serial.print("SLOT = ");
Serial.println(slot);
#endif
if (CRYPTO_AUTH == 4) {
if(!check_crc(keyboard_buffer)) {
memset(setBuffer, 0, 9);
memset(keyboard_buffer, 0, KEYBOARD_BUFFER_SIZE);
return;
}
outputmode=RAW_USB;
if (slot == 0x38) { //HMAC Slot 2 selected, 0x08 for slot 2, 0x00 for slot 1
okcore_flashget_ECC (RESERVED_KEY_HMACSHA1_2); //ECC slot 129 reserved for HMAC Slot 2 key
} else if (slot == 0x30){ //HMAC Slot 2 selected, 0x00 for slot 1
okcore_flashget_ECC (RESERVED_KEY_HMACSHA1_1); //ECC slot 130 reserved for HMAC Slot 1 key
} else if (slot >= 1 && slot <= 24) {
okcore_flashget_hmac(ecc_private_key, slot);
}
if (type == 0) { //Generate a key using the default key in slot 132 if there is no key set in slot
// Derive key from SHA256 hash of default key and added data temp
for(int i=0; i<32; i++) {
temp[i] = i + slot;
}
okcrypto_derive_key(0, temp, NULL);
}
outputmode=KEYBOARD_USB;
// Variable buffer size
// Any challenge less than 16 bytes in size is treated as 16 bytes, this means response will be different than Yubikey response
if (keyboard_buffer[57] == 0x20 && keyboard_buffer[58] == 0x20 && keyboard_buffer[59] == 0x20 && keyboard_buffer[60] == 0x20 && keyboard_buffer[61] == 0x20 && keyboard_buffer[62] == 0x20 && keyboard_buffer[63] == 0x20) {
inputlen = 32; //KeepassXC uses 0x20 for empty buffer
} else {
int i;
for (i = 63; i >= 15; i--) {
if (keyboard_buffer[i] != 0) { //YubiKey personalization tool uses 0 for empty buffer
break;
}
}
inputlen = i+1;
}
#ifdef DEBUG
Serial.print("HMACSHA1 Input = ");
byteprint(keyboard_buffer, 70);
Serial.print("Input Length");
Serial.println(inputlen);
#endif
//Load HMAC Key
Sha1.initHmac(ecc_private_key, 20);
//Generate HMACSHA1
Sha1.write(keyboard_buffer, inputlen);
ptr=keyboard_buffer;
ptr = Sha1.resultHmac();
memcpy(temp, ptr, 20);
memset(ecc_private_key, 0, 32);
#ifdef DEBUG
Serial.print("HMAC for CRC Input = ");
byteprint(temp, 20);
#endif
//Generate CRC of Output
crc = yubikey_crc16 (temp, 20);
memcpy(keyboard_buffer, temp, 7);
keyboard_buffer[7] = 0xC0; //Part 1 of HMAC
memcpy(keyboard_buffer+8, temp+7, 7);
keyboard_buffer[15] = 0xC1; //Part 2 of HMAC
memcpy(keyboard_buffer+16, temp+14, 6);
keyboard_buffer[23] = 0xC2; //Part 3 of HMAC
memset(keyboard_buffer +24, 0, KEYBOARD_BUFFER_SIZE-24);
// CRC Bytes expected are CRC-16/X-25 but yubikey_crc16 generates CRC-16/MCRF4XX,
// Weird that firmware uses a different CRC-16 than https://github.com/Yubico/yubikey-personalization/blob/master/ykcore/
// We can XOR CRC-16/MCRF4XX output to convert to CRC-16/X-25
crc ^= 0xFFFF;
keyboard_buffer[22] = crc & 0xFF;
keyboard_buffer[24] = crc >> 8;
keyboard_buffer[31] = 0xC3; //Part 4 contains part of CRC and mystery byte keyboard_buffer[28]
keyboard_buffer[28] = 0x4B;
#ifdef DEBUG
Serial.print("HMACSHA1 Output = ");
byteprint(keyboard_buffer, KEYBOARD_BUFFER_SIZE);
Serial.print("CRC = ");
Serial.println(crc);
#endif
return;
} else {
#ifdef DEBUG
Serial.println("Waiting for challenge buttons to be pressed");
#endif
}
}
int okcrypto_shared_secret (uint8_t *pub, uint8_t *secret) {
const struct uECC_Curve_t * curve;
switch (type) {
case KEYTYPE_NACL:
if (crypto_box_beforenm(secret, pub, ecc_private_key)) return 1;
else return 0;
case KEYTYPE_P256R1:
curve = uECC_secp256r1();
if (uECC_shared_secret(pub, ecc_private_key, secret, curve)) {
return 0;
}
else return 1;
case KEYTYPE_P256K1:
curve = uECC_secp256k1();
if (uECC_shared_secret(pub, ecc_private_key, secret, curve)) {
return 0;
}
else return 1;
case KEYTYPE_CURVE25519:
Curve25519::eval(secret, ecc_private_key, pub);
return 0;
case KEYTYPE_ECDH_P256R:
curve = uECC_secp256r1();
if (uECC_shared_secret2(pub, ecc_private_key, secret, curve)) {
return 0;
}
case KEYTYPE_ECDH_P256K:
curve = uECC_secp256k1();
if (uECC_shared_secret2(pub, ecc_private_key, secret, curve)) {
return 0;
}
default:
hidprint("Error ECC type incorrect");
return 1;
}
}
mbedtls_md_context_t sha512_ctx;
void crypto_sha512_init() {
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
mbedtls_md_init (&sha512_ctx);
mbedtls_md_setup(&sha512_ctx, mbedtls_md_info_from_type(md_type), 0); // 0 = not using HMAC
}
void crypto_sha512_update(const uint8_t * data, size_t len) {
mbedtls_md_update (&sha512_ctx, data, len);
}
void crypto_sha512_final(uint8_t * hash) {
mbedtls_md_finish (&sha512_ctx, hash);
mbedtls_md_free (&sha512_ctx);
}
void okcrypto_aes_crypto_box (uint8_t *buffer, int len, bool open) {
uint8_t iv[12];
memset(iv, 0, 12);
//msgcount++;
//int ctr = ((msgcount>>24)&0xff) | // move byte 3 to byte 0
// ((msgcount<<8)&0xff0000) | // move byte 1 to byte 2
// ((msgcount>>8)&0xff00) | // move byte 2 to byte 1
// ((msgcount<<24)&0xff000000); // byte 0 to byte 3
//memcpy(iv, &ctr, 4);
#ifdef DEBUG
Serial.print("IV");
byteprint(iv, 12);
#endif
#ifdef DEBUG
Serial.print("Key");
byteprint(transit_key, 32);
#endif
#ifdef DEBUG
Serial.print("buffer");
byteprint(buffer, len);
#endif
if (open) {
okcrypto_aes_gcm_decrypt2 (buffer, iv, transit_key, len, false);
}
else {
okcrypto_aes_gcm_encrypt2 (buffer, iv, transit_key, len, false);
}
}
int rsa_sign (int mlen, const uint8_t *msg, uint8_t *out)
{
//mbedtls_rsa_self_test(1);
mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
int ret = 0;
static mbedtls_rsa_context rsa;
uint8_t rsa_ciphertext[(type*128)];
mbedtls_mpi P1, Q1, H;
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
mbedtls_mpi_init (&P1); mbedtls_mpi_init (&Q1); mbedtls_mpi_init (&H);
rsa.len = (type*128);
MBEDTLS_MPI_CHK( mbedtls_mpi_lset (&rsa.E, 0x10001) );
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa.P, &rsa_private_key[0], ((type*128) / 2) ));
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa.Q, &rsa_private_key[((type*128) / 2)], ((type*128) / 2) ));
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&rsa.N, &rsa.P, &rsa.Q) );
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int (&P1, &rsa.P, 1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int (&Q1, &rsa.Q, 1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&H, &P1, &Q1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod (&rsa.D , &rsa.E, &H) );
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi (&rsa.DP, &rsa.D, &P1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi (&rsa.DQ, &rsa.D, &Q1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod (&rsa.QP, &rsa.Q, &rsa.P) );
#ifdef DEBUG
Serial.printf( "\nRSA len = " );
Serial.println(rsa.len);
#endif
ret = mbedtls_rsa_check_privkey( &rsa );
cleanup:
mbedtls_mpi_free (&P1); mbedtls_mpi_free (&Q1); mbedtls_mpi_free (&H);
if( ret != 0 ) {
#ifdef DEBUG
Serial.printf("Error with key check =%d", ret);
#endif
hidprint("Error invalid key, key check failed");
return -1;
}
if (ret == 0) {
#ifdef DEBUG
Serial.print("RSA sign messege length = ");
Serial.println(mlen);
#endif
if (mlen > ((type*128)-11)) mlen = ((type*128)-11);
switch (mlen) {
case 64:
md_type = MBEDTLS_MD_SHA512;
break;
case 48:
md_type = MBEDTLS_MD_SHA384;
break;
case 32:
md_type = MBEDTLS_MD_SHA256;
break;
case 28:
md_type = MBEDTLS_MD_SHA224;
break;
//case 20:
// md_type = MBEDTLS_MD_RIPEMD160;
//break;
default:
break;
}
ret = mbedtls_rsa_rsassa_pkcs1_v15_sign (&rsa, mbedtls_rand, NULL, MBEDTLS_RSA_PRIVATE, md_type, mlen, msg, rsa_ciphertext);
#ifdef DEBUG
Serial.print("Hash Value = ");
byteprint((uint8_t *)msg, mlen);
#endif
memcpy (out, rsa_ciphertext, (type*128));
/*int ret2 = mbedtls_rsa_rsassa_pkcs1_v15_verify ( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, mlen, msg, rsa_ciphertext );
#ifdef DEBUG
Serial.print("Hash Value = ");
byteprint((uint8_t *)msg, mlen);
#endif
if( ret2 != 0 ) {
#ifdef DEBUG
Serial.print("Signature Verification Failed ");
Serial.println(ret2);
#endif
return -1;
}*/
}
mbedtls_rsa_free (&rsa);
if (ret == 0) {
#ifdef DEBUG
Serial.println("completed successfully");
#endif
return 0;
}
else {
#ifdef DEBUG
Serial.print("MBEDTLS_ERR_RSA_XXX error code ");
Serial.println(ret);
#endif
hidprint("invalid data, or data does not match key");
return -1;
}
}
int rsa_decrypt (unsigned int *olen, const uint8_t *in, uint8_t *out) {
mbedtls_mpi P1, Q1, H;
int ret = 0;
static mbedtls_rsa_context rsa;
#ifdef DEBUG
Serial.printf ("\nRSA decrypt:");
Serial.println ((uint32_t)&ret);
#endif
mbedtls_rsa_init (&rsa, MBEDTLS_RSA_PKCS_V15, 0);
mbedtls_mpi_init (&P1); mbedtls_mpi_init (&Q1); mbedtls_mpi_init (&H);
rsa.len = (type*128);
MBEDTLS_MPI_CHK( mbedtls_mpi_lset (&rsa.E, 0x10001) );
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa.P, &rsa_private_key[0], ((type*128) / 2) ));
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa.Q, &rsa_private_key[((type*128) / 2)], ((type*128) / 2) ));
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&rsa.N, &rsa.P, &rsa.Q) );
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int (&P1, &rsa.P, 1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int (&Q1, &rsa.Q, 1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&H, &P1, &Q1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod (&rsa.D , &rsa.E, &H) );
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi (&rsa.DP, &rsa.D, &P1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi (&rsa.DQ, &rsa.D, &Q1) );
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod (&rsa.QP, &rsa.Q, &rsa.P) );
#ifdef DEBUG