• 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

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
/* 9.1 AlgorithmCap.c */
62
/* 9.1.1 Description */
63
/* This file contains the algorithm property definitions for the algorithms and the code for the
64
   TPM2_GetCapability() to return the algorithm properties. */
65
/* 9.1.2 Includes and Defines */
66
#include "Tpm.h"
67
typedef struct
68
{
69
    TPM_ALG_ID          algID;
70
    TPMA_ALGORITHM      attributes;
71
} ALGORITHM;
72
static const ALGORITHM    s_algorithms[] =
73
    {
74
        // The entries in this table need to be in ascending order but the table doesn't
75
        // need to be full (gaps are allowed). One day, a tool might exist to fill in the
76
        // table from the TPM_ALG description
77
#if ALG_RSA
78
        {TPM_ALG_RSA,           TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 1, 0, 0, 0, 0, 0)},
79
#endif
80
#if ALG_TDES
81
        {TPM_ALG_TDES,          TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
82
#endif
83
#if ALG_SHA1
84
        {TPM_ALG_SHA1,          TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
85
#endif
86
        {TPM_ALG_HMAC,          TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 1, 0, 0, 0)},
87
#if ALG_AES
88
        {TPM_ALG_AES,           TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
89
#endif
90
#if ALG_MGF1
91
        {TPM_ALG_MGF1,          TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
92
#endif
93
        {TPM_ALG_KEYEDHASH,     TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 1, 0, 1, 1, 0, 0)},
94
#if ALG_XOR
95
        {TPM_ALG_XOR,           TPMA_ALGORITHM_INITIALIZER(0, 1, 1, 0, 0, 0, 0, 0, 0)},
96
#endif
97
#if ALG_SHA256
98
        {TPM_ALG_SHA256,        TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
99
#endif
100
#if ALG_SHA384
101
        {TPM_ALG_SHA384,        TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
102
#endif
103
#if ALG_SHA512
104
        {TPM_ALG_SHA512,        TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
105
#endif
106
#if ALG_SM3_256
107
        {TPM_ALG_SM3_256,       TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)},
108
#endif
109
#if ALG_SM4
110
        {TPM_ALG_SM4,           TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
111
#endif
112
#if ALG_RSASSA
113
        {TPM_ALG_RSASSA,        TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
114
#endif
115
#if ALG_RSAES
116
        {TPM_ALG_RSAES,         TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 1, 0, 0)},
117
#endif
118
#if ALG_RSAPSS
119
        {TPM_ALG_RSAPSS,        TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
120
#endif
121
#if ALG_OAEP
122
        {TPM_ALG_OAEP,          TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 1, 0, 0)},
123
#endif
124
#if ALG_ECDSA
125
        {TPM_ALG_ECDSA,         TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
126
#endif
127
#if ALG_ECDH
128
        {TPM_ALG_ECDH,          TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 0, 1, 0)},
129
#endif
130
#if ALG_ECDAA
131
        {TPM_ALG_ECDAA,         TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
132
#endif
133
#if ALG_SM2
134
        {TPM_ALG_SM2,           TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 1, 0)},
135
#endif
136
#if ALG_ECSCHNORR
137
        {TPM_ALG_ECSCHNORR,      TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)},
138
#endif
139
#if ALG_ECMQV
140
        {TPM_ALG_ECMQV,          TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 0, 1, 0)},
141
#endif
142
#if ALG_KDF1_SP800_56A
143
        {TPM_ALG_KDF1_SP800_56A, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
144
#endif
145
#if ALG_KDF2
146
        {TPM_ALG_KDF2,           TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
147
#endif
148
#if ALG_KDF1_SP800_108
149
        {TPM_ALG_KDF1_SP800_108, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)},
150
#endif
151
#if ALG_ECC
152
        {TPM_ALG_ECC,            TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 1, 0, 0, 0, 0, 0)},
153
#endif
154
        {TPM_ALG_SYMCIPHER,      TPMA_ALGORITHM_INITIALIZER(0, 0, 0, 1, 0, 0, 0, 0, 0)},
155
#if ALG_CAMELLIA
156
        {TPM_ALG_CAMELLIA,       TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)},
157
#endif
158
#if ALG_CMAC
159
        {TPM_ALG_CMAC,           TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 1, 0, 0, 0)},
160
#endif
161
#if ALG_CTR
162
        {TPM_ALG_CTR,            TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
163
#endif
164
#if ALG_OFB
165
        {TPM_ALG_OFB,            TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
166
#endif
167
#if ALG_CBC
168
        {TPM_ALG_CBC,            TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
169
#endif
170
#if ALG_CFB
171
        {TPM_ALG_CFB,            TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
172
#endif
173
#if ALG_ECB
174
        {TPM_ALG_ECB,            TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)},
175
#endif
176
    };
177
/* 9.1.3 AlgorithmCapGetImplemented() */
178
/* This function is used by TPM2_GetCapability() to return a list of the implemented algorithms. */
179
/* Return Values Meaning */
180
/* YES more algorithms to report */
181
/* NO no more algorithms to report */
182
TPMI_YES_NO
183
AlgorithmCapGetImplemented(
50✔
184
                           TPM_ALG_ID                   algID,     // IN: the starting algorithm ID
185
                           UINT32                       count,     // IN: count of returned algorithms
186
                           TPML_ALG_PROPERTY           *algList    // OUT: algorithm list
187
                           )
188
{
189
    TPMI_YES_NO     more = NO;
50✔
190
    UINT32          i;
50✔
191
    UINT32          algNum;
50✔
192
    // initialize output algorithm list
193
    algList->count = 0;
50✔
194
    // The maximum count of algorithms we may return is MAX_CAP_ALGS.
195
    if(count > MAX_CAP_ALGS)
50✔
196
        count = MAX_CAP_ALGS;
197
    // Compute how many algorithms are defined in s_algorithms array.
198
    algNum = sizeof(s_algorithms) / sizeof(s_algorithms[0]);
50✔
199
    // Scan the implemented algorithm list to see if there is a match to 'algID'.
200
    for(i = 0; i < algNum; i++)
1,248✔
201
        {
202
            // If algID is less than the starting algorithm ID, skip it
203
            if(s_algorithms[i].algID < algID)
1,214✔
204
                continue;
480✔
205
            if(!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,// libtpms added begin
734✔
206
                                             s_algorithms[i].algID))
207
                continue;                                                        // libtpms added end
9✔
208
            if(algList->count < count)
725✔
209
                {
210
                    // If we have not filled up the return list, add more algorithms
211
                    // to it
212
                    algList->algProperties[algList->count].alg = s_algorithms[i].algID;
709✔
213
                    algList->algProperties[algList->count].algProperties =
709✔
214
                        s_algorithms[i].attributes;
709✔
215
                    algList->count++;
709✔
216
                }
217
            else
218
                {
219
                    // If the return list is full but we still have algorithms
220
                    // available, report this and stop scanning.
221
                    more = YES;
222
                    break;
223
                }
224
        }
225
    return more;
50✔
226
}
227

228
//** AlgorithmCapGetOneImplemented()
229
// This function returns whether a single algorithm was implemented, along
230
// with its properties (if implemented).
231
BOOL AlgorithmCapGetOneImplemented(
×
232
                                   TPM_ALG_ID         algID,       // IN: the algorithm ID
233
                                   TPMS_ALG_PROPERTY* algProperty  // OUT: algorithm properties
234
                                   )
235
{
236
    UINT32 i;
×
237
    UINT32 algNum;
×
238

239
    if(!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm, algID))// libtpms added
×
240
        return FALSE;                                                                // libtpms added
241

242
    // Compute how many algorithms are defined in s_algorithms array.
243
    algNum = sizeof(s_algorithms) / sizeof(s_algorithms[0]);
244

245
    // Scan the implemented algorithm list to see if there is a match to 'algID'.
246
    for(i = 0; i < algNum; i++)
×
247
        {
248
            // If algID is less than the starting algorithm ID, skip it
249
            if(s_algorithms[i].algID == algID)
×
250
                {
251
                    algProperty->alg           = algID;
×
252
                    algProperty->algProperties = s_algorithms[i].attributes;
×
253
                    return TRUE;
×
254
                }
255
        }
256
    return FALSE;
257
}
258

259

260
/* 9.1.4 AlgorithmGetImplementedVector()
261

262
   This function returns the bit vector of the implemented algorithms.
263
*/
264
LIB_EXPORT
265
void
266
AlgorithmGetImplementedVector(
12,465✔
267
                              ALGORITHM_VECTOR    *implemented    // OUT: the implemented bits are SET
268
                              )
269
{
270
    int                      index;
12,465✔
271
    // Nothing implemented until we say it is
272
    MemorySet(implemented, 0, sizeof(ALGORITHM_VECTOR));
12,465✔
273
    // Go through the list of implemented algorithms and SET the corresponding bit in
274
    // in the implemented vector
275
    for(index = (sizeof(s_algorithms) / sizeof(s_algorithms[0])) - 1;
12,465✔
276
        index >= 0; index--) {                                                        // libtpms changed
423,810✔
277
        if (RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,        // libtpms added begin
411,345✔
278
                                         s_algorithms[index].algID))
411,345✔
279
            SET_BIT(s_algorithms[index].algID, *implemented);
410,571✔
280
    }                                                                                // libtpms added end
281
    return;
12,465✔
282
}
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