aboutsummaryrefslogtreecommitdiff
path: root/PolicyNV.c
blob: 455b57d75deb369e6d86e570dbb5891fd81dee9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// This file was extracted from the TCG Published
// Trusted Platform Module Library
// Part 3: Commands
// Family "2.0"
// Level 00 Revision 01.16
// October 30, 2014

#include "InternalRoutines.h"
#include "PolicyNV_fp.h"
#include "Policy_spt_fp.h"
#include "NV_spt_fp.h"         // Include NV support routine for read access check
//
//
//     Error Returns                     Meaning
//
//     TPM_RC_AUTH_TYPE                  NV index authorization type is not correct
//     TPM_RC_NV_LOCKED                  NV index read locked
//     TPM_RC_NV_UNINITIALIZED           the NV index has not been initialized
//     TPM_RC_POLICY                     the comparison to the NV contents failed
//     TPM_RC_SIZE                       the size of nvIndex data starting at offset is less than the size of
//                                       operandB
//
TPM_RC
TPM2_PolicyNV(
   PolicyNV_In       *in                  // IN: input parameter list
   )
{
   TPM_RC                   result;
   SESSION                 *session;
   NV_INDEX                 nvIndex;
   BYTE                     nvBuffer[sizeof(in->operandB.t.buffer)];
   TPM2B_NAME               nvName;
   TPM_CC                   commandCode = TPM_CC_PolicyNV;
   HASH_STATE               hashState;
   TPM2B_DIGEST             argHash;

// Input Validation

   // Get NV index information
   NvGetIndexInfo(in->nvIndex, &nvIndex);

   // Get pointer to the session structure
   session = SessionGet(in->policySession);

   //If this is a trial policy, skip all validations and the operation
   if(session->attributes.isTrialPolicy == CLEAR)
   {
       // NV Read access check. NV index should be allowed for read. A
       // TPM_RC_AUTH_TYPE or TPM_RC_NV_LOCKED error may be return at this
       // point
       result = NvReadAccessChecks(in->authHandle, in->nvIndex);
       if(result != TPM_RC_SUCCESS) return result;

       // Valid NV data size should not be smaller than input operandB size
       if((nvIndex.publicArea.dataSize - in->offset) < in->operandB.t.size)
           return TPM_RC_SIZE + RC_PolicyNV_operandB;

       // Arithmetic Comparison

       // Get NV data. The size of NV data equals the input operand B size
       NvGetIndexData(in->nvIndex, &nvIndex, in->offset,
                      in->operandB.t.size, nvBuffer);

       switch(in->operation)
       {
          case TPM_EO_EQ:
              // compare A = B
              if(CryptCompare(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   != 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_NEQ:
              // compare A != B
              if(CryptCompare(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   == 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_SIGNED_GT:
              // compare A > B signed
              if(CryptCompareSigned(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   <= 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_UNSIGNED_GT:
              // compare A > B unsigned
              if(CryptCompare(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   <= 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_SIGNED_LT:
              // compare A < B signed
              if(CryptCompareSigned(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   >= 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_UNSIGNED_LT:
              // compare A < B unsigned
              if(CryptCompare(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   >= 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_SIGNED_GE:
              // compare A >= B signed
              if(CryptCompareSigned(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   < 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_UNSIGNED_GE:
              // compare A >= B unsigned
              if(CryptCompare(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   < 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_SIGNED_LE:
              // compare A <= B signed
              if(CryptCompareSigned(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   > 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_UNSIGNED_LE:
              // compare A <= B unsigned
              if(CryptCompare(in->operandB.t.size, nvBuffer,
                              in->operandB.t.size, in->operandB.t.buffer)   > 0)
                  return TPM_RC_POLICY;
              break;
          case TPM_EO_BITSET:
              // All bits SET in B are SET in A. ((A&B)=B)
          {
              UINT32 i;
              for (i = 0; i < in->operandB.t.size; i++)
                  if((nvBuffer[i] & in->operandB.t.buffer[i])
                             != in->operandB.t.buffer[i])
                         return TPM_RC_POLICY;
            }
            break;
            case TPM_EO_BITCLEAR:
                // All bits SET in B are CLEAR in A. ((A&B)=0)
            {
                UINT32 i;
                for (i = 0; i < in->operandB.t.size; i++)
                    if((nvBuffer[i] & in->operandB.t.buffer[i]) != 0)
                        return TPM_RC_POLICY;
            }
            break;
            default:
                pAssert(FALSE);
                break;
       }
   }

// Internal Data Update

   // Start argument hash
   argHash.t.size = CryptStartHash(session->authHashAlg, &hashState);

   // add operandB
   CryptUpdateDigest2B(&hashState, &in->operandB.b);

   // add offset
   CryptUpdateDigestInt(&hashState, sizeof(UINT16), &in->offset);

   // add operation
   CryptUpdateDigestInt(&hashState, sizeof(TPM_EO), &in->operation);

   // complete argument digest
   CryptCompleteHash2B(&hashState, &argHash.b);

   // Update policyDigest
   // Start digest
   CryptStartHash(session->authHashAlg, &hashState);

   // add old digest
   CryptUpdateDigest2B(&hashState, &session->u2.policyDigest.b);

   // add commandCode
   CryptUpdateDigestInt(&hashState, sizeof(TPM_CC), &commandCode);

   // add argument digest
   CryptUpdateDigest2B(&hashState, &argHash.b);

   // Adding nvName
   nvName.t.size = EntityGetName(in->nvIndex, &nvName.t.name);
   CryptUpdateDigest2B(&hashState, &nvName.b);

   // complete the digest
   CryptCompleteHash2B(&hashState, &session->u2.policyDigest.b);

   return TPM_RC_SUCCESS;
}