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

stefanberger / libtpms / #2066

11 Feb 2026 04:23PM UTC coverage: 77.19% (+0.002%) from 77.188%
#2066

push

travis-ci

web-flow
Merge 4b1bba735 into c2a8109f8

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

2185 existing lines in 87 files now uncovered.

36352 of 47094 relevant lines covered (77.19%)

125371.74 hits per line

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

75.0
/src/tpm2/TPMCmd/tpm/src/support/AlgorithmCap.c
1
// SPDX-License-Identifier: BSD-2-Clause
2

3
//** Description
4
// This file contains the algorithm property definitions for the algorithms and the
5
// code for the TPM2_GetCapability() to return the algorithm properties.
6

7
//** Includes and Defines
8

9
#include "Tpm.h"
10

11
typedef struct
12
{
13
    TPM_ALG_ID     algID;
14
    TPMA_ALGORITHM attributes;
15
} ALGORITHM;
16

17
static const ALGORITHM s_algorithms[] = {
18
// The entries in this table need to be in ascending order but the table doesn't
19
// need to be full (gaps are allowed). One day, a tool might exist to fill in the
20
// table from the TPM_ALG description
21
#if ALG_RSA
22
    {TPM_ALG_RSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 1, 0, 0, 0, 0, 0)},
23
#endif
24

25
#if ALG_TDES                                                                        // libtpms added begin
26
        {TPM_ALG_TDES,          TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
27
#endif                                                                                // libtpms added end
28

29
#if ALG_SHA1
30
    {TPM_ALG_SHA1, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
31
#endif
32

33
    {TPM_ALG_HMAC, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 1, 0, 0, 0)},
34

35
#if ALG_AES
36
    {TPM_ALG_AES, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
37
#endif
38
#if ALG_MGF1
39
    {TPM_ALG_MGF1, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
40
#endif
41

42
    {TPM_ALG_KEYEDHASH, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 1, 0, 1, 1, 0, 0)},
43

44
#if ALG_XOR
45
    {TPM_ALG_XOR, TPMA_ALGORITHM_INITIALIZER(0, 1, 1, 0, 0, 0, 0, 0, 0)},
46
#endif
47

48
#if ALG_SHA256
49
    {TPM_ALG_SHA256, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
50
#endif
51
#if ALG_SHA384
52
    {TPM_ALG_SHA384, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
53
#endif
54
#if ALG_SHA512
55
    {TPM_ALG_SHA512, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
56
#endif
57
#if ALG_SM3_256
58
    {TPM_ALG_SM3_256, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
59
#endif
60
#if ALG_SM4
61
    {TPM_ALG_SM4, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
62
#endif
63
#if ALG_RSASSA
64
    {TPM_ALG_RSASSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
65
#endif
66
#if ALG_RSAES
67
    {TPM_ALG_RSAES, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 1, 0, 0)},
68
#endif
69
#if ALG_RSAPSS
70
    {TPM_ALG_RSAPSS, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
71
#endif
72
#if ALG_OAEP
73
    {TPM_ALG_OAEP, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 1, 0, 0)},
74
#endif
75
#if ALG_ECDSA
76
    {TPM_ALG_ECDSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
77
#endif
78
#if ALG_ECDH
79
    {TPM_ALG_ECDH, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 0, 1, 0)},
80
#endif
81
#if ALG_ECDAA
82
    {TPM_ALG_ECDAA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
83
#endif
84
#if ALG_SM2
85
    {TPM_ALG_SM2, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 1, 0)},
86
#endif
87
#if ALG_ECSCHNORR
88
    {TPM_ALG_ECSCHNORR, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
89
#endif
90
#if ALG_ECMQV
91
    {TPM_ALG_ECMQV, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 0, 1, 0)},
92
#endif
93
#if ALG_KDF1_SP800_56A
94
    {TPM_ALG_KDF1_SP800_56A, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
95
#endif
96
#if ALG_KDF2
97
    {TPM_ALG_KDF2, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
98
#endif
99
#if ALG_KDF1_SP800_108
100
    {TPM_ALG_KDF1_SP800_108, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
101
#endif
102
#if ALG_ECC
103
    {TPM_ALG_ECC, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 1, 0, 0, 0, 0, 0)},
104
#endif
105

106
    {TPM_ALG_SYMCIPHER, TPMA_ALGORITHM_INITIALIZER(0, 0, 0, 1, 0, 0, 0, 0, 0)},
107

108
#if ALG_CAMELLIA
109
    {TPM_ALG_CAMELLIA, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
110
#endif
111
#if ALG_CMAC
112
    {TPM_ALG_CMAC, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 1, 0, 0, 0)},
113
#endif
114
#if ALG_CTR
115
    {TPM_ALG_CTR, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
116
#endif
117
#if ALG_OFB
118
    {TPM_ALG_OFB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
119
#endif
120
#if ALG_CBC
121
    {TPM_ALG_CBC, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
122
#endif
123
#if ALG_CFB
124
    {TPM_ALG_CFB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
125
#endif
126
#if ALG_ECB
127
    {TPM_ALG_ECB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
128
#endif
129
};
130

131
//** AlgorithmCapGetImplemented()
132
// This function is used by TPM2_GetCapability() to return a list of the
133
// implemented algorithms.
134
//
135
//  Return Type: TPMI_YES_NO
136
//  YES        more algorithms to report
137
//  NO         no more algorithms to report
138
TPMI_YES_NO
139
AlgorithmCapGetImplemented(TPM_ALG_ID algID,  // IN: the starting algorithm ID
50✔
140
                           UINT32     count,  // IN: count of returned algorithms
141
                           TPML_ALG_PROPERTY* algList  // OUT: algorithm list
142
)
143
{
144
    TPMI_YES_NO more = NO;
50✔
145
    UINT32      i;
50✔
146
    UINT32      algNum;
50✔
147

148
    // initialize output algorithm list
149
    algList->count = 0;
50✔
150

151
    // The maximum count of algorithms we may return is MAX_CAP_ALGS.
152
    if(count > MAX_CAP_ALGS)
50✔
153
        count = MAX_CAP_ALGS;
154

155
    // Compute how many algorithms are defined in s_algorithms array.
156
    algNum = sizeof(s_algorithms) / sizeof(s_algorithms[0]);
50✔
157

158
    // Scan the implemented algorithm list to see if there is a match to 'algID'.
159
    for(i = 0; i < algNum; i++)
1,248✔
160
    {
161
        // If algID is less than the starting algorithm ID, skip it
162
        if(s_algorithms[i].algID < algID)
1,214✔
163
            continue;
480✔
164
        if(!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,// libtpms added begin
734✔
165
                                         s_algorithms[i].algID))
166
            continue;                                                        // libtpms added end
9✔
167
        if(algList->count < count)
725✔
168
        {
169
            // If we have not filled up the return list, add more algorithms
170
            // to it
171
            algList->algProperties[algList->count].alg = s_algorithms[i].algID;
709✔
172
            algList->algProperties[algList->count].algProperties =
709✔
173
                s_algorithms[i].attributes;
709✔
174
            algList->count++;
709✔
175
        }
176
        else
177
        {
178
            // If the return list is full but we still have algorithms
179
            // available, report this and stop scanning.
180
            more = YES;
181
            break;
182
        }
183
    }
184

185
    return more;
50✔
186
}
187

188
//** AlgorithmCapGetOneImplemented()
189
// This function returns whether a single algorithm was implemented, along
190
// with its properties (if implemented).
UNCOV
191
BOOL AlgorithmCapGetOneImplemented(
×
192
    TPM_ALG_ID         algID,       // IN: the algorithm ID
193
    TPMS_ALG_PROPERTY* algProperty  // OUT: algorithm properties
194
)
195
{
UNCOV
196
    UINT32 i;
×
UNCOV
197
    UINT32 algNum;
×
198

UNCOV
199
    if(!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm, algID))// libtpms added
×
200
        return FALSE;                                                                // libtpms added
201

202
    // Compute how many algorithms are defined in s_algorithms array.
203
    algNum = sizeof(s_algorithms) / sizeof(s_algorithms[0]);
204

205
    // Scan the implemented algorithm list to see if there is a match to 'algID'.
UNCOV
206
    for(i = 0; i < algNum; i++)
×
207
    {
208
        // If algID is less than the starting algorithm ID, skip it
UNCOV
209
        if(s_algorithms[i].algID == algID)
×
210
        {
UNCOV
211
            algProperty->alg           = algID;
×
UNCOV
212
            algProperty->algProperties = s_algorithms[i].attributes;
×
UNCOV
213
            return TRUE;
×
214
        }
215
    }
216

217
    return FALSE;
218
}
219

220
//** AlgorithmGetImplementedVector()
221
// This function returns the bit vector of the implemented algorithms.
222
LIB_EXPORT
223
void AlgorithmGetImplementedVector(
12,495✔
224
    ALGORITHM_VECTOR* implemented  // OUT: the implemented bits are SET
225
)
226
{
227
    int index;
12,495✔
228

229
    // Nothing implemented until we say it is
230
    MemorySet(implemented, 0, sizeof(ALGORITHM_VECTOR));
12,495✔
231
    // Go through the list of implemented algorithms and SET the corresponding bit in
232
    // in the implemented vector
233
    for(index = (sizeof(s_algorithms) / sizeof(s_algorithms[0])) - 1; index >= 0;
437,325✔
234
        index--) {                                                                // libtpms changed
412,335✔
235
        if (RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,        // libtpms added begin
412,335✔
236
                                         s_algorithms[index].algID))
412,335✔
237
            SET_BIT(s_algorithms[index].algID, *implemented);
411,561✔
238
    }                                                                                // libtpms added end
239
    return;
12,495✔
240
}
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