xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/src/events/_TPM_Hash_Start.c (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1 /* Microsoft Reference Implementation for TPM 2.0
2  *
3  *  The copyright in this software is being made available under the BSD License,
4  *  included below. This software may be subject to other third party and
5  *  contributor rights, including patent rights, and no such rights are granted
6  *  under this license.
7  *
8  *  Copyright (c) Microsoft Corporation
9  *
10  *  All rights reserved.
11  *
12  *  BSD License
13  *
14  *  Redistribution and use in source and binary forms, with or without modification,
15  *  are permitted provided that the following conditions are met:
16  *
17  *  Redistributions of source code must retain the above copyright notice, this list
18  *  of conditions and the following disclaimer.
19  *
20  *  Redistributions in binary form must reproduce the above copyright notice, this
21  *  list of conditions and the following disclaimer in the documentation and/or
22  *  other materials provided with the distribution.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include "Tpm.h"
36 
37 // This function is called to process a _TPM_Hash_Start indication.
38 LIB_EXPORT void
_TPM_Hash_Start(void)39 _TPM_Hash_Start(
40     void
41     )
42 {
43     TPM_RC              result;
44     TPMI_DH_OBJECT      handle;
45 
46     // If a DRTM sequence object exists, free it up
47     if(g_DRTMHandle != TPM_RH_UNASSIGNED)
48     {
49         FlushObject(g_DRTMHandle);
50         g_DRTMHandle = TPM_RH_UNASSIGNED;
51     }
52 
53     // Create an event sequence object and store the handle in global
54     // g_DRTMHandle. A TPM_RC_OBJECT_MEMORY error may be returned at this point
55     // The NULL value for the first parameter will cause the sequence structure to
56     // be allocated without being set as present. This keeps the sequence from
57     // being left behind if the sequence is terminated early.
58     result = ObjectCreateEventSequence(NULL, &g_DRTMHandle);
59 
60     // If a free slot was not available, then free up a slot.
61     if(result != TPM_RC_SUCCESS)
62     {
63         // An implementation does not need to have a fixed relationship between
64         // slot numbers and handle numbers. To handle the general case, scan for
65         // a handle that is assigned and free it for the DRTM sequence.
66         // In the reference implementation, the relationship between handles and
67         // slots is fixed. So, if the call to ObjectCreateEvenSequence()
68         // failed indicating that all slots are occupied, then the first handle we
69         // are going to check (TRANSIENT_FIRST) will be occupied. It will be freed
70         // so that it can be assigned for use as the DRTM sequence object.
71         for(handle = TRANSIENT_FIRST; handle < TRANSIENT_LAST; handle++)
72         {
73             // try to flush the first object
74             if(IsObjectPresent(handle))
75                 break;
76         }
77         // If the first call to find a slot fails but none of the slots is occupied
78         // then there's a big problem
79         pAssert(handle < TRANSIENT_LAST);
80 
81         // Free the slot
82         FlushObject(handle);
83 
84         // Try to create an event sequence object again.  This time, we must
85         // succeed.
86         result = ObjectCreateEventSequence(NULL, &g_DRTMHandle);
87         if(result != TPM_RC_SUCCESS)
88             FAIL(FATAL_ERROR_INTERNAL);
89     }
90 
91     return;
92 }