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

stefanberger / libtpms / #2047

02 Jan 2026 05:02PM UTC coverage: 77.211% (-0.01%) from 77.225%
#2047

push

travis-ci

web-flow
Merge af9de2478 into 03ff2481e

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

523 existing lines in 42 files now uncovered.

36060 of 46703 relevant lines covered (77.21%)

101760.53 hits per line

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

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

42
#define _GNU_SOURCE
43
#include <assert.h>
44
#include <string.h>
45

46
#include "Tpm.h"
47
#include "NVMarshal.h"
48
#include "GpMacros.h"
49
#include "tpm_library_intern.h"
50

51
#define ALGO_SEPARATOR_C ','
52
#define ALGO_SEPARATOR_STR ","
53

54
struct KeySizes {
55
    BOOL enabled;
56
    UINT16 size;
57
    unsigned int stateFormatLevel; /* required stateFormatLevel to support this */
58
};
59

60
struct MinKeySize {
61
    unsigned int stateFormatLevel; /* required stateFormatLevel to support this */
62
};
63

64
static const struct KeySizes s_KeySizesAES[] = {
65
    { .enabled = AES_128, .size = 128, .stateFormatLevel = 1 },
66
    { .enabled = AES_192, .size = 192, .stateFormatLevel = 4 },
67
    { .enabled = AES_256, .size = 256, .stateFormatLevel = 1 },
68
    { .enabled = false  , .size = 0  , .stateFormatLevel = 0 },
69
};
70
static const struct KeySizes s_KeySizesSM4[] = {
71
    { .enabled = SM4_128, .size = 128, .stateFormatLevel = 0 }, // not supported
72
    { .enabled = false  , .size = 0  , .stateFormatLevel = 0 },
73
};
74
static const struct KeySizes s_KeySizesCamellia[] = {
75
    { .enabled = CAMELLIA_128, .size = 128, .stateFormatLevel = 1 },
76
    { .enabled = CAMELLIA_192, .size = 192, .stateFormatLevel = 4 },
77
    { .enabled = CAMELLIA_256, .size = 256, .stateFormatLevel = 1 },
78
    { .enabled = false       , .size = 0  , .stateFormatLevel = 0 },
79
};
80
static const struct KeySizes s_KeySizesTDES[] = {
81
    { .enabled = TDES_128, .size = 128, .stateFormatLevel = 1 },
82
    { .enabled = TDES_192, .size = 192, .stateFormatLevel = 1 },
83
    { .enabled = false   , .size = 0  , .stateFormatLevel = 0 },
84
};
85
static const struct KeySizes s_KeySizesRSA[] = {
86
    { .enabled = RSA_1024, .size = 1024, .stateFormatLevel = 1 },
87
    { .enabled = RSA_2048, .size = 2048, .stateFormatLevel = 1 },
88
    { .enabled = RSA_3072, .size = 3072, .stateFormatLevel = 1 },
89
    { .enabled = false   , .size = 0   , .stateFormatLevel = 0 },
90
};
91
static const struct KeySizes s_KeySizesECC[] = {
92
    { .enabled = ECC_NIST_P192, .size = 192, .stateFormatLevel = 1 },
93
    { .enabled = ECC_NIST_P224, .size = 224, .stateFormatLevel = 1 },
94
    { .enabled = ECC_NIST_P256, .size = 256, .stateFormatLevel = 1 },
95
    { .enabled = ECC_BN_P256  , .size = 256, .stateFormatLevel = 1 },
96
    { .enabled = ECC_SM2_P256 , .size = 256, .stateFormatLevel = 1 },
97
    { .enabled = ECC_NIST_P384, .size = 384, .stateFormatLevel = 1 },
98
    { .enabled = ECC_NIST_P521, .size = 521, .stateFormatLevel = 1 },
99
    { .enabled = ECC_BN_P638  , .size = 638, .stateFormatLevel = 1 },
100
    { .enabled = false        , .size = 0  , .stateFormatLevel = 0 },
101
};
102
static const struct MinKeySize s_MinKeySizeHMAC[] = {
103
    { .stateFormatLevel = 7 },
104
};
105

106
static const struct {
107
    const char   *name;
108
    struct {
109
        const struct KeySizes *keySizes;
110
        const struct MinKeySize *minKeySize;
111
    } u;
112
    BOOL          canBeDisabled;
113
    unsigned int  stateFormatLevel; /* required stateFormatLevel to support this */
114
} s_AlgorithmProperties[NUM_ENTRIES_ALGORITHM_PROPERTIES] = {
115
#define SYMMETRIC(ENABLED, NAME, KEYSIZES, CANDISABLE, SFL) \
116
    { .name = ENABLED ? NAME : NULL, .u.keySizes = KEYSIZES, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
117
#define ASYMMETRIC(ENABLED, NAME, KEYSIZES, CANDISABLE, SFL) \
118
    { .name = ENABLED ? NAME : NULL, .u.keySizes = KEYSIZES, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
119
#define HASH(ENABLED, NAME, CANDISABLE, SFL) \
120
    { .name = ENABLED ? NAME : NULL, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
121
#define HMAC(ENABLED, NAME, MINKEYSIZE, CANDISABLE, SFL) \
122
    { .name = ENABLED ? NAME : NULL, .u.minKeySize = MINKEYSIZE, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
123
#define SIGNING(ENABLED, NAME, CANDISABLE, SFL) \
124
    { .name = ENABLED ? NAME : NULL, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
125
#define ENCRYPTING(ENABLED, NAME, CANDISABLE, SFL) \
126
    { .name = ENABLED ? NAME : NULL, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
127
#define OTHER(ENABLED, NAME, CANDISABLE, SFL) \
128
    { .name = ENABLED ? NAME : NULL, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
129

130
    [TPM_ALG_RSA] = ASYMMETRIC(ALG_RSA, "rsa", s_KeySizesRSA, false, 1),
131
    [TPM_ALG_TDES] = SYMMETRIC(ALG_TDES, "tdes", s_KeySizesTDES, true, 1),
132
    [TPM_ALG_SHA1] = HASH(ALG_SHA1, "sha1", true, 1),
133
    [TPM_ALG_HMAC] = HMAC(ALG_HMAC, "hmac", s_MinKeySizeHMAC, false, 1),
134
    [TPM_ALG_AES] = SYMMETRIC(ALG_AES, "aes", s_KeySizesAES, false, 1), // never disable: context encryption
135
    [TPM_ALG_MGF1] = HASH(ALG_MGF1, "mgf1", false, 1),
136
    [TPM_ALG_KEYEDHASH] = HASH(ALG_KEYEDHASH, "keyedhash", false, 1),
137
    [TPM_ALG_XOR] = OTHER(ALG_XOR, "xor", false, 1),
138
    [TPM_ALG_SHA256] = HASH(ALG_SHA256, "sha256", false, 1),
139
    [TPM_ALG_SHA384] = HASH(ALG_SHA384, "sha384", false, 1),
140
    [TPM_ALG_SHA512] = HASH(ALG_SHA512, "sha512", true, 1),
141
    [TPM_ALG_NULL] = OTHER(true, "null", false, 1),
142
    [TPM_ALG_SM4] = SYMMETRIC(ALG_SM4, "sm4", s_KeySizesSM4, true, 0), // not supported
143
    [TPM_ALG_RSASSA] = SIGNING(ALG_RSASSA, "rsassa", true, 1),
144
    [TPM_ALG_RSAES] = ENCRYPTING(ALG_RSAES, "rsaes", true, 1),
145
    [TPM_ALG_RSAPSS] = SIGNING(ALG_RSAPSS, "rsapss", true, 1),
146
    [TPM_ALG_OAEP] = ENCRYPTING(ALG_OAEP, "oaep", false, 1), // never disable: CryptSecretEncrypt/Decrypt needs it
147
    [TPM_ALG_ECDSA] = SIGNING(ALG_ECDSA, "ecdsa", false, 1),
148
    [TPM_ALG_ECDH] = OTHER(ALG_ECDH, "ecdh", false, 1),
149
    [TPM_ALG_ECDAA] = OTHER(ALG_ECDAA, "ecdaa", true, 1),
150
    [TPM_ALG_SM2] = SIGNING(ALG_SM2, "sm2", true, 1),
151
    [TPM_ALG_ECSCHNORR] = SIGNING(ALG_ECSCHNORR, "ecschnorr", true, 1),
152
    [TPM_ALG_ECMQV] = OTHER(ALG_ECMQV, "ecmqv", true, 1),
153
    [TPM_ALG_KDF1_SP800_56A] = HASH(ALG_KDF1_SP800_56A, "kdf1-sp800-56a", false, 1),
154
    [TPM_ALG_KDF2] = HASH(ALG_KDF2, "kdf2", false, 1),
155
    [TPM_ALG_KDF1_SP800_108] = HASH(ALG_KDF1_SP800_108, "kdf1-sp800-108", false, 1),
156
    [TPM_ALG_ECC] = ASYMMETRIC(ALG_ECC, "ecc", s_KeySizesECC, false, 1),
157
    [TPM_ALG_SYMCIPHER] = OTHER(ALG_SYMCIPHER, "symcipher", false, 1),
158
    [TPM_ALG_CAMELLIA] = SYMMETRIC(ALG_CAMELLIA, "camellia", s_KeySizesCamellia, true, 1),
159
    [TPM_ALG_SHA3_256] = HASH(ALG_SHA3_256, "sha3-256", true, 0), // not supported
160
    [TPM_ALG_SHA3_384] = HASH(ALG_SHA3_384, "sha3-384", true, 0), // not supported
161
    [TPM_ALG_SHA3_512] = HASH(ALG_SHA3_512, "sha3-256", true, 0), // not supported
162
    [TPM_ALG_CMAC] = SIGNING(ALG_CMAC, "cmac", true, 1),
163
    [TPM_ALG_CTR] = ENCRYPTING(ALG_CTR, "ctr", true, 1),
164
    [TPM_ALG_OFB] = ENCRYPTING(ALG_OFB, "ofb", true, 1),
165
    [TPM_ALG_CBC] = ENCRYPTING(ALG_CBC, "cbc", true, 1),
166
    [TPM_ALG_CFB] = ENCRYPTING(ALG_CFB, "cfb", false, 1), // never disable: context entryption
167
    [TPM_ALG_ECB] = ENCRYPTING(ALG_ECB, "ecb", true, 1),
168
    /* all newly added algorithms must have .canBedisable=true so they can be disabled */
169
};
170

171
static const struct {
172
    const char   *name;
173
    BOOL          canBeDisabled;
174
    const char   *prefix;
175
} s_EccShortcuts[] = {
176
#define ECC_SHORTCUT(NAME, CANDISABLE, PREFIX) \
177
    { .name = NAME, .canBeDisabled = CANDISABLE, .prefix = PREFIX }
178
    [RUNTIME_ALGORITHM_ECC_NIST_BIT] = ECC_SHORTCUT("ecc-nist", true, "ecc-nist-p"),
179
    [RUNTIME_ALGORITHM_ECC_BN_BIT] = ECC_SHORTCUT("ecc-bn", true, "ecc-bn-p"),
180
};
181

182
static const struct {
183
    const char   *name;
184
    UINT16        keySize;
185
    BOOL          canBeDisabled;
186
    unsigned int  stateFormatLevel; /* required stateFormatLevel to support this */
187
} s_EccAlgorithmProperties[] = {
188
#define ECC(ENABLED, NAME, KEYSIZE, CANDISABLE, SFL) \
189
    { .name = ENABLED ? NAME : NULL, .keySize = KEYSIZE, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
190

191
    [TPM_ECC_NIST_P192] = ECC(ECC_NIST_P192, "ecc-nist-p192", 192, true, 1),
192
    [TPM_ECC_NIST_P224] = ECC(ECC_NIST_P224, "ecc-nist-p224", 224, true, 1),
193
    [TPM_ECC_NIST_P256] = ECC(ECC_NIST_P256, "ecc-nist-p256", 256, false, 1),
194
    [TPM_ECC_NIST_P384] = ECC(ECC_NIST_P384, "ecc-nist-p384", 384, false, 1),
195
    [TPM_ECC_NIST_P521] = ECC(ECC_NIST_P521, "ecc-nist-p521", 521, true, 1),
196
    [TPM_ECC_BN_P256] = ECC(ECC_BN_P256, "ecc-bn-p256", 256, true, 1),
197
    [TPM_ECC_BN_P638] = ECC(ECC_BN_P638, "ecc-bn-p638", 638, true, 1),
198
    [TPM_ECC_SM2_P256] = ECC(ECC_SM2_P256, "ecc-sm2-p256", 256, true, 1),
199
};
200

201
static const TPM_ALG_ID algsWithKeySizes[] = {
202
    TPM_ALG_RSA,
203
    TPM_ALG_TDES,
204
    TPM_ALG_AES,
205
    TPM_ALG_SM4,
206
    TPM_ALG_CAMELLIA,
207
};
208

209
static unsigned int
210
KeySizesGetMinimum(const struct KeySizes *ks)
211
{
212
    size_t i = 0;
213

214
    while (ks[i].size) {
175,642✔
215
        if (ks[i].enabled)
147,180✔
216
            return ks[i].size;
118,718✔
217
        i++;
28,462✔
218
    }
219
    return 0;
220
}
221

222
static void
223
RuntimeAlgorithmEnableAllAlgorithms(struct RuntimeAlgorithm *RuntimeAlgorithm)
24,316✔
224
{
225
    TPM_ECC_CURVE curveId;
24,316✔
226
    TPM_ALG_ID algId;
24,316✔
227

228
    MemorySet(RuntimeAlgorithm->enabledAlgorithms, 0 , sizeof(RuntimeAlgorithm->enabledAlgorithms));
24,316✔
229

230
    for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
1,726,436✔
231
        /* skip over unsupported algorithms */
232
        if (!s_AlgorithmProperties[algId].name)
1,677,804✔
233
            continue;
851,060✔
234
        SET_BIT(algId, RuntimeAlgorithm->enabledAlgorithms);
826,744✔
235
    }
236

237
    MemorySet(RuntimeAlgorithm->enabledEccCurves, 0 , sizeof(RuntimeAlgorithm->enabledEccCurves));
24,316✔
238

239
    for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
851,060✔
240
        if (!s_EccAlgorithmProperties[curveId].name)
802,428✔
241
            continue;
607,900✔
242
        SET_BIT(curveId, RuntimeAlgorithm->enabledEccCurves);
194,528✔
243
    }
244
}
24,316✔
245

246
LIB_EXPORT void
247
RuntimeAlgorithmInit(struct RuntimeAlgorithm *RuntimeAlgorithm)
28,462✔
248
{
249
    TPM_ALG_ID algId;
28,462✔
250
    size_t i;
28,462✔
251

252
    MemorySet(RuntimeAlgorithm->algosMinimumKeySizes, 0 , sizeof(RuntimeAlgorithm->algosMinimumKeySizes));
28,462✔
253

254
    for (i = 0; i < ARRAY_SIZE(algsWithKeySizes); i++) {
199,234✔
255
        algId = algsWithKeySizes[i];
142,310✔
256
        assert(algId < ARRAY_SIZE(RuntimeAlgorithm->algosMinimumKeySizes));
142,310✔
257
        assert(s_AlgorithmProperties[algId].u.keySizes != NULL);
142,310✔
258
        RuntimeAlgorithm->algosMinimumKeySizes[algId] = KeySizesGetMinimum(s_AlgorithmProperties[algId].u.keySizes);
142,310✔
259
    }
260
}
28,462✔
261

262
LIB_EXPORT void
263
RuntimeAlgorithmFree(struct RuntimeAlgorithm *RuntimeAlgorithm)
33,077✔
264
{
265
    free(RuntimeAlgorithm->algorithmProfile);
33,077✔
266
    RuntimeAlgorithm->algorithmProfile = NULL;
33,077✔
267
}
33,077✔
268

269
/* Set the default profile with all algorithms and all keysizes enabled */
270
static void
271
RuntimeAlgorithmSetDefault(struct RuntimeAlgorithm *RuntimeAlgorithm)
24,316✔
272
{
273
    RuntimeAlgorithmFree(RuntimeAlgorithm);
24,316✔
274
    RuntimeAlgorithmInit(RuntimeAlgorithm);
24,316✔
275
    RuntimeAlgorithmEnableAllAlgorithms(RuntimeAlgorithm);
24,316✔
276
}
24,316✔
277

278
/* Set the given profile and runtime-enable the given algorithms. A NULL pointer
279
 * for the profile parameter sets the default profile which enables all algorithms
280
 * and all key sizes without any restrictions.
281
 *
282
 * This function will adjust the stateFormatLevel to the number required for the
283
 * given algorithms and key sizes.
284
 */
285
LIB_EXPORT TPM_RC
286
RuntimeAlgorithmSetProfile(struct RuntimeAlgorithm  *RuntimeAlgorithm,
69,048✔
287
                           const char                    *newProfile,                // IN: colon-separated list of algorithm names
288
                           unsigned int             *stateFormatLevel,                // IN/OUT: stateFormatLevel
289
                           unsigned int                     maxStateFormatLevel        // IN: maximum allowed stateFormatLevel
290
                           )
291
{
292
    size_t toklen, cmplen, i, prefix_len, idx;
69,048✔
293
    const char *token, *comma, *prefix;
69,048✔
294
    const struct KeySizes *keysizes;
69,048✔
295
    TPM_RC retVal = TPM_RC_SUCCESS;
69,048✔
296
    unsigned long minKeySize;
69,048✔
297
    TPM_ECC_CURVE curveId;
69,048✔
298
    TPM_ALG_ID algId;
69,048✔
299
    char *endptr;
69,048✔
300
    bool found;
69,048✔
301

302
    /* NULL pointer for profile enables all */
303
    if (!newProfile) {
69,048✔
304
        RuntimeAlgorithmSetDefault(RuntimeAlgorithm);
24,316✔
305
        return TPM_RC_SUCCESS;
24,316✔
306
    }
307

308
    MemorySet(RuntimeAlgorithm->enabledAlgorithms, 0, sizeof(RuntimeAlgorithm->enabledAlgorithms));
44,732✔
309
    MemorySet(RuntimeAlgorithm->enabledEccCurves, 0 , sizeof(RuntimeAlgorithm->enabledEccCurves));
44,732✔
310
    MemorySet(RuntimeAlgorithm->enabledEccShortcuts, 0, sizeof(RuntimeAlgorithm->enabledEccShortcuts));
44,732✔
311

312
    token = newProfile;
44,732✔
313
    while (1) {
3,711,032✔
314
        comma = strchr(token, ALGO_SEPARATOR_C);
3,711,032✔
315
        if (comma)
1,877,882✔
316
            toklen = (size_t)(comma - token);
1,833,150✔
317
        else
318
            toklen = strlen(token);
44,732✔
319

320
        found = false;
1,877,882✔
321
        for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
57,035,754✔
322
            /* skip over unsupported algorithms */
323
            if (!s_AlgorithmProperties[algId].name)
56,901,032✔
324
                continue;
22,953,788✔
325
            cmplen = MAX(strlen(s_AlgorithmProperties[algId].name), toklen);
33,947,244✔
326
            if (!strncmp(token, s_AlgorithmProperties[algId].name, cmplen)) {
33,947,244✔
327
                if (s_AlgorithmProperties[algId].stateFormatLevel > maxStateFormatLevel) {
1,519,424✔
UNCOV
328
                    TPMLIB_LogTPM2Error("Requested algorithm %.*s requires StateFormatLevel %u but maximum allowed is %u.\n",
×
329
                                        (int)toklen, token,
330
                                        s_AlgorithmProperties[algId].stateFormatLevel,
331
                                        maxStateFormatLevel);
UNCOV
332
                    retVal = TPM_RC_VALUE;
×
333
                    goto exit;
×
334
                }
335
                SET_BIT(algId, RuntimeAlgorithm->enabledAlgorithms);
1,519,424✔
336
                assert(s_AlgorithmProperties[algId].stateFormatLevel > 0);
1,519,424✔
337
                *stateFormatLevel = MAX(*stateFormatLevel,
1,519,424✔
338
                                        s_AlgorithmProperties[algId].stateFormatLevel);
339
                found = true;
1,519,424✔
340
                break;
1,519,424✔
341
            } else if (s_AlgorithmProperties[algId].u.minKeySize) {
32,427,820✔
342
                size_t namelen = strlen(s_AlgorithmProperties[algId].name);
1,609,856✔
343
                if (strncmp(token,
1,609,856✔
344
                            s_AlgorithmProperties[algId].name, /* i.e., 'hmac' */
345
                            namelen) ||
152✔
346
                    strncmp(&token[namelen], "-min-key-size=", 14))
152✔
347
                    continue;
1,609,704✔
348
                minKeySize = strtoul(&token[namelen + 14], &endptr, 10);
152✔
349
                if ((*endptr != ALGO_SEPARATOR_C && *endptr != '\0')
152✔
350
                    || minKeySize > MAX_SYM_DATA * 8) {
152✔
UNCOV
351
                    retVal = TPM_RC_KEY_SIZE;
×
352
                    goto exit;
×
353
                }
354
                RuntimeAlgorithm->algosMinimumKeySizes[algId] = (UINT16)minKeySize;
152✔
355
                *stateFormatLevel = MAX(*stateFormatLevel,
152✔
356
                                        s_AlgorithmProperties[algId].u.minKeySize->stateFormatLevel);
357
                found = true;
152✔
358
                break;
152✔
359
            } else if (s_AlgorithmProperties[algId].u.keySizes) {
30,817,964✔
360
                size_t algnamelen = strlen(s_AlgorithmProperties[algId].name);
6,171,528✔
361
                if (strncmp(token, s_AlgorithmProperties[algId].name, algnamelen) ||
6,171,528✔
362
                    strncmp(&token[algnamelen], "-min-size=", 10))
492,110✔
363
                    continue;
5,947,944✔
364
                minKeySize = strtoul(&token[algnamelen + 10], &endptr, 10);
223,584✔
365
                if ((*endptr != ALGO_SEPARATOR_C && *endptr != '\0') ||  minKeySize > 4096) {
223,584✔
UNCOV
366
                    retVal = TPM_RC_KEY_SIZE;
×
367
                    goto exit;
×
368
                }
369

370
                /* determine stateFormatLevel needed; skip those key sizes that exceed max. stateFormatLevel */
371
                keysizes = s_AlgorithmProperties[algId].u.keySizes;
372
                for (i = 0; keysizes[i].size != 0; i++) {
1,073,258✔
373
                    if (keysizes[i].enabled &&
849,674✔
374
                        keysizes[i].size >= minKeySize &&
849,674✔
375
                        keysizes[i].stateFormatLevel <= maxStateFormatLevel) {
849,624✔
376
                        assert(keysizes[i].stateFormatLevel > 0);
777,038✔
377
                        *stateFormatLevel = MAX(*stateFormatLevel,
777,038✔
378
                                                keysizes[i].stateFormatLevel);
379
                    }
380
                }
381

382
                RuntimeAlgorithm->algosMinimumKeySizes[algId] = (UINT16)minKeySize;
223,584✔
383
                found = true;
223,584✔
384
                break;
223,584✔
385
            }
386
        }
387

388
        if (!found) {
1,743,160✔
389
            bool match_one = true;
270,226✔
390

391
            /* handling of ECC curves: shortcuts */
392
            for (idx = 0; idx < ARRAY_SIZE(s_EccShortcuts); idx++) {
270,226✔
393
                cmplen = MAX(strlen(s_EccShortcuts[idx].name), toklen);
224,796✔
394
                if (!strncmp(token, s_EccShortcuts[idx].name, cmplen)) {
224,796✔
395
                    SET_BIT(idx, RuntimeAlgorithm->enabledEccShortcuts);
89,292✔
396
                    match_one = false;
89,292✔
397
                    prefix = s_EccShortcuts[idx].prefix;
89,292✔
398
                    prefix_len = strlen(prefix);
89,292✔
399
                    break;
89,292✔
400
                }
401
            }
402
            if (match_one) {
134,722✔
403
                prefix = token;
45,430✔
404
                prefix_len = toklen;
45,430✔
405
            }
406
            for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
4,580,548✔
407
                if (!s_EccAlgorithmProperties[curveId].name)
4,445,826✔
408
                    continue;
3,368,050✔
409

410
                if (match_one)
1,077,776✔
411
                    cmplen = MAX(strlen(s_EccAlgorithmProperties[curveId].name), toklen);
363,440✔
412
                else
413
                    cmplen = prefix_len;
414

415
                if (!strncmp(prefix, s_EccAlgorithmProperties[curveId].name, cmplen)) {
1,077,776✔
416
                    if (s_EccAlgorithmProperties[curveId].stateFormatLevel > maxStateFormatLevel) {
357,958✔
417
                        /* specific match that is not allowed causes error, otherwise skip */
UNCOV
418
                        if (match_one) {
×
419
                            TPMLIB_LogTPM2Error("Requested curve %s requires StateFormatLevel %u but maximum allowed is %u.\n",
×
420
                                                s_EccAlgorithmProperties[curveId].name,
421
                                                s_EccAlgorithmProperties[curveId].stateFormatLevel,
422
                                                maxStateFormatLevel);
UNCOV
423
                            retVal = TPM_RC_VALUE;
×
424
                            goto exit;
×
425
                        }
UNCOV
426
                        continue;
×
427
                    }
428
                    *stateFormatLevel = MAX(*stateFormatLevel,
357,958✔
429
                                            s_EccAlgorithmProperties[curveId].stateFormatLevel);
430
                    SET_BIT(curveId, RuntimeAlgorithm->enabledEccCurves);
357,958✔
431
                    found = true;
357,958✔
432
                }
433
            }
434
        }
435

436
        if (!found) {
1,877,882✔
UNCOV
437
            TPMLIB_LogTPM2Error("Requested algorithm specifier %.*s is not supported.\n",
×
438
                                (int)toklen, token);
UNCOV
439
            retVal = TPM_RC_VALUE;
×
440
            goto exit;
×
441
        }
442

443
        if (!comma)
1,877,882✔
444
            break;
445
        token = &comma[1];
1,833,150✔
446
    }
447

448
    /* reconcile with what can be disabled per code instrumentation */
449
    for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
3,131,240✔
450
        /* skip over unsupported algorithms */
451
        if (!s_AlgorithmProperties[algId].name)
3,086,508✔
452
            continue;
1,565,620✔
453
        if (!s_AlgorithmProperties[algId].canBeDisabled &&
2,326,064✔
454
            !TEST_BIT(algId, RuntimeAlgorithm->enabledAlgorithms)) {
805,176✔
UNCOV
455
            TPMLIB_LogTPM2Error("Algorithm %s must be enabled.\n",
×
456
                                s_AlgorithmProperties[algId].name);
UNCOV
457
            retVal = TPM_RC_VALUE;
×
458
            goto exit;
×
459
        }
460
    }
461
    for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
1,520,888✔
462
        if (!s_EccAlgorithmProperties[curveId].name)
1,476,156✔
463
            continue;
1,118,300✔
464
        if (!s_EccAlgorithmProperties[curveId].canBeDisabled &&
447,320✔
465
            !TEST_BIT(curveId, RuntimeAlgorithm->enabledEccCurves)) {
89,464✔
UNCOV
466
            TPMLIB_LogTPM2Error("Elliptic curve %s must be enabled.\n",
×
467
                                s_EccAlgorithmProperties[curveId].name);
UNCOV
468
            retVal = TPM_RC_VALUE;
×
469
            goto exit;
×
470
        }
471
        /* disable curves that can be disabled and not meet min. keysize */
472
        if (RuntimeAlgorithm->algosMinimumKeySizes[TPM_ALG_ECC] >
357,856✔
473
               s_EccAlgorithmProperties[curveId].keySize &&
357,856✔
474
            s_EccAlgorithmProperties[curveId].canBeDisabled)
475
            CLEAR_BIT(curveId, RuntimeAlgorithm->enabledEccCurves);
24✔
476
    }
477

478
    /* some consistency checks */
479
    /* Do not allow aes-min-size > 128 while RSA=2048 otherwise standard EK certs cannot be created anymore */
480
    if (RuntimeAlgorithm->algosMinimumKeySizes[TPM_ALG_AES] > 128 &&
44,732✔
UNCOV
481
        RuntimeAlgorithm->algosMinimumKeySizes[TPM_ALG_RSA] == 2048) {
×
482
        TPMLIB_LogTPM2Error("AES minimum key size must be 128 when "
×
483
                            "2048 bit %s keys are used.\n",
484
                            "RSA");
UNCOV
485
        retVal = TPM_RC_KEY_SIZE;
×
486
        goto exit;
×
487
    }
488

489
    free(RuntimeAlgorithm->algorithmProfile);
44,732✔
490
    RuntimeAlgorithm->algorithmProfile = strdup(newProfile);
44,732✔
491
    if (!RuntimeAlgorithm->algorithmProfile)
44,732✔
492
        retVal = TPM_RC_MEMORY;
493

494
exit:
44,732✔
UNCOV
495
    if (retVal != TPM_RC_SUCCESS)
×
496
        RuntimeAlgorithmSetDefault(RuntimeAlgorithm);
×
497

498
    return retVal;
499
}
500

501
LIB_EXPORT TPM_RC
502
RuntimeAlgorithmSwitchProfile(struct RuntimeAlgorithm  *RuntimeAlgorithm,
8,045✔
503
                              const char               *newProfile,
504
                              unsigned int              maxStateFormatLevel,
505
                              char                    **oldProfile)
506
{
507
    TPM_RC retVal;
8,045✔
508
    unsigned int stateFormatLevel = 0; // ignored
8,045✔
509

510
    *oldProfile = RuntimeAlgorithm->algorithmProfile;
8,045✔
511
    RuntimeAlgorithm->algorithmProfile = NULL;
8,045✔
512

513
    retVal = RuntimeAlgorithmSetProfile(RuntimeAlgorithm, newProfile,
8,045✔
514
                                        &stateFormatLevel, maxStateFormatLevel);
515
    if (retVal != TPM_RC_SUCCESS) {
8,045✔
UNCOV
516
        RuntimeAlgorithmSetProfile(RuntimeAlgorithm, *oldProfile,
×
517
                                   &stateFormatLevel, maxStateFormatLevel);
UNCOV
518
        *oldProfile = NULL;
×
519
    }
520
    return retVal;
8,045✔
521
}
522

523
/* Check whether the given algorithm is runtime-enabled */
524
LIB_EXPORT BOOL
525
RuntimeAlgorithmCheckEnabled(struct RuntimeAlgorithm *RuntimeAlgorithm,
641,220✔
526
                             TPM_ALG_ID                      algId      // IN: the algorithm to check
527
                             )
528
{
529
    if ((algId >> 3) >= sizeof(RuntimeAlgorithm->enabledAlgorithms) ||
1,282,440✔
530
        !TestBit(algId, RuntimeAlgorithm->enabledAlgorithms,
641,220✔
531
                 sizeof(RuntimeAlgorithm->enabledAlgorithms)))
532
        return FALSE;
17,342✔
533
    return TRUE;
534
}
535

536
/* Check whether the given symmetric or asymmetric crypto algorithm is enabled
537
 * for the given keysize. The maxStateFormatLevel prevents certain key sizes
538
 * from being usable if these were only enabled after the algorithm was enabled.
539
 *
540
 * Example: Algorithm 'x' was enabled but keysize 192 was not enabled at this
541
 * point. The required stateFormatLevel for 'x' is 1. To use keysize 192
542
 * stateFormatLevel '4' is required but due to the profile's stateFormatLevel '1'
543
 * it needs to be filtered-out so that the profile doesn't need an upgrade to
544
 * stateFormatLevel '4'.
545
 */
546
LIB_EXPORT BOOL
547
RuntimeAlgorithmKeySizeCheckEnabled(struct RuntimeAlgorithm *RuntimeAlgorithm,
5,263✔
548
                                    TPM_ALG_ID               algId,                        // IN: the algorithm to check
549
                                    UINT16                   keySizeInBits,                // IN: size of the key in bits
550
                                    TPM_ECC_CURVE            curveId,                        // IN: curve Id if algId == TPM_ALG_ECC
551
                                    unsigned int             maxStateFormatLevel        // IN: maximum stateFormatLevel
552
                                    )
553
{
554
    const struct KeySizes *keysizes;
5,263✔
555
    UINT16 minKeySize;
5,263✔
556
    size_t i;
5,263✔
557

558
    if (!RuntimeAlgorithmCheckEnabled(RuntimeAlgorithm, algId))
5,263✔
559
        return FALSE;
560

561
    minKeySize = RuntimeAlgorithm->algosMinimumKeySizes[algId];
5,263✔
562
    if (minKeySize > keySizeInBits)
5,263✔
563
        return FALSE;
564

565
    if (s_AlgorithmProperties[algId].u.minKeySize)
5,259✔
566
        return TRUE;
567

568
    if (algId == TPM_ALG_ECC) {
5,191✔
569
        if ((curveId >> 3) >= sizeof(RuntimeAlgorithm->enabledEccCurves) ||
1,760✔
570
            !TestBit(curveId, RuntimeAlgorithm->enabledEccCurves,
880✔
571
                     sizeof(RuntimeAlgorithm->enabledEccCurves))) {
572
            return FALSE;
3✔
573
        }
574
    }
575

576
    keysizes = s_AlgorithmProperties[algId].u.keySizes;
5,188✔
577
    for (i = 0; keysizes[i].size != 0; i++) {
10,762✔
578
        if (keysizes[i].size == keySizeInBits) {
10,762✔
579
            if (keysizes[i].enabled &&
5,188✔
580
                keysizes[i].stateFormatLevel > maxStateFormatLevel) {
5,188✔
581
                return FALSE;
582
            }
583
            return TRUE;
5,068✔
584
        }
585
    }
586

587
    return TRUE;
588
}
589

590
static char *
591
RuntimeAlgorithmGetEcc(struct RuntimeAlgorithm   *RuntimeAlgorithm,
3,896✔
592
                       enum RuntimeAlgorithmType rat,
593
                       char                      *buffer,
594
                       BOOL                      *first)
595
{
596
    TPM_ECC_CURVE curveId;
3,896✔
597
    char *nbuffer = NULL;
3,896✔
598
    size_t idx;
3,896✔
599
    int n;
3,896✔
600

601
    for (idx = 0; idx < ARRAY_SIZE(s_EccShortcuts); idx++) {
11,688✔
602
        switch (rat) {
7,792✔
603
        case RUNTIME_ALGO_IMPLEMENTED:
604
            // no filter;
605
            break;
606
        case RUNTIME_ALGO_CAN_BE_DISABLED:
1,948✔
607
            if (!s_EccShortcuts[idx].canBeDisabled)
1,948✔
UNCOV
608
                continue;
×
609
            break;
610
        case RUNTIME_ALGO_ENABLED:
1,948✔
611
            if (!TEST_BIT(idx, RuntimeAlgorithm->enabledEccShortcuts))
1,948✔
612
                continue;
557✔
613
            break;
614
        case RUNTIME_ALGO_DISABLED:
1,948✔
615
            if (TEST_BIT(idx, RuntimeAlgorithm->enabledEccShortcuts))
1,948✔
616
                continue;
1,391✔
617
            break;
618
        default:
619
            break;
620
        }
621
        n = asprintf(&nbuffer, "%s%s%s",
11,688✔
622
                     buffer,
623
                     *first ? "" : ALGO_SEPARATOR_STR,
5,844✔
624
                     s_EccShortcuts[idx].name);
5,844✔
625
        free(buffer);
5,844✔
626
        if (n < 0)
5,844✔
627
            return NULL;
628
        buffer = nbuffer;
5,844✔
629
        *first = false;
5,844✔
630
    }
631

632
    for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
132,464✔
633
        if (!s_EccAlgorithmProperties[curveId].name)
128,568✔
634
            continue;
97,400✔
635

636
        switch (rat) {
31,168✔
637
        case RUNTIME_ALGO_IMPLEMENTED:
638
            // no filter
639
            break;
640
        case RUNTIME_ALGO_CAN_BE_DISABLED:
7,792✔
641
            if (!s_EccAlgorithmProperties[curveId].canBeDisabled)
7,792✔
642
               continue;
1,948✔
643
            break;
644
        case RUNTIME_ALGO_ENABLED:
7,792✔
645
            if (!TEST_BIT(curveId, RuntimeAlgorithm->enabledEccCurves))
7,792✔
646
                continue;
1,978✔
647
            break;
648
        case RUNTIME_ALGO_DISABLED:
7,792✔
649
            if (TEST_BIT(curveId, RuntimeAlgorithm->enabledEccCurves))
7,792✔
650
                continue;
5,814✔
651
            break;
652
        default:
653
            break;
654
        }
655
        n = asprintf(&nbuffer, "%s%s%s",
42,856✔
656
                     buffer,
657
                     *first ? "" : ALGO_SEPARATOR_STR,
21,428✔
658
                     s_EccAlgorithmProperties[curveId].name);
659
        free(buffer);
21,428✔
660
        if (n < 0)
21,428✔
661
            return NULL;
662
        buffer = nbuffer;
21,428✔
663
        *first = FALSE;
21,428✔
664
    }
665

666
    return buffer;
667
}
668

669
LIB_EXPORT char *
670
RuntimeAlgorithmPrint(struct RuntimeAlgorithm   *RuntimeAlgorithm,
3,896✔
671
                      enum RuntimeAlgorithmType rat)
672
{
673
    char *buffer, *nbuffer = NULL;
3,896✔
674
    unsigned int minKeySize;
3,896✔
675
    TPM_ALG_ID algId;
3,896✔
676
    int n;
3,896✔
677
    BOOL first = true;
3,896✔
678

679
    buffer = strdup("\"");
3,896✔
680
    if (!buffer)
3,896✔
681
        return NULL;
682

683
    for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
272,720✔
684
        // skip over unsupported algorithms
685
        if (!s_AlgorithmProperties[algId].name)
268,824✔
686
            continue;
136,360✔
687
        switch (rat) {
132,464✔
688
        case RUNTIME_ALGO_IMPLEMENTED:
689
            // no filter
690
            break;
691
        case RUNTIME_ALGO_CAN_BE_DISABLED:
33,116✔
692
            if (!s_AlgorithmProperties[algId].canBeDisabled)
33,116✔
693
                 goto skip; // TPM_ALG_ECC: need to print more
17,532✔
694
            break;
695
        case RUNTIME_ALGO_ENABLED:
33,116✔
696
            // skip over disabled ones
697
            if (!RuntimeAlgorithmCheckEnabled(RuntimeAlgorithm, algId))
33,116✔
698
                goto skip;
8,253✔
699
            break;
700
        case RUNTIME_ALGO_DISABLED:
33,116✔
701
            // skip over enabled ones
702
            if (RuntimeAlgorithmCheckEnabled(RuntimeAlgorithm, algId))
33,116✔
703
                goto skip;
24,863✔
704
            break;
UNCOV
705
        default:
×
706
            continue;
×
707
        }
708
        n = asprintf(&nbuffer, "%s%s%s",
163,632✔
709
                     buffer,
710
                     first ? "" : ALGO_SEPARATOR_STR,
81,816✔
711
                     s_AlgorithmProperties[algId].name);
712
        free(buffer);
81,816✔
713
        if (n < 0)
81,816✔
714
             return NULL;
715

716
        buffer = nbuffer;
81,816✔
717
        first = false;
81,816✔
718

719
        minKeySize = 0;
81,816✔
720

721
        switch (rat) {
81,816✔
722
        case RUNTIME_ALGO_IMPLEMENTED:
33,116✔
723
            if (s_AlgorithmProperties[algId].u.keySizes) {
33,116✔
724
                minKeySize = KeySizesGetMinimum(s_AlgorithmProperties[algId].u.keySizes);
9,284✔
725
            } else if (s_AlgorithmProperties[algId].u.minKeySize) {
28,246✔
726
                /* for it to appear as 'Implemented' */
727
                minKeySize = 1;
728
            }
729
            break;
730
        case RUNTIME_ALGO_ENABLED:
24,863✔
731
            if (s_AlgorithmProperties[algId].u.keySizes ||
24,863✔
732
                s_AlgorithmProperties[algId].u.minKeySize) {
21,214✔
733
                minKeySize = RuntimeAlgorithm->algosMinimumKeySizes[algId];
4,414✔
734
            }
735
            break;
736
        default:
737
            break;
738
        }
739
        if (minKeySize > 0) {
9,284✔
740
            const char *key = "";
9,613✔
741
            if (s_AlgorithmProperties[algId].u.minKeySize)
9,613✔
742
                key = "key-";
1,098✔
743

744
            n = asprintf(&nbuffer, "%s%s%s-min-%ssize=%u",
19,226✔
745
                         buffer,
746
                         ALGO_SEPARATOR_STR,
747
                         s_AlgorithmProperties[algId].name,
9,613✔
748
                         key,
749
                         minKeySize);
750
            free(buffer);
9,613✔
751
            if (n < 0)
9,613✔
752
                return NULL;
753

754
            buffer = nbuffer;
9,613✔
755
        }
756

757
skip:
645✔
758
        if (algId == TPM_ALG_ECC)
132,464✔
759
            buffer = RuntimeAlgorithmGetEcc(RuntimeAlgorithm, rat, buffer, &first);
3,896✔
760
    }
761

762
    n = asprintf(&nbuffer, "%s\"", buffer);
3,896✔
763
    free(buffer);
3,896✔
764
    if (n < 0)
3,896✔
765
        return NULL;
766

767
    return nbuffer;
3,896✔
768
}
769

770
LIB_EXPORT void
771
RuntimeAlgorithmsFilterPCRSelection(TPML_PCR_SELECTION *pcrSelection // IN/OUT: PCRSelection to filter
130✔
772
                                    )
773
{
774
    UINT32 i = 0;
130✔
775

776
    while (i < pcrSelection->count) {
650✔
777
        if (!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,
520✔
778
                                          pcrSelection->pcrSelections[i].hash)) {
520✔
779
            pcrSelection->count--;
53✔
780
            if (pcrSelection->count - 1 > i) {
53✔
781
                MemoryCopy(&pcrSelection->pcrSelections[i],
28✔
782
                           &pcrSelection->pcrSelections[i + 1],
28✔
783
                           sizeof(pcrSelection->pcrSelections[0]) * (pcrSelection->count - i));
28✔
784
            }
785
        } else {
786
            i++;
467✔
787
        }
788
    }
789
}
130✔
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