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

stefanberger / libtpms / #2065

11 Feb 2026 03:39PM UTC coverage: 77.188%. Remained the same
#2065

push

travis-ci

web-flow
Merge e061e2e5f into c2a8109f8

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

2186 existing lines in 88 files now uncovered.

36351 of 47094 relevant lines covered (77.19%)

126335.76 hits per line

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

89.74
/src/tpm2/RuntimeAlgorithm.c
1
// SPDX-License-Identifier: BSD-2-Clause
2

3
// (c) Copyright IBM Corporation, 2022
4

5
#define _GNU_SOURCE
6
#include <assert.h>
7
#include <string.h>
8

9
#include "Tpm.h"
10
#include "NVMarshal.h"
11
#include "tpm_public/GpMacros.h"
12
#include "tpm_library_intern.h"
13

14
#define ALGO_SEPARATOR_C ','
15
#define ALGO_SEPARATOR_STR ","
16

17
struct KeySizes {
18
    BOOL enabled;
19
    UINT16 size;
20
    unsigned int stateFormatLevel; /* required stateFormatLevel to support this */
21
};
22

23
struct MinKeySize {
24
    unsigned int stateFormatLevel; /* required stateFormatLevel to support this */
25
};
26

27
static const struct KeySizes s_KeySizesAES[] = {
28
    { .enabled = AES_128, .size = 128, .stateFormatLevel = 1 },
29
    { .enabled = AES_192, .size = 192, .stateFormatLevel = 4 },
30
    { .enabled = AES_256, .size = 256, .stateFormatLevel = 1 },
31
    { .enabled = false  , .size = 0  , .stateFormatLevel = 0 },
32
};
33
static const struct KeySizes s_KeySizesSM4[] = {
34
    { .enabled = SM4_128, .size = 128, .stateFormatLevel = 0 }, // not supported
35
    { .enabled = false  , .size = 0  , .stateFormatLevel = 0 },
36
};
37
static const struct KeySizes s_KeySizesCamellia[] = {
38
    { .enabled = CAMELLIA_128, .size = 128, .stateFormatLevel = 1 },
39
    { .enabled = CAMELLIA_192, .size = 192, .stateFormatLevel = 4 },
40
    { .enabled = CAMELLIA_256, .size = 256, .stateFormatLevel = 1 },
41
    { .enabled = false       , .size = 0  , .stateFormatLevel = 0 },
42
};
43
static const struct KeySizes s_KeySizesTDES[] = {
44
    { .enabled = TDES_128, .size = 128, .stateFormatLevel = 1 },
45
    { .enabled = TDES_192, .size = 192, .stateFormatLevel = 1 },
46
    { .enabled = false   , .size = 0  , .stateFormatLevel = 0 },
47
};
48
static const struct KeySizes s_KeySizesRSA[] = {
49
    { .enabled = RSA_1024, .size = 1024, .stateFormatLevel = 1 },
50
    { .enabled = RSA_2048, .size = 2048, .stateFormatLevel = 1 },
51
    { .enabled = RSA_3072, .size = 3072, .stateFormatLevel = 1 },
52
    { .enabled = RSA_4096, .size = 4096, .stateFormatLevel = 8 },
53
    { .enabled = false   , .size = 0   , .stateFormatLevel = 0 },
54
};
55
static const struct KeySizes s_KeySizesECC[] = {
56
    { .enabled = ECC_NIST_P192, .size = 192, .stateFormatLevel = 1 },
57
    { .enabled = ECC_NIST_P224, .size = 224, .stateFormatLevel = 1 },
58
    { .enabled = ECC_NIST_P256, .size = 256, .stateFormatLevel = 1 },
59
    { .enabled = ECC_BN_P256  , .size = 256, .stateFormatLevel = 1 },
60
    { .enabled = ECC_SM2_P256 , .size = 256, .stateFormatLevel = 1 },
61
    { .enabled = ECC_NIST_P384, .size = 384, .stateFormatLevel = 1 },
62
    { .enabled = ECC_NIST_P521, .size = 521, .stateFormatLevel = 1 },
63
    { .enabled = ECC_BN_P638  , .size = 638, .stateFormatLevel = 1 },
64
    { .enabled = false        , .size = 0  , .stateFormatLevel = 0 },
65
};
66
static const struct MinKeySize s_MinKeySizeHMAC[] = {
67
    { .stateFormatLevel = 7 },
68
};
69

70
static const struct {
71
    const char   *name;
72
    struct {
73
        const struct KeySizes *keySizes;
74
        const struct MinKeySize *minKeySize;
75
    } u;
76
    BOOL          canBeDisabled;
77
    unsigned int  stateFormatLevel; /* required stateFormatLevel to support this */
78
} s_AlgorithmProperties[NUM_ENTRIES_ALGORITHM_PROPERTIES] = {
79
#define SYMMETRIC(ENABLED, NAME, KEYSIZES, CANDISABLE, SFL) \
80
    { .name = ENABLED ? NAME : NULL, .u.keySizes = KEYSIZES, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
81
#define ASYMMETRIC(ENABLED, NAME, KEYSIZES, CANDISABLE, SFL) \
82
    { .name = ENABLED ? NAME : NULL, .u.keySizes = KEYSIZES, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
83
#define HASH(ENABLED, NAME, CANDISABLE, SFL) \
84
    { .name = ENABLED ? NAME : NULL, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
85
#define HMAC(ENABLED, NAME, MINKEYSIZE, CANDISABLE, SFL) \
86
    { .name = ENABLED ? NAME : NULL, .u.minKeySize = MINKEYSIZE, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
87
#define SIGNING(ENABLED, NAME, CANDISABLE, SFL) \
88
    { .name = ENABLED ? NAME : NULL, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
89
#define ENCRYPTING(ENABLED, NAME, CANDISABLE, SFL) \
90
    { .name = ENABLED ? NAME : NULL, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
91
#define OTHER(ENABLED, NAME, CANDISABLE, SFL) \
92
    { .name = ENABLED ? NAME : NULL, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
93

94
    [TPM_ALG_RSA] = ASYMMETRIC(ALG_RSA, "rsa", s_KeySizesRSA, false, 1),
95
    [TPM_ALG_TDES] = SYMMETRIC(ALG_TDES, "tdes", s_KeySizesTDES, true, 1),
96
    [TPM_ALG_SHA1] = HASH(ALG_SHA1, "sha1", true, 1),
97
    [TPM_ALG_HMAC] = HMAC(ALG_HMAC, "hmac", s_MinKeySizeHMAC, false, 1),
98
    [TPM_ALG_AES] = SYMMETRIC(ALG_AES, "aes", s_KeySizesAES, false, 1), // never disable: context encryption
99
    [TPM_ALG_MGF1] = HASH(ALG_MGF1, "mgf1", false, 1),
100
    [TPM_ALG_KEYEDHASH] = HASH(ALG_KEYEDHASH, "keyedhash", false, 1),
101
    [TPM_ALG_XOR] = OTHER(ALG_XOR, "xor", false, 1),
102
    [TPM_ALG_SHA256] = HASH(ALG_SHA256, "sha256", false, 1),
103
    [TPM_ALG_SHA384] = HASH(ALG_SHA384, "sha384", false, 1),
104
    [TPM_ALG_SHA512] = HASH(ALG_SHA512, "sha512", true, 1),
105
    [TPM_ALG_NULL] = OTHER(true, "null", false, 1),
106
    [TPM_ALG_SM4] = SYMMETRIC(ALG_SM4, "sm4", s_KeySizesSM4, true, 0), // not supported
107
    [TPM_ALG_RSASSA] = SIGNING(ALG_RSASSA, "rsassa", true, 1),
108
    [TPM_ALG_RSAES] = ENCRYPTING(ALG_RSAES, "rsaes", true, 1),
109
    [TPM_ALG_RSAPSS] = SIGNING(ALG_RSAPSS, "rsapss", true, 1),
110
    [TPM_ALG_OAEP] = ENCRYPTING(ALG_OAEP, "oaep", false, 1), // never disable: CryptSecretEncrypt/Decrypt needs it
111
    [TPM_ALG_ECDSA] = SIGNING(ALG_ECDSA, "ecdsa", false, 1),
112
    [TPM_ALG_ECDH] = OTHER(ALG_ECDH, "ecdh", false, 1),
113
    [TPM_ALG_ECDAA] = OTHER(ALG_ECDAA, "ecdaa", true, 1),
114
    [TPM_ALG_SM2] = SIGNING(ALG_SM2, "sm2", true, 1),
115
    [TPM_ALG_ECSCHNORR] = SIGNING(ALG_ECSCHNORR, "ecschnorr", true, 1),
116
    [TPM_ALG_ECMQV] = OTHER(ALG_ECMQV, "ecmqv", true, 1),
117
    [TPM_ALG_KDF1_SP800_56A] = HASH(ALG_KDF1_SP800_56A, "kdf1-sp800-56a", false, 1),
118
    [TPM_ALG_KDF2] = HASH(ALG_KDF2, "kdf2", false, 1),
119
    [TPM_ALG_KDF1_SP800_108] = HASH(ALG_KDF1_SP800_108, "kdf1-sp800-108", false, 1),
120
    [TPM_ALG_ECC] = ASYMMETRIC(ALG_ECC, "ecc", s_KeySizesECC, false, 1),
121
    [TPM_ALG_SYMCIPHER] = OTHER(ALG_SYMCIPHER, "symcipher", false, 1),
122
    [TPM_ALG_CAMELLIA] = SYMMETRIC(ALG_CAMELLIA, "camellia", s_KeySizesCamellia, true, 1),
123
    [TPM_ALG_SHA3_256] = HASH(ALG_SHA3_256, "sha3-256", true, 0), // not supported
124
    [TPM_ALG_SHA3_384] = HASH(ALG_SHA3_384, "sha3-384", true, 0), // not supported
125
    [TPM_ALG_SHA3_512] = HASH(ALG_SHA3_512, "sha3-256", true, 0), // not supported
126
    [TPM_ALG_CMAC] = SIGNING(ALG_CMAC, "cmac", true, 1),
127
    [TPM_ALG_CTR] = ENCRYPTING(ALG_CTR, "ctr", true, 1),
128
    [TPM_ALG_OFB] = ENCRYPTING(ALG_OFB, "ofb", true, 1),
129
    [TPM_ALG_CBC] = ENCRYPTING(ALG_CBC, "cbc", true, 1),
130
    [TPM_ALG_CFB] = ENCRYPTING(ALG_CFB, "cfb", false, 1), // never disable: context entryption
131
    [TPM_ALG_ECB] = ENCRYPTING(ALG_ECB, "ecb", true, 1),
132
    /* all newly added algorithms must have .canBedisable=true so they can be disabled */
133
};
134

135
static const struct {
136
    const char   *name;
137
    BOOL          canBeDisabled;
138
    const char   *prefix;
139
} s_EccShortcuts[] = {
140
#define ECC_SHORTCUT(NAME, CANDISABLE, PREFIX) \
141
    { .name = NAME, .canBeDisabled = CANDISABLE, .prefix = PREFIX }
142
    [RUNTIME_ALGORITHM_ECC_NIST_BIT] = ECC_SHORTCUT("ecc-nist", true, "ecc-nist-p"),
143
    [RUNTIME_ALGORITHM_ECC_BN_BIT] = ECC_SHORTCUT("ecc-bn", true, "ecc-bn-p"),
144
};
145

146
static const struct {
147
    const char   *name;
148
    UINT16        keySize;
149
    BOOL          canBeDisabled;
150
    unsigned int  stateFormatLevel; /* required stateFormatLevel to support this */
151
} s_EccAlgorithmProperties[] = {
152
#define ECC(ENABLED, NAME, KEYSIZE, CANDISABLE, SFL) \
153
    { .name = ENABLED ? NAME : NULL, .keySize = KEYSIZE, .canBeDisabled = CANDISABLE, .stateFormatLevel = SFL }
154

155
    [TPM_ECC_NIST_P192] = ECC(ECC_NIST_P192, "ecc-nist-p192", 192, true, 1),
156
    [TPM_ECC_NIST_P224] = ECC(ECC_NIST_P224, "ecc-nist-p224", 224, true, 1),
157
    [TPM_ECC_NIST_P256] = ECC(ECC_NIST_P256, "ecc-nist-p256", 256, false, 1),
158
    [TPM_ECC_NIST_P384] = ECC(ECC_NIST_P384, "ecc-nist-p384", 384, false, 1),
159
    [TPM_ECC_NIST_P521] = ECC(ECC_NIST_P521, "ecc-nist-p521", 521, true, 1),
160
    [TPM_ECC_BN_P256] = ECC(ECC_BN_P256, "ecc-bn-p256", 256, true, 1),
161
    [TPM_ECC_BN_P638] = ECC(ECC_BN_P638, "ecc-bn-p638", 638, true, 1),
162
    [TPM_ECC_SM2_P256] = ECC(ECC_SM2_P256, "ecc-sm2-p256", 256, true, 1),
163
};
164

165
static const TPM_ALG_ID algsWithKeySizes[] = {
166
    TPM_ALG_RSA,
167
    TPM_ALG_TDES,
168
    TPM_ALG_AES,
169
    TPM_ALG_SM4,
170
    TPM_ALG_CAMELLIA,
171
};
172

173
static unsigned int
174
KeySizesGetMinimum(const struct KeySizes *ks)
175
{
176
    size_t i = 0;
177

178
    while (ks[i].size) {
176,070✔
179
        if (ks[i].enabled)
147,580✔
180
            return ks[i].size;
119,090✔
181
        i++;
28,490✔
182
    }
183
    return 0;
184
}
185

186
static void
187
RuntimeAlgorithmEnableAllAlgorithms(struct RuntimeAlgorithm *RuntimeAlgorithm)
24,333✔
188
{
189
    TPM_ECC_CURVE curveId;
24,333✔
190
    TPM_ALG_ID algId;
24,333✔
191

192
    MemorySet(RuntimeAlgorithm->enabledAlgorithms, 0 , sizeof(RuntimeAlgorithm->enabledAlgorithms));
24,333✔
193

194
    for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
2,822,628✔
195
        /* skip over unsupported algorithms */
196
        if (!s_AlgorithmProperties[algId].name)
2,773,962✔
197
            continue;
1,946,640✔
198
        SET_BIT(algId, RuntimeAlgorithm->enabledAlgorithms);
827,322✔
199
    }
200

201
    MemorySet(RuntimeAlgorithm->enabledEccCurves, 0 , sizeof(RuntimeAlgorithm->enabledEccCurves));
24,333✔
202

203
    for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
851,655✔
204
        if (!s_EccAlgorithmProperties[curveId].name)
802,989✔
205
            continue;
608,325✔
206
        SET_BIT(curveId, RuntimeAlgorithm->enabledEccCurves);
194,664✔
207
    }
208
}
24,333✔
209

210
LIB_EXPORT void
211
RuntimeAlgorithmInit(struct RuntimeAlgorithm *RuntimeAlgorithm)
28,490✔
212
{
213
    TPM_ALG_ID algId;
28,490✔
214
    size_t i;
28,490✔
215

216
    MemorySet(RuntimeAlgorithm->algosMinimumKeySizes, 0 , sizeof(RuntimeAlgorithm->algosMinimumKeySizes));
28,490✔
217

218
    for (i = 0; i < ARRAY_SIZE(algsWithKeySizes); i++) {
199,430✔
219
        algId = algsWithKeySizes[i];
142,450✔
220
        assert(algId < ARRAY_SIZE(RuntimeAlgorithm->algosMinimumKeySizes));
142,450✔
221
        assert(s_AlgorithmProperties[algId].u.keySizes != NULL);
142,450✔
222
        RuntimeAlgorithm->algosMinimumKeySizes[algId] = KeySizesGetMinimum(s_AlgorithmProperties[algId].u.keySizes);
142,450✔
223
    }
224
}
28,490✔
225

226
LIB_EXPORT void
227
RuntimeAlgorithmFree(struct RuntimeAlgorithm *RuntimeAlgorithm)
33,126✔
228
{
229
    free(RuntimeAlgorithm->algorithmProfile);
33,126✔
230
    RuntimeAlgorithm->algorithmProfile = NULL;
33,126✔
231
}
33,126✔
232

233
/* Set the default profile with all algorithms and all keysizes enabled */
234
static void
235
RuntimeAlgorithmSetDefault(struct RuntimeAlgorithm *RuntimeAlgorithm)
24,333✔
236
{
237
    RuntimeAlgorithmFree(RuntimeAlgorithm);
24,333✔
238
    RuntimeAlgorithmInit(RuntimeAlgorithm);
24,333✔
239
    RuntimeAlgorithmEnableAllAlgorithms(RuntimeAlgorithm);
24,333✔
240
}
24,333✔
241

242
/* Set the given profile and runtime-enable the given algorithms. A NULL pointer
243
 * for the profile parameter sets the default profile which enables all algorithms
244
 * and all key sizes without any restrictions.
245
 *
246
 * This function will adjust the stateFormatLevel to the number required for the
247
 * given algorithms and key sizes.
248
 */
249
LIB_EXPORT TPM_RC
250
RuntimeAlgorithmSetProfile(struct RuntimeAlgorithm  *RuntimeAlgorithm,
69,109✔
251
                           const char                    *newProfile,                // IN: colon-separated list of algorithm names
252
                           unsigned int             *stateFormatLevel,                // IN/OUT: stateFormatLevel
253
                           unsigned int                     maxStateFormatLevel        // IN: maximum allowed stateFormatLevel
254
                           )
255
{
256
    size_t toklen, cmplen, i, prefix_len, idx;
69,109✔
257
    const char *token, *comma, *prefix;
69,109✔
258
    const struct KeySizes *keysizes;
69,109✔
259
    TPM_RC retVal = TPM_RC_SUCCESS;
69,109✔
260
    unsigned long minKeySize;
69,109✔
261
    TPM_ECC_CURVE curveId;
69,109✔
262
    TPM_ALG_ID algId;
69,109✔
263
    char *endptr;
69,109✔
264
    bool found;
69,109✔
265

266
    /* NULL pointer for profile enables all */
267
    if (!newProfile) {
69,109✔
268
        RuntimeAlgorithmSetDefault(RuntimeAlgorithm);
24,333✔
269
        return TPM_RC_SUCCESS;
24,333✔
270
    }
271

272
    MemorySet(RuntimeAlgorithm->enabledAlgorithms, 0, sizeof(RuntimeAlgorithm->enabledAlgorithms));
44,776✔
273
    MemorySet(RuntimeAlgorithm->enabledEccCurves, 0 , sizeof(RuntimeAlgorithm->enabledEccCurves));
44,776✔
274
    MemorySet(RuntimeAlgorithm->enabledEccShortcuts, 0, sizeof(RuntimeAlgorithm->enabledEccShortcuts));
44,776✔
275

276
    token = newProfile;
44,776✔
277
    while (1) {
3,714,684✔
278
        comma = strchr(token, ALGO_SEPARATOR_C);
3,714,684✔
279
        if (comma)
1,879,730✔
280
            toklen = (size_t)(comma - token);
1,834,954✔
281
        else
282
            toklen = strlen(token);
44,776✔
283

284
        found = false;
1,879,730✔
285
        for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
63,160,284✔
286
            /* skip over unsupported algorithms */
287
            if (!s_AlgorithmProperties[algId].name)
63,025,430✔
288
                continue;
29,044,790✔
289
            cmplen = MAX(strlen(s_AlgorithmProperties[algId].name), toklen);
33,980,640✔
290
            if (!strncmp(token, s_AlgorithmProperties[algId].name, cmplen)) {
33,980,640✔
291
                if (s_AlgorithmProperties[algId].stateFormatLevel > maxStateFormatLevel) {
1,520,920✔
UNCOV
292
                    TPMLIB_LogTPM2Error("Requested algorithm %.*s requires StateFormatLevel %u but maximum allowed is %u.\n",
×
293
                                        (int)toklen, token,
294
                                        s_AlgorithmProperties[algId].stateFormatLevel,
295
                                        maxStateFormatLevel);
UNCOV
296
                    retVal = TPM_RC_VALUE;
×
UNCOV
297
                    goto exit;
×
298
                }
299
                SET_BIT(algId, RuntimeAlgorithm->enabledAlgorithms);
1,520,920✔
300
                assert(s_AlgorithmProperties[algId].stateFormatLevel > 0);
1,520,920✔
301
                *stateFormatLevel = MAX(*stateFormatLevel,
1,520,920✔
302
                                        s_AlgorithmProperties[algId].stateFormatLevel);
303
                found = true;
1,520,920✔
304
                break;
1,520,920✔
305
            } else if (s_AlgorithmProperties[algId].u.minKeySize) {
32,459,720✔
306
                size_t namelen = strlen(s_AlgorithmProperties[algId].name);
1,611,440✔
307
                if (strncmp(token,
1,611,440✔
308
                            s_AlgorithmProperties[algId].name, /* i.e., 'hmac' */
309
                            namelen) ||
152✔
310
                    strncmp(&token[namelen], "-min-key-size=", 14))
152✔
311
                    continue;
1,611,288✔
312
                minKeySize = strtoul(&token[namelen + 14], &endptr, 10);
152✔
313
                if ((*endptr != ALGO_SEPARATOR_C && *endptr != '\0')
152✔
314
                    || minKeySize > MAX_SYM_DATA * 8) {
152✔
UNCOV
315
                    retVal = TPM_RC_KEY_SIZE;
×
UNCOV
316
                    goto exit;
×
317
                }
318
                RuntimeAlgorithm->algosMinimumKeySizes[algId] = (UINT16)minKeySize;
152✔
319
                *stateFormatLevel = MAX(*stateFormatLevel,
152✔
320
                                        s_AlgorithmProperties[algId].u.minKeySize->stateFormatLevel);
321
                found = true;
152✔
322
                break;
152✔
323
            } else if (s_AlgorithmProperties[algId].u.keySizes) {
30,848,280✔
324
                size_t algnamelen = strlen(s_AlgorithmProperties[algId].name);
6,177,600✔
325
                if (strncmp(token, s_AlgorithmProperties[algId].name, algnamelen) ||
6,177,600✔
326
                    strncmp(&token[algnamelen], "-min-size=", 10))
492,594✔
327
                    continue;
5,953,796✔
328
                minKeySize = strtoul(&token[algnamelen + 10], &endptr, 10);
223,804✔
329
                if ((*endptr != ALGO_SEPARATOR_C && *endptr != '\0') ||  minKeySize > 4096) {
223,804✔
UNCOV
330
                    retVal = TPM_RC_KEY_SIZE;
×
UNCOV
331
                    goto exit;
×
332
                }
333

334
                /* determine stateFormatLevel needed; skip those key sizes that exceed max. stateFormatLevel */
335
                keysizes = s_AlgorithmProperties[algId].u.keySizes;
336
                for (i = 0; keysizes[i].size != 0; i++) {
1,119,090✔
337
                    if (keysizes[i].enabled &&
895,286✔
338
                        keysizes[i].size >= minKeySize &&
895,286✔
339
                        keysizes[i].stateFormatLevel <= maxStateFormatLevel) {
895,236✔
340
                        assert(keysizes[i].stateFormatLevel > 0);
786,176✔
341
                        *stateFormatLevel = MAX(*stateFormatLevel,
786,176✔
342
                                                keysizes[i].stateFormatLevel);
343
                    }
344
                }
345

346
                RuntimeAlgorithm->algosMinimumKeySizes[algId] = (UINT16)minKeySize;
223,804✔
347
                found = true;
223,804✔
348
                break;
223,804✔
349
            }
350
        }
351

352
        if (!found) {
1,744,876✔
353
            bool match_one = true;
270,490✔
354

355
            /* handling of ECC curves: shortcuts */
356
            for (idx = 0; idx < ARRAY_SIZE(s_EccShortcuts); idx++) {
270,490✔
357
                cmplen = MAX(strlen(s_EccShortcuts[idx].name), toklen);
225,016✔
358
                if (!strncmp(token, s_EccShortcuts[idx].name, cmplen)) {
225,016✔
359
                    SET_BIT(idx, RuntimeAlgorithm->enabledEccShortcuts);
89,380✔
360
                    match_one = false;
89,380✔
361
                    prefix = s_EccShortcuts[idx].prefix;
89,380✔
362
                    prefix_len = strlen(prefix);
89,380✔
363
                    break;
89,380✔
364
                }
365
            }
366
            if (match_one) {
134,854✔
367
                prefix = token;
45,474✔
368
                prefix_len = toklen;
45,474✔
369
            }
370
            for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
4,585,036✔
371
                if (!s_EccAlgorithmProperties[curveId].name)
4,450,182✔
372
                    continue;
3,371,350✔
373

374
                if (match_one)
1,078,832✔
375
                    cmplen = MAX(strlen(s_EccAlgorithmProperties[curveId].name), toklen);
363,792✔
376
                else
377
                    cmplen = prefix_len;
378

379
                if (!strncmp(prefix, s_EccAlgorithmProperties[curveId].name, cmplen)) {
1,078,832✔
380
                    if (s_EccAlgorithmProperties[curveId].stateFormatLevel > maxStateFormatLevel) {
358,310✔
381
                        /* specific match that is not allowed causes error, otherwise skip */
382
                        if (match_one) {
×
UNCOV
383
                            TPMLIB_LogTPM2Error("Requested curve %s requires StateFormatLevel %u but maximum allowed is %u.\n",
×
384
                                                s_EccAlgorithmProperties[curveId].name,
385
                                                s_EccAlgorithmProperties[curveId].stateFormatLevel,
386
                                                maxStateFormatLevel);
UNCOV
387
                            retVal = TPM_RC_VALUE;
×
UNCOV
388
                            goto exit;
×
389
                        }
UNCOV
390
                        continue;
×
391
                    }
392
                    *stateFormatLevel = MAX(*stateFormatLevel,
358,310✔
393
                                            s_EccAlgorithmProperties[curveId].stateFormatLevel);
394
                    SET_BIT(curveId, RuntimeAlgorithm->enabledEccCurves);
358,310✔
395
                    found = true;
358,310✔
396
                }
397
            }
398
        }
399

400
        if (!found) {
1,879,730✔
UNCOV
401
            TPMLIB_LogTPM2Error("Requested algorithm specifier %.*s is not supported.\n",
×
402
                                (int)toklen, token);
UNCOV
403
            retVal = TPM_RC_VALUE;
×
UNCOV
404
            goto exit;
×
405
        }
406

407
        if (!comma)
1,879,730✔
408
            break;
409
        token = &comma[1];
1,834,954✔
410
    }
411

412
    /* reconcile with what can be disabled per code instrumentation */
413
    for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
5,149,240✔
414
        /* skip over unsupported algorithms */
415
        if (!s_AlgorithmProperties[algId].name)
5,104,464✔
416
            continue;
3,582,080✔
417
        if (!s_AlgorithmProperties[algId].canBeDisabled &&
2,328,352✔
418
            !TEST_BIT(algId, RuntimeAlgorithm->enabledAlgorithms)) {
805,968✔
UNCOV
419
            TPMLIB_LogTPM2Error("Algorithm %s must be enabled.\n",
×
420
                                s_AlgorithmProperties[algId].name);
UNCOV
421
            retVal = TPM_RC_VALUE;
×
422
            goto exit;
×
423
        }
424
    }
425
    for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
1,522,384✔
426
        if (!s_EccAlgorithmProperties[curveId].name)
1,477,608✔
427
            continue;
1,119,400✔
428
        if (!s_EccAlgorithmProperties[curveId].canBeDisabled &&
447,760✔
429
            !TEST_BIT(curveId, RuntimeAlgorithm->enabledEccCurves)) {
89,552✔
UNCOV
430
            TPMLIB_LogTPM2Error("Elliptic curve %s must be enabled.\n",
×
431
                                s_EccAlgorithmProperties[curveId].name);
UNCOV
432
            retVal = TPM_RC_VALUE;
×
UNCOV
433
            goto exit;
×
434
        }
435
        /* disable curves that can be disabled and not meet min. keysize */
436
        if (RuntimeAlgorithm->algosMinimumKeySizes[TPM_ALG_ECC] >
358,208✔
437
               s_EccAlgorithmProperties[curveId].keySize &&
358,208✔
438
            s_EccAlgorithmProperties[curveId].canBeDisabled)
439
            CLEAR_BIT(curveId, RuntimeAlgorithm->enabledEccCurves);
24✔
440
    }
441

442
    /* some consistency checks */
443
    /* Do not allow aes-min-size > 128 while RSA=2048 otherwise standard EK certs cannot be created anymore */
444
    if (RuntimeAlgorithm->algosMinimumKeySizes[TPM_ALG_AES] > 128 &&
44,776✔
445
        RuntimeAlgorithm->algosMinimumKeySizes[TPM_ALG_RSA] == 2048) {
×
UNCOV
446
        TPMLIB_LogTPM2Error("AES minimum key size must be 128 when "
×
447
                            "2048 bit %s keys are used.\n",
448
                            "RSA");
UNCOV
449
        retVal = TPM_RC_KEY_SIZE;
×
UNCOV
450
        goto exit;
×
451
    }
452

453
    free(RuntimeAlgorithm->algorithmProfile);
44,776✔
454
    RuntimeAlgorithm->algorithmProfile = strdup(newProfile);
44,776✔
455
    if (!RuntimeAlgorithm->algorithmProfile)
44,776✔
456
        retVal = TPM_RC_MEMORY;
457

458
exit:
44,776✔
UNCOV
459
    if (retVal != TPM_RC_SUCCESS)
×
UNCOV
460
        RuntimeAlgorithmSetDefault(RuntimeAlgorithm);
×
461

462
    return retVal;
463
}
464

465
LIB_EXPORT TPM_RC
466
RuntimeAlgorithmSwitchProfile(struct RuntimeAlgorithm  *RuntimeAlgorithm,
8,046✔
467
                              const char               *newProfile,
468
                              unsigned int              maxStateFormatLevel,
469
                              char                    **oldProfile)
470
{
471
    TPM_RC retVal;
8,046✔
472
    unsigned int stateFormatLevel = 0; // ignored
8,046✔
473

474
    *oldProfile = RuntimeAlgorithm->algorithmProfile;
8,046✔
475
    RuntimeAlgorithm->algorithmProfile = NULL;
8,046✔
476

477
    retVal = RuntimeAlgorithmSetProfile(RuntimeAlgorithm, newProfile,
8,046✔
478
                                        &stateFormatLevel, maxStateFormatLevel);
479
    if (retVal != TPM_RC_SUCCESS) {
8,046✔
UNCOV
480
        RuntimeAlgorithmSetProfile(RuntimeAlgorithm, *oldProfile,
×
481
                                   &stateFormatLevel, maxStateFormatLevel);
UNCOV
482
        *oldProfile = NULL;
×
483
    }
484
    return retVal;
8,046✔
485
}
486

487
/* Check whether the given algorithm is runtime-enabled */
488
LIB_EXPORT BOOL
489
RuntimeAlgorithmCheckEnabled(struct RuntimeAlgorithm *RuntimeAlgorithm,
646,864✔
490
                             TPM_ALG_ID                      algId      // IN: the algorithm to check
491
                             )
492
{
493
    if ((algId >> 3) >= sizeof(RuntimeAlgorithm->enabledAlgorithms) ||
1,293,728✔
494
        !TestBit(algId, RuntimeAlgorithm->enabledAlgorithms,
646,864✔
495
                 sizeof(RuntimeAlgorithm->enabledAlgorithms)))
496
        return FALSE;
18,702✔
497
    return TRUE;
498
}
499

500
/* Check whether the given symmetric or asymmetric crypto algorithm is enabled
501
 * for the given keysize. The maxStateFormatLevel prevents certain key sizes
502
 * from being usable if these were only enabled after the algorithm was enabled.
503
 *
504
 * Example: Algorithm 'x' was enabled but keysize 192 was not enabled at this
505
 * point. The required stateFormatLevel for 'x' is 1. To use keysize 192
506
 * stateFormatLevel '4' is required but due to the profile's stateFormatLevel '1'
507
 * it needs to be filtered-out so that the profile doesn't need an upgrade to
508
 * stateFormatLevel '4'.
509
 */
510
LIB_EXPORT BOOL
511
RuntimeAlgorithmKeySizeCheckEnabled(struct RuntimeAlgorithm *RuntimeAlgorithm,
5,448✔
512
                                    TPM_ALG_ID               algId,                        // IN: the algorithm to check
513
                                    UINT16                   keySizeInBits,                // IN: size of the key in bits
514
                                    TPM_ECC_CURVE            curveId,                        // IN: curve Id if algId == TPM_ALG_ECC
515
                                    unsigned int             maxStateFormatLevel        // IN: maximum stateFormatLevel
516
                                    )
517
{
518
    const struct KeySizes *keysizes;
5,448✔
519
    UINT16 minKeySize;
5,448✔
520
    size_t i;
5,448✔
521

522
    if (!RuntimeAlgorithmCheckEnabled(RuntimeAlgorithm, algId))
5,448✔
523
        return FALSE;
524

525
    minKeySize = RuntimeAlgorithm->algosMinimumKeySizes[algId];
5,448✔
526
    if (minKeySize > keySizeInBits)
5,448✔
527
        return FALSE;
528

529
    if (s_AlgorithmProperties[algId].u.minKeySize)
5,444✔
530
        return TRUE;
531

532
    if (algId == TPM_ALG_ECC) {
5,376✔
533
        if ((curveId >> 3) >= sizeof(RuntimeAlgorithm->enabledEccCurves) ||
1,850✔
534
            !TestBit(curveId, RuntimeAlgorithm->enabledEccCurves,
925✔
535
                     sizeof(RuntimeAlgorithm->enabledEccCurves))) {
536
            return FALSE;
3✔
537
        }
538
    }
539

540
    keysizes = s_AlgorithmProperties[algId].u.keySizes;
5,373✔
541
    for (i = 0; keysizes[i].size != 0; i++) {
11,606✔
542
        if (keysizes[i].size == keySizeInBits) {
11,606✔
543
            if (keysizes[i].enabled &&
5,373✔
544
                keysizes[i].stateFormatLevel > maxStateFormatLevel) {
5,373✔
545
                return FALSE;
546
            }
547
            return TRUE;
5,298✔
548
        }
549
    }
550

551
    return TRUE;
552
}
553

554
static char *
555
RuntimeAlgorithmGetEcc(struct RuntimeAlgorithm   *RuntimeAlgorithm,
4,104✔
556
                       enum RuntimeAlgorithmType rat,
557
                       char                      *buffer,
558
                       BOOL                      *first)
559
{
560
    TPM_ECC_CURVE curveId;
4,104✔
561
    char *nbuffer = NULL;
4,104✔
562
    size_t idx;
4,104✔
563
    int n;
4,104✔
564

565
    for (idx = 0; idx < ARRAY_SIZE(s_EccShortcuts); idx++) {
12,312✔
566
        switch (rat) {
8,208✔
567
        case RUNTIME_ALGO_IMPLEMENTED:
568
            // no filter;
569
            break;
570
        case RUNTIME_ALGO_CAN_BE_DISABLED:
2,052✔
571
            if (!s_EccShortcuts[idx].canBeDisabled)
2,052✔
UNCOV
572
                continue;
×
573
            break;
574
        case RUNTIME_ALGO_ENABLED:
2,052✔
575
            if (!TEST_BIT(idx, RuntimeAlgorithm->enabledEccShortcuts))
2,052✔
576
                continue;
597✔
577
            break;
578
        case RUNTIME_ALGO_DISABLED:
2,052✔
579
            if (TEST_BIT(idx, RuntimeAlgorithm->enabledEccShortcuts))
2,052✔
580
                continue;
1,455✔
581
            break;
582
        default:
583
            break;
584
        }
585
        n = asprintf(&nbuffer, "%s%s%s",
12,312✔
586
                     buffer,
587
                     *first ? "" : ALGO_SEPARATOR_STR,
6,156✔
588
                     s_EccShortcuts[idx].name);
6,156✔
589
        free(buffer);
6,156✔
590
        if (n < 0)
6,156✔
591
            return NULL;
592
        buffer = nbuffer;
6,156✔
593
        *first = false;
6,156✔
594
    }
595

596
    for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
139,536✔
597
        if (!s_EccAlgorithmProperties[curveId].name)
135,432✔
598
            continue;
102,600✔
599

600
        switch (rat) {
32,832✔
601
        case RUNTIME_ALGO_IMPLEMENTED:
602
            // no filter
603
            break;
604
        case RUNTIME_ALGO_CAN_BE_DISABLED:
8,208✔
605
            if (!s_EccAlgorithmProperties[curveId].canBeDisabled)
8,208✔
606
               continue;
2,052✔
607
            break;
608
        case RUNTIME_ALGO_ENABLED:
8,208✔
609
            if (!TEST_BIT(curveId, RuntimeAlgorithm->enabledEccCurves))
8,208✔
610
                continue;
2,138✔
611
            break;
612
        case RUNTIME_ALGO_DISABLED:
8,208✔
613
            if (TEST_BIT(curveId, RuntimeAlgorithm->enabledEccCurves))
8,208✔
614
                continue;
6,070✔
615
            break;
616
        default:
617
            break;
618
        }
619
        n = asprintf(&nbuffer, "%s%s%s",
45,144✔
620
                     buffer,
621
                     *first ? "" : ALGO_SEPARATOR_STR,
22,572✔
622
                     s_EccAlgorithmProperties[curveId].name);
623
        free(buffer);
22,572✔
624
        if (n < 0)
22,572✔
625
            return NULL;
626
        buffer = nbuffer;
22,572✔
627
        *first = FALSE;
22,572✔
628
    }
629

630
    return buffer;
631
}
632

633
LIB_EXPORT char *
634
RuntimeAlgorithmPrint(struct RuntimeAlgorithm   *RuntimeAlgorithm,
4,104✔
635
                      enum RuntimeAlgorithmType rat)
636
{
637
    char *buffer, *nbuffer = NULL;
4,104✔
638
    unsigned int minKeySize;
4,104✔
639
    TPM_ALG_ID algId;
4,104✔
640
    int n;
4,104✔
641
    BOOL first = true;
4,104✔
642

643
    buffer = strdup("\"");
4,104✔
644
    if (!buffer)
4,104✔
645
        return NULL;
646

647
    for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
471,960✔
648
        // skip over unsupported algorithms
649
        if (!s_AlgorithmProperties[algId].name)
467,856✔
650
            continue;
328,320✔
651
        switch (rat) {
139,536✔
652
        case RUNTIME_ALGO_IMPLEMENTED:
653
            // no filter
654
            break;
655
        case RUNTIME_ALGO_CAN_BE_DISABLED:
34,884✔
656
            if (!s_AlgorithmProperties[algId].canBeDisabled)
34,884✔
657
                 goto skip; // TPM_ALG_ECC: need to print more
18,468✔
658
            break;
659
        case RUNTIME_ALGO_ENABLED:
34,884✔
660
            // skip over disabled ones
661
            if (!RuntimeAlgorithmCheckEnabled(RuntimeAlgorithm, algId))
34,884✔
662
                goto skip;
8,933✔
663
            break;
664
        case RUNTIME_ALGO_DISABLED:
34,884✔
665
            // skip over enabled ones
666
            if (RuntimeAlgorithmCheckEnabled(RuntimeAlgorithm, algId))
34,884✔
667
                goto skip;
25,951✔
668
            break;
UNCOV
669
        default:
×
UNCOV
670
            continue;
×
671
        }
672
        n = asprintf(&nbuffer, "%s%s%s",
172,368✔
673
                     buffer,
674
                     first ? "" : ALGO_SEPARATOR_STR,
86,184✔
675
                     s_AlgorithmProperties[algId].name);
676
        free(buffer);
86,184✔
677
        if (n < 0)
86,184✔
678
             return NULL;
679

680
        buffer = nbuffer;
86,184✔
681
        first = false;
86,184✔
682

683
        minKeySize = 0;
86,184✔
684

685
        switch (rat) {
86,184✔
686
        case RUNTIME_ALGO_IMPLEMENTED:
34,884✔
687
            if (s_AlgorithmProperties[algId].u.keySizes) {
34,884✔
688
                minKeySize = KeySizesGetMinimum(s_AlgorithmProperties[algId].u.keySizes);
9,736✔
689
            } else if (s_AlgorithmProperties[algId].u.minKeySize) {
29,754✔
690
                /* for it to appear as 'Implemented' */
691
                minKeySize = 1;
692
            }
693
            break;
694
        case RUNTIME_ALGO_ENABLED:
25,951✔
695
            if (s_AlgorithmProperties[algId].u.keySizes ||
25,951✔
696
                s_AlgorithmProperties[algId].u.minKeySize) {
22,142✔
697
                minKeySize = RuntimeAlgorithm->algosMinimumKeySizes[algId];
4,606✔
698
            }
699
            break;
700
        default:
701
            break;
702
        }
703
        if (minKeySize > 0) {
9,736✔
704
            const char *key = "";
10,085✔
705
            if (s_AlgorithmProperties[algId].u.minKeySize)
10,085✔
706
                key = "key-";
1,150✔
707

708
            n = asprintf(&nbuffer, "%s%s%s-min-%ssize=%u",
20,170✔
709
                         buffer,
710
                         ALGO_SEPARATOR_STR,
711
                         s_AlgorithmProperties[algId].name,
10,085✔
712
                         key,
713
                         minKeySize);
714
            free(buffer);
10,085✔
715
            if (n < 0)
10,085✔
716
                return NULL;
717

718
            buffer = nbuffer;
10,085✔
719
        }
720

721
skip:
677✔
722
        if (algId == TPM_ALG_ECC)
139,536✔
723
            buffer = RuntimeAlgorithmGetEcc(RuntimeAlgorithm, rat, buffer, &first);
4,104✔
724
    }
725

726
    n = asprintf(&nbuffer, "%s\"", buffer);
4,104✔
727
    free(buffer);
4,104✔
728
    if (n < 0)
4,104✔
729
        return NULL;
730

731
    return nbuffer;
4,104✔
732
}
733

734
LIB_EXPORT void
735
RuntimeAlgorithmsFilterPCRSelection(TPML_PCR_SELECTION *pcrSelection // IN/OUT: PCRSelection to filter
140✔
736
                                    )
737
{
738
    UINT32 i = 0;
140✔
739

740
    while (i < pcrSelection->count) {
700✔
741
        if (!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,
560✔
742
                                          pcrSelection->pcrSelections[i].hash)) {
560✔
743
            pcrSelection->count--;
53✔
744
            if (pcrSelection->count - 1 > i) {
53✔
745
                MemoryCopy(&pcrSelection->pcrSelections[i],
28✔
746
                           &pcrSelection->pcrSelections[i + 1],
28✔
747
                           sizeof(pcrSelection->pcrSelections[0]) * (pcrSelection->count - i));
28✔
748
            }
749
        } else {
750
            i++;
507✔
751
        }
752
    }
753
}
140✔
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