| 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 |
// |
| 37 |
// This file contains implementation of cryptographic functions for hashing. |
| 38 |
// |
| 39 |
//** Includes, Defines, and Types |
| 40 |
|
| 41 |
#define _CRYPT_HASH_C_ |
| 42 |
#include "Tpm.h" |
| 43 |
#include "CryptHash_fp.h" |
| 44 |
#include "CryptHash.h" |
| 45 |
#include "OIDs.h" |
| 46 |
|
| 47 |
#define HASH_TABLE_SIZE (HASH_COUNT + 1) |
| 48 |
|
| 49 |
|
| 50 |
#if ALG_SHA1 |
| 51 |
HASH_DEF_TEMPLATE(SHA1, Sha1); |
| 52 |
#endif |
| 53 |
#if ALG_SHA256 |
| 54 |
HASH_DEF_TEMPLATE(SHA256, Sha256); |
| 55 |
#endif |
| 56 |
#if ALG_SHA384 |
| 57 |
HASH_DEF_TEMPLATE(SHA384, Sha384); |
| 58 |
#endif |
| 59 |
#if ALG_SHA512 |
| 60 |
HASH_DEF_TEMPLATE(SHA512, Sha512); |
| 61 |
#endif |
| 62 |
#if ALG_SM3_256 |
| 63 |
HASH_DEF_TEMPLATE(SM3_256, Sm3_256); |
| 64 |
#endif |
| 65 |
HASH_DEF NULL_Def = {{0}}; |
| 66 |
|
| 67 |
PHASH_DEF HashDefArray[] = { |
| 68 |
#if ALG_SHA1 |
| 69 |
&Sha1_Def, |
| 70 |
#endif |
| 71 |
#if ALG_SHA256 |
| 72 |
&Sha256_Def, |
| 73 |
#endif |
| 74 |
#if ALG_SHA384 |
| 75 |
&Sha384_Def, |
| 76 |
#endif |
| 77 |
#if ALG_SHA512 |
| 78 |
&Sha512_Def, |
| 79 |
#endif |
| 80 |
#if ALG_SM3_256 |
| 81 |
&Sm3_256_Def, |
| 82 |
#endif |
| 83 |
&NULL_Def |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
//** Obligatory Initialization Functions |
| 88 |
|
| 89 |
//*** CryptHashInit() |
| 90 |
// This function is called by _TPM_Init do perform the initialization operations for |
| 91 |
// the library. |
| 92 |
BOOL |
| 93 |
CryptHashInit( |
| 94 |
void |
| 95 |
) |
| 96 |
{ |
| 97 |
LibHashInit(); |
| 98 |
return TRUE; |
| 99 |
} |
| 100 |
|
| 101 |
//*** CryptHashStartup() |
| 102 |
// This function is called by TPM2_Startup(). It checks that the size of the |
| 103 |
// HashDefArray is consistent with the HASH_COUNT. |
| 104 |
BOOL |
| 105 |
CryptHashStartup( |
| 106 |
void |
| 107 |
) |
| 108 |
{ |
| 109 |
int i = sizeof(HashDefArray) / sizeof(PHASH_DEF) - 1; |
| 110 |
return (i == HASH_COUNT); |
| 111 |
} |
| 112 |
|
| 113 |
//** Hash Information Access Functions |
| 114 |
//*** Introduction |
| 115 |
// These functions provide access to the hash algorithm description information. |
| 116 |
|
| 117 |
//*** CryptGetHashDef() |
| 118 |
// This function accesses the hash descriptor associated with a hash a |
| 119 |
// algorithm. The function returns a pointer to a 'null' descriptor if hashAlg is |
| 120 |
// TPM_ALG_NULL or not a defined algorithm. |
| 121 |
PHASH_DEF |
| 122 |
CryptGetHashDef( |
| 123 |
TPM_ALG_ID hashAlg |
| 124 |
) |
| 125 |
{ |
| 126 |
size_t i; |
| 127 |
#define HASHES (sizeof(HashDefArray) / sizeof(PHASH_DEF)) |
| 128 |
for(i = 0; i < HASHES; i++) |
| 129 |
{ |
| 130 |
PHASH_DEF p = HashDefArray[i]; |
| 131 |
if(p->hashAlg == hashAlg) |
| 132 |
return p; |
| 133 |
} |
| 134 |
return &NULL_Def; |
| 135 |
} |
| 136 |
|
| 137 |
//*** CryptHashIsValidAlg() |
| 138 |
// This function tests to see if an algorithm ID is a valid hash algorithm. If |
| 139 |
// flag is true, then TPM_ALG_NULL is a valid hash. |
| 140 |
// Return Type: BOOL |
| 141 |
// TRUE(1) hashAlg is a valid, implemented hash on this TPM |
| 142 |
// FALSE(0) hashAlg is not valid for this TPM |
| 143 |
BOOL |
| 144 |
CryptHashIsValidAlg( |
| 145 |
TPM_ALG_ID hashAlg, // IN: the algorithm to check |
| 146 |
BOOL flag // IN: TRUE if TPM_ALG_NULL is to be treated |
| 147 |
// as a valid hash |
| 148 |
) |
| 149 |
{ |
| 150 |
if(hashAlg == TPM_ALG_NULL) |
| 151 |
return flag; |
| 152 |
return CryptGetHashDef(hashAlg) != &NULL_Def; |
| 153 |
} |
| 154 |
|
| 155 |
//*** CryptHashGetAlgByIndex() |
| 156 |
// This function is used to iterate through the hashes. TPM_ALG_NULL |
| 157 |
// is returned for all indexes that are not valid hashes. |
| 158 |
// If the TPM implements 3 hashes, then an 'index' value of 0 will |
| 159 |
// return the first implemented hash and an 'index' of 2 will return the |
| 160 |
// last. All other index values will return TPM_ALG_NULL. |
| 161 |
// |
| 162 |
// Return Type: TPM_ALG_ID |
| 163 |
// TPM_ALG_xxx a hash algorithm |
| 164 |
// TPM_ALG_NULL this can be used as a stop value |
| 165 |
LIB_EXPORT TPM_ALG_ID |
| 166 |
CryptHashGetAlgByIndex( |
| 167 |
UINT32 index // IN: the index |
| 168 |
) |
| 169 |
{ |
| 170 |
TPM_ALG_ID hashAlg; |
| 171 |
if(index >= HASH_COUNT) |
| 172 |
hashAlg = TPM_ALG_NULL; |
| 173 |
else |
| 174 |
hashAlg = HashDefArray[index]->hashAlg; |
| 175 |
return hashAlg; |
| 176 |
} |
| 177 |
|
| 178 |
//*** CryptHashGetDigestSize() |
| 179 |
// Returns the size of the digest produced by the hash. If 'hashAlg' is not a hash |
| 180 |
// algorithm, the TPM will FAIL. |
| 181 |
// Return Type: UINT16 |
| 182 |
// 0 TPM_ALG_NULL |
| 183 |
// > 0 the digest size |
| 184 |
// |
| 185 |
LIB_EXPORT UINT16 |
| 186 |
CryptHashGetDigestSize( |
| 187 |
TPM_ALG_ID hashAlg // IN: hash algorithm to look up |
| 188 |
) |
| 189 |
{ |
| 190 |
return CryptGetHashDef(hashAlg)->digestSize; |
| 191 |
} |
| 192 |
|
| 193 |
//*** CryptHashGetBlockSize() |
| 194 |
// Returns the size of the block used by the hash. If 'hashAlg' is not a hash |
| 195 |
// algorithm, the TPM will FAIL. |
| 196 |
// Return Type: UINT16 |
| 197 |
// 0 TPM_ALG_NULL |
| 198 |
// > 0 the digest size |
| 199 |
// |
| 200 |
LIB_EXPORT UINT16 |
| 201 |
CryptHashGetBlockSize( |
| 202 |
TPM_ALG_ID hashAlg // IN: hash algorithm to look up |
| 203 |
) |
| 204 |
{ |
| 205 |
return CryptGetHashDef(hashAlg)->blockSize; |
| 206 |
} |
| 207 |
|
| 208 |
//*** CryptHashGetOid() |
| 209 |
// This function returns a pointer to DER=encoded OID for a hash algorithm. All OIDs |
| 210 |
// are full OID values including the Tag (0x06) and length byte. |
| 211 |
LIB_EXPORT const BYTE * |
| 212 |
CryptHashGetOid( |
| 213 |
TPM_ALG_ID hashAlg |
| 214 |
) |
| 215 |
{ |
| 216 |
return CryptGetHashDef(hashAlg)->OID; |
| 217 |
} |
| 218 |
|
| 219 |
//*** CryptHashGetContextAlg() |
| 220 |
// This function returns the hash algorithm associated with a hash context. |
| 221 |
TPM_ALG_ID |
| 222 |
CryptHashGetContextAlg( |
| 223 |
PHASH_STATE state // IN: the context to check |
| 224 |
) |
| 225 |
{ |
| 226 |
return state->hashAlg; |
| 227 |
} |
| 228 |
|
| 229 |
//** State Import and Export |
| 230 |
|
| 231 |
//*** CryptHashCopyState |
| 232 |
// This function is used to clone a HASH_STATE. |
| 233 |
LIB_EXPORT void |
| 234 |
CryptHashCopyState( |
| 235 |
HASH_STATE *out, // OUT: destination of the state |
| 236 |
const HASH_STATE *in // IN: source of the state |
| 237 |
) |
| 238 |
{ |
| 239 |
pAssert(out->type == in->type); |
| 240 |
out->hashAlg = in->hashAlg; |
| 241 |
out->def = in->def; |
| 242 |
if(in->hashAlg != TPM_ALG_NULL) |
| 243 |
{ |
| 244 |
HASH_STATE_COPY(out, in); |
| 245 |
} |
| 246 |
if(in->type == HASH_STATE_HMAC) |
| 247 |
{ |
| 248 |
const HMAC_STATE *hIn = (HMAC_STATE *)in; |
| 249 |
HMAC_STATE *hOut = (HMAC_STATE *)out; |
| 250 |
hOut->hmacKey = hIn->hmacKey; |
| 251 |
} |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
//*** CryptHashExportState() |
| 256 |
// This function is used to export a hash or HMAC hash state. This function |
| 257 |
// would be called when preparing to context save a sequence object. |
| 258 |
void |
| 259 |
CryptHashExportState( |
| 260 |
PCHASH_STATE internalFmt, // IN: the hash state formatted for use by |
| 261 |
// library |
| 262 |
PEXPORT_HASH_STATE externalFmt // OUT: the exported hash state |
| 263 |
) |
| 264 |
{ |
| 265 |
BYTE *outBuf = (BYTE *)externalFmt; |
| 266 |
// |
| 267 |
cAssert(sizeof(HASH_STATE) <= sizeof(EXPORT_HASH_STATE)); |
| 268 |
// the following #define is used to move data from an aligned internal data |
| 269 |
// structure to a byte buffer (external format data. |
| 270 |
#define CopyToOffset(value) \ |
| 271 |
memcpy(&outBuf[offsetof(HASH_STATE,value)], &internalFmt->value, \ |
| 272 |
sizeof(internalFmt->value)) |
| 273 |
// Copy the hashAlg |
| 274 |
CopyToOffset(hashAlg); |
| 275 |
CopyToOffset(type); |
| 276 |
#ifdef HASH_STATE_SMAC |
| 277 |
if(internalFmt->type == HASH_STATE_SMAC) |
| 278 |
{ |
| 279 |
memcpy(outBuf, internalFmt, sizeof(HASH_STATE)); |
| 280 |
return; |
| 281 |
|
| 282 |
} |
| 283 |
#endif |
| 284 |
if(internalFmt->type == HASH_STATE_HMAC) |
| 285 |
{ |
| 286 |
HMAC_STATE *from = (HMAC_STATE *)internalFmt; |
| 287 |
memcpy(&outBuf[offsetof(HMAC_STATE, hmacKey)], &from->hmacKey, |
| 288 |
sizeof(from->hmacKey)); |
| 289 |
} |
| 290 |
if(internalFmt->hashAlg != TPM_ALG_NULL) |
| 291 |
HASH_STATE_EXPORT(externalFmt, internalFmt); |
| 292 |
} |
| 293 |
|
| 294 |
//*** CryptHashImportState() |
| 295 |
// This function is used to import the hash state. This function |
| 296 |
// would be called to import a hash state when the context of a sequence object |
| 297 |
// was being loaded. |
| 298 |
void |
| 299 |
CryptHashImportState( |
| 300 |
PHASH_STATE internalFmt, // OUT: the hash state formatted for use by |
| 301 |
// the library |
| 302 |
PCEXPORT_HASH_STATE externalFmt // IN: the exported hash state |
| 303 |
) |
| 304 |
{ |
| 305 |
BYTE *inBuf = (BYTE *)externalFmt; |
| 306 |
// |
| 307 |
#define CopyFromOffset(value) \ |
| 308 |
memcpy(&internalFmt->value, &inBuf[offsetof(HASH_STATE,value)], \ |
| 309 |
sizeof(internalFmt->value)) |
| 310 |
|
| 311 |
// Copy the hashAlg of the byte-aligned input structure to the structure-aligned |
| 312 |
// internal structure. |
| 313 |
CopyFromOffset(hashAlg); |
| 314 |
CopyFromOffset(type); |
| 315 |
if(internalFmt->hashAlg != TPM_ALG_NULL) |
| 316 |
{ |
| 317 |
#ifdef HASH_STATE_SMAC |
| 318 |
if(internalFmt->type == HASH_STATE_SMAC) |
| 319 |
{ |
| 320 |
memcpy(internalFmt, inBuf, sizeof(HASH_STATE)); |
| 321 |
return; |
| 322 |
} |
| 323 |
#endif |
| 324 |
internalFmt->def = CryptGetHashDef(internalFmt->hashAlg); |
| 325 |
HASH_STATE_IMPORT(internalFmt, inBuf); |
| 326 |
if(internalFmt->type == HASH_STATE_HMAC) |
| 327 |
{ |
| 328 |
HMAC_STATE *to = (HMAC_STATE *)internalFmt; |
| 329 |
memcpy(&to->hmacKey, &inBuf[offsetof(HMAC_STATE, hmacKey)], |
| 330 |
sizeof(to->hmacKey)); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
//** State Modification Functions |
| 336 |
|
| 337 |
//***HashEnd() |
| 338 |
// Local function to complete a hash that uses the hashDef instead of an algorithm |
| 339 |
// ID. This function is used to complete the hash and only return a partial digest. |
| 340 |
// The return value is the size of the data copied. |
| 341 |
static UINT16 |
| 342 |
HashEnd( |
| 343 |
PHASH_STATE hashState, // IN: the hash state |
| 344 |
UINT32 dOutSize, // IN: the size of receive buffer |
| 345 |
PBYTE dOut // OUT: the receive buffer |
| 346 |
) |
| 347 |
{ |
| 348 |
BYTE temp[MAX_DIGEST_SIZE]; |
| 349 |
if((hashState->hashAlg == TPM_ALG_NULL) |
| 350 |
|| (hashState->type != HASH_STATE_HASH)) |
| 351 |
dOutSize = 0; |
| 352 |
if(dOutSize > 0) |
| 353 |
{ |
| 354 |
hashState->def = CryptGetHashDef(hashState->hashAlg); |
| 355 |
// Set the final size |
| 356 |
dOutSize = MIN(dOutSize, hashState->def->digestSize); |
| 357 |
// Complete into the temp buffer and then copy |
| 358 |
HASH_END(hashState, temp); |
| 359 |
// Don't want any other functions calling the HASH_END method |
| 360 |
// directly. |
| 361 |
#undef HASH_END |
| 362 |
memcpy(dOut, &temp, dOutSize); |
| 363 |
} |
| 364 |
hashState->type = HASH_STATE_EMPTY; |
| 365 |
return (UINT16)dOutSize; |
| 366 |
} |
| 367 |
|
| 368 |
//*** CryptHashStart() |
| 369 |
// Functions starts a hash stack |
| 370 |
// Start a hash stack and returns the digest size. As a side effect, the |
| 371 |
// value of 'stateSize' in hashState is updated to indicate the number of bytes |
| 372 |
// of state that were saved. This function calls GetHashServer() and that function |
| 373 |
// will put the TPM into failure mode if the hash algorithm is not supported. |
| 374 |
// |
| 375 |
// This function does not use the sequence parameter. If it is necessary to import |
| 376 |
// or export context, this will start the sequence in a local state |
| 377 |
// and export the state to the input buffer. Will need to add a flag to the state |
| 378 |
// structure to indicate that it needs to be imported before it can be used. |
| 379 |
// (BLEH). |
| 380 |
// Return Type: UINT16 |
| 381 |
// 0 hash is TPM_ALG_NULL |
| 382 |
// >0 digest size |
| 383 |
LIB_EXPORT UINT16 |
| 384 |
CryptHashStart( |
| 385 |
PHASH_STATE hashState, // OUT: the running hash state |
| 386 |
TPM_ALG_ID hashAlg // IN: hash algorithm |
| 387 |
) |
| 388 |
{ |
| 389 |
UINT16 retVal; |
| 390 |
|
| 391 |
TEST(hashAlg); |
| 392 |
|
| 393 |
hashState->hashAlg = hashAlg; |
| 394 |
if(hashAlg == TPM_ALG_NULL) |
| 395 |
{ |
| 396 |
retVal = 0; |
| 397 |
} |
| 398 |
else |
| 399 |
{ |
| 400 |
hashState->def = CryptGetHashDef(hashAlg); |
| 401 |
HASH_START(hashState); |
| 402 |
retVal = hashState->def->digestSize; |
| 403 |
} |
| 404 |
#undef HASH_START |
| 405 |
hashState->type = HASH_STATE_HASH; |
| 406 |
return retVal; |
| 407 |
} |
| 408 |
|
| 409 |
//*** CryptDigestUpdate() |
| 410 |
// Add data to a hash or HMAC, SMAC stack. |
| 411 |
// |
| 412 |
void |
| 413 |
CryptDigestUpdate( |
| 414 |
PHASH_STATE hashState, // IN: the hash context information |
| 415 |
UINT32 dataSize, // IN: the size of data to be added |
| 416 |
const BYTE *data // IN: data to be hashed |
| 417 |
) |
| 418 |
{ |
| 419 |
if(hashState->hashAlg != TPM_ALG_NULL) |
| 420 |
{ |
| 421 |
if((hashState->type == HASH_STATE_HASH) |
| 422 |
|| (hashState->type == HASH_STATE_HMAC)) |
| 423 |
HASH_DATA(hashState, dataSize, (BYTE *)data); |
| 424 |
#if SMAC_IMPLEMENTED |
| 425 |
else if(hashState->type == HASH_STATE_SMAC) |
| 426 |
(hashState->state.smac.smacMethods.data)(&hashState->state.smac.state, |
| 427 |
dataSize, data); |
| 428 |
#endif // SMAC_IMPLEMENTED |
| 429 |
else |
| 430 |
FAIL(FATAL_ERROR_INTERNAL); |
| 431 |
} |
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
//*** CryptHashEnd() |
| 436 |
// Complete a hash or HMAC computation. This function will place the smaller of |
| 437 |
// 'digestSize' or the size of the digest in 'dOut'. The number of bytes in the |
| 438 |
// placed in the buffer is returned. If there is a failure, the returned value |
| 439 |
// is <= 0. |
| 440 |
// Return Type: UINT16 |
| 441 |
// 0 no data returned |
| 442 |
// > 0 the number of bytes in the digest or dOutSize, whichever is smaller |
| 443 |
LIB_EXPORT UINT16 |
| 444 |
CryptHashEnd( |
| 445 |
PHASH_STATE hashState, // IN: the state of hash stack |
| 446 |
UINT32 dOutSize, // IN: size of digest buffer |
| 447 |
BYTE *dOut // OUT: hash digest |
| 448 |
) |
| 449 |
{ |
| 450 |
pAssert(hashState->type == HASH_STATE_HASH); |
| 451 |
return HashEnd(hashState, dOutSize, dOut); |
| 452 |
} |
| 453 |
|
| 454 |
//*** CryptHashBlock() |
| 455 |
// Start a hash, hash a single block, update 'digest' and return the size of |
| 456 |
// the results. |
| 457 |
// |
| 458 |
// The 'digestSize' parameter can be smaller than the digest. If so, only the more |
| 459 |
// significant bytes are returned. |
| 460 |
// Return Type: UINT16 |
| 461 |
// >= 0 number of bytes placed in 'dOut' |
| 462 |
LIB_EXPORT UINT16 |
| 463 |
CryptHashBlock( |
| 464 |
TPM_ALG_ID hashAlg, // IN: The hash algorithm |
| 465 |
UINT32 dataSize, // IN: size of buffer to hash |
| 466 |
const BYTE *data, // IN: the buffer to hash |
| 467 |
UINT32 dOutSize, // IN: size of the digest buffer |
| 468 |
BYTE *dOut // OUT: digest buffer |
| 469 |
) |
| 470 |
{ |
| 471 |
HASH_STATE state; |
| 472 |
CryptHashStart(&state, hashAlg); |
| 473 |
CryptDigestUpdate(&state, dataSize, data); |
| 474 |
return HashEnd(&state, dOutSize, dOut); |
| 475 |
} |
| 476 |
|
| 477 |
//*** CryptDigestUpdate2B() |
| 478 |
// This function updates a digest (hash or HMAC) with a TPM2B. |
| 479 |
// |
| 480 |
// This function can be used for both HMAC and hash functions so the |
| 481 |
// 'digestState' is void so that either state type can be passed. |
| 482 |
LIB_EXPORT void |
| 483 |
CryptDigestUpdate2B( |
| 484 |
PHASH_STATE state, // IN: the digest state |
| 485 |
const TPM2B *bIn // IN: 2B containing the data |
| 486 |
) |
| 487 |
{ |
| 488 |
// Only compute the digest if a pointer to the 2B is provided. |
| 489 |
// In CryptDigestUpdate(), if size is zero or buffer is NULL, then no change |
| 490 |
// to the digest occurs. This function should not provide a buffer if bIn is |
| 491 |
// not provided. |
| 492 |
pAssert(bIn != NULL); |
| 493 |
CryptDigestUpdate(state, bIn->size, bIn->buffer); |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
//*** CryptHashEnd2B() |
| 498 |
// This function is the same as CryptCompleteHash() but the digest is |
| 499 |
// placed in a TPM2B. This is the most common use and this is provided |
| 500 |
// for specification clarity. 'digest.size' should be set to indicate the number of |
| 501 |
// bytes to place in the buffer |
| 502 |
// Return Type: UINT16 |
| 503 |
// >=0 the number of bytes placed in 'digest.buffer' |
| 504 |
LIB_EXPORT UINT16 |
| 505 |
CryptHashEnd2B( |
| 506 |
PHASH_STATE state, // IN: the hash state |
| 507 |
P2B digest // IN: the size of the buffer Out: requested |
| 508 |
// number of bytes |
| 509 |
) |
| 510 |
{ |
| 511 |
return CryptHashEnd(state, digest->size, digest->buffer); |
| 512 |
} |
| 513 |
|
| 514 |
//*** CryptDigestUpdateInt() |
| 515 |
// This function is used to include an integer value to a hash stack. The function |
| 516 |
// marshals the integer into its canonical form before calling CryptDigestUpdate(). |
| 517 |
LIB_EXPORT void |
| 518 |
CryptDigestUpdateInt( |
| 519 |
void *state, // IN: the state of hash stack |
| 520 |
UINT32 intSize, // IN: the size of 'intValue' in bytes |
| 521 |
UINT64 intValue // IN: integer value to be hashed |
| 522 |
) |
| 523 |
{ |
| 524 |
#if LITTLE_ENDIAN_TPM |
| 525 |
intValue = REVERSE_ENDIAN_64(intValue); |
| 526 |
#endif |
| 527 |
CryptDigestUpdate(state, intSize, &((BYTE *)&intValue)[8 - intSize]); |
| 528 |
} |
| 529 |
|
| 530 |
//** HMAC Functions |
| 531 |
|
| 532 |
//*** CryptHmacStart() |
| 533 |
// This function is used to start an HMAC using a temp |
| 534 |
// hash context. The function does the initialization |
| 535 |
// of the hash with the HMAC key XOR iPad and updates the |
| 536 |
// HMAC key XOR oPad. |
| 537 |
// |
| 538 |
// The function returns the number of bytes in a digest produced by 'hashAlg'. |
| 539 |
// Return Type: UINT16 |
| 540 |
// >= 0 number of bytes in digest produced by 'hashAlg' (may be zero) |
| 541 |
// |
| 542 |
LIB_EXPORT UINT16 |
| 543 |
CryptHmacStart( |
| 544 |
PHMAC_STATE state, // IN/OUT: the state buffer |
| 545 |
TPM_ALG_ID hashAlg, // IN: the algorithm to use |
| 546 |
UINT16 keySize, // IN: the size of the HMAC key |
| 547 |
const BYTE *key // IN: the HMAC key |
| 548 |
) |
| 549 |
{ |
| 550 |
PHASH_DEF hashDef; |
| 551 |
BYTE * pb; |
| 552 |
UINT32 i; |
| 553 |
// |
| 554 |
hashDef = CryptGetHashDef(hashAlg); |
| 555 |
if(hashDef->digestSize != 0) |
| 556 |
{ |
| 557 |
// If the HMAC key is larger than the hash block size, it has to be reduced |
| 558 |
// to fit. The reduction is a digest of the hashKey. |
| 559 |
if(keySize > hashDef->blockSize) |
| 560 |
{ |
| 561 |
// if the key is too big, reduce it to a digest of itself |
| 562 |
state->hmacKey.t.size = CryptHashBlock(hashAlg, keySize, key, |
| 563 |
hashDef->digestSize, |
| 564 |
state->hmacKey.t.buffer); |
| 565 |
} |
| 566 |
else |
| 567 |
{ |
| 568 |
memcpy(state->hmacKey.t.buffer, key, keySize); |
| 569 |
state->hmacKey.t.size = keySize; |
| 570 |
} |
| 571 |
// XOR the key with iPad (0x36) |
| 572 |
pb = state->hmacKey.t.buffer; |
| 573 |
for(i = state->hmacKey.t.size; i > 0; i--) |
| 574 |
*pb++ ^= 0x36; |
| 575 |
|
| 576 |
// if the keySize is smaller than a block, fill the rest with 0x36 |
| 577 |
for(i = hashDef->blockSize - state->hmacKey.t.size; i > 0; i--) |
| 578 |
*pb++ = 0x36; |
| 579 |
|
| 580 |
// Increase the oPadSize to a full block |
| 581 |
state->hmacKey.t.size = hashDef->blockSize; |
| 582 |
|
| 583 |
// Start a new hash with the HMAC key |
| 584 |
// This will go in the caller's state structure and may be a sequence or not |
| 585 |
CryptHashStart((PHASH_STATE)state, hashAlg); |
| 586 |
CryptDigestUpdate((PHASH_STATE)state, state->hmacKey.t.size, |
| 587 |
state->hmacKey.t.buffer); |
| 588 |
// XOR the key block with 0x5c ^ 0x36 |
| 589 |
for(pb = state->hmacKey.t.buffer, i = hashDef->blockSize; i > 0; i--) |
| 590 |
*pb++ ^= (0x5c ^ 0x36); |
| 591 |
} |
| 592 |
// Set the hash algorithm |
| 593 |
state->hashState.hashAlg = hashAlg; |
| 594 |
// Set the hash state type |
| 595 |
state->hashState.type = HASH_STATE_HMAC; |
| 596 |
|
| 597 |
return hashDef->digestSize; |
| 598 |
} |
| 599 |
|
| 600 |
//*** CryptHmacEnd() |
| 601 |
// This function is called to complete an HMAC. It will finish the current |
| 602 |
// digest, and start a new digest. It will then add the oPadKey and the |
| 603 |
// completed digest and return the results in dOut. It will not return more |
| 604 |
// than dOutSize bytes. |
| 605 |
// Return Type: UINT16 |
| 606 |
// >= 0 number of bytes in 'dOut' (may be zero) |
| 607 |
LIB_EXPORT UINT16 |
| 608 |
CryptHmacEnd( |
| 609 |
PHMAC_STATE state, // IN: the hash state buffer |
| 610 |
UINT32 dOutSize, // IN: size of digest buffer |
| 611 |
BYTE *dOut // OUT: hash digest |
| 612 |
) |
| 613 |
{ |
| 614 |
BYTE temp[MAX_DIGEST_SIZE]; |
| 615 |
PHASH_STATE hState = (PHASH_STATE)&state->hashState; |
| 616 |
|
| 617 |
#if SMAC_IMPLEMENTED |
| 618 |
if(hState->type == HASH_STATE_SMAC) |
| 619 |
return (state->hashState.state.smac.smacMethods.end) |
| 620 |
(&state->hashState.state.smac.state, |
| 621 |
dOutSize, |
| 622 |
dOut); |
| 623 |
#endif |
| 624 |
pAssert(hState->type == HASH_STATE_HMAC); |
| 625 |
hState->def = CryptGetHashDef(hState->hashAlg); |
| 626 |
// Change the state type for completion processing |
| 627 |
hState->type = HASH_STATE_HASH; |
| 628 |
if(hState->hashAlg == TPM_ALG_NULL) |
| 629 |
dOutSize = 0; |
| 630 |
else |
| 631 |
{ |
| 632 |
|
| 633 |
// Complete the current hash |
| 634 |
HashEnd(hState, hState->def->digestSize, temp); |
| 635 |
// Do another hash starting with the oPad |
| 636 |
CryptHashStart(hState, hState->hashAlg); |
| 637 |
CryptDigestUpdate(hState, state->hmacKey.t.size, state->hmacKey.t.buffer); |
| 638 |
CryptDigestUpdate(hState, hState->def->digestSize, temp); |
| 639 |
} |
| 640 |
return HashEnd(hState, dOutSize, dOut); |
| 641 |
} |
| 642 |
|
| 643 |
//*** CryptHmacStart2B() |
| 644 |
// This function starts an HMAC and returns the size of the digest |
| 645 |
// that will be produced. |
| 646 |
// |
| 647 |
// This function is provided to support the most common use of starting an HMAC |
| 648 |
// with a TPM2B key. |
| 649 |
// |
| 650 |
// The caller must provide a block of memory in which the hash sequence state |
| 651 |
// is kept. The caller should not alter the contents of this buffer until the |
| 652 |
// hash sequence is completed or abandoned. |
| 653 |
// |
| 654 |
// Return Type: UINT16 |
| 655 |
// > 0 the digest size of the algorithm |
| 656 |
// = 0 the hashAlg was TPM_ALG_NULL |
| 657 |
LIB_EXPORT UINT16 |
| 658 |
CryptHmacStart2B( |
| 659 |
PHMAC_STATE hmacState, // OUT: the state of HMAC stack. It will be used |
| 660 |
// in HMAC update and completion |
| 661 |
TPMI_ALG_HASH hashAlg, // IN: hash algorithm |
| 662 |
P2B key // IN: HMAC key |
| 663 |
) |
| 664 |
{ |
| 665 |
return CryptHmacStart(hmacState, hashAlg, key->size, key->buffer); |
| 666 |
} |
| 667 |
|
| 668 |
//*** CryptHmacEnd2B() |
| 669 |
// This function is the same as CryptHmacEnd() but the HMAC result |
| 670 |
// is returned in a TPM2B which is the most common use. |
| 671 |
// Return Type: UINT16 |
| 672 |
// >=0 the number of bytes placed in 'digest' |
| 673 |
LIB_EXPORT UINT16 |
| 674 |
CryptHmacEnd2B( |
| 675 |
PHMAC_STATE hmacState, // IN: the state of HMAC stack |
| 676 |
P2B digest // OUT: HMAC |
| 677 |
) |
| 678 |
{ |
| 679 |
return CryptHmacEnd(hmacState, digest->size, digest->buffer); |
| 680 |
} |
| 681 |
|
| 682 |
//** Mask and Key Generation Functions |
| 683 |
//*** CryptMGF1() |
| 684 |
// This function performs MGF1 using the selected hash. MGF1 is |
| 685 |
// T(n) = T(n-1) || H(seed || counter). |
| 686 |
// This function returns the length of the mask produced which |
| 687 |
// could be zero if the digest algorithm is not supported |
| 688 |
// Return Type: UINT16 |
| 689 |
// 0 hash algorithm was TPM_ALG_NULL |
| 690 |
// > 0 should be the same as 'mSize' |
| 691 |
LIB_EXPORT UINT16 |
| 692 |
CryptMGF1( |
| 693 |
UINT32 mSize, // IN: length of the mask to be produced |
| 694 |
BYTE *mask, // OUT: buffer to receive the mask |
| 695 |
TPM_ALG_ID hashAlg, // IN: hash to use |
| 696 |
UINT32 seedSize, // IN: size of the seed |
| 697 |
BYTE *seed // IN: seed size |
| 698 |
) |
| 699 |
{ |
| 700 |
HASH_STATE hashState; |
| 701 |
PHASH_DEF hDef = CryptGetHashDef(hashAlg); |
| 702 |
UINT32 remaining; |
| 703 |
UINT32 counter = 0; |
| 704 |
BYTE swappedCounter[4]; |
| 705 |
|
| 706 |
// If there is no digest to compute return |
| 707 |
if((hashAlg == TPM_ALG_NULL) || (mSize == 0)) |
| 708 |
return 0; |
| 709 |
|
| 710 |
for(remaining = mSize; ; remaining -= hDef->digestSize) |
| 711 |
{ |
| 712 |
// Because the system may be either Endian... |
| 713 |
UINT32_TO_BYTE_ARRAY(counter, swappedCounter); |
| 714 |
|
| 715 |
// Start the hash and include the seed and counter |
| 716 |
CryptHashStart(&hashState, hashAlg); |
| 717 |
CryptDigestUpdate(&hashState, seedSize, seed); |
| 718 |
CryptDigestUpdate(&hashState, 4, swappedCounter); |
| 719 |
|
| 720 |
// Handling the completion depends on how much space remains in the mask |
| 721 |
// buffer. If it can hold the entire digest, put it there. If not |
| 722 |
// put the digest in a temp buffer and only copy the amount that |
| 723 |
// will fit into the mask buffer. |
| 724 |
HashEnd(&hashState, remaining, mask); |
| 725 |
if(remaining <= hDef->digestSize) |
| 726 |
break; |
| 727 |
mask = &mask[hDef->digestSize]; |
| 728 |
counter++; |
| 729 |
} |
| 730 |
return (UINT16)mSize; |
| 731 |
} |
| 732 |
|
| 733 |
//*** CryptKDFa() |
| 734 |
// This function performs the key generation according to Part 1 of the |
| 735 |
// TPM specification. |
| 736 |
// |
| 737 |
// This function returns the number of bytes generated which may be zero. |
| 738 |
// |
| 739 |
// The 'key' and 'keyStream' pointers are not allowed to be NULL. The other |
| 740 |
// pointer values may be NULL. The value of 'sizeInBits' must be no larger |
| 741 |
// than (2^18)-1 = 256K bits (32385 bytes). |
| 742 |
// |
| 743 |
// The 'once' parameter is set to allow incremental generation of a large |
| 744 |
// value. If this flag is TRUE, 'sizeInBits' will be used in the HMAC computation |
| 745 |
// but only one iteration of the KDF is performed. This would be used for |
| 746 |
// XOR obfuscation so that the mask value can be generated in digest-sized |
| 747 |
// chunks rather than having to be generated all at once in an arbitrarily |
| 748 |
// large buffer and then XORed into the result. If 'once' is TRUE, then |
| 749 |
// 'sizeInBits' must be a multiple of 8. |
| 750 |
// |
| 751 |
// Any error in the processing of this command is considered fatal. |
| 752 |
// Return Type: UINT16 |
| 753 |
// 0 hash algorithm is not supported or is TPM_ALG_NULL |
| 754 |
// > 0 the number of bytes in the 'keyStream' buffer |
| 755 |
LIB_EXPORT UINT16 |
| 756 |
CryptKDFa( |
| 757 |
TPM_ALG_ID hashAlg, // IN: hash algorithm used in HMAC |
| 758 |
const TPM2B *key, // IN: HMAC key |
| 759 |
const TPM2B *label, // IN: a label for the KDF |
| 760 |
const TPM2B *contextU, // IN: context U |
| 761 |
const TPM2B *contextV, // IN: context V |
| 762 |
UINT32 sizeInBits, // IN: size of generated key in bits |
| 763 |
BYTE *keyStream, // OUT: key buffer |
| 764 |
UINT32 *counterInOut, // IN/OUT: caller may provide the iteration |
| 765 |
// counter for incremental operations to |
| 766 |
// avoid large intermediate buffers. |
| 767 |
UINT16 blocks // IN: If non-zero, this is the maximum number |
| 768 |
// of blocks to be returned, regardless |
| 769 |
// of sizeInBits |
| 770 |
) |
| 771 |
{ |
| 772 |
UINT32 counter = 0; // counter value |
| 773 |
INT16 bytes; // number of bytes to produce |
| 774 |
UINT16 generated; // number of bytes generated |
| 775 |
BYTE *stream = keyStream; |
| 776 |
HMAC_STATE hState; |
| 777 |
UINT16 digestSize = CryptHashGetDigestSize(hashAlg); |
| 778 |
|
| 779 |
pAssert(key != NULL && keyStream != NULL); |
| 780 |
|
| 781 |
TEST(TPM_ALG_KDF1_SP800_108); |
| 782 |
|
| 783 |
if(digestSize == 0) |
| 784 |
return 0; |
| 785 |
|
| 786 |
if(counterInOut != NULL) |
| 787 |
counter = *counterInOut; |
| 788 |
|
| 789 |
// If the size of the request is larger than the numbers will handle, |
| 790 |
// it is a fatal error. |
| 791 |
pAssert(((sizeInBits + 7) / 8) <= INT16_MAX); |
| 792 |
|
| 793 |
// The number of bytes to be generated is the smaller of the sizeInBits bytes or |
| 794 |
// the number of requested blocks. The number of blocks is the smaller of the |
| 795 |
// number requested or the number allowed by sizeInBits. A partial block is |
| 796 |
// a full block. |
| 797 |
bytes = (blocks > 0) ? blocks * digestSize : (UINT16)BITS_TO_BYTES(sizeInBits); |
| 798 |
generated = bytes; |
| 799 |
|
| 800 |
// Generate required bytes |
| 801 |
for(; bytes > 0; bytes -= digestSize) |
| 802 |
{ |
| 803 |
counter++; |
| 804 |
// Start HMAC |
| 805 |
if(CryptHmacStart(&hState, hashAlg, key->size, key->buffer) == 0) |
| 806 |
return 0; |
| 807 |
// Adding counter |
| 808 |
CryptDigestUpdateInt(&hState.hashState, 4, counter); |
| 809 |
|
| 810 |
// Adding label |
| 811 |
if(label != NULL) |
| 812 |
HASH_DATA(&hState.hashState, label->size, (BYTE *)label->buffer); |
| 813 |
// Add a null. SP108 is not very clear about when the 0 is needed but to |
| 814 |
// make this like the previous version that did not add an 0x00 after |
| 815 |
// a null-terminated string, this version will only add a null byte |
| 816 |
// if the label parameter did not end in a null byte, or if no label |
| 817 |
// is present. |
| 818 |
if((label == NULL) |
| 819 |
|| (label->size == 0) |
| 820 |
|| (label->buffer[label->size - 1] != 0)) |
| 821 |
CryptDigestUpdateInt(&hState.hashState, 1, 0); |
| 822 |
// Adding contextU |
| 823 |
if(contextU != NULL) |
| 824 |
HASH_DATA(&hState.hashState, contextU->size, contextU->buffer); |
| 825 |
// Adding contextV |
| 826 |
if(contextV != NULL) |
| 827 |
HASH_DATA(&hState.hashState, contextV->size, contextV->buffer); |
| 828 |
// Adding size in bits |
| 829 |
CryptDigestUpdateInt(&hState.hashState, 4, sizeInBits); |
| 830 |
|
| 831 |
// Complete and put the data in the buffer |
| 832 |
CryptHmacEnd(&hState, bytes, stream); |
| 833 |
stream = &stream[digestSize]; |
| 834 |
} |
| 835 |
// Masking in the KDF is disabled. If the calling function wants something |
| 836 |
// less than even number of bytes, then the caller should do the masking |
| 837 |
// because there is no universal way to do it here |
| 838 |
if(counterInOut != NULL) |
| 839 |
*counterInOut = counter; |
| 840 |
return generated; |
| 841 |
} |
| 842 |
|
| 843 |
//*** CryptKDFe() |
| 844 |
// This function implements KDFe() as defined in TPM specification part 1. |
| 845 |
// |
| 846 |
// This function returns the number of bytes generated which may be zero. |
| 847 |
// |
| 848 |
// The 'Z' and 'keyStream' pointers are not allowed to be NULL. The other |
| 849 |
// pointer values may be NULL. The value of 'sizeInBits' must be no larger |
| 850 |
// than (2^18)-1 = 256K bits (32385 bytes). |
| 851 |
// Any error in the processing of this command is considered fatal. |
| 852 |
// Return Type: UINT16 |
| 853 |
// 0 hash algorithm is not supported or is TPM_ALG_NULL |
| 854 |
// > 0 the number of bytes in the 'keyStream' buffer |
| 855 |
// |
| 856 |
LIB_EXPORT UINT16 |
| 857 |
CryptKDFe( |
| 858 |
TPM_ALG_ID hashAlg, // IN: hash algorithm used in HMAC |
| 859 |
TPM2B *Z, // IN: Z |
| 860 |
const TPM2B *label, // IN: a label value for the KDF |
| 861 |
TPM2B *partyUInfo, // IN: PartyUInfo |
| 862 |
TPM2B *partyVInfo, // IN: PartyVInfo |
| 863 |
UINT32 sizeInBits, // IN: size of generated key in bits |
| 864 |
BYTE *keyStream // OUT: key buffer |
| 865 |
) |
| 866 |
{ |
| 867 |
HASH_STATE hashState; |
| 868 |
PHASH_DEF hashDef = CryptGetHashDef(hashAlg); |
| 869 |
|
| 870 |
UINT32 counter = 0; // counter value |
| 871 |
UINT16 hLen; |
| 872 |
BYTE *stream = keyStream; |
| 873 |
INT16 bytes; // number of bytes to generate |
| 874 |
|
| 875 |
pAssert(keyStream != NULL && Z != NULL && ((sizeInBits + 7) / 8) < INT16_MAX); |
| 876 |
// |
| 877 |
hLen = hashDef->digestSize; |
| 878 |
bytes = (INT16)((sizeInBits + 7) / 8); |
| 879 |
if(hashAlg == TPM_ALG_NULL || bytes == 0) |
| 880 |
return 0; |
| 881 |
|
| 882 |
// Generate required bytes |
| 883 |
//The inner loop of that KDF uses: |
| 884 |
// Hash[i] := H(counter | Z | OtherInfo) (5) |
| 885 |
// Where: |
| 886 |
// Hash[i] the hash generated on the i-th iteration of the loop. |
| 887 |
// H() an approved hash function |
| 888 |
// counter a 32-bit counter that is initialized to 1 and incremented |
| 889 |
// on each iteration |
| 890 |
// Z the X coordinate of the product of a public ECC key and a |
| 891 |
// different private ECC key. |
| 892 |
// OtherInfo a collection of qualifying data for the KDF defined below. |
| 893 |
// In this specification, OtherInfo will be constructed by: |
| 894 |
// OtherInfo := Use | PartyUInfo | PartyVInfo |
| 895 |
for(; bytes > 0; stream = &stream[hLen], bytes = bytes - hLen) |
| 896 |
{ |
| 897 |
if(bytes < hLen) |
| 898 |
hLen = bytes; |
| 899 |
counter++; |
| 900 |
// Do the hash |
| 901 |
CryptHashStart(&hashState, hashAlg); |
| 902 |
// Add counter |
| 903 |
CryptDigestUpdateInt(&hashState, 4, counter); |
| 904 |
|
| 905 |
// Add Z |
| 906 |
if(Z != NULL) |
| 907 |
CryptDigestUpdate2B(&hashState, Z); |
| 908 |
// Add label |
| 909 |
if(label != NULL) |
| 910 |
CryptDigestUpdate2B(&hashState, label); |
| 911 |
// Add a null. SP108 is not very clear about when the 0 is needed but to |
| 912 |
// make this like the previous version that did not add an 0x00 after |
| 913 |
// a null-terminated string, this version will only add a null byte |
| 914 |
// if the label parameter did not end in a null byte, or if no label |
| 915 |
// is present. |
| 916 |
if((label == NULL) |
| 917 |
|| (label->size == 0) |
| 918 |
|| (label->buffer[label->size - 1] != 0)) |
| 919 |
CryptDigestUpdateInt(&hashState, 1, 0); |
| 920 |
// Add PartyUInfo |
| 921 |
if(partyUInfo != NULL) |
| 922 |
CryptDigestUpdate2B(&hashState, partyUInfo); |
| 923 |
|
| 924 |
// Add PartyVInfo |
| 925 |
if(partyVInfo != NULL) |
| 926 |
CryptDigestUpdate2B(&hashState, partyVInfo); |
| 927 |
|
| 928 |
// Compute Hash. hLen was changed to be the smaller of bytes or hLen |
| 929 |
// at the start of each iteration. |
| 930 |
CryptHashEnd(&hashState, hLen, stream); |
| 931 |
} |
| 932 |
|
| 933 |
// Mask off bits if the required bits is not a multiple of byte size |
| 934 |
if((sizeInBits % 8) != 0) |
| 935 |
keyStream[0] &= ((1 << (sizeInBits % 8)) - 1); |
| 936 |
|
| 937 |
return (UINT16)((sizeInBits + 7) / 8); |
| 938 |
} |