-
Notifications
You must be signed in to change notification settings - Fork 16
/
tee_svc_cryp.c
3473 lines (3247 loc) · 91.1 KB
/
tee_svc_cryp.c
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
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2014, STMicroelectronics International N.V.
* All rights reserved.
*
* 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.
*
* 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 HOLDER 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 <assert.h>
#include <crypto/crypto.h>
#include <kernel/tee_ta_manager.h>
//#include <mm/tee_mmu.h>
//#include <string_ext.h>
#include <string.h>
#include <sys/queue.h>
#include <tee_api_types.h>
#include <tee/tee_cryp_utl.h>
#include <tee/tee_obj.h>
#include <tee/tee_svc_cryp.h>
#include <tee/tee_svc.h>
#include <trace.h>
#include <utee_defines.h>
#include <util.h>
#include <kernel/user_ta.h>
#if defined(CFG_CRYPTO_HKDF) || defined(CFG_CRYPTO_CONCAT_KDF) || \
defined(CFG_CRYPTO_PBKDF2)
#include <tee_api_defines_extensions.h>
#endif
#if defined(CFG_CRYPTO_HKDF)
#include <tee/tee_cryp_hkdf.h>
#endif
#if defined(CFG_CRYPTO_CONCAT_KDF)
#include <tee/tee_cryp_concat_kdf.h>
#endif
#if defined(CFG_CRYPTO_PBKDF2)
#include <tee/tee_cryp_pbkdf2.h>
#endif
typedef void (*tee_cryp_ctx_finalize_func_t) (void *ctx, uint32_t algo);
struct tee_cryp_state {
TAILQ_ENTRY(tee_cryp_state) link;
uint32_t algo;
uint32_t mode;
vaddr_t key1;
vaddr_t key2;
void *ctx;
tee_cryp_ctx_finalize_func_t ctx_finalize;
};
struct tee_cryp_obj_secret {
uint32_t key_size;
uint32_t alloc_size;
/*
* Pseudo code visualize layout of structure
* Next follows data, such as:
* uint8_t data[alloc_size]
* key_size must never exceed alloc_size
*/
};
#define TEE_TYPE_ATTR_OPTIONAL 0x0
#define TEE_TYPE_ATTR_REQUIRED 0x1
#define TEE_TYPE_ATTR_OPTIONAL_GROUP 0x2
#define TEE_TYPE_ATTR_SIZE_INDICATOR 0x4
#define TEE_TYPE_ATTR_GEN_KEY_OPT 0x8
#define TEE_TYPE_ATTR_GEN_KEY_REQ 0x10
/* Handle storing of generic secret keys of varying lengths */
#define ATTR_OPS_INDEX_SECRET 0
/* Convert to/from big-endian byte array and provider-specific bignum */
#define ATTR_OPS_INDEX_BIGNUM 1
/* Convert to/from value attribute depending on direction */
#define ATTR_OPS_INDEX_VALUE 2
struct tee_cryp_obj_type_attrs {
uint32_t attr_id;
uint16_t flags;
uint16_t ops_index;
uint16_t raw_offs;
uint16_t raw_size;
};
#define RAW_DATA(_x, _y) \
.raw_offs = offsetof(_x, _y), .raw_size = MEMBER_SIZE(_x, _y)
static const struct tee_cryp_obj_type_attrs
tee_cryp_obj_secret_value_attrs[] = {
{
.attr_id = TEE_ATTR_SECRET_VALUE,
.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
.ops_index = ATTR_OPS_INDEX_SECRET,
.raw_offs = 0,
.raw_size = 0
},
};
//static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_pub_key_attrs[] = {
// {
// .attr_id = TEE_ATTR_RSA_MODULUS,
// .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_public_key, n)
// },
//
// {
// .attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_public_key, e)
// },
//};
//
//static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_keypair_attrs[] = {
// {
// .attr_id = TEE_ATTR_RSA_MODULUS,
// .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_keypair, n)
// },
//
// {
// .attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_keypair, e)
// },
//
// {
// .attr_id = TEE_ATTR_RSA_PRIVATE_EXPONENT,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_keypair, d)
// },
//
// {
// .attr_id = TEE_ATTR_RSA_PRIME1,
// .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_keypair, p)
// },
//
// {
// .attr_id = TEE_ATTR_RSA_PRIME2,
// .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_keypair, q)
// },
//
// {
// .attr_id = TEE_ATTR_RSA_EXPONENT1,
// .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_keypair, dp)
// },
//
// {
// .attr_id = TEE_ATTR_RSA_EXPONENT2,
// .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_keypair, dq)
// },
//
// {
// .attr_id = TEE_ATTR_RSA_COEFFICIENT,
// .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct rsa_keypair, qp)
// },
//};
//
//static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_pub_key_attrs[] = {
// {
// .attr_id = TEE_ATTR_DSA_PRIME,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_public_key, p)
// },
//
// {
// .attr_id = TEE_ATTR_DSA_SUBPRIME,
// .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_public_key, q)
// },
//
// {
// .attr_id = TEE_ATTR_DSA_BASE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_public_key, g)
// },
//
// {
// .attr_id = TEE_ATTR_DSA_PUBLIC_VALUE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_public_key, y)
// },
//};
//
//static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_keypair_attrs[] = {
// {
// .attr_id = TEE_ATTR_DSA_PRIME,
// .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_keypair, p)
// },
//
// {
// .attr_id = TEE_ATTR_DSA_SUBPRIME,
// .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR |
// TEE_TYPE_ATTR_GEN_KEY_REQ,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_keypair, q)
// },
//
// {
// .attr_id = TEE_ATTR_DSA_BASE,
// .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_keypair, g)
// },
//
// {
// .attr_id = TEE_ATTR_DSA_PRIVATE_VALUE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_keypair, x)
// },
//
// {
// .attr_id = TEE_ATTR_DSA_PUBLIC_VALUE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dsa_keypair, y)
// },
//};
//
//static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dh_keypair_attrs[] = {
// {
// .attr_id = TEE_ATTR_DH_PRIME,
// .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR |
// TEE_TYPE_ATTR_GEN_KEY_REQ,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dh_keypair, p)
// },
//
// {
// .attr_id = TEE_ATTR_DH_BASE,
// .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dh_keypair, g)
// },
//
// {
// .attr_id = TEE_ATTR_DH_PUBLIC_VALUE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dh_keypair, y)
// },
//
// {
// .attr_id = TEE_ATTR_DH_PRIVATE_VALUE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dh_keypair, x)
// },
//
// {
// .attr_id = TEE_ATTR_DH_SUBPRIME,
// .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP | TEE_TYPE_ATTR_GEN_KEY_OPT,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct dh_keypair, q)
// },
//
// {
// .attr_id = TEE_ATTR_DH_X_BITS,
// .flags = TEE_TYPE_ATTR_GEN_KEY_OPT,
// .ops_index = ATTR_OPS_INDEX_VALUE,
// RAW_DATA(struct dh_keypair, xbits)
// },
//};
#if defined(CFG_CRYPTO_HKDF)
static const struct tee_cryp_obj_type_attrs
tee_cryp_obj_hkdf_ikm_attrs[] = {
{
.attr_id = TEE_ATTR_HKDF_IKM,
.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
.ops_index = ATTR_OPS_INDEX_SECRET,
.raw_offs = 0,
.raw_size = 0
},
};
#endif
#if defined(CFG_CRYPTO_CONCAT_KDF)
static const struct tee_cryp_obj_type_attrs
tee_cryp_obj_concat_kdf_z_attrs[] = {
{
.attr_id = TEE_ATTR_CONCAT_KDF_Z,
.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
.ops_index = ATTR_OPS_INDEX_SECRET,
.raw_offs = 0,
.raw_size = 0
},
};
#endif
#if defined(CFG_CRYPTO_PBKDF2)
static const struct tee_cryp_obj_type_attrs
tee_cryp_obj_pbkdf2_passwd_attrs[] = {
{
.attr_id = TEE_ATTR_PBKDF2_PASSWORD,
.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
.ops_index = ATTR_OPS_INDEX_SECRET,
.raw_offs = 0,
.raw_size = 0
},
};
#endif
//static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_pub_key_attrs[] = {
// {
// .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct ecc_public_key, x)
// },
//
// {
// .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct ecc_public_key, y)
// },
//
// {
// .attr_id = TEE_ATTR_ECC_CURVE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_VALUE,
// RAW_DATA(struct ecc_public_key, curve)
// },
//};
//
//static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_keypair_attrs[] = {
// {
// .attr_id = TEE_ATTR_ECC_PRIVATE_VALUE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct ecc_keypair, d)
// },
//
// {
// .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct ecc_keypair, x)
// },
//
// {
// .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_BIGNUM,
// RAW_DATA(struct ecc_keypair, y)
// },
//
// {
// .attr_id = TEE_ATTR_ECC_CURVE,
// .flags = TEE_TYPE_ATTR_REQUIRED,
// .ops_index = ATTR_OPS_INDEX_VALUE,
// RAW_DATA(struct ecc_keypair, curve)
// },
//};
struct tee_cryp_obj_type_props {
TEE_ObjectType obj_type;
uint16_t min_size; /* may not be smaller than this */
uint16_t max_size; /* may not be larger than this */
uint16_t alloc_size; /* this many bytes are allocated to hold data */
uint8_t quanta; /* may only be an multiple of this */
uint8_t num_type_attrs;
const struct tee_cryp_obj_type_attrs *type_attrs;
};
#define PROP(obj_type, quanta, min_size, max_size, alloc_size, type_attrs) \
{ (obj_type), (min_size), (max_size), (alloc_size), (quanta), \
ARRAY_SIZE(type_attrs), (type_attrs) }
static const struct tee_cryp_obj_type_props tee_cryp_obj_props[] = {
PROP(TEE_TYPE_AES, 64, 128, 256, /* valid sizes 128, 192, 256 */
256 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_DES, 56, 56, 56,
/*
* Valid size 56 without parity, note that we still allocate
* for 64 bits since the key is supplied with parity.
*/
64 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_DES3, 56, 112, 168,
/*
* Valid sizes 112, 168 without parity, note that we still
* allocate for with space for the parity since the key is
* supplied with parity.
*/
192 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_HMAC_MD5, 8, 64, 512,
512 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_HMAC_SHA1, 8, 80, 512,
512 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_HMAC_SHA224, 8, 112, 512,
512 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_HMAC_SHA256, 8, 192, 1024,
1024 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_HMAC_SHA384, 8, 256, 1024,
1024 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_HMAC_SHA512, 8, 256, 1024,
1024 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
PROP(TEE_TYPE_GENERIC_SECRET, 8, 0, 4096,
4096 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_secret_value_attrs),
#if defined(CFG_CRYPTO_HKDF)
PROP(TEE_TYPE_HKDF_IKM, 8, 0, 4096,
4096 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_hkdf_ikm_attrs),
#endif
#if defined(CFG_CRYPTO_CONCAT_KDF)
PROP(TEE_TYPE_CONCAT_KDF_Z, 8, 0, 4096,
4096 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_concat_kdf_z_attrs),
#endif
#if defined(CFG_CRYPTO_PBKDF2)
PROP(TEE_TYPE_PBKDF2_PASSWORD, 8, 0, 4096,
4096 / 8 + sizeof(struct tee_cryp_obj_secret),
tee_cryp_obj_pbkdf2_passwd_attrs),
#endif
// PROP(TEE_TYPE_RSA_PUBLIC_KEY, 1, 256, 2048,
// sizeof(struct rsa_public_key),
// tee_cryp_obj_rsa_pub_key_attrs),
//
// PROP(TEE_TYPE_RSA_KEYPAIR, 1, 256, 2048,
// sizeof(struct rsa_keypair),
// tee_cryp_obj_rsa_keypair_attrs),
//
// PROP(TEE_TYPE_DSA_PUBLIC_KEY, 64, 512, 3072,
// sizeof(struct dsa_public_key),
// tee_cryp_obj_dsa_pub_key_attrs),
//
// PROP(TEE_TYPE_DSA_KEYPAIR, 64, 512, 3072,
// sizeof(struct dsa_keypair),
// tee_cryp_obj_dsa_keypair_attrs),
//
// PROP(TEE_TYPE_DH_KEYPAIR, 1, 256, 2048,
// sizeof(struct dh_keypair),
// tee_cryp_obj_dh_keypair_attrs),
//
// PROP(TEE_TYPE_ECDSA_PUBLIC_KEY, 1, 192, 521,
// sizeof(struct ecc_public_key),
// tee_cryp_obj_ecc_pub_key_attrs),
//
// PROP(TEE_TYPE_ECDSA_KEYPAIR, 1, 192, 521,
// sizeof(struct ecc_keypair),
// tee_cryp_obj_ecc_keypair_attrs),
//
// PROP(TEE_TYPE_ECDH_PUBLIC_KEY, 1, 192, 521,
// sizeof(struct ecc_public_key),
// tee_cryp_obj_ecc_pub_key_attrs),
//
// PROP(TEE_TYPE_ECDH_KEYPAIR, 1, 192, 521,
// sizeof(struct ecc_keypair),
// tee_cryp_obj_ecc_keypair_attrs),
};
struct attr_ops {
TEE_Result (*from_user)(void *attr, const void *buffer, size_t size);
TEE_Result (*to_user)(void *attr, struct tee_ta_session *sess,
void *buffer, uint64_t *size);
TEE_Result (*to_binary)(void *attr, void *data, size_t data_len,
size_t *offs);
bool (*from_binary)(void *attr, const void *data, size_t data_len,
size_t *offs);
TEE_Result (*from_obj)(void *attr, void *src_attr);
void (*free)(void *attr);
void (*clear)(void *attr);
};
static TEE_Result op_u32_to_binary_helper(uint32_t v, uint8_t *data,
size_t data_len, size_t *offs)
{
uint32_t field;
size_t next_offs;
if (ADD_OVERFLOW(*offs, sizeof(field), &next_offs))
return TEE_ERROR_OVERFLOW;
if (data && next_offs <= data_len) {
field = TEE_U32_TO_BIG_ENDIAN(v);
memcpy(data + *offs, &field, sizeof(field));
}
(*offs) = next_offs;
return TEE_SUCCESS;
}
static bool op_u32_from_binary_helper(uint32_t *v, const uint8_t *data,
size_t data_len, size_t *offs)
{
uint32_t field;
if (!data || (*offs + sizeof(field)) > data_len)
return false;
memcpy(&field, data + *offs, sizeof(field));
*v = TEE_U32_FROM_BIG_ENDIAN(field);
(*offs) += sizeof(field);
return true;
}
static TEE_Result op_attr_secret_value_from_user(void *attr, const void *buffer,
size_t size)
{
struct tee_cryp_obj_secret *key = attr;
/* Data size has to fit in allocated buffer */
if (size > key->alloc_size)
return TEE_ERROR_SECURITY;
memcpy(key + 1, buffer, size);
key->key_size = size;
return TEE_SUCCESS;
}
static TEE_Result op_attr_secret_value_to_user(void *attr,
struct tee_ta_session *sess __unused,
void *buffer, uint64_t *size)
{
TEE_Result res;
struct tee_cryp_obj_secret *key = attr;
uint64_t s;
uint64_t key_size;
res = tee_svc_copy_from_user(&s, size, sizeof(s));
if (res != TEE_SUCCESS)
return res;
key_size = key->key_size;
res = tee_svc_copy_to_user(size, &key_size, sizeof(key_size));
if (res != TEE_SUCCESS)
return res;
if (s < key->key_size)
return TEE_ERROR_SHORT_BUFFER;
return tee_svc_copy_to_user(buffer, key + 1, key->key_size);
}
static TEE_Result op_attr_secret_value_to_binary(void *attr, void *data,
size_t data_len, size_t *offs)
{
TEE_Result res;
struct tee_cryp_obj_secret *key = attr;
size_t next_offs;
res = op_u32_to_binary_helper(key->key_size, data, data_len, offs);
if (res != TEE_SUCCESS)
return res;
if (ADD_OVERFLOW(*offs, key->key_size, &next_offs))
return TEE_ERROR_OVERFLOW;
if (data && next_offs <= data_len)
memcpy((uint8_t *)data + *offs, key + 1, key->key_size);
(*offs) = next_offs;
return TEE_SUCCESS;
}
static bool op_attr_secret_value_from_binary(void *attr, const void *data,
size_t data_len, size_t *offs)
{
struct tee_cryp_obj_secret *key = attr;
uint32_t s;
if (!op_u32_from_binary_helper(&s, data, data_len, offs))
return false;
if ((*offs + s) > data_len)
return false;
/* Data size has to fit in allocated buffer */
if (s > key->alloc_size)
return false;
key->key_size = s;
memcpy(key + 1, (const uint8_t *)data + *offs, s);
(*offs) += s;
return true;
}
static TEE_Result op_attr_secret_value_from_obj(void *attr, void *src_attr)
{
struct tee_cryp_obj_secret *key = attr;
struct tee_cryp_obj_secret *src_key = src_attr;
if (src_key->key_size > key->alloc_size)
return TEE_ERROR_BAD_STATE;
memcpy(key + 1, src_key + 1, src_key->key_size);
key->key_size = src_key->key_size;
return TEE_SUCCESS;
}
static void op_attr_secret_value_clear(void *attr)
{
struct tee_cryp_obj_secret *key = attr;
key->key_size = 0;
memset(key + 1, 0, key->alloc_size);
}
//static TEE_Result op_attr_bignum_from_user(void *attr, const void *buffer,
// size_t size)
//{
// struct bignum **bn = attr;
//
// return crypto_bignum_bin2bn(buffer, size, *bn);
//}
//
//static TEE_Result op_attr_bignum_to_user(void *attr,
// struct tee_ta_session *sess,
// void *buffer, uint64_t *size)
//{
// TEE_Result res;
// struct bignum **bn = attr;
// uint64_t req_size;
// uint64_t s;
//
// res = tee_svc_copy_from_user(&s, size, sizeof(s));
// if (res != TEE_SUCCESS)
// return res;
//
// req_size = crypto_bignum_num_bytes(*bn);
// res = tee_svc_copy_to_user(size, &req_size, sizeof(req_size));
// if (res != TEE_SUCCESS)
// return res;
// if (!req_size)
// return TEE_SUCCESS;
// if (s < req_size)
// return TEE_ERROR_SHORT_BUFFER;
//
// /* Check we can access data using supplied user mode pointer */
// res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx),
// TEE_MEMORY_ACCESS_READ |
// TEE_MEMORY_ACCESS_WRITE |
// TEE_MEMORY_ACCESS_ANY_OWNER,
// (uaddr_t)buffer, req_size);
// if (res != TEE_SUCCESS)
// return res;
// /*
// * Write the bignum (wich raw data points to) into an array of
// * bytes (stored in buffer)
// */
// crypto_bignum_bn2bin(*bn, buffer);
// return TEE_SUCCESS;
//}
//
//static TEE_Result op_attr_bignum_to_binary(void *attr, void *data,
// size_t data_len, size_t *offs)
//{
// TEE_Result res;
// struct bignum **bn = attr;
// uint32_t n = crypto_bignum_num_bytes(*bn);
// size_t next_offs;
//
// res = op_u32_to_binary_helper(n, data, data_len, offs);
// if (res != TEE_SUCCESS)
// return res;
//
// if (ADD_OVERFLOW(*offs, n, &next_offs))
// return TEE_ERROR_OVERFLOW;
//
// if (data && next_offs <= data_len)
// crypto_bignum_bn2bin(*bn, (uint8_t *)data + *offs);
// (*offs) = next_offs;
//
// return TEE_SUCCESS;
//}
//
//static bool op_attr_bignum_from_binary(void *attr, const void *data,
// size_t data_len, size_t *offs)
//{
// struct bignum **bn = attr;
// uint32_t n;
//
// if (!op_u32_from_binary_helper(&n, data, data_len, offs))
// return false;
//
// if ((*offs + n) > data_len)
// return false;
// if (crypto_bignum_bin2bn((const uint8_t *)data + *offs, n, *bn))
// return false;
// (*offs) += n;
// return true;
//}
//
//static TEE_Result op_attr_bignum_from_obj(void *attr, void *src_attr)
//{
// struct bignum **bn = attr;
// struct bignum **src_bn = src_attr;
//
// crypto_bignum_copy(*bn, *src_bn);
// return TEE_SUCCESS;
//}
//
//static void op_attr_bignum_clear(void *attr)
//{
// struct bignum **bn = attr;
//
// crypto_bignum_clear(*bn);
//}
//
//static void op_attr_bignum_free(void *attr)
//{
// struct bignum **bn = attr;
//
// crypto_bignum_free(*bn);
// *bn = NULL;
//}
//
//static TEE_Result op_attr_value_from_user(void *attr, const void *buffer,
// size_t size)
//{
// uint32_t *v = attr;
//
// if (size != sizeof(uint32_t) * 2)
// return TEE_ERROR_GENERIC; /* "can't happen */
//
// /* Note that only the first value is copied */
// memcpy(v, buffer, sizeof(uint32_t));
// return TEE_SUCCESS;
//}
//
//static TEE_Result op_attr_value_to_user(void *attr,
// struct tee_ta_session *sess __unused,
// void *buffer, uint64_t *size)
//{
// TEE_Result res;
// uint32_t *v = attr;
// uint64_t s;
// uint32_t value[2] = { *v };
// uint64_t req_size = sizeof(value);
//
// res = tee_svc_copy_from_user(&s, size, sizeof(s));
// if (res != TEE_SUCCESS)
// return res;
//
// if (s < req_size)
// return TEE_ERROR_SHORT_BUFFER;
//
// return tee_svc_copy_to_user(buffer, value, req_size);
//}
//
//static TEE_Result op_attr_value_to_binary(void *attr, void *data,
// size_t data_len, size_t *offs)
//{
// uint32_t *v = attr;
//
// return op_u32_to_binary_helper(*v, data, data_len, offs);
//}
//
//static bool op_attr_value_from_binary(void *attr, const void *data,
// size_t data_len, size_t *offs)
//{
// uint32_t *v = attr;
//
// return op_u32_from_binary_helper(v, data, data_len, offs);
//}
//
//static TEE_Result op_attr_value_from_obj(void *attr, void *src_attr)
//{
// uint32_t *v = attr;
// uint32_t *src_v = src_attr;
//
// *v = *src_v;
// return TEE_SUCCESS;
//}
//
//static void op_attr_value_clear(void *attr)
//{
// uint32_t *v = attr;
//
// *v = 0;
//}
//
static const struct attr_ops attr_ops[] = {
[ATTR_OPS_INDEX_SECRET] = {
.from_user = op_attr_secret_value_from_user,
.to_user = op_attr_secret_value_to_user,
.to_binary = op_attr_secret_value_to_binary,
.from_binary = op_attr_secret_value_from_binary,
.from_obj = op_attr_secret_value_from_obj,
.free = op_attr_secret_value_clear, /* not a typo */
.clear = op_attr_secret_value_clear,
},
// [ATTR_OPS_INDEX_BIGNUM] = {
// .from_user = op_attr_bignum_from_user,
// .to_user = op_attr_bignum_to_user,
// .to_binary = op_attr_bignum_to_binary,
// .from_binary = op_attr_bignum_from_binary,
// .from_obj = op_attr_bignum_from_obj,
// .free = op_attr_bignum_free,
// .clear = op_attr_bignum_clear,
// },
// [ATTR_OPS_INDEX_VALUE] = {
// .from_user = op_attr_value_from_user,
// .to_user = op_attr_value_to_user,
// .to_binary = op_attr_value_to_binary,
// .from_binary = op_attr_value_from_binary,
// .from_obj = op_attr_value_from_obj,
// .free = op_attr_value_clear, /* not a typo */
// .clear = op_attr_value_clear,
// },
};
TEE_Result utee_cryp_obj_get_info(unsigned long obj, TEE_ObjectInfo *info)
{
TEE_Result res;
struct tee_ta_session *sess;
struct tee_obj *o;
res = tee_ta_get_current_session(&sess);
if (res != TEE_SUCCESS)
goto exit;
res = tee_obj_get(to_user_ta_ctx(sess->ctx),
tee_svc_uref_to_vaddr(obj), &o);
if (res != TEE_SUCCESS)
goto exit;
res = tee_svc_copy_to_user(info, &o->info, sizeof(o->info));
exit:
return res;
}
//TEE_Result syscall_cryp_obj_restrict_usage(unsigned long obj,
// unsigned long usage)
//{
// TEE_Result res;
// struct tee_ta_session *sess;
// struct tee_obj *o;
//
// res = tee_ta_get_current_session(&sess);
// if (res != TEE_SUCCESS)
// goto exit;
//
// res = tee_obj_get(to_user_ta_ctx(sess->ctx),
// tee_svc_uref_to_vaddr(obj), &o);
// if (res != TEE_SUCCESS)
// goto exit;
//
// o->info.objectUsage &= usage;
//
//exit:
// return res;
//}
static int tee_svc_cryp_obj_find_type_attr_idx(
uint32_t attr_id,
const struct tee_cryp_obj_type_props *type_props)
{
size_t n;
for (n = 0; n < type_props->num_type_attrs; n++) {
if (attr_id == type_props->type_attrs[n].attr_id)
return n;
}
return -1;
}
static const struct tee_cryp_obj_type_props *tee_svc_find_type_props(
TEE_ObjectType obj_type)
{
size_t n;
for (n = 0; n < ARRAY_SIZE(tee_cryp_obj_props); n++) {
if (tee_cryp_obj_props[n].obj_type == obj_type)
return tee_cryp_obj_props + n;
}
return NULL;
}
///* Set an attribute on an object */
//static void set_attribute(struct tee_obj *o,
// const struct tee_cryp_obj_type_props *props,
// uint32_t attr)
//{
// int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props);
//
// if (idx < 0)
// return;
// o->have_attrs |= BIT(idx);
//}
//
///* Get an attribute on an object */
//static uint32_t get_attribute(const struct tee_obj *o,
// const struct tee_cryp_obj_type_props *props,
// uint32_t attr)
//{
// int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props);
//
// if (idx < 0)
// return 0;
// return o->have_attrs & BIT(idx);
//}
//
//TEE_Result syscall_cryp_obj_get_attr(unsigned long obj, unsigned long attr_id,
// void *buffer, uint64_t *size)
//{
// TEE_Result res;
// struct tee_ta_session *sess;
// struct tee_obj *o;
// const struct tee_cryp_obj_type_props *type_props;
// int idx;
// const struct attr_ops *ops;
// void *attr;
//
// res = tee_ta_get_current_session(&sess);
// if (res != TEE_SUCCESS)
// return res;
//
// res = tee_obj_get(to_user_ta_ctx(sess->ctx),
// tee_svc_uref_to_vaddr(obj), &o);
// if (res != TEE_SUCCESS)
// return TEE_ERROR_ITEM_NOT_FOUND;
//
// /* Check that the object is initialized */
// if (!(o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED))
// return TEE_ERROR_BAD_PARAMETERS;
//
// /* Check that getting the attribute is allowed */
// if (!(attr_id & TEE_ATTR_BIT_PROTECTED) &&
// !(o->info.objectUsage & TEE_USAGE_EXTRACTABLE))
// return TEE_ERROR_BAD_PARAMETERS;
//
// type_props = tee_svc_find_type_props(o->info.objectType);
// if (!type_props) {
// /* Unknown object type, "can't happen" */
// return TEE_ERROR_BAD_STATE;
// }
//
// idx = tee_svc_cryp_obj_find_type_attr_idx(attr_id, type_props);
// if ((idx < 0) || ((o->have_attrs & (1 << idx)) == 0))
// return TEE_ERROR_ITEM_NOT_FOUND;
//
// ops = attr_ops + type_props->type_attrs[idx].ops_index;
// attr = (uint8_t *)o->attr + type_props->type_attrs[idx].raw_offs;
// return ops->to_user(attr, sess, buffer, size);
//}
//
void tee_obj_attr_free(struct tee_obj *o)
{
// DMSG("******** FREE *********");
const struct tee_cryp_obj_type_props *tp;
size_t n;
if (!o->attr)
return;
tp = tee_svc_find_type_props(o->info.objectType);
if (!tp)