• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stefanberger / libtpms / #2063

11 Feb 2026 02:58PM UTC coverage: 77.18% (-0.02%) from 77.195%
#2063

push

travis-ci

web-flow
Merge c175b6f1e into c2a8109f8

1174 of 1370 new or added lines in 95 files covered. (85.69%)

2190 existing lines in 89 files now uncovered.

36347 of 47094 relevant lines covered (77.18%)

125164.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.5
/src/tpm2/Volatile.c
1
// SPDX-License-Identifier: BSD-2-Clause
2

3
// (c) Copyright IBM Corporation 2017,2018.
4

5
#if defined __FreeBSD__ || defined __DragonFly__
6
# include <sys/endian.h>
7
#elif defined __APPLE__
8
# include <libkern/OSByteOrder.h>
9
#else
10
# include <endian.h>
11
#endif
12
#include <string.h>
13

14
#include "config.h"
15

16
#include "assert.h"
17
#include "Marshal.h"
18
#include "Volatile.h"
19
#include "RuntimeAlgorithm_fp.h"
20

21
#define TPM_HAVE_TPM2_DECLARATIONS
22
#include "tpm_library_intern.h"
23

24
TPM_RC
25
VolatileState_Load(BYTE **buffer, INT32 *size)
8,014✔
26
{
27
    TPM_RC rc = TPM_RC_SUCCESS, irc;
8,014✔
28
    BYTE hash[SHA1_DIGEST_SIZE], acthash[SHA1_DIGEST_SIZE];
8,014✔
29
    unsigned int stateFormatLevel = 0; // ignored
8,014✔
30
    UINT16 hashAlg = TPM_ALG_SHA1;
8,014✔
31
    char *oldProfile = NULL;
8,014✔
32

33
    if (rc == TPM_RC_SUCCESS) {
8,014✔
34
        if ((UINT32)*size < sizeof(hash))
8,014✔
35
            return TPM_RC_INSUFFICIENT;
36

37
        rc = RuntimeAlgorithmSwitchProfile(&g_RuntimeProfile.RuntimeAlgorithm,
8,014✔
38
                                           NULL, ~0, &oldProfile);
39
        if (rc != TPM_RC_SUCCESS)
8,014✔
40
            return rc;
41
    }
42

43
    if (rc == TPM_RC_SUCCESS) {
8,014✔
44

45
        CryptHashBlock(hashAlg, *size - sizeof(hash), *buffer,
8,014✔
46
                       sizeof(acthash), acthash);
47
        rc = VolatileState_Unmarshal(buffer, size);
8,014✔
48
        /* specific error has already been reported */
49
    }
50

51
    if (rc == TPM_RC_SUCCESS) {
8,014✔
52
        /*
53
         * advance pointer towards hash if we have a later version of
54
         * the state that has extra data we didn't read
55
         */
56
        if (*size > 0 && (UINT32)*size > sizeof(hash)) {
8,014✔
UNCOV
57
            *buffer += *size - sizeof(hash);
×
UNCOV
58
            *size = sizeof(hash);
×
59
        }
60
        rc = Array_Unmarshal(hash, sizeof(hash), buffer, size);
8,014✔
61
        if (rc != TPM_RC_SUCCESS)
8,014✔
UNCOV
62
            TPMLIB_LogTPM2Error("Error unmarshalling volatile state hash: "
×
63
                                "0x%02x\n", rc);
64
    }
65

66
    if (rc == TPM_RC_SUCCESS) {
8,014✔
67
        if (memcmp(acthash, hash, sizeof(hash))) {
8,014✔
UNCOV
68
            rc = TPM_RC_HASH;
×
UNCOV
69
            TPMLIB_LogTPM2Error("Volatile state checksum error: 0x%02x\n",
×
70
                                rc);
71
        }
72
    }
73

74
    irc = RuntimeAlgorithmSetProfile(&g_RuntimeProfile.RuntimeAlgorithm, oldProfile,
8,014✔
75
                                     &stateFormatLevel, ~0);
76
    free(oldProfile);
8,014✔
77
    if (irc != TPM_RC_SUCCESS && rc == TPM_RC_SUCCESS)
8,014✔
UNCOV
78
        rc = irc;
×
79

80
    if (rc != TPM_RC_SUCCESS)
8,014✔
NEW
81
        _plat__SetInFailureMode(TRUE);
×
82

83
    return rc;
84
}
85

86
UINT16
87
VolatileState_Save(BYTE **buffer, INT32 *size)
4,018✔
88
{
89
    UINT16 written;
4,018✔
90
    const BYTE *start;
4,018✔
91
    BYTE hash[SHA1_DIGEST_SIZE];
4,018✔
92
    TPM_ALG_ID hashAlg = TPM_ALG_SHA1;
4,018✔
93

94
    start = *buffer;
4,018✔
95
    written = VolatileState_Marshal(buffer, size, &g_RuntimeProfile);
4,018✔
96

97
    /* append the checksum */
98
    CryptHashBlock(hashAlg, written, start, sizeof(hash), hash);
4,018✔
99
    written += Array_Marshal(hash, sizeof(hash), buffer, size);
4,018✔
100

101
    return written;
4,018✔
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc