1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2024, Linaro Limited
4  */
5 /* Microsoft Reference Implementation for TPM 2.0
6  *
7  * The copyright in this software is being made available under the BSD
8  * License, included below. This software may be subject to other third
9  * party and contributor rights, including patent rights, and no such
10  * rights are granted under this license.
11  *
12  * Copyright (c) 2018-2023 Microsoft Corporation
13  *
14  * All rights reserved.
15  *
16  * BSD License
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met:
21  *
22  * Redistributions of source code must retain the above copyright notice,
23  * this list of conditions and the following disclaimer.
24  *
25  * Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimer in the
27  * documentation and/or other materials provided with the distribution.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
30  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
35  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 //
43 // Platform Endorsement Primary Seed
44 //
45 
46 #include "TpmError.h"
47 #include "Admin.h"
48 
49 #include <string.h>
50 #include <tee_internal_api.h>
51 #include <tee_internal_api_extensions.h>
52 
53 #define TEE_EPS_SIZE      (256/2)   // From TPM2B_RSA_TEST_PRIME in Hierarchy.c
54 
55 void
_plat__GetEPS(UINT16 Size,uint8_t * EndorsementSeed)56 _plat__GetEPS(UINT16 Size, uint8_t *EndorsementSeed)
57 {
58     TEE_Result Result = TEE_ERROR_ITEM_NOT_FOUND;
59     uint8_t EPS[TEE_EPS_SIZE] = { 0 };
60     size_t EPSLen;
61 
62     IMSG("Size=%" PRIu16 "",Size);
63     IMSG("EPS=%d",TEE_EPS_SIZE);
64 
65     pAssert(Size <= (TEE_EPS_SIZE));
66 
67     Result = TEE_GetPropertyAsBinaryBlock(TEE_PROPSET_CURRENT_TA,
68                                           "com.microsoft.ta.endorsementSeed",
69                                           EPS,
70                                           &EPSLen);
71 
72     if ((EPSLen < Size) || (Result != TEE_SUCCESS)) {
73         // We failed to access the property. We can't continue without it
74         // and we can't just fail to manufacture, so randomize EPS and
75         // continue. If necessary, fTPM TA storage can be cleared, or the
76         // TA updated, and we can trigger remanufacture and try again.
77         _plat__GetEntropy(EndorsementSeed, TEE_EPS_SIZE);
78         return;
79     }
80 
81     memcpy(EndorsementSeed, EPS, Size);
82 
83 #ifdef fTPMDebug
84     {
85         uint32_t x;
86         uint8_t *seed = EndorsementSeed;
87         DMSG("TEE_GetProperty 0x%x, seedLen 0x%x\n", Result, Size);
88         for (x = 0; x < Size; x = x + 8) {
89             DMSG(" seed(%2.2d): %2.2x,%2.2x,%2.2x,%2.2x,%2.2x,%2.2x,%2.2x,%2.2x\n", x,
90                 seed[x + 0], seed[x + 1], seed[x + 2], seed[x + 3],
91                 seed[x + 4], seed[x + 5], seed[x + 6], seed[x + 7]);
92         }
93     }
94 #endif
95 
96     return;
97 }
98