| 1 |
/* Microsoft Reference Implementation for TPM 2.0 |
| 2 |
* |
| 3 |
* The copyright in this software is being made available under the BSD License, |
| 4 |
* included below. This software may be subject to other third party and |
| 5 |
* contributor rights, including patent rights, and no such rights are granted |
| 6 |
* under this license. |
| 7 |
* |
| 8 |
* Copyright (c) Microsoft Corporation |
| 9 |
* |
| 10 |
* All rights reserved. |
| 11 |
* |
| 12 |
* BSD License |
| 13 |
* |
| 14 |
* Redistribution and use in source and binary forms, with or without modification, |
| 15 |
* are permitted provided that the following conditions are met: |
| 16 |
* |
| 17 |
* Redistributions of source code must retain the above copyright notice, this list |
| 18 |
* of conditions and the following disclaimer. |
| 19 |
* |
| 20 |
* Redistributions in binary form must reproduce the above copyright notice, this |
| 21 |
* list of conditions and the following disclaimer in the documentation and/or |
| 22 |
* other materials provided with the distribution. |
| 23 |
* |
| 24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" |
| 25 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 28 |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 31 |
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
//** Description |
| 36 |
// This file contains the algorithm property definitions for the algorithms and the |
| 37 |
// code for the TPM2_GetCapability() to return the algorithm properties. |
| 38 |
|
| 39 |
//** Includes and Defines |
| 40 |
|
| 41 |
#include "Tpm.h" |
| 42 |
|
| 43 |
typedef struct |
| 44 |
{ |
| 45 |
TPM_ALG_ID algID; |
| 46 |
TPMA_ALGORITHM attributes; |
| 47 |
} ALGORITHM; |
| 48 |
|
| 49 |
static const ALGORITHM s_algorithms[] = |
| 50 |
{ |
| 51 |
// The entries in this table need to be in ascending order but the table doesn't |
| 52 |
// need to be full (gaps are allowed). One day, a tool might exist to fill in the |
| 53 |
// table from the TPM_ALG description |
| 54 |
#if ALG_RSA |
| 55 |
{TPM_ALG_RSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 1, 0, 0, 0, 0, 0)}, |
| 56 |
#endif |
| 57 |
#if ALG_TDES |
| 58 |
{TPM_ALG_TDES, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)}, |
| 59 |
#endif |
| 60 |
#if ALG_SHA1 |
| 61 |
{TPM_ALG_SHA1, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)}, |
| 62 |
#endif |
| 63 |
|
| 64 |
{TPM_ALG_HMAC, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 1, 0, 0, 0)}, |
| 65 |
|
| 66 |
#if ALG_AES |
| 67 |
{TPM_ALG_AES, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)}, |
| 68 |
#endif |
| 69 |
#if ALG_MGF1 |
| 70 |
{TPM_ALG_MGF1, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)}, |
| 71 |
#endif |
| 72 |
|
| 73 |
{TPM_ALG_KEYEDHASH, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 1, 0, 1, 1, 0, 0)}, |
| 74 |
|
| 75 |
#if ALG_XOR |
| 76 |
{TPM_ALG_XOR, TPMA_ALGORITHM_INITIALIZER(0, 1, 1, 0, 0, 0, 0, 0, 0)}, |
| 77 |
#endif |
| 78 |
|
| 79 |
#if ALG_SHA256 |
| 80 |
{TPM_ALG_SHA256, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)}, |
| 81 |
#endif |
| 82 |
#if ALG_SHA384 |
| 83 |
{TPM_ALG_SHA384, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)}, |
| 84 |
#endif |
| 85 |
#if ALG_SHA512 |
| 86 |
{TPM_ALG_SHA512, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)}, |
| 87 |
#endif |
| 88 |
#if ALG_SM3_256 |
| 89 |
{TPM_ALG_SM3_256, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 0, 0)}, |
| 90 |
#endif |
| 91 |
#if ALG_SM4 |
| 92 |
{TPM_ALG_SM4, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)}, |
| 93 |
#endif |
| 94 |
#if ALG_RSASSA |
| 95 |
{TPM_ALG_RSASSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)}, |
| 96 |
#endif |
| 97 |
#if ALG_RSAES |
| 98 |
{TPM_ALG_RSAES, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 1, 0, 0)}, |
| 99 |
#endif |
| 100 |
#if ALG_RSAPSS |
| 101 |
{TPM_ALG_RSAPSS, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)}, |
| 102 |
#endif |
| 103 |
#if ALG_OAEP |
| 104 |
{TPM_ALG_OAEP, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 1, 0, 0)}, |
| 105 |
#endif |
| 106 |
#if ALG_ECDSA |
| 107 |
{TPM_ALG_ECDSA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 1, 0)}, |
| 108 |
#endif |
| 109 |
#if ALG_ECDH |
| 110 |
{TPM_ALG_ECDH, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 0, 1, 0)}, |
| 111 |
#endif |
| 112 |
#if ALG_ECDAA |
| 113 |
{TPM_ALG_ECDAA, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)}, |
| 114 |
#endif |
| 115 |
#if ALG_SM2 |
| 116 |
{TPM_ALG_SM2, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 1, 0)}, |
| 117 |
#endif |
| 118 |
#if ALG_ECSCHNORR |
| 119 |
{TPM_ALG_ECSCHNORR, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 1, 0, 0, 0)}, |
| 120 |
#endif |
| 121 |
#if ALG_ECMQV |
| 122 |
{TPM_ALG_ECMQV, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 0, 0, 0, 0, 1, 0)}, |
| 123 |
#endif |
| 124 |
#if ALG_KDF1_SP800_56A |
| 125 |
{TPM_ALG_KDF1_SP800_56A, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)}, |
| 126 |
#endif |
| 127 |
#if ALG_KDF2 |
| 128 |
{TPM_ALG_KDF2, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)}, |
| 129 |
#endif |
| 130 |
#if ALG_KDF1_SP800_108 |
| 131 |
{TPM_ALG_KDF1_SP800_108, TPMA_ALGORITHM_INITIALIZER(0, 0, 1, 0, 0, 0, 0, 1, 0)}, |
| 132 |
#endif |
| 133 |
#if ALG_ECC |
| 134 |
{TPM_ALG_ECC, TPMA_ALGORITHM_INITIALIZER(1, 0, 0, 1, 0, 0, 0, 0, 0)}, |
| 135 |
#endif |
| 136 |
|
| 137 |
{TPM_ALG_SYMCIPHER, TPMA_ALGORITHM_INITIALIZER(0, 0, 0, 1, 0, 0, 0, 0, 0)}, |
| 138 |
|
| 139 |
#if ALG_CAMELLIA |
| 140 |
{TPM_ALG_CAMELLIA, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 0, 0, 0)}, |
| 141 |
#endif |
| 142 |
#if ALG_CMAC |
| 143 |
{TPM_ALG_CMAC, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 1, 0, 0, 0)}, |
| 144 |
#endif |
| 145 |
#if ALG_CTR |
| 146 |
{TPM_ALG_CTR, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)}, |
| 147 |
#endif |
| 148 |
#if ALG_OFB |
| 149 |
{TPM_ALG_OFB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)}, |
| 150 |
#endif |
| 151 |
#if ALG_CBC |
| 152 |
{TPM_ALG_CBC, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)}, |
| 153 |
#endif |
| 154 |
#if ALG_CFB |
| 155 |
{TPM_ALG_CFB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)}, |
| 156 |
#endif |
| 157 |
#if ALG_ECB |
| 158 |
{TPM_ALG_ECB, TPMA_ALGORITHM_INITIALIZER(0, 1, 0, 0, 0, 0, 1, 0, 0)}, |
| 159 |
#endif |
| 160 |
}; |
| 161 |
|
| 162 |
//** AlgorithmCapGetImplemented() |
| 163 |
// This function is used by TPM2_GetCapability() to return a list of the |
| 164 |
// implemented algorithms. |
| 165 |
// Return Type: TPMI_YES_NO |
| 166 |
// YES more algorithms to report |
| 167 |
// NO no more algorithms to report |
| 168 |
TPMI_YES_NO |
| 169 |
AlgorithmCapGetImplemented( |
| 170 |
TPM_ALG_ID algID, // IN: the starting algorithm ID |
| 171 |
UINT32 count, // IN: count of returned algorithms |
| 172 |
TPML_ALG_PROPERTY *algList // OUT: algorithm list |
| 173 |
) |
| 174 |
{ |
| 175 |
TPMI_YES_NO more = NO; |
| 176 |
UINT32 i; |
| 177 |
UINT32 algNum; |
| 178 |
|
| 179 |
// initialize output algorithm list |
| 180 |
algList->count = 0; |
| 181 |
|
| 182 |
// The maximum count of algorithms we may return is MAX_CAP_ALGS. |
| 183 |
if(count > MAX_CAP_ALGS) |
| 184 |
count = MAX_CAP_ALGS; |
| 185 |
|
| 186 |
// Compute how many algorithms are defined in s_algorithms array. |
| 187 |
algNum = sizeof(s_algorithms) / sizeof(s_algorithms[0]); |
| 188 |
|
| 189 |
// Scan the implemented algorithm list to see if there is a match to 'algID'. |
| 190 |
for(i = 0; i < algNum; i++) |
| 191 |
{ |
| 192 |
// If algID is less than the starting algorithm ID, skip it |
| 193 |
if(s_algorithms[i].algID < algID) |
| 194 |
continue; |
| 195 |
if(algList->count < count) |
| 196 |
{ |
| 197 |
// If we have not filled up the return list, add more algorithms |
| 198 |
// to it |
| 199 |
algList->algProperties[algList->count].alg = s_algorithms[i].algID; |
| 200 |
algList->algProperties[algList->count].algProperties = |
| 201 |
s_algorithms[i].attributes; |
| 202 |
algList->count++; |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
// If the return list is full but we still have algorithms |
| 207 |
// available, report this and stop scanning. |
| 208 |
more = YES; |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return more; |
| 214 |
} |
| 215 |
|
| 216 |
//** AlgorithmGetImplementedVector() |
| 217 |
// This function returns the bit vector of the implemented algorithms. |
| 218 |
LIB_EXPORT |
| 219 |
void |
| 220 |
AlgorithmGetImplementedVector( |
| 221 |
ALGORITHM_VECTOR *implemented // OUT: the implemented bits are SET |
| 222 |
) |
| 223 |
{ |
| 224 |
int index; |
| 225 |
|
| 226 |
// Nothing implemented until we say it is |
| 227 |
MemorySet(implemented, 0, sizeof(ALGORITHM_VECTOR)); |
| 228 |
|
| 229 |
for(index = (sizeof(s_algorithms) / sizeof(s_algorithms[0])) - 1; |
| 230 |
index >= 0; |
| 231 |
index--) |
| 232 |
SET_BIT(s_algorithms[index].algID, *implemented); |
| 233 |
return; |
| 234 |
} |