aboutsummaryrefslogtreecommitdiff
path: root/PolicyCounterTimer.c
blob: 283cb91c14c8c920e4f3dd861829f509524d2fb9 (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
// 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 "PolicyCounterTimer_fp.h"
#include "Policy_spt_fp.h"
//
//
//     Error Returns                  Meaning
//
//     TPM_RC_POLICY                  the comparison of the selected portion of the TPMS_TIME_INFO with
//                                    operandB failed
//     TPM_RC_RANGE                   offset + size exceed size of TPMS_TIME_INFO structure
//
TPM_RC
TPM2_PolicyCounterTimer(
   PolicyCounterTimer_In      *in              // IN: input parameter list
   )
{
   TPM_RC                result;
   SESSION              *session;
   TIME_INFO             infoData;      // data buffer of TPMS_TIME_INFO
   TPM_CC                commandCode = TPM_CC_PolicyCounterTimer;
   HASH_STATE            hashState;
   TPM2B_DIGEST          argHash;

// Input Validation

   // If the command is going to use any part of the counter or timer, need
   // to verify that time is advancing.
   // The time and clock vales are the first two 64-bit values in the clock
   if(in->offset < sizeof(UINT64) + sizeof(UINT64))
   {
       // Using Clock or Time so see if clock is running. Clock doesn't run while
       // NV is unavailable.
       // TPM_RC_NV_UNAVAILABLE or TPM_RC_NV_RATE error may be returned here.
       result = NvIsAvailable();
       if(result != TPM_RC_SUCCESS)
           return result;
   }
   // 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)
   {
       // Get time data info. The size of time info data equals the input
       // operand B size. A TPM_RC_RANGE error may be returned at this point
       result = TimeGetRange(in->offset, in->operandB.t.size, &infoData);
       if(result != TPM_RC_SUCCESS) return result;

         // Arithmetic Comparison
         switch(in->operation)
         {
             case TPM_EO_EQ:
                 // compare A = B
                 if(CryptCompare(in->operandB.t.size, infoData,
                                 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, infoData,
                                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, infoData,
                                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, infoData,
                                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, infoData,
                                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, infoData,
                                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, infoData,
                                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, infoData,
                                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, infoData,
                                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, infoData,
                                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(   (infoData[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((infoData[i] & in->operandB.t.buffer[i]) != 0)
                      return TPM_RC_POLICY;
          }
          break;
          default:
              pAssert(FALSE);
              break;
      }
  }

// Internal Data Update

  // Start argument list 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 hash
  CryptCompleteHash2B(&hashState, &argHash.b);

  // update policyDigest
  // start hash
  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);

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

   return TPM_RC_SUCCESS;
}