Coverage Report

Created: 2025-07-12 06:29

/src/libtpms/src/tpm2/AlgorithmCap.c
Line
Count
Source (jump to first uncovered line)
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(
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
53
{
189
53
    TPMI_YES_NO     more = NO;
190
53
    UINT32          i;
191
53
    UINT32          algNum;
192
    // initialize output algorithm list
193
53
    algList->count = 0;
194
    // The maximum count of algorithms we may return is MAX_CAP_ALGS.
195
53
    if(count > MAX_CAP_ALGS)
196
40
  count = MAX_CAP_ALGS;
197
    // Compute how many algorithms are defined in s_algorithms array.
198
53
    algNum = sizeof(s_algorithms) / sizeof(s_algorithms[0]);
199
    // Scan the implemented algorithm list to see if there is a match to 'algID'.
200
1.59k
    for(i = 0; i < algNum; i++)
201
1.55k
  {
202
      // If algID is less than the starting algorithm ID, skip it
203
1.55k
      if(s_algorithms[i].algID < algID)
204
1.08k
    continue;
205
470
      if(!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,// libtpms added begin
206
470
               s_algorithms[i].algID))
207
0
    continue;             // libtpms added end
208
470
      if(algList->count < count)
209
462
    {
210
        // If we have not filled up the return list, add more algorithms
211
        // to it
212
462
        algList->algProperties[algList->count].alg = s_algorithms[i].algID;
213
462
        algList->algProperties[algList->count].algProperties =
214
462
      s_algorithms[i].attributes;
215
462
        algList->count++;
216
462
    }
217
8
      else
218
8
    {
219
        // If the return list is full but we still have algorithms
220
        // available, report this and stop scanning.
221
8
        more = YES;
222
8
        break;
223
8
    }
224
470
  }
225
53
    return more;
226
53
}
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
0
{
236
0
    UINT32 i;
237
0
    UINT32 algNum;
238
239
0
    if(!RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm, algID))// libtpms added
240
0
  return FALSE;               // libtpms added
241
242
    // Compute how many algorithms are defined in s_algorithms array.
243
0
    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
0
    for(i = 0; i < algNum; i++)
247
0
  {
248
      // If algID is less than the starting algorithm ID, skip it
249
0
      if(s_algorithms[i].algID == algID)
250
0
    {
251
0
        algProperty->alg           = algID;
252
0
        algProperty->algProperties = s_algorithms[i].attributes;
253
0
        return TRUE;
254
0
    }
255
0
  }
256
0
    return FALSE;
257
0
}
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(
267
            ALGORITHM_VECTOR    *implemented    // OUT: the implemented bits are SET
268
            )
269
17.8k
{
270
17.8k
    int                      index;
271
    // Nothing implemented until we say it is
272
17.8k
    MemorySet(implemented, 0, sizeof(ALGORITHM_VECTOR));
273
    // Go through the list of implemented algorithms and SET the corresponding bit in
274
    // in the implemented vector
275
17.8k
    for(index = (sizeof(s_algorithms) / sizeof(s_algorithms[0])) - 1;
276
607k
  index >= 0; index--) {             // libtpms changed
277
590k
  if (RuntimeAlgorithmCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,  // libtpms added begin
278
590k
           s_algorithms[index].algID))
279
590k
      SET_BIT(s_algorithms[index].algID, *implemented);
280
590k
    }                    // libtpms added end
281
17.8k
    return;
282
17.8k
}