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_Write_fp.h"
37 
38 #if CC_NV_Write  // Conditional expansion of this file
39 
40 /*(See part 3 specification)
41 // Write to a NV index
42 */
43 //  Return Type: TPM_RC
44 //      TPM_RC_ATTRIBUTES               Index referenced by 'nvIndex' has either
45 //                                      TPMA_NV_BITS, TPMA_NV_COUNTER, or
46 //                                      TPMA_NV_EVENT attribute SET
47 //      TPM_RC_NV_AUTHORIZATION         the authorization was valid but the
48 //                                      authorizing entity ('authHandle')
49 //                                      is not allowed to write to the Index
50 //                                      referenced by 'nvIndex'
51 //      TPM_RC_NV_LOCKED                Index referenced by 'nvIndex' is write
52 //                                      locked
53 //      TPM_RC_NV_RANGE                 if TPMA_NV_WRITEALL is SET then the write
54 //                                      is not the size of the Index referenced by
55 //                                      'nvIndex'; otherwise, the write extends
56 //                                      beyond the limits of the Index
57 //
58 TPM_RC
TPM2_NV_Write(NV_Write_In * in)59 TPM2_NV_Write(
60     NV_Write_In     *in             // IN: input parameter list
61     )
62 {
63     NV_INDEX        *nvIndex = NvGetIndexInfo(in->nvIndex, NULL);
64     TPMA_NV          attributes = nvIndex->publicArea.attributes;
65     TPM_RC           result;
66 
67 // Input Validation
68 
69     // Common access checks, NvWriteAccessCheck() may return TPM_RC_NV_AUTHORIZATION
70     // or TPM_RC_NV_LOCKED
71     result = NvWriteAccessChecks(in->authHandle,
72                                  in->nvIndex,
73                                  attributes);
74     if(result != TPM_RC_SUCCESS)
75         return result;
76 
77     // Bits index, extend index or counter index may not be updated by
78     // TPM2_NV_Write
79     if(IsNvCounterIndex(attributes)
80        || IsNvBitsIndex(attributes)
81        || IsNvExtendIndex(attributes))
82         return TPM_RC_ATTRIBUTES;
83 
84     // Make sure that the offset is not too large
85     if(in->offset > nvIndex->publicArea.dataSize)
86         return TPM_RCS_VALUE + RC_NV_Write_offset;
87 
88     // Make sure that the selection is within the range of the Index
89     if(in->data.t.size > (nvIndex->publicArea.dataSize - in->offset))
90         return TPM_RC_NV_RANGE;
91 
92     // If this index requires a full sized write, make sure that input range is
93     // full sized.
94     // Note: if the requested size is the same as the Index data size, then offset
95     // will have to be zero. Otherwise, the range check above would have failed.
96     if(IS_ATTRIBUTE(attributes, TPMA_NV, WRITEALL)
97        && in->data.t.size < nvIndex->publicArea.dataSize)
98         return TPM_RC_NV_RANGE;
99 
100 // Internal Data Update
101 
102     // Perform the write.  This called routine will SET the TPMA_NV_WRITTEN
103     // attribute if it has not already been SET. If NV isn't available, an error
104     // will be returned.
105     return NvWriteIndexData(nvIndex, in->offset, in->data.t.size,
106                             in->data.t.buffer);
107 }
108 
109 #endif // CC_NV_Write