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

stefanberger / libtpms / #2046

02 Jan 2026 04:38PM UTC coverage: 77.225% (+0.01%) from 77.211%
#2046

push

travis-ci

web-flow
Merge d93cf7173 into ee31b0b23

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

527 existing lines in 42 files now uncovered.

36112 of 46762 relevant lines covered (77.23%)

126004.05 hits per line

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

75.0
/src/tpm2/AlgorithmCap.c
1
/********************************************************************************/
2
/*                                                                                */
3
/*                         Algorithm Property Definitions                         */
4
/*                             Written by Ken Goldman                                */
5
/*                       IBM Thomas J. Watson Research Center                        */
6
/*                                                                                */
7
/*  Licenses and Notices                                                        */
8
/*                                                                                */
9
/*  1. Copyright Licenses:                                                        */
10
/*                                                                                */
11
/*  - Trusted Computing Group (TCG) grants to the user of the source code in        */
12
/*    this specification (the "Source Code") a worldwide, irrevocable,                 */
13
/*    nonexclusive, royalty free, copyright license to reproduce, create         */
14
/*    derivative works, distribute, display and perform the Source Code and        */
15
/*    derivative works thereof, and to grant others the rights granted herein.        */
16
/*                                                                                */
17
/*  - The TCG grants to the user of the other parts of the specification         */
18
/*    (other than the Source Code) the rights to reproduce, distribute,         */
19
/*    display, and perform the specification solely for the purpose of                 */
20
/*    developing products based on such documents.                                */
21
/*                                                                                */
22
/*  2. Source Code Distribution Conditions:                                        */
23
/*                                                                                */
24
/*  - Redistributions of Source Code must retain the above copyright licenses,         */
25
/*    this list of conditions and the following disclaimers.                        */
26
/*                                                                                */
27
/*  - Redistributions in binary form must reproduce the above copyright         */
28
/*    licenses, this list of conditions        and the following disclaimers in the         */
29
/*    documentation and/or other materials provided with the distribution.        */
30
/*                                                                                */
31
/*  3. Disclaimers:                                                                */
32
/*                                                                                */
33
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF        */
34
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH        */
35
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES)        */
36
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.                */
37
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for                 */
38
/*  information on specification licensing rights available through TCG         */
39
/*  membership agreements.                                                        */
40
/*                                                                                */
41
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED         */
42
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR         */
43
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR                 */
44
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY                 */
45
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.                */
46
/*                                                                                */
47
/*  - Without limitation, TCG and its members and licensors disclaim all         */
48
/*    liability, including liability for infringement of any proprietary         */
49
/*    rights, relating to use of information in this specification and to the        */
50
/*    implementation of this specification, and TCG disclaims all liability for        */
51
/*    cost of procurement of substitute goods or services, lost profits, loss         */
52
/*    of use, loss of data or any incidental, consequential, direct, indirect,         */
53
/*    or special damages, whether under contract, tort, warranty or otherwise,         */
54
/*    arising in any way out of use or reliance upon this specification or any         */
55
/*    information herein.                                                        */
56
/*                                                                                */
57
/*  (c) Copyright IBM Corp. and others, 2016 - 2023                                */
58
/*                                                                                */
59
/********************************************************************************/
60

61
//** Description
62
// This file contains the algorithm property definitions for the algorithms and the
63
// code for the TPM2_GetCapability() to return the algorithm properties.
64

65
//** Includes and Defines
66

67
#include "Tpm.h"
68

69
typedef struct
70
{
71
    TPM_ALG_ID     algID;
72
    TPMA_ALGORITHM attributes;
73
} ALGORITHM;
74

75
static const ALGORITHM s_algorithms[] = {
76
// The entries in this table need to be in ascending order but the table doesn't
77
// need to be full (gaps are allowed). One day, a tool might exist to fill in the
78
// table from the TPM_ALG description
79
#if ALG_RSA
80
    {TPM_ALG_RSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 1, 0, 0, 0, 0, 0)},
81
#endif
82

83
#if ALG_TDES                                                                        // libtpms added begin
84
        {TPM_ALG_TDES,          TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
85
#endif                                                                                // libtpms added end
86

87
#if ALG_SHA1
88
    {TPM_ALG_SHA1, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
89
#endif
90

91
    {TPM_ALG_HMAC, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 1, 0, 0, 0)},
92

93
#if ALG_AES
94
    {TPM_ALG_AES, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
95
#endif
96
#if ALG_MGF1
97
    {TPM_ALG_MGF1, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
98
#endif
99

100
    {TPM_ALG_KEYEDHASH, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 1, 0, 1, 1, 0, 0)},
101

102
#if ALG_XOR
103
    {TPM_ALG_XOR, TPMA_ALGORITHM_INITIALIZER(0, 1, 1, 0, 0, 0, 0, 0, 0)},
104
#endif
105

106
#if ALG_SHA256
107
    {TPM_ALG_SHA256, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
108
#endif
109
#if ALG_SHA384
110
    {TPM_ALG_SHA384, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
111
#endif
112
#if ALG_SHA512
113
    {TPM_ALG_SHA512, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
114
#endif
115
#if ALG_SM3_256
116
    {TPM_ALG_SM3_256, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
117
#endif
118
#if ALG_SM4
119
    {TPM_ALG_SM4, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
120
#endif
121
#if ALG_RSASSA
122
    {TPM_ALG_RSASSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
123
#endif
124
#if ALG_RSAES
125
    {TPM_ALG_RSAES, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 1, 0, 0)},
126
#endif
127
#if ALG_RSAPSS
128
    {TPM_ALG_RSAPSS, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
129
#endif
130
#if ALG_OAEP
131
    {TPM_ALG_OAEP, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 1, 0, 0)},
132
#endif
133
#if ALG_ECDSA
134
    {TPM_ALG_ECDSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
135
#endif
136
#if ALG_ECDH
137
    {TPM_ALG_ECDH, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 0, 1, 0)},
138
#endif
139
#if ALG_ECDAA
140
    {TPM_ALG_ECDAA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
141
#endif
142
#if ALG_SM2
143
    {TPM_ALG_SM2, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 1, 0)},
144
#endif
145
#if ALG_ECSCHNORR
146
    {TPM_ALG_ECSCHNORR, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
147
#endif
148
#if ALG_ECMQV
149
    {TPM_ALG_ECMQV, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 0, 1, 0)},
150
#endif
151
#if ALG_KDF1_SP800_56A
152
    {TPM_ALG_KDF1_SP800_56A, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
153
#endif
154
#if ALG_KDF2
155
    {TPM_ALG_KDF2, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
156
#endif
157
#if ALG_KDF1_SP800_108
158
    {TPM_ALG_KDF1_SP800_108, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
159
#endif
160
#if ALG_ECC
161
    {TPM_ALG_ECC, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 1, 0, 0, 0, 0, 0)},
162
#endif
163

164
    {TPM_ALG_SYMCIPHER, TPMA_ALGORITHM_INITIALIZER(0, 0, 0, 1, 0, 0, 0, 0, 0)},
165

166
#if ALG_CAMELLIA
167
    {TPM_ALG_CAMELLIA, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
168
#endif
169
#if ALG_CMAC
170
    {TPM_ALG_CMAC, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 1, 0, 0, 0)},
171
#endif
172
#if ALG_CTR
173
    {TPM_ALG_CTR, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
174
#endif
175
#if ALG_OFB
176
    {TPM_ALG_OFB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
177
#endif
178
#if ALG_CBC
179
    {TPM_ALG_CBC, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
180
#endif
181
#if ALG_CFB
182
    {TPM_ALG_CFB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
183
#endif
184
#if ALG_ECB
185
    {TPM_ALG_ECB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
186
#endif
187
};
188

189
//** AlgorithmCapGetImplemented()
190
// This function is used by TPM2_GetCapability() to return a list of the
191
// implemented algorithms.
192
//
193
//  Return Type: TPMI_YES_NO
194
//  YES        more algorithms to report
195
//  NO         no more algorithms to report
196
TPMI_YES_NO
197
AlgorithmCapGetImplemented(TPM_ALG_ID algID,  // IN: the starting algorithm ID
50✔
198
                           UINT32     count,  // IN: count of returned algorithms
199
                           TPML_ALG_PROPERTY* algList  // OUT: algorithm list
200
)
201
{
202
    TPMI_YES_NO more = NO;
50✔
203
    UINT32      i;
50✔
204
    UINT32      algNum;
50✔
205

206
    // initialize output algorithm list
207
    algList->count = 0;
50✔
208

209
    // The maximum count of algorithms we may return is MAX_CAP_ALGS.
210
    if(count > MAX_CAP_ALGS)
50✔
211
        count = MAX_CAP_ALGS;
212

213
    // Compute how many algorithms are defined in s_algorithms array.
214
    algNum = sizeof(s_algorithms) / sizeof(s_algorithms[0]);
50✔
215

216
    // Scan the implemented algorithm list to see if there is a match to 'algID'.
217
    for(i = 0; i < algNum; i++)
1,248✔
218
    {
219
        // If algID is less than the starting algorithm ID, skip it
220
        if(s_algorithms[i].algID < algID)
1,214✔
221
            continue;
480✔
222
        if(!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,// libtpms added begin
734✔
223
                                         s_algorithms[i].algID))
224
            continue;                                                        // libtpms added end
9✔
225
        if(algList->count < count)
725✔
226
        {
227
            // If we have not filled up the return list, add more algorithms
228
            // to it
229
            algList->algProperties[algList->count].alg = s_algorithms[i].algID;
709✔
230
            algList->algProperties[algList->count].algProperties =
709✔
231
                s_algorithms[i].attributes;
709✔
232
            algList->count++;
709✔
233
        }
234
        else
235
        {
236
            // If the return list is full but we still have algorithms
237
            // available, report this and stop scanning.
238
            more = YES;
239
            break;
240
        }
241
    }
242

243
    return more;
50✔
244
}
245

246
//** AlgorithmCapGetOneImplemented()
247
// This function returns whether a single algorithm was implemented, along
248
// with its properties (if implemented).
249
BOOL AlgorithmCapGetOneImplemented(
×
250
    TPM_ALG_ID         algID,       // IN: the algorithm ID
251
    TPMS_ALG_PROPERTY* algProperty  // OUT: algorithm properties
252
)
253
{
UNCOV
254
    UINT32 i;
×
UNCOV
255
    UINT32 algNum;
×
256

UNCOV
257
    if(!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm, algID))// libtpms added
×
258
        return FALSE;                                                                // libtpms added
259

260
    // Compute how many algorithms are defined in s_algorithms array.
261
    algNum = sizeof(s_algorithms) / sizeof(s_algorithms[0]);
262

263
    // Scan the implemented algorithm list to see if there is a match to 'algID'.
UNCOV
264
    for(i = 0; i < algNum; i++)
×
265
    {
266
        // If algID is less than the starting algorithm ID, skip it
UNCOV
267
        if(s_algorithms[i].algID == algID)
×
268
        {
UNCOV
269
            algProperty->alg           = algID;
×
UNCOV
270
            algProperty->algProperties = s_algorithms[i].attributes;
×
UNCOV
271
            return TRUE;
×
272
        }
273
    }
274

275
    return FALSE;
276
}
277

278
//** AlgorithmGetImplementedVector()
279
// This function returns the bit vector of the implemented algorithms.
280
LIB_EXPORT
281
void AlgorithmGetImplementedVector(
12,494✔
282
    ALGORITHM_VECTOR* implemented  // OUT: the implemented bits are SET
283
)
284
{
285
    int index;
12,494✔
286

287
    // Nothing implemented until we say it is
288
    MemorySet(implemented, 0, sizeof(ALGORITHM_VECTOR));
12,494✔
289
    // Go through the list of implemented algorithms and SET the corresponding bit in
290
    // in the implemented vector
291
    for(index = (sizeof(s_algorithms) / sizeof(s_algorithms[0])) - 1; index >= 0;
437,290✔
292
        index--) {                                                                // libtpms changed
412,302✔
293
        if (RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,        // libtpms added begin
412,302✔
294
                                         s_algorithms[index].algID))
412,302✔
295
            SET_BIT(s_algorithms[index].algID, *implemented);
411,528✔
296
    }                                                                                // libtpms added end
297
    return;
12,494✔
298
}
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