summaryrefslogtreecommitdiffstats
path: root/src/usr/secureboot/trusted/trustedTypes.C
blob: 331031adee39af098da21266992becfbed437a7e (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/secureboot/trusted/trustedTypes.C $                   */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2016                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file trustedTypes.C
 *
 * @brief Trusted boot type inline functions
 */

/////////////////////////////////////////////////////////////////
// NOTE: This file is exportable as TSS-Lite for skiboot/PHYP  //
/////////////////////////////////////////////////////////////////

// ----------------------------------------------
// Includes
// ----------------------------------------------
#include <string.h>
#include "trustedboot.H"
#include "trustedTypes.H"

#ifdef __cplusplus
namespace TRUSTEDBOOT
{
#endif

    uint8_t* unmarshalChunk(uint8_t* i_tpmBuf,
                            size_t * io_tpmBufSize,
                            void* o_chunkPtr,
                            size_t i_chunkSize);

    uint8_t* marshalChunk(uint8_t* o_tpmBuf,
                          size_t i_tpmBufSize,
                          size_t * io_cmdSize,
                          void* i_chunkPtr,
                          size_t i_chunkSize);

    uint8_t* unmarshalChunk(uint8_t* i_tpmBuf,
                            size_t * io_tpmBufSize,
                            void* o_chunkPtr,
                            size_t i_chunkSize)
    {
        if (NULL != i_tpmBuf)
        {
            if (i_chunkSize > *io_tpmBufSize)
            {
                return NULL;
            }
            memcpy(o_chunkPtr, i_tpmBuf, i_chunkSize);
            i_tpmBuf += i_chunkSize;
            *io_tpmBufSize -= i_chunkSize;
        }
        return i_tpmBuf;
    }

    uint8_t* marshalChunk(uint8_t* o_tpmBuf,
                          size_t i_tpmBufSize,
                          size_t * io_cmdSize,
                          void* i_chunkPtr,
                          size_t i_chunkSize)
    {
        if (NULL != o_tpmBuf)
        {
            if ((*io_cmdSize + i_chunkSize) > i_tpmBufSize)
            {
                return NULL;
            }
            memcpy(o_tpmBuf, i_chunkPtr, i_chunkSize);
            o_tpmBuf += i_chunkSize;
            *io_cmdSize += i_chunkSize;
        }
        return o_tpmBuf;
    }

    uint8_t* TPML_TAGGED_TPM_PROPERTY_unmarshal(TPML_TAGGED_TPM_PROPERTY* val,
                                                uint8_t* i_tpmBuf,
                                                size_t* io_tpmBufSize)
    {

        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->count), sizeof(val->count));

        // Now we know the count as well
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->tpmProperty[0]),
                                  sizeof(TPMS_TAGGED_PROPERTY) * val->count);

        return i_tpmBuf;
    }

    uint8_t* TPMS_CAPABILITY_DATA_unmarshal(TPMS_CAPABILITY_DATA* val,
                                            uint8_t* i_tpmBuf,
                                            size_t * io_tpmBufSize)
    {
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->capability),
                                  sizeof(val->capability));

        switch (val->capability)
        {
          case TPM_CAP_TPM_PROPERTIES:
              {
                  return TPML_TAGGED_TPM_PROPERTY_unmarshal(
                                      &(val->data.tpmProperties), i_tpmBuf,
                                      io_tpmBufSize);
              }
              break;
          default:
              {
                  TRACFCOMP( g_trac_trustedboot,
                       "TPMS_CAPABILITY_DATA::unmarshal Unknown capability");
                  return NULL;
              }
              break;
        }
        return NULL;
    }

    uint8_t* TPMT_HA_marshal(TPMT_HA* val,
                             uint8_t* o_tpmBuf,
                             size_t i_tpmBufSize,
                             size_t * io_cmdSize)
    {
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                &(val->algorithmId), sizeof(val->algorithmId));
        if (getDigestSize((TPM_Alg_Id)val->algorithmId) == 0)
        {
            return NULL;
        }
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                &(val->digest.bytes),
                                getDigestSize((TPM_Alg_Id)val->algorithmId));
        return o_tpmBuf;
    }

    size_t TCG_PCR_EVENT_marshalSize(TCG_PCR_EVENT* val)
    {
        return (sizeof(TCG_PCR_EVENT) + val->eventSize - MAX_TPM_LOG_MSG);
    }

    size_t TPMT_HA_marshalSize(TPMT_HA* val)
    {
        return (sizeof(val->algorithmId) +
                getDigestSize((TPM_Alg_Id)(val->algorithmId)));
    }

    size_t TPM_EVENT_FIELD_marshalSize(TPM_EVENT_FIELD* val)
    {
        return (sizeof(val->eventSize) + val->eventSize);
    }


    uint8_t* TPML_DIGEST_VALUES_marshal(TPML_DIGEST_VALUES* val,
                                        uint8_t* o_tpmBuf,
                                        size_t i_tpmBufSize,
                                        size_t * io_cmdSize)
    {
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                &(val->count), sizeof(val->count));
        if (NULL != o_tpmBuf && HASH_COUNT < val->count)
        {
            o_tpmBuf = NULL;
        }
        else
        {
            for (size_t idx = 0; idx < val->count; idx++)
            {
                o_tpmBuf = TPMT_HA_marshal(&(val->digests[idx]),
                                           o_tpmBuf,
                                           i_tpmBufSize,
                                           io_cmdSize);
                if (NULL == o_tpmBuf)
                {
                    break;
                }
            }
        }
        return o_tpmBuf;
    }

    uint8_t* TPM2_BaseIn_marshal(TPM2_BaseIn* val, uint8_t* o_tpmBuf,
                                 size_t i_tpmBufSize, size_t* io_cmdSize)
    {
        return marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                            val, sizeof(TPM2_BaseIn));
    }

    uint8_t* TPM2_BaseOut_unmarshal(TPM2_BaseOut* val, uint8_t* i_tpmBuf,
                                    size_t* io_tpmBufSize, size_t i_outBufSize)
    {
        if (sizeof(TPM2_BaseOut) > i_outBufSize)
        {
            return NULL;
        }
        return unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                              val, sizeof(TPM2_BaseOut));
    }

    uint8_t* TPM2_2ByteIn_marshal(TPM2_2ByteIn* val,
                                  uint8_t* o_tpmBuf,
                                  size_t i_tpmBufSize,
                                  size_t* io_cmdSize)
    {
        // Base has already been marshaled
        return marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                            &(val->param), sizeof(val->param));
    }

    uint8_t* TPM2_4ByteIn_marshal(TPM2_4ByteIn* val,
                                  uint8_t* o_tpmBuf,
                                  size_t i_tpmBufSize,
                                  size_t* io_cmdSize)
    {
        // Base has already been marshaled
        return marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                            &(val->param), sizeof(val->param));
    }

    uint8_t* TPM2_GetCapabilityIn_marshal(TPM2_GetCapabilityIn* val,
                                          uint8_t* o_tpmBuf,
                                          size_t i_tpmBufSize,
                                          size_t* io_cmdSize)
    {
        // Base has already been marshaled
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                &(val->capability),
                                sizeof(val->capability));
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                &(val->property),
                                sizeof(val->property));
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                &(val->propertyCount),
                                sizeof(val->propertyCount));
        return o_tpmBuf;
    }

    uint8_t* TPM2_GetCapabilityOut_unmarshal(TPM2_GetCapabilityOut* val,
                                             uint8_t* i_tpmBuf,
                                             size_t* io_tpmBufSize,
                                             size_t i_outBufSize)
    {
        // Base has already been unmarshaled
        if (sizeof(TPM2_GetCapabilityOut) > i_outBufSize)
        {
            return NULL;
        }
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->moreData), sizeof(val->moreData));

        // Capability data block
        return TPMS_CAPABILITY_DATA_unmarshal(&(val->capData), i_tpmBuf,
                                              io_tpmBufSize);

    }

    uint8_t* TPM2_ExtendIn_marshalHandle(TPM2_ExtendIn* val,
                                         uint8_t* o_tpmBuf,
                                         size_t i_tpmBufSize,
                                         size_t* io_cmdSize)
    {
        // Base has already been marshaled
        // only marshal the pcr handle in this stage
        return marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                            &(val->pcrHandle), sizeof(val->pcrHandle));
    }

    uint8_t* TPM2_ExtendIn_marshalParms(TPM2_ExtendIn* val,
                                        uint8_t* o_tpmBuf,
                                        size_t i_tpmBufSize,
                                        size_t* io_cmdSize)
    {
        // Base and handle has already been marshaled
        return (TPML_DIGEST_VALUES_marshal(&(val->digests), o_tpmBuf,
                                           i_tpmBufSize, io_cmdSize));
    }

    uint8_t* TPMS_PCR_SELECTION_marshal(TPMS_PCR_SELECTION* val,
                                        uint8_t* o_tpmBuf,
                                        size_t i_tpmBufSize,
                                        size_t* io_cmdSize)
    {
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                &(val->algorithmId), sizeof(val->algorithmId));
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                               &(val->sizeOfSelect), sizeof(val->sizeOfSelect));

        if (NULL != o_tpmBuf &&
            PCR_SELECT_MAX < val->sizeOfSelect)
        {
            return NULL;
        }

        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                val->pcrSelect, val->sizeOfSelect);
        return o_tpmBuf;
    }

    uint8_t* TPMS_PCR_SELECTION_unmarshal(TPMS_PCR_SELECTION* val,
                                          uint8_t* i_tpmBuf,
                                          size_t* io_tpmBufSize)
    {
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->algorithmId),
                                  sizeof(val->algorithmId));
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->sizeOfSelect),
                                  sizeof(val->sizeOfSelect));
        if (NULL != i_tpmBuf &&
            PCR_SELECT_MAX < val->sizeOfSelect)
        {
            return NULL;
        }
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  val->pcrSelect, val->sizeOfSelect);

        return i_tpmBuf;
    }

    uint8_t* TPM2B_DIGEST_unmarshal(TPM2B_DIGEST* val,
                                    uint8_t* i_tpmBuf,
                                    size_t* io_tpmBufSize)
    {
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &val->size, sizeof(val->size));
        if (NULL != i_tpmBuf &&
            sizeof(TPMU_HA) < val->size)
        {
            TRACUCOMP( g_trac_trustedboot,
                       "TPM2B_DIGEST::unmarshal invalid size");
            return NULL;
        }
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  val->buffer, val->size);
        return i_tpmBuf;

    }

    uint8_t* TPML_DIGEST_unmarshal(TPML_DIGEST* val,
                                   uint8_t* i_tpmBuf,
                                   size_t* io_tpmBufSize)
    {
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->count), sizeof(val->count));
        if (NULL != i_tpmBuf && HASH_COUNT < val->count)
        {
            TRACUCOMP( g_trac_trustedboot,
                       "TPML_DIGEST::unmarshal invalid count %d", val->count);
            i_tpmBuf = NULL;
        }
        else if (NULL != i_tpmBuf)
        {
            for (size_t idx = 0; idx < val->count; idx++)
            {
                i_tpmBuf = TPM2B_DIGEST_unmarshal(&(val->digests[idx]),
                                                  i_tpmBuf,
                                                  io_tpmBufSize);
                if (NULL == i_tpmBuf)
                {
                    break;
                }
            }
        }
        return i_tpmBuf;

    }

    uint8_t* TPML_PCR_SELECTION_marshal(TPML_PCR_SELECTION* val,
                                        uint8_t* o_tpmBuf,
                                        size_t i_tpmBufSize,
                                        size_t* io_cmdSize)
    {
        o_tpmBuf = marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                                &(val->count), sizeof(val->count));
        if (NULL != o_tpmBuf && HASH_COUNT < val->count)
        {
            TRACUCOMP( g_trac_trustedboot,
                       "TPML_PCR_SELECTION::marshal invalid count");
            o_tpmBuf = NULL;
        }
        else if (NULL != o_tpmBuf)
        {
            for (size_t idx = 0; idx < val->count; idx++)
            {
                o_tpmBuf = TPMS_PCR_SELECTION_marshal(
                                          &(val->pcrSelections[idx]),
                                          o_tpmBuf,
                                          i_tpmBufSize,
                                          io_cmdSize);
                if (NULL == o_tpmBuf)
                {
                    break;
                }
            }
        }
        return o_tpmBuf;
    }

    uint8_t* TPML_PCR_SELECTION_unmarshal(TPML_PCR_SELECTION* val,
                                          uint8_t* i_tpmBuf,
                                          size_t* io_tpmBufSize)
    {
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->count), sizeof(val->count));
        if (NULL != i_tpmBuf && HASH_COUNT < val->count)
        {
            TRACUCOMP( g_trac_trustedboot,
                       "TPML_PCR_SELECTION::unmarshal invalid count");
            i_tpmBuf = NULL;
        }
        else if (NULL != i_tpmBuf)
        {
            for (size_t idx = 0; idx < val->count; idx++)
            {
                i_tpmBuf = TPMS_PCR_SELECTION_unmarshal(
                                 &(val->pcrSelections[idx]),
                                 i_tpmBuf,
                                 io_tpmBufSize);
                if (NULL == i_tpmBuf)
                {
                    break;
                }
            }
        }
        return i_tpmBuf;

    }

    uint8_t* TPM2_PcrReadIn_marshal(TPM2_PcrReadIn* val,
                                    uint8_t* o_tpmBuf,
                                    size_t i_tpmBufSize,
                                    size_t* io_cmdSize)
    {
        // Base and handle has already been marshaled
        return (TPML_PCR_SELECTION_marshal(&(val->pcrSelectionIn), o_tpmBuf,
                                           i_tpmBufSize, io_cmdSize));
    }

    uint8_t* TPM2_PcrReadOut_unmarshal(TPM2_PcrReadOut* val,
                                       uint8_t* i_tpmBuf,
                                       size_t* io_tpmBufSize,
                                       size_t i_outBufSize)
    {
        // Base and handle has already been marshaled
        if (sizeof(TPM2_PcrReadOut) > i_outBufSize) return NULL;
        i_tpmBuf = unmarshalChunk(i_tpmBuf, io_tpmBufSize,
                                  &(val->pcrUpdateCounter),
                                  sizeof(val->pcrUpdateCounter));

        i_tpmBuf = TPML_PCR_SELECTION_unmarshal(&(val->pcrSelectionOut),
                                                i_tpmBuf, io_tpmBufSize);
        i_tpmBuf = TPML_DIGEST_unmarshal(&(val->pcrValues), i_tpmBuf,
                                         io_tpmBufSize);
        return i_tpmBuf;

    }

    uint8_t* TPMS_AUTH_COMMAND_marshal(TPMS_AUTH_COMMAND* val,
                                       uint8_t* o_tpmBuf,
                                       size_t i_tpmBufSize,
                                       size_t* io_cmdSize)
    {
        return marshalChunk(o_tpmBuf, i_tpmBufSize, io_cmdSize,
                            val, sizeof(TPMS_AUTH_COMMAND));
    }

#ifdef __cplusplus
} // end TRUSTEDBOOT
#endif
OpenPOWER on IntegriCloud