summaryrefslogtreecommitdiffstats
path: root/src/usr/secureboot/trusted/test/tpmLogMgrTest.H
blob: f2f1d4d5409eaf27fa3abdb573c9c19745be4aab (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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/secureboot/trusted/test/tpmLogMgrTest.H $             */
/*                                                                        */
/* 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                                                     */
#ifndef __TPMLOGMGRTEST_H
#define __TPMLOGMGRTEST_H

/**
 *  @file tpmLogMgrTest.H
 *
 *  @brief Test cases for the tpm log manager
 */

#include <sys/time.h>
#include <cxxtest/TestSuite.H>
#include <errl/errlmanager.H>
#include <errl/errlentry.H>
#include "../trustedTypes.H"
#include "../tpmLogMgr.H"


using namespace TRUSTEDBOOT;


class TPMLogMgrTest: public CxxTest::TestSuite
{
    public:

        /**
         * @brief Retrieve a Tpm log manager to test with
         */
        void getTestLogMgr(TpmLogMgr* logMgr)
        {

            errlHndl_t err = TpmLogMgr_initialize(logMgr);
            if( NULL != err )
            {
                TS_FAIL( "getTestLogMgr - Error detected" );
                errlCommit( err, SECURE_COMP_ID );
                delete err;
                err = NULL;
            }
            else if (TpmLogMgr_getLogSize(logMgr) != 69)
                // 69 is size of header entry
            {
                TS_FAIL( "getTestLogMgr - Failed to find "
                         "proper header log Len=%d",
                         TpmLogMgr_getLogSize(logMgr));
            }
            else
            {
                TRACUCOMP(g_trac_trustedboot, "getTestLogMgr - "
                          "Allocate returned as expected. Len=%d",
                          TpmLogMgr_getLogSize(logMgr));
            }
        }

        /**
         * @brief TPM Log Allocate Test
         */
        void testTPMLogMgrAllocate ( void )
        {
            errlHndl_t err = NULL;
            int64_t fails = 0, num_ops = 0;
            TCG_PCR_EVENT2 log;
            uint32_t totalLogSize = 0;

            TRACFCOMP( g_trac_trustedboot,
                       "testTPMLogMgrAllocate - Start" );

            do
            {
                // Initialize logMgr
                TpmLogMgr logMgr;
                getTestLogMgr(&logMgr);

                // Bump full size by the header
                totalLogSize = TpmLogMgr_getLogSize(&logMgr);

                for (size_t idx = 0; idx < 10; idx ++)
                {

                    // Build up and add an event to the log
                    memset(&log, 0, sizeof(log));
                    log.pcrIndex = 0xF1F2F300 + idx;
                    log.eventType = 0xE1E2E3E4;

                    log.digests.count=1;

                    uint32_t digestSize = TPM_ALG_SHA256_SIZE;
                    log.digests.digests[0].algorithmId = TPM_ALG_SHA256;

                    num_ops++;
                    if (TPMT_HA_marshalSize(&(log.digests.digests[0])) !=
                        (digestSize + sizeof(uint16_t)))
                    {
                        fails++;
                        TS_FAIL( "testTPMLogMgrAllocate(%d) - Invalid digest "
                                 "size Act=%d Exp=%d",
                                 TPMT_HA_marshalSize(&(log.digests.digests[0])),
                                 idx,
                                 (digestSize + sizeof(uint16_t)));
                        break;
                    }

                    memset(log.digests.digests[0].digest.bytes,
                           0x51+idx, digestSize);

                    log.event.eventSize = 21+idx;
                    memset(log.event.event, 0x31+idx, log.event.eventSize);

                    // Full log size dependent on algorithm used
                    uint32_t logSize = (sizeof(uint32_t) * 2) +
                        sizeof(uint32_t) +
                        sizeof(uint16_t) + TPM_ALG_SHA256_SIZE +
                        sizeof(uint32_t) + 21 + idx;
                    totalLogSize += logSize;

                    num_ops++;
                    if (TCG_PCR_EVENT2_marshalSize(&log) != logSize)
                    {
                        fails++;
                        TS_FAIL( "testTPMLogMgrAllocate(%d) - "
                                 "Invalid final log size "
                                 "Act=%d Exp=%d",
                                 idx, TCG_PCR_EVENT2_marshalSize(&log),
                                 logSize);
                        break;
                    }

                    // Ok, finally add this to the TPM log
                    err = TpmLogMgr_addEvent(&logMgr,&log);
                    num_ops ++;
                    if( NULL != err )
                    {
                        fails++;
                        TS_FAIL( "testTPMLogMgrAllocate(%d) - "
                                 "Error detected adding event", idx);
                        errlCommit( err,
                                    SECURE_COMP_ID );
                        delete err;
                        err = NULL;
                        break;
                    }
                    else if (TpmLogMgr_getLogSize(&logMgr) != totalLogSize)
                    {
                        fails++;
                        TS_FAIL( "testTPMLogMgrAllocate(%d) - Failed to find "
                                 "log ActLen=%d ExpLen=%d",
                                 idx,
                                 TpmLogMgr_getLogSize(&logMgr),
                                 totalLogSize);
                        break;

                    }
                    else
                    {
                        TRACUCOMP(g_trac_trustedboot,
                                  "testTPMLogMgrAllocate(%d) "
                                  "- Addevent returned as expected. "
                                  "Len=%d",
                                  idx,
                                  TpmLogMgr_getLogSize(&logMgr));
                    }

                } // End for

                // Lastly dump the log to a trace so we can review it
                TpmLogMgr_dumpLog(&logMgr);

            } while( 0 );
            TRACFCOMP( g_trac_trustedboot,
                       "testTPMLogMgrAllocate - End: %d/%d fails",
                       fails, num_ops );
        }

        /**
         * @brief Add generic test event to log and return the event added
         */
        TCG_PCR_EVENT2 addTestLogEvent(TpmLogMgr& i_logMgr,
                                       const char* i_logMsg)
        {
            // Set components of TCG_PCR_EVENT2
            TPM_Pcr pcr = PCR_0;
            TPM_Alg_Id algId = TPM_ALG_SHA256;
            size_t digestSize = TPM_ALG_SHA256_SIZE;
            uint8_t digest[digestSize];
            for (size_t idx = 0; idx < digestSize; idx++)
            {
                digest[idx] = idx+1;
            }

            // Get a TCG_PCR_EVENT2
            TCG_PCR_EVENT2 eventLog = TpmLogMgr_genLogEventPcrExtend(
                                        pcr, algId,
                                        digest, digestSize, i_logMsg);

            // Add event to log
            errlHndl_t err = TpmLogMgr_addEvent(&i_logMgr, &eventLog);
            if (err)
            {
                TS_FAIL("addTestLogEvent - Failed to addEvent with message = %s",
                        i_logMsg);
                errlCommit( err, SECURE_COMP_ID );
                delete err;
                err = NULL;
            }

            return eventLog;
        }

        /**
         * @brief TPM Log read single entry test
         */
        void testTpmLogReadSingleEntry ( void )
        {
            TRACFCOMP( g_trac_trustedboot, "testTpmLogReadSingleEntry - Start" );

            do {
                // Initialize logMgr
                TpmLogMgr logMgr;
                getTestLogMgr(&logMgr);

                // Add event to log
                const char* logMsg = "testTpmLogReadSingleEntry";
                TCG_PCR_EVENT2 eventLog = addTestLogEvent(logMgr, logMsg);

                // Retrive event from log
                TCG_PCR_EVENT2 resultEventLog;
                const uint8_t* eventHndl = TpmLogMgr_getFirstEvent(&logMgr);
                if (eventHndl == NULL)
                {
                    TS_FAIL( "testTpmLogReadSingleEntry - Log only contains header event" );
                    break;
                }
                bool unMarshalError = false;
                eventHndl = TpmLogMgr_getNextEvent(&logMgr, eventHndl,
                                                   &resultEventLog,
                                                   &unMarshalError);
                if (unMarshalError)
                {
                    TS_FAIL( "testTpmLogReadSingleEntry - LogUnmarshall failed");
                    break;
                }
                // Ensure getNextEvent returned NULL as there is only one entry
                if (eventHndl != NULL)
                {
                    TS_FAIL( "testTpmLogReadSingleEntry - Unexpected entries in eventLog");
                    break;
                }

                // Ensure == operator is correct
                if (memcmp(&eventLog, &resultEventLog, sizeof(TCG_PCR_EVENT2))
                    != 0)
                {
                    TS_FAIL( "testTpmLogReadSingleEntry - ==operator failed to compare events");
                    break;
                }

                // Ensure what we read out matches what we put in
                if ( eventLog == resultEventLog )
                {
                    TRACFCOMP( g_trac_trustedboot, "testTpmLogReadSingleEntry - Success read Event");
                }
                else
                {
                    TS_FAIL( "testTpmLogReadSingleEntry - Failed to read Event");
                    break;
                }

            } while(0);

            TRACFCOMP( g_trac_trustedboot, "testTpmLogReadSingleEntry - End" );
        }

        /**
         * @brief TPM Log read empty log
         */
        void testTpmLogReadEmptyLog ( void )
        {
            TRACFCOMP( g_trac_trustedboot, "testTpmLogReadEmptyLog - Start" );

            do {
                // Initialize logMgr
                TpmLogMgr logMgr;
                getTestLogMgr(&logMgr);

                // Ensure there is only a header event in the log
                if (TpmLogMgr_getFirstEvent(&logMgr) != NULL)
                {
                    TS_FAIL( "testTpmLogReadEmptyLog - Read past actual eventLog");
                    break;
                }
            }
            while (0);
        }

        /**
         * @brief TPM Log read past valid log test
         */
        void testTpmLogReadPastValidLog ( void )
        {
            TRACFCOMP( g_trac_trustedboot, "testTpmLogReadPastValidLog - Start" );

            do {
                // Initialize logMgr
                TpmLogMgr logMgr;
                getTestLogMgr(&logMgr);

                // Add event to log
                const char* logMsg = "testTpmLogReadPastValidLog";
                addTestLogEvent(logMgr, logMsg);

                // Retrive event from log
                TCG_PCR_EVENT2 resultEventLog;
                const uint8_t* eventHndl = TpmLogMgr_getFirstEvent(&logMgr);
                if (eventHndl == NULL)
                {
                    TS_FAIL( "testTpmLogReadPastValidLog - Log only contains header event" );
                    break;
                }

                // Try reading past a valid log
                bool unMarshalError = false;
                for (int i = 0; i < 2; ++i)
                {
                    eventHndl = TpmLogMgr_getNextEvent(&logMgr, eventHndl,
                                                       &resultEventLog,
                                                       &unMarshalError);
                    if (i == 0 && unMarshalError)
                    {
                        TS_FAIL( "testTpmLogReadPastValidLog - LogUnmarshall failed");
                        break;
                    }
                    if (i == 1 && (eventHndl != NULL || !unMarshalError ) )
                    {
                        TS_FAIL( "testTpmLogReadPastValidLog - Read past actual eventLog without posting error and returning NULL");
                    }
                }
            }
            while (0);
        }


        /**
         * @brief TPM Log calcLogSize tests
         */
        void testTpmLogCalcLogSize ( void )
        {
            TRACFCOMP( g_trac_trustedboot, "testTpmLogCalcLogSize - Start" );

            do {
                // Initialize logMgr
                TpmLogMgr logMgr;
                getTestLogMgr(&logMgr);

                size_t firstEventSize = TpmLogMgr_getLogSize(&logMgr);

                // No events beyond initial one
                if ((TpmLogMgr_calcLogSize(&logMgr) !=
                     TpmLogMgr_getLogSize(&logMgr)))
                {
                    TS_FAIL( "testTpmLogCalcLogSize - "
                             "Invalid first event calc LS(%d) CS(%d)",
                             TpmLogMgr_getLogSize(&logMgr),
                             TpmLogMgr_calcLogSize(&logMgr));
                    break;
                }

                // Add an event to log
                TCG_PCR_EVENT2 eventLog = addTestLogEvent(logMgr,
                                                          "CalcLog11");
                if (TpmLogMgr_calcLogSize(&logMgr) !=
                    TpmLogMgr_getLogSize(&logMgr) ||
                    ((firstEventSize +
                      TCG_PCR_EVENT2_marshalSize(&eventLog))
                     != TpmLogMgr_getLogSize(&logMgr)) )
                {
                    TS_FAIL( "testTpmLogCalcLogSize - "
                             "Invalid second event calc LS(%d) CS(%d)",
                             TpmLogMgr_getLogSize(&logMgr),
                             TpmLogMgr_calcLogSize(&logMgr));
                    break;
                }

                // Add more events to log
                for (int idx = 0; idx < 10; idx ++)
                {
                    eventLog = addTestLogEvent(logMgr,
                                               "CalcLog3434");
                    if (TpmLogMgr_calcLogSize(&logMgr) !=
                        TpmLogMgr_getLogSize(&logMgr))
                    {
                        TS_FAIL( "testTpmLogCalcLogSize - IDX (%d) "
                                 "Invalid additional event calc LS(%d) CS(%d)",
                                 idx,
                                 TpmLogMgr_getLogSize(&logMgr),
                                 TpmLogMgr_calcLogSize(&logMgr));
                        break;
                    }
                }
            }
            while (0);
        }

        /**
         * @brief TPM Log initializeUsingExistingLog tests
         */
        void testTpmLogInitExisting ( void )
        {
            TRACFCOMP( g_trac_trustedboot, "testTpmLogInitExisting - Start" );
            errlHndl_t err = NULL;
            TCG_PCR_EVENT2 eventLog;

            do {
                // Initialize logMgr
                TpmLogMgr logMgr;
                getTestLogMgr(&logMgr);
                // Create a logMgr clone
                TpmLogMgr cloneMgr;

                err = TpmLogMgr_initializeUsingExistingLog(&cloneMgr,
                                           TpmLogMgr_getLogStartPtr(&logMgr),
                                           logMgr.logMaxSize);

                // No events beyond initial one
                if (NULL != err ||
                    TpmLogMgr_getLogSize(&logMgr) !=
                    TpmLogMgr_getLogSize(&cloneMgr))
                {
                    TS_FAIL( "testTpmLogInitExisting - "
                             "test fail on initial init "
                             "err(%d) LMS(%d) CLS(%d)",
                             (NULL == err) ? 0 : 1,
                             TpmLogMgr_getLogSize(&logMgr),
                             TpmLogMgr_getLogSize(&cloneMgr));
                    break;
                }


                // Add more events to log
                for (int idx = 0; idx < 10; idx ++)
                {
                    eventLog = addTestLogEvent(logMgr,
                                               "CalcLog3434");
                }

                err = TpmLogMgr_initializeUsingExistingLog(&cloneMgr,
                                           TpmLogMgr_getLogStartPtr(&logMgr),
                                           logMgr.logMaxSize);

                if (NULL != err ||
                    TpmLogMgr_getLogSize(&logMgr) !=
                    TpmLogMgr_getLogSize(&cloneMgr))
                {
                    TS_FAIL( "testTpmLogInitExisting - "
                             "test fail on multiple log init "
                             "err(%d) LMS(%d) CLS(%d)",
                             (NULL == err) ? 0 : 1,
                             TpmLogMgr_getLogSize(&logMgr),
                             TpmLogMgr_getLogSize(&cloneMgr));
                    break;
                }

                // Now try adding an event to the clone logMgr
                eventLog = addTestLogEvent(cloneMgr, "Clone123 23434");
                if (TpmLogMgr_getLogSize(&logMgr) ==
                    TpmLogMgr_getLogSize(&cloneMgr))
                {
                    TS_FAIL( "testTpmLogInitExisting - "
                             "test fail on addlog to clone"
                             "LMS(%d) CLS(%d)",
                             TpmLogMgr_getLogSize(&logMgr),
                             TpmLogMgr_getLogSize(&cloneMgr));
                    break;
                }
            }
            while (0);
            TRACFCOMP( g_trac_trustedboot, "testTpmLogInitExisting - End" );
        }

        /**
         * @brief TPM Log initializeUsingExistingLog failing tests
         */
        void testTpmLogInitExistingFails ( void )
        {
            TRACFCOMP( g_trac_trustedboot,
                       "testTpmLogInitExistingFails - Start" );
            errlHndl_t err = NULL;

            do {
                // Initialize logMgr
                TpmLogMgr logMgr;
                getTestLogMgr(&logMgr);
                // Create a logMgr clone
                TpmLogMgr cloneMgr;
                uint8_t logBuffer[256];

                // Zero'd buffer without initial header entry
                memset(logBuffer,0, sizeof(logBuffer));
                err = TpmLogMgr_initializeUsingExistingLog(&cloneMgr,
                                                           logBuffer,
                                                           sizeof(logBuffer));

                if (NULL == err)
                {
                    TS_FAIL( "testTpmLogInitExistingFails - "
                             "test fail on initial init "
                             "err(%d) LMS(%d) CLS(%d)",
                             (NULL == err) ? 0 : 1,
                             TpmLogMgr_getLogSize(&logMgr),
                             TpmLogMgr_getLogSize(&cloneMgr));
                    break;
                }
                else
                {
                    delete err;
                    err = NULL;
                }


                // Buffer too small
                err = TpmLogMgr_initializeUsingExistingLog(&cloneMgr,
                                           TpmLogMgr_getLogStartPtr(&logMgr),
                                           10);
                if (NULL == err)
                {
                    TS_FAIL( "testTpmLogInitExistingFails - "
                             "test fail on initial init buff too small"
                             "err(%d) LMS(%d) CLS(%d)",
                             (NULL == err) ? 0 : 1,
                             TpmLogMgr_getLogSize(&logMgr),
                             TpmLogMgr_getLogSize(&cloneMgr));
                    break;
                }
                else
                {
                    delete err;
                    err = NULL;
                }
            }
            while (0);
            TRACFCOMP( g_trac_trustedboot,
                       "testTpmLogInitExistingFails - End" );
        }

};

#endif
OpenPOWER on IntegriCloud