1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016-2020, Linaro Limited.
4  */
5 
6 #include <kernel/tee_common_otp.h>
7 #include <kernel/tee_ta_manager.h>
8 #include <kernel/user_access.h>
9 #include <tee/tee_cryp_utl.h>
10 #include <tee/tee_svc.h>
11 #include <user_ta_header.h>
12 #include <util.h>
13 
14 /*
15  * The data to hash is 48 bytes made up of:
16  * - 16 bytes: the UUID of the calling TA.
17  * - 32 bytes: the hardware device ID
18  * The resulting endorsement seed is 32 bytes.
19  *
20  * The output buffer is the "binary" struct defined in
21  * the "prop_value" union and therefore comprises:
22  * -  4 bytes: the size of the binary value data (32)
23  * - 32 bytes: the binary value data (endorsement seed)
24  *
25  * Note that this code assumes an endorsement seed
26  * size == device ID size for convenience.
27  */
get_prop_endorsement(struct ts_session * sess,void * buf,size_t * blen)28 static TEE_Result get_prop_endorsement(struct ts_session *sess,
29 				       void *buf, size_t *blen)
30 {
31 	TEE_Result res;
32 	uint32_t ta_endorsement_seed_size = 32;
33 	uint8_t data[sizeof(TEE_UUID) + ta_endorsement_seed_size];
34 	uint32_t bin[1 + ta_endorsement_seed_size / sizeof(uint32_t)];
35 	uint32_t *bin_len = (uint32_t *)bin;
36 	uint8_t *bin_val = (uint8_t *)(&bin[1]);
37 
38 	if (*blen < sizeof(bin)) {
39 		*blen = sizeof(bin);
40 		return TEE_ERROR_SHORT_BUFFER;
41 	}
42 	*blen = sizeof(bin);
43 
44 	memcpy(data, &sess->ctx->uuid, sizeof(TEE_UUID));
45 
46 	if (tee_otp_get_die_id(&data[sizeof(TEE_UUID)],
47 			       ta_endorsement_seed_size))
48 		return TEE_ERROR_BAD_STATE;
49 
50 	res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data),
51 				    bin_val, ta_endorsement_seed_size);
52 	if (res != TEE_SUCCESS)
53 		return TEE_ERROR_BAD_STATE;
54 
55 	*bin_len = ta_endorsement_seed_size;
56 
57 	return copy_to_user(buf, bin, sizeof(bin));
58 }
59 
60 static const struct tee_props vendor_propset_array_tee[] = {
61 	{
62 		.name = "com.microsoft.ta.endorsementSeed",
63 		.prop_type = USER_TA_PROP_TYPE_BINARY_BLOCK,
64 		.get_prop_func = get_prop_endorsement
65 	},
66 };
67 
68 const struct tee_vendor_props vendor_props_tee = {
69 	.props = vendor_propset_array_tee,
70 	.len = ARRAY_SIZE(vendor_propset_array_tee),
71 };
72