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

stefanberger / libtpms / #2103

23 Mar 2026 09:40PM UTC coverage: 77.261% (+0.04%) from 77.222%
#2103

push

travis-ci

web-flow
Merge e6fa0a3fd into de20bf477

1 of 1 new or added line in 1 file covered. (100.0%)

1218 existing lines in 27 files now uncovered.

36434 of 47157 relevant lines covered (77.26%)

125481.14 hits per line

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

95.92
/src/tpm2/BackwardsCompatibilityBitArray.c
1
// SPDX-License-Identifier: BSD-2-Clause
2

3
// (c) Copyright IBM Corporation 2023.
4

5
#include <assert.h>
6

7
#include "BackwardsCompatibilityBitArray.h"
8

9
/* The following array contains exactly the commands that libtpms v0.9 had enabled
10
 * when 'compressed lists' were used. Do not change this array anymore!
11
 * A bit in the PERSISTEN_DATA.auditCommands array corresponds to the index in
12
 * this array where the command code can be found.
13
 */
14
static const struct
15
{
16
    TPM_CC cc;
17

18
#define ENTRY(CC, INDEX) [INDEX] = {.cc = CC}
19

20
} CCToCompressedListIndex[] = {
21
    ENTRY(TPM_CC_NV_UndefineSpaceSpecial, 0),
22
    ENTRY(TPM_CC_EvictControl, 1),
23
    ENTRY(TPM_CC_HierarchyControl, 2),
24
    ENTRY(TPM_CC_NV_UndefineSpace, 3),
25
    ENTRY(TPM_CC_ChangeEPS, 4),
26
    ENTRY(TPM_CC_ChangePPS, 5),
27
    ENTRY(TPM_CC_Clear, 6),
28
    ENTRY(TPM_CC_ClearControl, 7),
29
    ENTRY(TPM_CC_ClockSet, 8),
30
    ENTRY(TPM_CC_HierarchyChangeAuth, 9),
31
    ENTRY(TPM_CC_NV_DefineSpace, 10),
32
    ENTRY(TPM_CC_PCR_Allocate, 11),
33
    ENTRY(TPM_CC_PCR_SetAuthPolicy, 12),
34
    ENTRY(TPM_CC_PP_Commands, 13),
35
    ENTRY(TPM_CC_SetPrimaryPolicy, 14),
36
    /* CC_FieldUpdateStart */
37
    ENTRY(TPM_CC_ClockRateAdjust, 15),
38
    ENTRY(TPM_CC_CreatePrimary, 16),
39
    ENTRY(TPM_CC_NV_GlobalWriteLock, 17),
40
    ENTRY(TPM_CC_GetCommandAuditDigest, 18),
41
    ENTRY(TPM_CC_NV_Increment, 19),
42
    ENTRY(TPM_CC_NV_SetBits, 20),
43
    ENTRY(TPM_CC_NV_Extend, 21),
44
    ENTRY(TPM_CC_NV_Write, 22),
45
    ENTRY(TPM_CC_NV_WriteLock, 23),
46
    ENTRY(TPM_CC_DictionaryAttackLockReset, 24),
47
    ENTRY(TPM_CC_DictionaryAttackParameters, 25),
48
    ENTRY(TPM_CC_NV_ChangeAuth, 26),
49
    ENTRY(TPM_CC_PCR_Event, 27),
50
    ENTRY(TPM_CC_PCR_Reset, 28),
51
    ENTRY(TPM_CC_SequenceComplete, 29),
52
    ENTRY(TPM_CC_SetAlgorithmSet, 30),
53
    ENTRY(TPM_CC_SetCommandCodeAuditStatus, 31),
54
    /* CC_FieldUpgradeData */
55
    ENTRY(TPM_CC_IncrementalSelfTest, 32),
56
    ENTRY(TPM_CC_SelfTest, 33),
57
    ENTRY(TPM_CC_Startup, 34),
58
    ENTRY(TPM_CC_Shutdown, 35),
59
    ENTRY(TPM_CC_StirRandom, 36),
60
    ENTRY(TPM_CC_ActivateCredential, 37),
61
    ENTRY(TPM_CC_Certify, 38),
62
    ENTRY(TPM_CC_PolicyNV, 39),
63
    ENTRY(TPM_CC_CertifyCreation, 40),
64
    ENTRY(TPM_CC_Duplicate, 41),
65
    ENTRY(TPM_CC_GetTime, 42),
66
    ENTRY(TPM_CC_GetSessionAuditDigest, 43),
67
    ENTRY(TPM_CC_NV_Read, 44),
68
    ENTRY(TPM_CC_NV_ReadLock, 45),
69
    ENTRY(TPM_CC_ObjectChangeAuth, 46),
70
    ENTRY(TPM_CC_PolicySecret, 47),
71
    ENTRY(TPM_CC_Rewrap, 48),
72
    ENTRY(TPM_CC_Create, 49),
73
    ENTRY(TPM_CC_ECDH_ZGen, 50),
74
    ENTRY(TPM_CC_HMAC, 51),
75
    ENTRY(TPM_CC_Import, 52),
76
    ENTRY(TPM_CC_Load, 53),
77
    ENTRY(TPM_CC_Quote, 54),
78
    ENTRY(TPM_CC_RSA_Decrypt, 55),
79
    ENTRY(TPM_CC_HMAC_Start, 56),
80
    ENTRY(TPM_CC_SequenceUpdate, 57),
81
    ENTRY(TPM_CC_Sign, 58),
82
    ENTRY(TPM_CC_Unseal, 59),
83
    ENTRY(TPM_CC_PolicySigned, 60),
84
    ENTRY(TPM_CC_ContextLoad, 61),
85
    ENTRY(TPM_CC_ContextSave, 62),
86
    ENTRY(TPM_CC_ECDH_KeyGen, 63),
87
    ENTRY(TPM_CC_EncryptDecrypt, 64),
88
    ENTRY(TPM_CC_FlushContext, 65),
89
    ENTRY(TPM_CC_LoadExternal, 66),
90
    ENTRY(TPM_CC_MakeCredential, 67),
91
    ENTRY(TPM_CC_NV_ReadPublic, 68),
92
    ENTRY(TPM_CC_PolicyAuthorize, 69),
93
    ENTRY(TPM_CC_PolicyAuthValue, 70),
94
    ENTRY(TPM_CC_PolicyCommandCode, 71),
95
    ENTRY(TPM_CC_PolicyCounterTimer, 72),
96
    ENTRY(TPM_CC_PolicyCpHash, 73),
97
    ENTRY(TPM_CC_PolicyLocality, 74),
98
    ENTRY(TPM_CC_PolicyNameHash, 75),
99
    ENTRY(TPM_CC_PolicyOR, 76),
100
    ENTRY(TPM_CC_PolicyTicket, 77),
101
    ENTRY(TPM_CC_ReadPublic, 78),
102
    ENTRY(TPM_CC_RSA_Encrypt, 79),
103
    ENTRY(TPM_CC_StartAuthSession, 80),
104
    ENTRY(TPM_CC_VerifySignature, 81),
105
    ENTRY(TPM_CC_ECC_Parameters, 82),
106
    /* CC_FirmwareRead */
107
    ENTRY(TPM_CC_GetCapability, 83),
108
    ENTRY(TPM_CC_GetRandom, 84),
109
    ENTRY(TPM_CC_GetTestResult, 85),
110
    ENTRY(TPM_CC_Hash, 86),
111
    ENTRY(TPM_CC_PCR_Read, 87),
112
    ENTRY(TPM_CC_PolicyPCR, 88),
113
    ENTRY(TPM_CC_PolicyRestart, 89),
114
    ENTRY(TPM_CC_ReadClock, 90),
115
    ENTRY(TPM_CC_PCR_Extend, 91),
116
    ENTRY(TPM_CC_PCR_SetAuthValue, 92),
117
    ENTRY(TPM_CC_NV_Certify, 93),
118
    ENTRY(TPM_CC_EventSequenceComplete, 94),
119
    ENTRY(TPM_CC_HashSequenceStart, 95),
120
    ENTRY(TPM_CC_PolicyPhysicalPresence, 96),
121
    ENTRY(TPM_CC_PolicyDuplicationSelect, 97),
122
    ENTRY(TPM_CC_PolicyGetDigest, 98),
123
    ENTRY(TPM_CC_TestParms, 99),
124
    ENTRY(TPM_CC_Commit, 100),
125
    ENTRY(TPM_CC_PolicyPassword, 101),
126
    ENTRY(TPM_CC_ZGen_2Phase, 102),
127
    ENTRY(TPM_CC_EC_Ephemeral, 103),
128
    ENTRY(TPM_CC_PolicyNvWritten, 104),
129
    ENTRY(TPM_CC_PolicyTemplate, 105),
130
    ENTRY(TPM_CC_CreateLoaded, 106),
131
    ENTRY(TPM_CC_PolicyAuthorizeNV, 107),
132
    ENTRY(TPM_CC_EncryptDecrypt2, 108),
133
    /* CC_AC_GetCapability -- never enable here */
134
    /* CC_AC_Send -- never enable here */
135
    /* CC_Policy_AC_SendSelect */
136
    ENTRY(TPM_CC_CertifyX509, 109),
137
    /* CC_ACT_SetTimeout -- never enable here */
138
    /* CC_ECC_Encrypt -- never enable here */
139
    /* CC_ECC_Decrypt -- never enable here */
140
    /* never add new commands */
141
};
142

143
/* Convert from a bit array from the time when COMPRESSED_LIST was YES
144
 * to an array where the indices do NOT correspond to a COMPRESSED_LIST.
145
 */
146
TPM_RC
147
ConvertFromCompressedBitArray(BYTE*  inAuditCommands,
32,212✔
148
                              size_t inAuditCommandsLen,
149
                              BYTE*  outAuditCommands,
150
                              size_t outAuditCommandsLen)
151
{
152
    size_t max_bit = MIN(inAuditCommandsLen * 8, ARRAY_SIZE(CCToCompressedListIndex));
32,212✔
153
    size_t bit     = 0;
32,212✔
154

155
    MemorySet(outAuditCommands, 0, outAuditCommandsLen);
32,212✔
156

157
    while(bit < max_bit)
483,180✔
158
    {
159
        BYTE   bits = inAuditCommands[bit >> 3];
450,968✔
160
        BYTE   mask = 1;
450,968✔
161
        size_t lbit = bit;
450,968✔
162

163
        while(bits != 0 && lbit < max_bit)
676,452✔
164
        {
165
            if((bits & mask) != 0)
225,484✔
166
            {
167
                TPM_CC        cc  = CCToCompressedListIndex[lbit].cc;
32,212✔
168
                COMMAND_INDEX idx = cc - TPM_CC_NV_UndefineSpaceSpecial;
32,212✔
169

170
                assert(idx != UNIMPLEMENTED_COMMAND_INDEX);
32,212✔
171

172
                SetBit(idx, outAuditCommands, outAuditCommandsLen);
32,212✔
173
                bits ^= mask; /* unset bit */
32,212✔
174
            }
175
            mask <<= 1;
225,484✔
176
            lbit++;
225,484✔
177
        }
178
        bit += 8;
450,968✔
179
    }
180

181
    return TPM_RC_SUCCESS;
32,212✔
182
}
183

184
static size_t FindCCInCompressedListIndexArray(TPM_CC cc)
35,826✔
185
{
186
    size_t e_index = ARRAY_SIZE(CCToCompressedListIndex) - 1;
35,826✔
187
    size_t s_index = 0;
35,826✔
188

189
    while(true)
161,217✔
190
    {
191
        size_t index = (e_index + s_index) >> 1;
161,217✔
192

193
        if(cc == CCToCompressedListIndex[index].cc)
161,217✔
194
        {
195
            return index;
35,826✔
196
        }
197
        if(e_index == s_index)
125,391✔
198
        {
199
            break;
200
        }
201
        if(cc < CCToCompressedListIndex[index].cc)
125,391✔
202
        {
203
            e_index = index;
204
        }
205
        else
206
        {
207
            if(s_index != index)
35,826✔
208
                s_index = index;
209
            else
UNCOV
210
                s_index++;
×
211
        }
212
    }
213
    /* entry must have been found */
UNCOV
214
    pAssert(false);
×
215
}
216

217
/* Convert to a bit array from the time when COMPRESSED_LIST was YES
218
 * from an array where the indices do NOT correspond to a COMPRESSED_LIST.
219
 */
220
TPM_RC
221
ConvertToCompressedBitArray(BYTE*  inAuditCommands,
35,826✔
222
                            size_t inAuditCommandsLen,
223
                            BYTE*  outAuditCommands,
224
                            size_t outAuditCommandsLen)
225
{
226
    size_t max_idx = inAuditCommandsLen * 8;
35,826✔
227
    size_t idx     = 0;
35,826✔
228

229
    MemorySet(outAuditCommands, 0, outAuditCommandsLen);
35,826✔
230

231
    while(idx < max_idx)
644,868✔
232
    {
233
        BYTE   bits = inAuditCommands[idx >> 3];
609,042✔
234
        BYTE   mask = 1;
609,042✔
235
        size_t lidx = idx;
609,042✔
236

237
        /* handle bits set in one byte in the loop */
238
        while(bits != 0 && lidx < max_idx)
770,259✔
239
        {
240
            if((bits & mask) != 0)
161,217✔
241
            {
242
                TPM_CC cc  = lidx + TPM_CC_NV_UndefineSpaceSpecial;
35,826✔
243
                size_t bit = FindCCInCompressedListIndexArray(cc);
35,826✔
244

245
                SetBit(bit, outAuditCommands, outAuditCommandsLen);
35,826✔
246
                bits ^= mask; /* unset bit */
35,826✔
247
            }
248
            mask <<= 1;
161,217✔
249
            lidx++;
161,217✔
250
        }
251
        idx += 8;
609,042✔
252
    }
253

254
    return TPM_RC_SUCCESS;
35,826✔
255
}
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