forked from OP-TEE/optee_os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tee_rpmb_fs.c
3143 lines (2616 loc) · 75.7 KB
/
tee_rpmb_fs.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.
*/
#include <assert.h>
#include <config.h>
#include <crypto/crypto.h>
#include <kernel/huk_subkey.h>
#include <kernel/misc.h>
#include <kernel/msg_param.h>
#include <kernel/mutex.h>
#include <kernel/panic.h>
#include <kernel/tee_common.h>
#include <kernel/tee_common_otp.h>
#include <kernel/tee_misc.h>
#include <kernel/thread.h>
#include <kernel/user_access.h>
#include <mempool.h>
#include <mm/core_memprot.h>
#include <mm/mobj.h>
#include <mm/tee_mm.h>
#include <optee_rpc_cmd.h>
#include <stdlib.h>
#include <string_ext.h>
#include <string.h>
#include <sys/queue.h>
#include <tee/tee_fs.h>
#include <tee/tee_fs_key_manager.h>
#include <tee/tee_pobj.h>
#include <tee/tee_svc_storage.h>
#include <trace.h>
#include <util.h>
#define RPMB_STORAGE_START_ADDRESS 0
#define RPMB_FS_FAT_START_ADDRESS 512
#define RPMB_BLOCK_SIZE_SHIFT 8
#define RPMB_CID_PRV_OFFSET 9
#define RPMB_CID_CRC_OFFSET 15
#define RPMB_FS_MAGIC 0x52504D42
#define FS_VERSION 2
#define FILE_IS_ACTIVE (1u << 0)
#define FILE_IS_LAST_ENTRY (1u << 1)
#define TEE_RPMB_FS_FILENAME_LENGTH 224
#define TMP_BLOCK_SIZE 4096U
#define RPMB_MAX_RETRIES 10
/**
* Utilized when caching is enabled, i.e., when CFG_RPMB_FS_CACHE_ENTRIES > 0.
* Cache size + the number of entries that are repeatedly read in and buffered
* once the cache is full.
*/
#define RPMB_BUF_MAX_ENTRIES (CFG_RPMB_FS_CACHE_ENTRIES + \
CFG_RPMB_FS_RD_ENTRIES)
/**
* FS parameters: Information often used by internal functions.
* fat_start_address will be set by rpmb_fs_setup().
* rpmb_fs_parameters can be read by any other function.
*/
struct rpmb_fs_parameters {
uint32_t fat_start_address;
uint32_t max_rpmb_address;
};
/**
* File entry for a single file in a RPMB_FS partition.
*/
struct rpmb_fat_entry {
uint32_t start_address;
uint32_t data_size;
uint32_t flags;
uint32_t unused;
uint8_t fek[TEE_FS_KM_FEK_SIZE];
char filename[TEE_RPMB_FS_FILENAME_LENGTH];
};
/**
* Structure that describes buffered/cached FAT FS entries in RPMB storage.
* This structure is used in functions traversing the FAT FS.
*/
struct rpmb_fat_entry_dir {
/*
* Buffer storing the FAT FS entries read in from RPMB storage. It
* includes the optional cache entries (CFG_RPMB_FS_CACHE_ENTRIES)
* and entries temporary read for current FAT FS traversal
* (CFG_RPMB_FS_RD_ENTRIES) when not found from cached entries.
*/
struct rpmb_fat_entry *rpmb_fat_entry_buf;
/* Current index of FAT FS entry to read from buffer. */
uint32_t idx_curr;
/* Total number of FAT FS entries in buffer. */
uint32_t num_buffered;
/* Total number of FAT FS entries read during traversal. */
uint32_t num_total_read;
/* Indicates that last FAT FS entry was read. */
bool last_reached;
};
/**
* FAT entry context with reference to a FAT entry and its
* location in RPMB.
*/
struct rpmb_file_handle {
struct rpmb_fat_entry fat_entry;
const TEE_UUID *uuid;
char filename[TEE_RPMB_FS_FILENAME_LENGTH];
/* Address for current entry in RPMB */
uint32_t rpmb_fat_address;
};
/**
* RPMB_FS partition data
*/
struct rpmb_fs_partition {
uint32_t rpmb_fs_magic;
uint32_t fs_version;
uint32_t write_counter;
uint32_t fat_start_address;
/* Do not use reserved[] for other purpose than partition data. */
uint8_t reserved[112];
};
/**
* A node in a list of directory entries.
*/
struct tee_rpmb_fs_dirent {
struct tee_fs_dirent entry;
SIMPLEQ_ENTRY(tee_rpmb_fs_dirent) link;
};
/**
* The RPMB directory representation. It contains a queue of
* RPMB directory entries: 'next'.
* The current pointer points to the last directory entry
* returned by readdir().
*/
struct tee_fs_dir {
struct tee_rpmb_fs_dirent *current;
/* */
SIMPLEQ_HEAD(next_head, tee_rpmb_fs_dirent) next;
};
static struct rpmb_fs_parameters *fs_par;
static struct rpmb_fat_entry_dir *fat_entry_dir;
/*
* Lower interface to RPMB device
*/
#define RPMB_DATA_OFFSET (RPMB_STUFF_DATA_SIZE + RPMB_KEY_MAC_SIZE)
#define RPMB_MAC_PROTECT_DATA_SIZE (RPMB_DATA_FRAME_SIZE - RPMB_DATA_OFFSET)
#define RPMB_MSG_TYPE_REQ_AUTH_KEY_PROGRAM 0x0001
#define RPMB_MSG_TYPE_REQ_WRITE_COUNTER_VAL_READ 0x0002
#define RPMB_MSG_TYPE_REQ_AUTH_DATA_WRITE 0x0003
#define RPMB_MSG_TYPE_REQ_AUTH_DATA_READ 0x0004
#define RPMB_MSG_TYPE_REQ_RESULT_READ 0x0005
#define RPMB_MSG_TYPE_RESP_AUTH_KEY_PROGRAM 0x0100
#define RPMB_MSG_TYPE_RESP_WRITE_COUNTER_VAL_READ 0x0200
#define RPMB_MSG_TYPE_RESP_AUTH_DATA_WRITE 0x0300
#define RPMB_MSG_TYPE_RESP_AUTH_DATA_READ 0x0400
#define RPMB_STUFF_DATA_SIZE 196
#define RPMB_KEY_MAC_SIZE 32
#define RPMB_DATA_SIZE 256
#define RPMB_NONCE_SIZE 16
#define RPMB_DATA_FRAME_SIZE 512
#define RPMB_RESULT_OK 0x00
#define RPMB_RESULT_GENERAL_FAILURE 0x01
#define RPMB_RESULT_AUTH_FAILURE 0x02
#define RPMB_RESULT_COUNTER_FAILURE 0x03
#define RPMB_RESULT_ADDRESS_FAILURE 0x04
#define RPMB_RESULT_WRITE_FAILURE 0x05
#define RPMB_RESULT_READ_FAILURE 0x06
#define RPMB_RESULT_AUTH_KEY_NOT_PROGRAMMED 0x07
#define RPMB_RESULT_MASK 0x3F
#define RPMB_RESULT_WR_CNT_EXPIRED 0x80
/* RPMB internal commands */
#define RPMB_CMD_DATA_REQ 0x00
#define RPMB_CMD_GET_DEV_INFO 0x01
#define RPMB_SIZE_SINGLE (128 * 1024)
/* Error codes for get_dev_info request/response. */
#define RPMB_CMD_GET_DEV_INFO_RET_OK 0x00
#define RPMB_CMD_GET_DEV_INFO_RET_ERROR 0x01
struct rpmb_data_frame {
uint8_t stuff_bytes[RPMB_STUFF_DATA_SIZE];
uint8_t key_mac[RPMB_KEY_MAC_SIZE];
uint8_t data[RPMB_DATA_SIZE];
uint8_t nonce[RPMB_NONCE_SIZE];
uint8_t write_counter[4];
uint8_t address[2];
uint8_t block_count[2];
uint8_t op_result[2];
uint8_t msg_type[2];
};
struct rpmb_req {
uint16_t cmd;
uint16_t dev_id;
uint16_t block_count;
/* variable length of data */
/* uint8_t data[]; REMOVED! */
};
#define TEE_RPMB_REQ_DATA(req) \
((void *)((struct rpmb_req *)(req) + 1))
struct rpmb_raw_data {
uint16_t msg_type;
uint16_t *op_result;
uint16_t *block_count;
uint16_t *blk_idx;
uint32_t *write_counter;
uint8_t *nonce;
uint8_t *key_mac;
uint8_t *data;
/* data length to read or write */
uint32_t len;
/* Byte address offset in the first block involved */
uint8_t byte_offset;
};
#define RPMB_EMMC_CID_SIZE 16
struct rpmb_dev_info {
uint8_t cid[RPMB_EMMC_CID_SIZE];
/* EXT CSD-slice 168 "RPMB Size" */
uint8_t rpmb_size_mult;
/* EXT CSD-slice 222 "Reliable Write Sector Count" */
uint8_t rel_wr_sec_c;
/* Check the ret code and accept the data only if it is OK. */
uint8_t ret_code;
};
/*
* Struct for rpmb context data.
*
* @key RPMB key.
* @cid eMMC card ID.
* @wr_cnt Current write counter.
* @max_blk_idx The highest block index supported by current device.
* @rel_wr_blkcnt Max number of data blocks for each reliable write.
* @dev_id Device ID of the eMMC device.
* @wr_cnt_synced Flag indicating if write counter is synced to RPMB.
* @key_derived Flag indicating if key has been generated.
* @key_verified Flag indicating the key generated is verified ok.
* @dev_info_synced Flag indicating if dev info has been retrieved from RPMB.
* @shm_type Indicates type of shared memory to allocate
*/
struct tee_rpmb_ctx {
uint8_t key[RPMB_KEY_MAC_SIZE];
uint8_t cid[RPMB_EMMC_CID_SIZE];
uint32_t wr_cnt;
uint16_t max_blk_idx;
uint16_t rel_wr_blkcnt;
uint16_t dev_id;
bool wr_cnt_synced;
bool key_derived;
bool key_verified;
bool dev_info_synced;
enum thread_shm_type shm_type;
};
static struct tee_rpmb_ctx *rpmb_ctx;
/* If set to true, don't try to access RPMB until rebooted */
static bool rpmb_dead;
/*
* Mutex to serialize the operations exported by this file.
* It protects rpmb_ctx and prevents overlapping operations on eMMC devices with
* different IDs.
*/
static struct mutex rpmb_mutex = MUTEX_INITIALIZER;
#ifdef CFG_RPMB_TESTKEY
static const uint8_t rpmb_test_key[RPMB_KEY_MAC_SIZE] = {
0xD3, 0xEB, 0x3E, 0xC3, 0x6E, 0x33, 0x4C, 0x9F,
0x98, 0x8C, 0xE2, 0xC0, 0xB8, 0x59, 0x54, 0x61,
0x0D, 0x2B, 0xCF, 0x86, 0x64, 0x84, 0x4D, 0xF2,
0xAB, 0x56, 0xE6, 0xC6, 0x1B, 0xB7, 0x01, 0xE4
};
static TEE_Result tee_rpmb_key_gen(uint8_t *key, uint32_t len)
{
TEE_Result res = TEE_SUCCESS;
if (!key || RPMB_KEY_MAC_SIZE != len) {
res = TEE_ERROR_BAD_PARAMETERS;
goto out;
}
DMSG("RPMB: Using test key");
memcpy(key, rpmb_test_key, RPMB_KEY_MAC_SIZE);
out:
return res;
}
#else /* !CFG_RPMB_TESTKEY */
static TEE_Result tee_rpmb_key_gen(uint8_t *key, uint32_t len)
{
uint8_t message[RPMB_EMMC_CID_SIZE];
if (!key || RPMB_KEY_MAC_SIZE != len)
return TEE_ERROR_BAD_PARAMETERS;
IMSG("RPMB: Using generated key");
/*
* PRV/CRC would be changed when doing eMMC FFU
* The following fields should be masked off when deriving RPMB key
*
* CID [55: 48]: PRV (Product revision)
* CID [07: 01]: CRC (CRC7 checksum)
* CID [00]: not used
*/
memcpy(message, rpmb_ctx->cid, RPMB_EMMC_CID_SIZE);
memset(message + RPMB_CID_PRV_OFFSET, 0, 1);
memset(message + RPMB_CID_CRC_OFFSET, 0, 1);
return huk_subkey_derive(HUK_SUBKEY_RPMB, message, sizeof(message),
key, len);
}
#endif /* !CFG_RPMB_TESTKEY */
static void u32_to_bytes(uint32_t u32, uint8_t *bytes)
{
*bytes = (uint8_t) (u32 >> 24);
*(bytes + 1) = (uint8_t) (u32 >> 16);
*(bytes + 2) = (uint8_t) (u32 >> 8);
*(bytes + 3) = (uint8_t) u32;
}
static void bytes_to_u32(uint8_t *bytes, uint32_t *u32)
{
*u32 = (uint32_t) ((*(bytes) << 24) +
(*(bytes + 1) << 16) +
(*(bytes + 2) << 8) + (*(bytes + 3)));
}
static void u16_to_bytes(uint16_t u16, uint8_t *bytes)
{
*bytes = (uint8_t) (u16 >> 8);
*(bytes + 1) = (uint8_t) u16;
}
static void bytes_to_u16(uint8_t *bytes, uint16_t *u16)
{
*u16 = (uint16_t) ((*bytes << 8) + *(bytes + 1));
}
static void get_op_result_bits(uint8_t *bytes, uint8_t *res)
{
*res = *(bytes + 1) & RPMB_RESULT_MASK;
}
static TEE_Result tee_rpmb_mac_calc(uint8_t *mac, uint32_t macsize,
uint8_t *key, uint32_t keysize,
struct rpmb_data_frame *datafrms,
uint16_t blkcnt)
{
TEE_Result res = TEE_ERROR_GENERIC;
int i;
void *ctx = NULL;
if (!mac || !key || !datafrms)
return TEE_ERROR_BAD_PARAMETERS;
res = crypto_mac_alloc_ctx(&ctx, TEE_ALG_HMAC_SHA256);
if (res)
return res;
res = crypto_mac_init(ctx, key, keysize);
if (res != TEE_SUCCESS)
goto func_exit;
for (i = 0; i < blkcnt; i++) {
res = crypto_mac_update(ctx, datafrms[i].data,
RPMB_MAC_PROTECT_DATA_SIZE);
if (res != TEE_SUCCESS)
goto func_exit;
}
res = crypto_mac_final(ctx, mac, macsize);
if (res != TEE_SUCCESS)
goto func_exit;
res = TEE_SUCCESS;
func_exit:
crypto_mac_free_ctx(ctx);
return res;
}
struct tee_rpmb_mem {
struct mobj *mobj;
size_t req_size;
size_t resp_size;
};
static TEE_Result tee_rpmb_alloc(size_t req_size, size_t resp_size,
struct tee_rpmb_mem *mem, void **req, void **resp)
{
size_t req_s = ROUNDUP(req_size, sizeof(uint32_t));
size_t resp_s = ROUNDUP(resp_size, sizeof(uint32_t));
struct mobj *mobj = NULL;
void *va = NULL;
if (!mem)
return TEE_ERROR_BAD_PARAMETERS;
va = thread_rpc_shm_cache_alloc(THREAD_SHM_CACHE_USER_RPMB,
rpmb_ctx->shm_type, req_s + resp_s,
&mobj);
if (!va)
return TEE_ERROR_OUT_OF_MEMORY;
*mem = (struct tee_rpmb_mem){
.mobj = mobj,
.req_size = req_size,
.resp_size = resp_size,
};
*req = mobj_get_va(mem->mobj, 0, req_s);
*resp = mobj_get_va(mem->mobj, req_s, resp_s);
if (!*req || !*resp)
return TEE_ERROR_GENERIC;
return TEE_SUCCESS;
}
static TEE_Result tee_rpmb_invoke(struct tee_rpmb_mem *mem)
{
struct thread_param params[2] = {
[0] = THREAD_PARAM_MEMREF(IN, mem->mobj, 0, mem->req_size),
[1] = THREAD_PARAM_MEMREF(OUT, mem->mobj,
ROUNDUP(mem->req_size,
sizeof(uint32_t)),
mem->resp_size),
};
return thread_rpc_cmd(OPTEE_RPC_CMD_RPMB, 2, params);
}
static TEE_Result rpmb_probe_reset(void)
{
struct thread_param params[1] = {
[0] = THREAD_PARAM_VALUE(OUT, 0, 0, 0),
};
TEE_Result res = TEE_SUCCESS;
res = thread_rpc_cmd(OPTEE_RPC_CMD_RPMB_PROBE_RESET, 1, params);
if (res)
return res;
switch (params[0].u.value.a) {
case OPTEE_RPC_SHM_TYPE_APPL:
rpmb_ctx->shm_type = THREAD_SHM_TYPE_APPLICATION;
return TEE_SUCCESS;
case OPTEE_RPC_SHM_TYPE_KERNEL:
rpmb_ctx->shm_type = THREAD_SHM_TYPE_KERNEL_PRIVATE;
return TEE_SUCCESS;
default:
return TEE_ERROR_GENERIC;
}
}
static TEE_Result rpmb_probe_next(struct rpmb_dev_info *dev_info)
{
struct thread_param params[2] = {
[0] = THREAD_PARAM_VALUE(OUT, 0, 0, 0),
};
TEE_Result res = TEE_SUCCESS;
struct mobj *mobj = NULL;
void *va = NULL;
va = thread_rpc_shm_cache_alloc(THREAD_SHM_CACHE_USER_RPMB,
THREAD_SHM_TYPE_KERNEL_PRIVATE,
sizeof(dev_info->cid), &mobj);
if (!va)
return TEE_ERROR_OUT_OF_MEMORY;
params[1] = THREAD_PARAM_MEMREF(OUT, mobj, 0, sizeof(dev_info->cid));
res = thread_rpc_cmd(OPTEE_RPC_CMD_RPMB_PROBE_NEXT, 2, params);
if (res)
return res;
if (params[0].u.value.a != OPTEE_RPC_RPMB_EMMC)
return TEE_ERROR_NOT_SUPPORTED;
*dev_info = (struct rpmb_dev_info ){
.rpmb_size_mult = params[0].u.value.b,
.rel_wr_sec_c = params[0].u.value.c,
.ret_code = RPMB_CMD_GET_DEV_INFO_RET_OK,
};
memcpy(dev_info->cid, va, sizeof(dev_info->cid));
return TEE_SUCCESS;
}
static bool is_zero(const uint8_t *buf, size_t size)
{
size_t i;
for (i = 0; i < size; i++)
if (buf[i])
return false;
return true;
}
static TEE_Result encrypt_block(uint8_t *out, const uint8_t *in,
uint16_t blk_idx, const uint8_t *fek,
const TEE_UUID *uuid)
{
return tee_fs_crypt_block(uuid, out, in, RPMB_DATA_SIZE,
blk_idx, fek, TEE_MODE_ENCRYPT);
}
static TEE_Result decrypt_block(uint8_t *out, const uint8_t *in,
uint16_t blk_idx, const uint8_t *fek,
const TEE_UUID *uuid)
{
return tee_fs_crypt_block(uuid, out, in, RPMB_DATA_SIZE,
blk_idx, fek, TEE_MODE_DECRYPT);
}
/* Decrypt/copy at most one block of data */
static TEE_Result decrypt(uint8_t *out, const struct rpmb_data_frame *frm,
size_t size, size_t offset,
uint16_t blk_idx __maybe_unused, const uint8_t *fek,
const TEE_UUID *uuid)
{
uint8_t *tmp __maybe_unused;
TEE_Result res = TEE_SUCCESS;
if ((size + offset < size) || (size + offset > RPMB_DATA_SIZE))
panic("invalid size or offset");
if (!fek) {
/* Block is not encrypted (not a file data block) */
memcpy(out, frm->data + offset, size);
} else if (is_zero(fek, TEE_FS_KM_FEK_SIZE)) {
/* The file was created with encryption disabled */
return TEE_ERROR_SECURITY;
} else {
/* Block is encrypted */
if (size < RPMB_DATA_SIZE) {
/*
* Since output buffer is not large enough to hold one
* block we must allocate a temporary buffer.
*/
tmp = malloc(RPMB_DATA_SIZE);
if (!tmp)
return TEE_ERROR_OUT_OF_MEMORY;
res = decrypt_block(tmp, frm->data, blk_idx, fek, uuid);
if (res == TEE_SUCCESS)
memcpy(out, tmp + offset, size);
free(tmp);
} else {
res = decrypt_block(out, frm->data, blk_idx, fek, uuid);
}
}
return res;
}
static TEE_Result tee_rpmb_req_pack(struct rpmb_req *req,
struct rpmb_raw_data *rawdata,
uint16_t nbr_frms,
const uint8_t *fek, const TEE_UUID *uuid)
{
TEE_Result res = TEE_ERROR_GENERIC;
int i;
struct rpmb_data_frame *datafrm;
if (!req || !rawdata || !nbr_frms)
return TEE_ERROR_BAD_PARAMETERS;
/*
* Check write blockcount is not bigger than reliable write
* blockcount.
*/
if ((rawdata->msg_type == RPMB_MSG_TYPE_REQ_AUTH_DATA_WRITE) &&
(nbr_frms > rpmb_ctx->rel_wr_blkcnt)) {
DMSG("wr_blkcnt(%d) > rel_wr_blkcnt(%d)", nbr_frms,
rpmb_ctx->rel_wr_blkcnt);
return TEE_ERROR_GENERIC;
}
req->cmd = RPMB_CMD_DATA_REQ;
req->dev_id = rpmb_ctx->dev_id;
/* Allocate memory for construct all data packets and calculate MAC. */
datafrm = calloc(nbr_frms, RPMB_DATA_FRAME_SIZE);
if (!datafrm)
return TEE_ERROR_OUT_OF_MEMORY;
for (i = 0; i < nbr_frms; i++) {
u16_to_bytes(rawdata->msg_type, datafrm[i].msg_type);
if (rawdata->block_count)
u16_to_bytes(*rawdata->block_count,
datafrm[i].block_count);
if (rawdata->blk_idx) {
/* Check the block index is within range. */
if ((*rawdata->blk_idx + nbr_frms - 1) >
rpmb_ctx->max_blk_idx) {
res = TEE_ERROR_GENERIC;
goto func_exit;
}
u16_to_bytes(*rawdata->blk_idx, datafrm[i].address);
}
if (rawdata->write_counter)
u32_to_bytes(*rawdata->write_counter,
datafrm[i].write_counter);
if (rawdata->nonce)
memcpy(datafrm[i].nonce, rawdata->nonce,
RPMB_NONCE_SIZE);
if (rawdata->data) {
if (fek) {
res = encrypt_block(datafrm[i].data,
rawdata->data +
(i * RPMB_DATA_SIZE),
*rawdata->blk_idx + i,
fek, uuid);
if (res != TEE_SUCCESS)
goto func_exit;
} else {
memcpy(datafrm[i].data,
rawdata->data + (i * RPMB_DATA_SIZE),
RPMB_DATA_SIZE);
}
}
}
if (rawdata->key_mac) {
if (rawdata->msg_type == RPMB_MSG_TYPE_REQ_AUTH_DATA_WRITE) {
res =
tee_rpmb_mac_calc(rawdata->key_mac,
RPMB_KEY_MAC_SIZE, rpmb_ctx->key,
RPMB_KEY_MAC_SIZE, datafrm,
nbr_frms);
if (res != TEE_SUCCESS)
goto func_exit;
}
memcpy(datafrm[nbr_frms - 1].key_mac,
rawdata->key_mac, RPMB_KEY_MAC_SIZE);
}
memcpy(TEE_RPMB_REQ_DATA(req), datafrm,
nbr_frms * RPMB_DATA_FRAME_SIZE);
if (IS_ENABLED(CFG_RPMB_FS_DEBUG_DATA)) {
for (i = 0; i < nbr_frms; i++) {
DMSG("Dumping data frame %d:", i);
DHEXDUMP((uint8_t *)&datafrm[i] + RPMB_STUFF_DATA_SIZE,
512 - RPMB_STUFF_DATA_SIZE);
}
}
res = TEE_SUCCESS;
func_exit:
free(datafrm);
return res;
}
static TEE_Result data_cpy_mac_calc_1b(struct rpmb_raw_data *rawdata,
struct rpmb_data_frame *frm,
const uint8_t *fek, const TEE_UUID *uuid)
{
TEE_Result res;
uint8_t *data;
uint16_t idx;
if (rawdata->len + rawdata->byte_offset > RPMB_DATA_SIZE)
return TEE_ERROR_BAD_PARAMETERS;
res = tee_rpmb_mac_calc(rawdata->key_mac, RPMB_KEY_MAC_SIZE,
rpmb_ctx->key, RPMB_KEY_MAC_SIZE, frm, 1);
if (res != TEE_SUCCESS)
return res;
data = rawdata->data;
bytes_to_u16(frm->address, &idx);
res = decrypt(data, frm, rawdata->len, rawdata->byte_offset, idx, fek,
uuid);
return res;
}
static TEE_Result tee_rpmb_data_cpy_mac_calc(struct rpmb_data_frame *datafrm,
struct rpmb_raw_data *rawdata,
uint16_t nbr_frms,
struct rpmb_data_frame *lastfrm,
const uint8_t *fek,
const TEE_UUID *uuid)
{
TEE_Result res = TEE_ERROR_GENERIC;
int i;
void *ctx = NULL;
uint16_t offset;
uint32_t size;
uint8_t *data;
uint16_t start_idx;
struct rpmb_data_frame localfrm;
if (!datafrm || !rawdata || !nbr_frms || !lastfrm)
return TEE_ERROR_BAD_PARAMETERS;
if (nbr_frms == 1)
return data_cpy_mac_calc_1b(rawdata, lastfrm, fek, uuid);
/* nbr_frms > 1 */
data = rawdata->data;
res = crypto_mac_alloc_ctx(&ctx, TEE_ALG_HMAC_SHA256);
if (res)
goto func_exit;
res = crypto_mac_init(ctx, rpmb_ctx->key, RPMB_KEY_MAC_SIZE);
if (res != TEE_SUCCESS)
goto func_exit;
/*
* Note: JEDEC JESD84-B51: "In every packet the address is the start
* address of the full access (not address of the individual half a
* sector)"
*/
bytes_to_u16(lastfrm->address, &start_idx);
for (i = 0; i < (nbr_frms - 1); i++) {
/*
* By working on a local copy of the RPMB frame, we ensure that
* the data can not be modified after the MAC is computed but
* before the payload is decrypted/copied to the output buffer.
*/
memcpy(&localfrm, &datafrm[i], RPMB_DATA_FRAME_SIZE);
res = crypto_mac_update(ctx, localfrm.data,
RPMB_MAC_PROTECT_DATA_SIZE);
if (res != TEE_SUCCESS)
goto func_exit;
if (i == 0) {
/* First block */
offset = rawdata->byte_offset;
size = RPMB_DATA_SIZE - offset;
} else {
/* Middle blocks */
size = RPMB_DATA_SIZE;
offset = 0;
}
res = decrypt(data, &localfrm, size, offset, start_idx + i,
fek, uuid);
if (res != TEE_SUCCESS)
goto func_exit;
data += size;
}
/* Last block */
size = (rawdata->len + rawdata->byte_offset) % RPMB_DATA_SIZE;
if (size == 0)
size = RPMB_DATA_SIZE;
res = decrypt(data, lastfrm, size, 0, start_idx + nbr_frms - 1, fek,
uuid);
if (res != TEE_SUCCESS)
goto func_exit;
/* Update MAC against the last block */
res = crypto_mac_update(ctx, lastfrm->data, RPMB_MAC_PROTECT_DATA_SIZE);
if (res != TEE_SUCCESS)
goto func_exit;
res = crypto_mac_final(ctx, rawdata->key_mac, RPMB_KEY_MAC_SIZE);
if (res != TEE_SUCCESS)
goto func_exit;
res = TEE_SUCCESS;
func_exit:
crypto_mac_free_ctx(ctx);
return res;
}
static TEE_Result tee_rpmb_resp_unpack_verify(struct rpmb_data_frame *datafrm,
struct rpmb_raw_data *rawdata,
uint16_t nbr_frms,
const uint8_t *fek,
const TEE_UUID *uuid)
{
TEE_Result res = TEE_ERROR_GENERIC;
uint16_t msg_type;
uint32_t wr_cnt;
uint16_t blk_idx;
uint8_t op_result;
struct rpmb_data_frame lastfrm;
if (!datafrm || !rawdata || !nbr_frms)
return TEE_ERROR_BAD_PARAMETERS;
if (IS_ENABLED(CFG_RPMB_FS_DEBUG_DATA)) {
size_t i = 0;
for (i = 0; i < nbr_frms; i++) {
DMSG("Dumping data frame %zu:", i);
DHEXDUMP((uint8_t *)&datafrm[i] + RPMB_STUFF_DATA_SIZE,
512 - RPMB_STUFF_DATA_SIZE);
}
}
/* Make sure the last data packet can't be modified once verified */
memcpy(&lastfrm, &datafrm[nbr_frms - 1], RPMB_DATA_FRAME_SIZE);
/* Handle operation result and translate to TEEC error code. */
get_op_result_bits(lastfrm.op_result, &op_result);
if (op_result == RPMB_RESULT_AUTH_KEY_NOT_PROGRAMMED)
return TEE_ERROR_ITEM_NOT_FOUND;
if (op_result != RPMB_RESULT_OK)
return TEE_ERROR_GENERIC;
/* Check the response msg_type. */
bytes_to_u16(lastfrm.msg_type, &msg_type);
if (msg_type != rawdata->msg_type) {
DMSG("Unexpected msg_type (0x%04x != 0x%04x)", msg_type,
rawdata->msg_type);
return TEE_ERROR_GENERIC;
}
if (rawdata->blk_idx) {
bytes_to_u16(lastfrm.address, &blk_idx);
if (blk_idx != *rawdata->blk_idx) {
DMSG("Unexpected block index");
return TEE_ERROR_GENERIC;
}
}
if (rawdata->write_counter) {
wr_cnt = *rawdata->write_counter;
bytes_to_u32(lastfrm.write_counter, rawdata->write_counter);
if (msg_type == RPMB_MSG_TYPE_RESP_AUTH_DATA_WRITE) {
/* Verify the write counter is incremented by 1 */
if (*rawdata->write_counter != wr_cnt + 1) {
DMSG("Counter mismatched (0x%04x/0x%04x)",
*rawdata->write_counter, wr_cnt + 1);
return TEE_ERROR_SECURITY;
}
rpmb_ctx->wr_cnt++;
}
}
if (rawdata->nonce) {
if (buf_compare_ct(rawdata->nonce, lastfrm.nonce,
RPMB_NONCE_SIZE) != 0) {
DMSG("Nonce mismatched");
return TEE_ERROR_SECURITY;
}
}
if (rawdata->key_mac) {
if (msg_type == RPMB_MSG_TYPE_RESP_AUTH_DATA_READ) {
if (!rawdata->data)
return TEE_ERROR_GENERIC;
res = tee_rpmb_data_cpy_mac_calc(datafrm, rawdata,
nbr_frms, &lastfrm,
fek, uuid);
if (res != TEE_SUCCESS)
return res;
} else {
/*
* There should be only one data frame for
* other msg types.
*/
if (nbr_frms != 1)
return TEE_ERROR_GENERIC;
res = tee_rpmb_mac_calc(rawdata->key_mac,
RPMB_KEY_MAC_SIZE,
rpmb_ctx->key,
RPMB_KEY_MAC_SIZE,
&lastfrm, 1);
if (res != TEE_SUCCESS)
return res;
}
if (consttime_memcmp(rawdata->key_mac,
(datafrm + nbr_frms - 1)->key_mac,
RPMB_KEY_MAC_SIZE) != 0) {
DMSG("MAC mismatched:");
if (IS_ENABLED(CFG_RPMB_FS_DEBUG_DATA))
DHEXDUMP(rawdata->key_mac, RPMB_KEY_MAC_SIZE);
return TEE_ERROR_SECURITY;
}
}
return TEE_SUCCESS;
}
static TEE_Result tee_rpmb_get_dev_info(uint16_t dev_id,
struct rpmb_dev_info *dev_info)
{
TEE_Result res = TEE_ERROR_GENERIC;
struct tee_rpmb_mem mem;
struct rpmb_dev_info *di;
struct rpmb_req *req = NULL;
uint8_t *resp = NULL;
uint32_t req_size;
uint32_t resp_size;
if (!dev_info)
return TEE_ERROR_BAD_PARAMETERS;
req_size = sizeof(struct rpmb_req);
resp_size = sizeof(struct rpmb_dev_info);
res = tee_rpmb_alloc(req_size, resp_size, &mem,
(void *)&req, (void *)&resp);
if (res != TEE_SUCCESS)
return res;
req->cmd = RPMB_CMD_GET_DEV_INFO;
req->dev_id = dev_id;
di = (struct rpmb_dev_info *)resp;
di->ret_code = RPMB_CMD_GET_DEV_INFO_RET_ERROR;
res = tee_rpmb_invoke(&mem);
if (res != TEE_SUCCESS)
return res;
if (di->ret_code != RPMB_CMD_GET_DEV_INFO_RET_OK)
return TEE_ERROR_GENERIC;
memcpy((uint8_t *)dev_info, resp, sizeof(struct rpmb_dev_info));
if (IS_ENABLED(CFG_RPMB_FS_DEBUG_DATA)) {
DMSG("Dumping dev_info:");
DHEXDUMP((uint8_t *)dev_info, sizeof(struct rpmb_dev_info));
}
return TEE_SUCCESS;
}
static TEE_Result tee_rpmb_init_read_wr_cnt(uint32_t *wr_cnt)
{
TEE_Result res = TEE_ERROR_GENERIC;
struct tee_rpmb_mem mem;
uint16_t msg_type;
uint8_t nonce[RPMB_NONCE_SIZE];
uint8_t hmac[RPMB_KEY_MAC_SIZE];
struct rpmb_req *req = NULL;
struct rpmb_data_frame *resp = NULL;
struct rpmb_raw_data rawdata;
uint32_t req_size;
uint32_t resp_size;
uint16_t op_result = 0;
if (!wr_cnt)
return TEE_ERROR_BAD_PARAMETERS;
req_size = sizeof(struct rpmb_req) + RPMB_DATA_FRAME_SIZE;
resp_size = RPMB_DATA_FRAME_SIZE;
res = tee_rpmb_alloc(req_size, resp_size, &mem,
(void *)&req, (void *)&resp);
if (res != TEE_SUCCESS)
return res;
res = crypto_rng_read(nonce, RPMB_NONCE_SIZE);
if (res != TEE_SUCCESS)
return res;
msg_type = RPMB_MSG_TYPE_REQ_WRITE_COUNTER_VAL_READ;
memset(&rawdata, 0x00, sizeof(struct rpmb_raw_data));
rawdata.msg_type = msg_type;
rawdata.nonce = nonce;