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

stefanberger / libtpms / #2056

20 Jan 2026 10:12PM UTC coverage: 77.177% (-0.006%) from 77.183%
#2056

push

travis-ci

web-flow
Merge 26872a5bf into c2a8109f8

1063 of 1263 new or added lines in 88 files covered. (84.16%)

1810 existing lines in 64 files now uncovered.

36345 of 47093 relevant lines covered (77.18%)

125599.29 hits per line

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

82.5
/src/tpm2/Volatile.c
1
/********************************************************************************/
2
/*                                                                                */
3
/*                          Marshalling and unmarshalling of state                */
4
/*                             Written by Stefan Berger                                */
5
/*                       IBM Thomas J. Watson Research Center                        */
6
/*                                                                                */
7
/* (c) Copyright IBM Corporation 2017,2018.                                        */
8
/*                                                                                */
9
/* All rights reserved.                                                                */
10
/*                                                                                 */
11
/* Redistribution and use in source and binary forms, with or without                */
12
/* modification, are permitted provided that the following conditions are        */
13
/* met:                                                                                */
14
/*                                                                                 */
15
/* Redistributions of source code must retain the above copyright notice,        */
16
/* this list of conditions and the following disclaimer.                        */
17
/*                                                                                 */
18
/* Redistributions in binary form must reproduce the above copyright                */
19
/* notice, this list of conditions and the following disclaimer in the                */
20
/* documentation and/or other materials provided with the distribution.                */
21
/*                                                                                 */
22
/* Neither the names of the IBM Corporation nor the names of its                */
23
/* contributors may be used to endorse or promote products derived from                */
24
/* this software without specific prior written permission.                        */
25
/*                                                                                 */
26
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS                */
27
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT                */
28
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR        */
29
/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
30
/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,        */
31
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT                */
32
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,        */
33
/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY        */
34
/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT                */
35
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE        */
36
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
37
/********************************************************************************/
38

39
#if defined __FreeBSD__ || defined __DragonFly__
40
# include <sys/endian.h>
41
#elif defined __APPLE__
42
# include <libkern/OSByteOrder.h>
43
#else
44
# include <endian.h>
45
#endif
46
#include <string.h>
47

48
#include "config.h"
49

50
#include "assert.h"
51
#include "Marshal.h"
52
#include "Volatile.h"
53
#include "RuntimeAlgorithm_fp.h"
54

55
#define TPM_HAVE_TPM2_DECLARATIONS
56
#include "tpm_library_intern.h"
57

58
TPM_RC
59
VolatileState_Load(BYTE **buffer, INT32 *size)
8,014✔
60
{
61
    TPM_RC rc = TPM_RC_SUCCESS, irc;
8,014✔
62
    BYTE hash[SHA1_DIGEST_SIZE], acthash[SHA1_DIGEST_SIZE];
8,014✔
63
    unsigned int stateFormatLevel = 0; // ignored
8,014✔
64
    UINT16 hashAlg = TPM_ALG_SHA1;
8,014✔
65
    char *oldProfile = NULL;
8,014✔
66

67
    if (rc == TPM_RC_SUCCESS) {
8,014✔
68
        if ((UINT32)*size < sizeof(hash))
8,014✔
69
            return TPM_RC_INSUFFICIENT;
70

71
        rc = RuntimeAlgorithmSwitchProfile(&g_RuntimeProfile.RuntimeAlgorithm,
8,014✔
72
                                           NULL, ~0, &oldProfile);
73
        if (rc != TPM_RC_SUCCESS)
8,014✔
74
            return rc;
75
    }
76

77
    if (rc == TPM_RC_SUCCESS) {
8,014✔
78

79
        CryptHashBlock(hashAlg, *size - sizeof(hash), *buffer,
8,014✔
80
                       sizeof(acthash), acthash);
81
        rc = VolatileState_Unmarshal(buffer, size);
8,014✔
82
        /* specific error has already been reported */
83
    }
84

85
    if (rc == TPM_RC_SUCCESS) {
8,014✔
86
        /*
87
         * advance pointer towards hash if we have a later version of
88
         * the state that has extra data we didn't read
89
         */
90
        if (*size > 0 && (UINT32)*size > sizeof(hash)) {
8,014✔
91
            *buffer += *size - sizeof(hash);
×
92
            *size = sizeof(hash);
×
93
        }
94
        rc = Array_Unmarshal(hash, sizeof(hash), buffer, size);
8,014✔
95
        if (rc != TPM_RC_SUCCESS)
8,014✔
96
            TPMLIB_LogTPM2Error("Error unmarshalling volatile state hash: "
×
97
                                "0x%02x\n", rc);
98
    }
99

100
    if (rc == TPM_RC_SUCCESS) {
8,014✔
101
        if (memcmp(acthash, hash, sizeof(hash))) {
8,014✔
102
            rc = TPM_RC_HASH;
×
103
            TPMLIB_LogTPM2Error("Volatile state checksum error: 0x%02x\n",
×
104
                                rc);
105
        }
106
    }
107

108
    irc = RuntimeAlgorithmSetProfile(&g_RuntimeProfile.RuntimeAlgorithm, oldProfile,
8,014✔
109
                                     &stateFormatLevel, ~0);
110
    free(oldProfile);
8,014✔
111
    if (irc != TPM_RC_SUCCESS && rc == TPM_RC_SUCCESS)
8,014✔
112
        rc = irc;
×
113

114
    if (rc != TPM_RC_SUCCESS)
8,014✔
NEW
115
        _plat__SetInFailureMode(TRUE);
×
116

117
    return rc;
118
}
119

120
UINT16
121
VolatileState_Save(BYTE **buffer, INT32 *size)
4,018✔
122
{
123
    UINT16 written;
4,018✔
124
    const BYTE *start;
4,018✔
125
    BYTE hash[SHA1_DIGEST_SIZE];
4,018✔
126
    TPM_ALG_ID hashAlg = TPM_ALG_SHA1;
4,018✔
127

128
    start = *buffer;
4,018✔
129
    written = VolatileState_Marshal(buffer, size, &g_RuntimeProfile);
4,018✔
130

131
    /* append the checksum */
132
    CryptHashBlock(hashAlg, written, start, sizeof(hash), hash);
4,018✔
133
    written += Array_Marshal(hash, sizeof(hash), buffer, size);
4,018✔
134

135
    return written;
4,018✔
136
}
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