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 #include "NV_Increment_fp.h"
37
38 #if CC_NV_Increment // Conditional expansion of this file
39
40 /*(See part 3 specification)
41 // Increment a NV counter
42 */
43 // Return Type: TPM_RC
44 // TPM_RC_ATTRIBUTES NV index is not a counter
45 // TPM_RC_NV_AUTHORIZATION authorization failure
46 // TPM_RC_NV_LOCKED Index is write locked
47 TPM_RC
TPM2_NV_Increment(NV_Increment_In * in)48 TPM2_NV_Increment(
49 NV_Increment_In *in // IN: input parameter list
50 )
51 {
52 TPM_RC result;
53 NV_REF locator;
54 NV_INDEX *nvIndex = NvGetIndexInfo(in->nvIndex, &locator);
55 UINT64 countValue;
56
57 // Input Validation
58
59 // Common access checks, NvWriteAccessCheck() may return TPM_RC_NV_AUTHORIZATION
60 // or TPM_RC_NV_LOCKED
61 result = NvWriteAccessChecks(in->authHandle,
62 in->nvIndex,
63 nvIndex->publicArea.attributes);
64 if(result != TPM_RC_SUCCESS)
65 return result;
66
67 // Make sure that this is a counter
68 if(!IsNvCounterIndex(nvIndex->publicArea.attributes))
69 return TPM_RCS_ATTRIBUTES + RC_NV_Increment_nvIndex;
70
71 // Internal Data Update
72
73 // If counter index is not been written, initialize it
74 if(!IS_ATTRIBUTE(nvIndex->publicArea.attributes, TPMA_NV, WRITTEN))
75 countValue = NvReadMaxCount();
76 else
77 // Read NV data in native format for TPM CPU.
78 countValue = NvGetUINT64Data(nvIndex, locator);
79
80 // Do the increment
81 countValue++;
82
83 // Write NV data back. A TPM_RC_NV_UNAVAILABLE or TPM_RC_NV_RATE error may
84 // be returned at this point. If necessary, this function will set the
85 // TPMA_NV_WRITTEN attribute
86 result = NvWriteUINT64Data(nvIndex, countValue);
87 if(result == TPM_RC_SUCCESS)
88 {
89 // If a counter just rolled over, then force the NV update.
90 // Note, if this is an orderly counter, then the write-back needs to be
91 // forced, for other counters, the write-back will happen anyway
92 if(IS_ATTRIBUTE(nvIndex->publicArea.attributes, TPMA_NV, ORDERLY)
93 && (countValue & MAX_ORDERLY_COUNT) == 0 )
94 {
95 // Need to force an NV update of orderly data
96 SET_NV_UPDATE(UT_ORDERLY);
97 }
98 }
99 return result;
100 }
101
102 #endif // CC_NV_Increment