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

stefanberger / libtpms / #2043

29 Dec 2025 06:12PM UTC coverage: 77.222% (-0.005%) from 77.227%
#2043

push

travis-ci

web-flow
Merge dad4446a5 into a3917cb92

36065 of 46703 relevant lines covered (77.22%)

101207.87 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,664✔
215
        if (ks[i].enabled)
147,200✔
216
            return ks[i].size;
118,736✔
217
        i++;
28,464✔
218
    }
219
    return 0;
220
}
221

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

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

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

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

239
    for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
851,130✔
240
        if (!s_EccAlgorithmProperties[curveId].name)
802,494✔
241
            continue;
607,950✔
242
        SET_BIT(curveId, RuntimeAlgorithm->enabledEccCurves);
194,544✔
243
    }
244
}
24,318✔
245

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

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

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

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

269
/* Set the default profile with all algorithms and all keysizes enabled */
270
static void
271
RuntimeAlgorithmSetDefault(struct RuntimeAlgorithm *RuntimeAlgorithm)
24,318✔
272
{
273
    RuntimeAlgorithmFree(RuntimeAlgorithm);
24,318✔
274
    RuntimeAlgorithmInit(RuntimeAlgorithm);
24,318✔
275
    RuntimeAlgorithmEnableAllAlgorithms(RuntimeAlgorithm);
24,318✔
276
}
24,318✔
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,053✔
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,053✔
293
    const char *token, *comma, *prefix;
69,053✔
294
    const struct KeySizes *keysizes;
69,053✔
295
    TPM_RC retVal = TPM_RC_SUCCESS;
69,053✔
296
    unsigned long minKeySize;
69,053✔
297
    TPM_ECC_CURVE curveId;
69,053✔
298
    TPM_ALG_ID algId;
69,053✔
299
    char *endptr;
69,053✔
300
    bool found;
69,053✔
301

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

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

312
    token = newProfile;
44,735✔
313
    while (1) {
3,711,281✔
314
        comma = strchr(token, ALGO_SEPARATOR_C);
3,711,281✔
315
        if (comma)
1,878,008✔
316
            toklen = (size_t)(comma - token);
1,833,273✔
317
        else
318
            toklen = strlen(token);
44,735✔
319

320
        found = false;
1,878,008✔
321
        for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
57,039,579✔
322
            /* skip over unsupported algorithms */
323
            if (!s_AlgorithmProperties[algId].name)
56,904,848✔
324
                continue;
22,955,327✔
325
            cmplen = MAX(strlen(s_AlgorithmProperties[algId].name), toklen);
33,949,521✔
326
            if (!strncmp(token, s_AlgorithmProperties[algId].name, cmplen)) {
33,949,521✔
327
                if (s_AlgorithmProperties[algId].stateFormatLevel > maxStateFormatLevel) {
1,519,526✔
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);
332
                    retVal = TPM_RC_VALUE;
×
333
                    goto exit;
×
334
                }
335
                SET_BIT(algId, RuntimeAlgorithm->enabledAlgorithms);
1,519,526✔
336
                assert(s_AlgorithmProperties[algId].stateFormatLevel > 0);
1,519,526✔
337
                *stateFormatLevel = MAX(*stateFormatLevel,
1,519,526✔
338
                                        s_AlgorithmProperties[algId].stateFormatLevel);
339
                found = true;
1,519,526✔
340
                break;
1,519,526✔
341
            } else if (s_AlgorithmProperties[algId].u.minKeySize) {
32,429,995✔
342
                size_t namelen = strlen(s_AlgorithmProperties[algId].name);
1,609,964✔
343
                if (strncmp(token,
1,609,964✔
344
                            s_AlgorithmProperties[algId].name, /* i.e., 'hmac' */
345
                            namelen) ||
152✔
346
                    strncmp(&token[namelen], "-min-key-size=", 14))
152✔
347
                    continue;
1,609,812✔
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✔
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,820,031✔
360
                size_t algnamelen = strlen(s_AlgorithmProperties[algId].name);
6,171,942✔
361
                if (strncmp(token, s_AlgorithmProperties[algId].name, algnamelen) ||
6,171,942✔
362
                    strncmp(&token[algnamelen], "-min-size=", 10))
492,143✔
363
                    continue;
5,948,343✔
364
                minKeySize = strtoul(&token[algnamelen + 10], &endptr, 10);
223,599✔
365
                if ((*endptr != ALGO_SEPARATOR_C && *endptr != '\0') ||  minKeySize > 4096) {
223,599✔
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,330✔
373
                    if (keysizes[i].enabled &&
849,731✔
374
                        keysizes[i].size >= minKeySize &&
849,731✔
375
                        keysizes[i].stateFormatLevel <= maxStateFormatLevel) {
849,681✔
376
                        assert(keysizes[i].stateFormatLevel > 0);
777,091✔
377
                        *stateFormatLevel = MAX(*stateFormatLevel,
777,091✔
378
                                                keysizes[i].stateFormatLevel);
379
                    }
380
                }
381

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

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

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

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

415
                if (!strncmp(prefix, s_EccAlgorithmProperties[curveId].name, cmplen)) {
1,077,848✔
416
                    if (s_EccAlgorithmProperties[curveId].stateFormatLevel > maxStateFormatLevel) {
357,982✔
417
                        /* specific match that is not allowed causes error, otherwise skip */
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);
423
                            retVal = TPM_RC_VALUE;
×
424
                            goto exit;
×
425
                        }
426
                        continue;
×
427
                    }
428
                    *stateFormatLevel = MAX(*stateFormatLevel,
357,982✔
429
                                            s_EccAlgorithmProperties[curveId].stateFormatLevel);
430
                    SET_BIT(curveId, RuntimeAlgorithm->enabledEccCurves);
357,982✔
431
                    found = true;
357,982✔
432
                }
433
            }
434
        }
435

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

443
        if (!comma)
1,878,008✔
444
            break;
445
        token = &comma[1];
1,833,273✔
446
    }
447

448
    /* reconcile with what can be disabled per code instrumentation */
449
    for (algId = 0; algId < ARRAY_SIZE(s_AlgorithmProperties); algId++) {
3,131,450✔
450
        /* skip over unsupported algorithms */
451
        if (!s_AlgorithmProperties[algId].name)
3,086,715✔
452
            continue;
1,565,725✔
453
        if (!s_AlgorithmProperties[algId].canBeDisabled &&
2,326,220✔
454
            !TEST_BIT(algId, RuntimeAlgorithm->enabledAlgorithms)) {
805,230✔
455
            TPMLIB_LogTPM2Error("Algorithm %s must be enabled.\n",
×
456
                                s_AlgorithmProperties[algId].name);
457
            retVal = TPM_RC_VALUE;
×
458
            goto exit;
×
459
        }
460
    }
461
    for (curveId = 0; curveId < ARRAY_SIZE(s_EccAlgorithmProperties); curveId++) {
1,520,990✔
462
        if (!s_EccAlgorithmProperties[curveId].name)
1,476,255✔
463
            continue;
1,118,375✔
464
        if (!s_EccAlgorithmProperties[curveId].canBeDisabled &&
447,350✔
465
            !TEST_BIT(curveId, RuntimeAlgorithm->enabledEccCurves)) {
89,470✔
466
            TPMLIB_LogTPM2Error("Elliptic curve %s must be enabled.\n",
×
467
                                s_EccAlgorithmProperties[curveId].name);
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,880✔
473
               s_EccAlgorithmProperties[curveId].keySize &&
357,880✔
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,735✔
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");
485
        retVal = TPM_RC_KEY_SIZE;
×
486
        goto exit;
×
487
    }
488

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

494
exit:
44,735✔
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,046✔
503
                              const char               *newProfile,
504
                              unsigned int              maxStateFormatLevel,
505
                              char                    **oldProfile)
506
{
507
    TPM_RC retVal;
8,046✔
508
    unsigned int stateFormatLevel = 0; // ignored
8,046✔
509

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

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

523
/* Check whether the given algorithm is runtime-enabled */
524
LIB_EXPORT BOOL
525
RuntimeAlgorithmCheckEnabled(struct RuntimeAlgorithm *RuntimeAlgorithm,
641,398✔
526
                             TPM_ALG_ID                      algId      // IN: the algorithm to check
527
                             )
528
{
529
    if ((algId >> 3) >= sizeof(RuntimeAlgorithm->enabledAlgorithms) ||
1,282,796✔
530
        !TestBit(algId, RuntimeAlgorithm->enabledAlgorithms,
641,398✔
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,904✔
592
                       enum RuntimeAlgorithmType rat,
593
                       char                      *buffer,
594
                       BOOL                      *first)
595
{
596
    TPM_ECC_CURVE curveId;
3,904✔
597
    char *nbuffer = NULL;
3,904✔
598
    size_t idx;
3,904✔
599
    int n;
3,904✔
600

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

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

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

666
    return buffer;
667
}
668

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

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

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

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

719
        minKeySize = 0;
81,984✔
720

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

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

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

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

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

767
    return nbuffer;
3,904✔
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