| 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 |
//** Introduction |
| 36 |
// |
| 37 |
// This file contains the implementation of the symmetric block cipher modes |
| 38 |
// allowed for a TPM. These functions only use the single block encryption functions |
| 39 |
// of the selected symmetric crypto library. |
| 40 |
|
| 41 |
//** Includes, Defines, and Typedefs |
| 42 |
#include "Tpm.h" |
| 43 |
|
| 44 |
#include "CryptSym.h" |
| 45 |
|
| 46 |
#define KEY_BLOCK_SIZES(ALG, alg) \ |
| 47 |
static const INT16 alg##KeyBlockSizes[] = { \ |
| 48 |
ALG##_KEY_SIZES_BITS, -1, ALG##_BLOCK_SIZES }; |
| 49 |
|
| 50 |
#if ALG_AES |
| 51 |
KEY_BLOCK_SIZES(AES, aes); |
| 52 |
#endif // ALG_AES |
| 53 |
#if ALG_SM4 |
| 54 |
KEY_BLOCK_SIZES(SM4, sm4); |
| 55 |
#endif |
| 56 |
#if ALG_CAMELLIA |
| 57 |
KEY_BLOCK_SIZES(CAMELLIA, camellia); |
| 58 |
#endif |
| 59 |
#if ALG_TDES |
| 60 |
KEY_BLOCK_SIZES(TDES, tdes); |
| 61 |
#endif |
| 62 |
|
| 63 |
//** Initialization and Data Access Functions |
| 64 |
// |
| 65 |
//*** CryptSymInit() |
| 66 |
// This function is called to do _TPM_Init processing |
| 67 |
BOOL |
| 68 |
CryptSymInit( |
| 69 |
void |
| 70 |
) |
| 71 |
{ |
| 72 |
return TRUE; |
| 73 |
} |
| 74 |
|
| 75 |
//*** CryptSymStartup() |
| 76 |
// This function is called to do TPM2_Startup() processing |
| 77 |
BOOL |
| 78 |
CryptSymStartup( |
| 79 |
void |
| 80 |
) |
| 81 |
{ |
| 82 |
return TRUE; |
| 83 |
} |
| 84 |
|
| 85 |
//*** CryptGetSymmetricBlockSize() |
| 86 |
// This function returns the block size of the algorithm. The table of bit sizes has |
| 87 |
// an entry for each allowed key size. The entry for a key size is 0 if the TPM does |
| 88 |
// not implement that key size. The key size table is delimited with a negative number |
| 89 |
// (-1). After the delimiter is a list of block sizes with each entry corresponding |
| 90 |
// to the key bit size. For most symmetric algorithms, the block size is the same |
| 91 |
// regardless of the key size but this arrangement allows them to be different. |
| 92 |
// Return Type: INT16 |
| 93 |
// <= 0 cipher not supported |
| 94 |
// > 0 the cipher block size in bytes |
| 95 |
LIB_EXPORT INT16 |
| 96 |
CryptGetSymmetricBlockSize( |
| 97 |
TPM_ALG_ID symmetricAlg, // IN: the symmetric algorithm |
| 98 |
UINT16 keySizeInBits // IN: the key size |
| 99 |
) |
| 100 |
{ |
| 101 |
const INT16 *sizes; |
| 102 |
INT16 i; |
| 103 |
#define ALG_CASE(SYM, sym) case ALG_##SYM##_VALUE: sizes = sym##KeyBlockSizes; break |
| 104 |
switch(symmetricAlg) |
| 105 |
{ |
| 106 |
#if ALG_AES |
| 107 |
ALG_CASE(AES, aes); |
| 108 |
#endif |
| 109 |
#if ALG_SM4 |
| 110 |
ALG_CASE(SM4, sm4); |
| 111 |
#endif |
| 112 |
#if ALG_CAMELLIA |
| 113 |
ALG_CASE(CAMELLIA, camellia); |
| 114 |
#endif |
| 115 |
#if ALG_TDES |
| 116 |
ALG_CASE(TDES, tdes); |
| 117 |
#endif |
| 118 |
default: |
| 119 |
return 0; |
| 120 |
} |
| 121 |
// Find the index of the indicated keySizeInBits |
| 122 |
for(i = 0; *sizes >= 0; i++, sizes++) |
| 123 |
{ |
| 124 |
if(*sizes == keySizeInBits) |
| 125 |
break; |
| 126 |
} |
| 127 |
// If sizes is pointing at the end of the list of key sizes, then the desired |
| 128 |
// key size was not found so set the block size to zero. |
| 129 |
if(*sizes++ < 0) |
| 130 |
return 0; |
| 131 |
// Advance until the end of the list is found |
| 132 |
while(*sizes++ >= 0); |
| 133 |
// sizes is pointing to the first entry in the list of block sizes. Use the |
| 134 |
// ith index to find the block size for the corresponding key size. |
| 135 |
return sizes[i]; |
| 136 |
} |
| 137 |
|
| 138 |
//** Symmetric Encryption |
| 139 |
// This function performs symmetric encryption based on the mode. |
| 140 |
// Return Type: TPM_RC |
| 141 |
// TPM_RC_SIZE 'dSize' is not a multiple of the block size for an |
| 142 |
// algorithm that requires it |
| 143 |
// TPM_RC_FAILURE Fatal error |
| 144 |
LIB_EXPORT TPM_RC |
| 145 |
CryptSymmetricEncrypt( |
| 146 |
BYTE *dOut, // OUT: |
| 147 |
TPM_ALG_ID algorithm, // IN: the symmetric algorithm |
| 148 |
UINT16 keySizeInBits, // IN: key size in bits |
| 149 |
const BYTE *key, // IN: key buffer. The size of this buffer |
| 150 |
// in bytes is (keySizeInBits + 7) / 8 |
| 151 |
TPM2B_IV *ivInOut, // IN/OUT: IV for decryption. |
| 152 |
TPM_ALG_ID mode, // IN: Mode to use |
| 153 |
INT32 dSize, // IN: data size (may need to be a |
| 154 |
// multiple of the blockSize) |
| 155 |
const BYTE *dIn // IN: data buffer |
| 156 |
) |
| 157 |
{ |
| 158 |
BYTE *pIv; |
| 159 |
int i; |
| 160 |
BYTE tmp[MAX_SYM_BLOCK_SIZE]; |
| 161 |
BYTE *pT; |
| 162 |
tpmCryptKeySchedule_t keySchedule; |
| 163 |
INT16 blockSize; |
| 164 |
TpmCryptSetSymKeyCall_t encrypt; |
| 165 |
BYTE *iv; |
| 166 |
BYTE defaultIv[MAX_SYM_BLOCK_SIZE] = {0}; |
| 167 |
// |
| 168 |
pAssert(dOut != NULL && key != NULL && dIn != NULL); |
| 169 |
if(dSize == 0) |
| 170 |
return TPM_RC_SUCCESS; |
| 171 |
|
| 172 |
TEST(algorithm); |
| 173 |
blockSize = CryptGetSymmetricBlockSize(algorithm, keySizeInBits); |
| 174 |
if(blockSize == 0) |
| 175 |
return TPM_RC_FAILURE; |
| 176 |
// If the iv is provided, then it is expected to be block sized. In some cases, |
| 177 |
// the caller is providing an array of 0's that is equal to [MAX_SYM_BLOCK_SIZE] |
| 178 |
// with no knowledge of the actual block size. This function will set it. |
| 179 |
if((ivInOut != NULL) && (mode != ALG_ECB_VALUE)) |
| 180 |
{ |
| 181 |
ivInOut->t.size = blockSize; |
| 182 |
iv = ivInOut->t.buffer; |
| 183 |
} |
| 184 |
else |
| 185 |
iv = defaultIv; |
| 186 |
pIv = iv; |
| 187 |
|
| 188 |
// Create encrypt key schedule and set the encryption function pointer. |
| 189 |
|
| 190 |
SELECT(ENCRYPT); |
| 191 |
|
| 192 |
switch(mode) |
| 193 |
{ |
| 194 |
#if ALG_CTR |
| 195 |
case ALG_CTR_VALUE: |
| 196 |
for(; dSize > 0; dSize -= blockSize) |
| 197 |
{ |
| 198 |
// Encrypt the current value of the IV(counter) |
| 199 |
ENCRYPT(&keySchedule, iv, tmp); |
| 200 |
|
| 201 |
//increment the counter (counter is big-endian so start at end) |
| 202 |
for(i = blockSize - 1; i >= 0; i--) |
| 203 |
if((iv[i] += 1) != 0) |
| 204 |
break; |
| 205 |
// XOR the encrypted counter value with input and put into output |
| 206 |
pT = tmp; |
| 207 |
for(i = (dSize < blockSize) ? dSize : blockSize; i > 0; i--) |
| 208 |
*dOut++ = *dIn++ ^ *pT++; |
| 209 |
} |
| 210 |
break; |
| 211 |
#endif |
| 212 |
#if ALG_OFB |
| 213 |
case ALG_OFB_VALUE: |
| 214 |
// This is written so that dIn and dOut may be the same |
| 215 |
for(; dSize > 0; dSize -= blockSize) |
| 216 |
{ |
| 217 |
// Encrypt the current value of the "IV" |
| 218 |
ENCRYPT(&keySchedule, iv, iv); |
| 219 |
|
| 220 |
// XOR the encrypted IV into dIn to create the cipher text (dOut) |
| 221 |
pIv = iv; |
| 222 |
for(i = (dSize < blockSize) ? dSize : blockSize; i > 0; i--) |
| 223 |
*dOut++ = (*pIv++ ^ *dIn++); |
| 224 |
} |
| 225 |
break; |
| 226 |
#endif |
| 227 |
#if ALG_CBC |
| 228 |
case ALG_CBC_VALUE: |
| 229 |
// For CBC the data size must be an even multiple of the |
| 230 |
// cipher block size |
| 231 |
if((dSize % blockSize) != 0) |
| 232 |
return TPM_RC_SIZE; |
| 233 |
// XOR the data block into the IV, encrypt the IV into the IV |
| 234 |
// and then copy the IV to the output |
| 235 |
for(; dSize > 0; dSize -= blockSize) |
| 236 |
{ |
| 237 |
pIv = iv; |
| 238 |
for(i = blockSize; i > 0; i--) |
| 239 |
*pIv++ ^= *dIn++; |
| 240 |
ENCRYPT(&keySchedule, iv, iv); |
| 241 |
pIv = iv; |
| 242 |
for(i = blockSize; i > 0; i--) |
| 243 |
*dOut++ = *pIv++; |
| 244 |
} |
| 245 |
break; |
| 246 |
#endif |
| 247 |
// CFB is not optional |
| 248 |
case ALG_CFB_VALUE: |
| 249 |
// Encrypt the IV into the IV, XOR in the data, and copy to output |
| 250 |
for(; dSize > 0; dSize -= blockSize) |
| 251 |
{ |
| 252 |
// Encrypt the current value of the IV |
| 253 |
ENCRYPT(&keySchedule, iv, iv); |
| 254 |
pIv = iv; |
| 255 |
for(i = (int)(dSize < blockSize) ? dSize : blockSize; i > 0; i--) |
| 256 |
// XOR the data into the IV to create the cipher text |
| 257 |
// and put into the output |
| 258 |
*dOut++ = *pIv++ ^= *dIn++; |
| 259 |
} |
| 260 |
// If the inner loop (i loop) was smaller than blockSize, then dSize |
| 261 |
// would have been smaller than blockSize and it is now negative. If |
| 262 |
// it is negative, then it indicates how many bytes are needed to pad |
| 263 |
// out the IV for the next round. |
| 264 |
for(; dSize < 0; dSize++) |
| 265 |
*pIv++ = 0; |
| 266 |
break; |
| 267 |
#if ALG_ECB |
| 268 |
case ALG_ECB_VALUE: |
| 269 |
// For ECB the data size must be an even multiple of the |
| 270 |
// cipher block size |
| 271 |
if((dSize % blockSize) != 0) |
| 272 |
return TPM_RC_SIZE; |
| 273 |
// Encrypt the input block to the output block |
| 274 |
for(; dSize > 0; dSize -= blockSize) |
| 275 |
{ |
| 276 |
ENCRYPT(&keySchedule, dIn, dOut); |
| 277 |
dIn = &dIn[blockSize]; |
| 278 |
dOut = &dOut[blockSize]; |
| 279 |
} |
| 280 |
break; |
| 281 |
#endif |
| 282 |
default: |
| 283 |
return TPM_RC_FAILURE; |
| 284 |
} |
| 285 |
return TPM_RC_SUCCESS; |
| 286 |
} |
| 287 |
|
| 288 |
//*** CryptSymmetricDecrypt() |
| 289 |
// This function performs symmetric decryption based on the mode. |
| 290 |
// Return Type: TPM_RC |
| 291 |
// TPM_RC_FAILURE A fatal error |
| 292 |
// TPM_RCS_SIZE 'dSize' is not a multiple of the block size for an |
| 293 |
// algorithm that requires it |
| 294 |
LIB_EXPORT TPM_RC |
| 295 |
CryptSymmetricDecrypt( |
| 296 |
BYTE *dOut, // OUT: decrypted data |
| 297 |
TPM_ALG_ID algorithm, // IN: the symmetric algorithm |
| 298 |
UINT16 keySizeInBits, // IN: key size in bits |
| 299 |
const BYTE *key, // IN: key buffer. The size of this buffer |
| 300 |
// in bytes is (keySizeInBits + 7) / 8 |
| 301 |
TPM2B_IV *ivInOut, // IN/OUT: IV for decryption. |
| 302 |
TPM_ALG_ID mode, // IN: Mode to use |
| 303 |
INT32 dSize, // IN: data size (may need to be a |
| 304 |
// multiple of the blockSize) |
| 305 |
const BYTE *dIn // IN: data buffer |
| 306 |
) |
| 307 |
{ |
| 308 |
BYTE *pIv; |
| 309 |
int i; |
| 310 |
BYTE tmp[MAX_SYM_BLOCK_SIZE]; |
| 311 |
BYTE *pT; |
| 312 |
tpmCryptKeySchedule_t keySchedule; |
| 313 |
INT16 blockSize; |
| 314 |
BYTE *iv; |
| 315 |
TpmCryptSetSymKeyCall_t encrypt; |
| 316 |
TpmCryptSetSymKeyCall_t decrypt; |
| 317 |
BYTE defaultIv[MAX_SYM_BLOCK_SIZE] = {0}; |
| 318 |
|
| 319 |
// These are used but the compiler can't tell because they are initialized |
| 320 |
// in case statements and it can't tell if they are always initialized |
| 321 |
// when needed, so... Comment these out if the compiler can tell or doesn't |
| 322 |
// care that these are initialized before use. |
| 323 |
encrypt = NULL; |
| 324 |
decrypt = NULL; |
| 325 |
|
| 326 |
pAssert(dOut != NULL && key != NULL && dIn != NULL); |
| 327 |
if(dSize == 0) |
| 328 |
return TPM_RC_SUCCESS; |
| 329 |
|
| 330 |
TEST(algorithm); |
| 331 |
blockSize = CryptGetSymmetricBlockSize(algorithm, keySizeInBits); |
| 332 |
if(blockSize == 0) |
| 333 |
return TPM_RC_FAILURE; |
| 334 |
// If the iv is provided, then it is expected to be block sized. In some cases, |
| 335 |
// the caller is providing an array of 0's that is equal to [MAX_SYM_BLOCK_SIZE] |
| 336 |
// with no knowledge of the actual block size. This function will set it. |
| 337 |
if((ivInOut != NULL) && (mode != ALG_ECB_VALUE)) |
| 338 |
{ |
| 339 |
ivInOut->t.size = blockSize; |
| 340 |
iv = ivInOut->t.buffer; |
| 341 |
} |
| 342 |
else |
| 343 |
iv = defaultIv; |
| 344 |
|
| 345 |
pIv = iv; |
| 346 |
// Use the mode to select the key schedule to create. Encrypt always uses the |
| 347 |
// encryption schedule. Depending on the mode, decryption might use either |
| 348 |
// the decryption or encryption schedule. |
| 349 |
switch(mode) |
| 350 |
{ |
| 351 |
#if ALG_CBC || ALG_ECB |
| 352 |
case ALG_CBC_VALUE: // decrypt = decrypt |
| 353 |
case ALG_ECB_VALUE: |
| 354 |
// For ECB and CBC, the data size must be an even multiple of the |
| 355 |
// cipher block size |
| 356 |
if((dSize % blockSize) != 0) |
| 357 |
return TPM_RC_SIZE; |
| 358 |
SELECT(DECRYPT); |
| 359 |
break; |
| 360 |
#endif |
| 361 |
default: |
| 362 |
// For the remaining stream ciphers, use encryption to decrypt |
| 363 |
SELECT(ENCRYPT); |
| 364 |
break; |
| 365 |
} |
| 366 |
// Now do the mode-dependent decryption |
| 367 |
switch(mode) |
| 368 |
{ |
| 369 |
#if ALG_CBC |
| 370 |
case ALG_CBC_VALUE: |
| 371 |
// Copy the input data to a temp buffer, decrypt the buffer into the |
| 372 |
// output, XOR in the IV, and copy the temp buffer to the IV and repeat. |
| 373 |
for(; dSize > 0; dSize -= blockSize) |
| 374 |
{ |
| 375 |
pT = tmp; |
| 376 |
for(i = blockSize; i > 0; i--) |
| 377 |
*pT++ = *dIn++; |
| 378 |
DECRYPT(&keySchedule, tmp, dOut); |
| 379 |
pIv = iv; |
| 380 |
pT = tmp; |
| 381 |
for(i = blockSize; i > 0; i--) |
| 382 |
{ |
| 383 |
*dOut++ ^= *pIv; |
| 384 |
*pIv++ = *pT++; |
| 385 |
} |
| 386 |
} |
| 387 |
break; |
| 388 |
#endif |
| 389 |
case ALG_CFB_VALUE: |
| 390 |
for(; dSize > 0; dSize -= blockSize) |
| 391 |
{ |
| 392 |
// Encrypt the IV into the temp buffer |
| 393 |
ENCRYPT(&keySchedule, iv, tmp); |
| 394 |
pT = tmp; |
| 395 |
pIv = iv; |
| 396 |
for(i = (dSize < blockSize) ? dSize : blockSize; i > 0; i--) |
| 397 |
// Copy the current cipher text to IV, XOR |
| 398 |
// with the temp buffer and put into the output |
| 399 |
*dOut++ = *pT++ ^ (*pIv++ = *dIn++); |
| 400 |
} |
| 401 |
// If the inner loop (i loop) was smaller than blockSize, then dSize |
| 402 |
// would have been smaller than blockSize and it is now negative |
| 403 |
// If it is negative, then it indicates how may fill bytes |
| 404 |
// are needed to pad out the IV for the next round. |
| 405 |
for(; dSize < 0; dSize++) |
| 406 |
*pIv++ = 0; |
| 407 |
|
| 408 |
break; |
| 409 |
#if ALG_CTR |
| 410 |
case ALG_CTR_VALUE: |
| 411 |
for(; dSize > 0; dSize -= blockSize) |
| 412 |
{ |
| 413 |
// Encrypt the current value of the IV(counter) |
| 414 |
ENCRYPT(&keySchedule, iv, tmp); |
| 415 |
|
| 416 |
//increment the counter (counter is big-endian so start at end) |
| 417 |
for(i = blockSize - 1; i >= 0; i--) |
| 418 |
if((iv[i] += 1) != 0) |
| 419 |
break; |
| 420 |
// XOR the encrypted counter value with input and put into output |
| 421 |
pT = tmp; |
| 422 |
for(i = (dSize < blockSize) ? dSize : blockSize; i > 0; i--) |
| 423 |
*dOut++ = *dIn++ ^ *pT++; |
| 424 |
} |
| 425 |
break; |
| 426 |
#endif |
| 427 |
#if ALG_ECB |
| 428 |
case ALG_ECB_VALUE: |
| 429 |
for(; dSize > 0; dSize -= blockSize) |
| 430 |
{ |
| 431 |
DECRYPT(&keySchedule, dIn, dOut); |
| 432 |
dIn = &dIn[blockSize]; |
| 433 |
dOut = &dOut[blockSize]; |
| 434 |
} |
| 435 |
break; |
| 436 |
#endif |
| 437 |
#if ALG_OFB |
| 438 |
case ALG_OFB_VALUE: |
| 439 |
// This is written so that dIn and dOut may be the same |
| 440 |
for(; dSize > 0; dSize -= blockSize) |
| 441 |
{ |
| 442 |
// Encrypt the current value of the "IV" |
| 443 |
ENCRYPT(&keySchedule, iv, iv); |
| 444 |
|
| 445 |
// XOR the encrypted IV into dIn to create the cipher text (dOut) |
| 446 |
pIv = iv; |
| 447 |
for(i = (dSize < blockSize) ? dSize : blockSize; i > 0; i--) |
| 448 |
*dOut++ = (*pIv++ ^ *dIn++); |
| 449 |
} |
| 450 |
break; |
| 451 |
#endif |
| 452 |
default: |
| 453 |
return TPM_RC_FAILURE; |
| 454 |
} |
| 455 |
return TPM_RC_SUCCESS; |
| 456 |
} |
| 457 |
|
| 458 |
//*** CryptSymKeyValidate() |
| 459 |
// Validate that a provided symmetric key meets the requirements of the TPM |
| 460 |
// Return Type: TPM_RC |
| 461 |
// TPM_RC_KEY_SIZE Key size specifiers do not match |
| 462 |
// TPM_RC_KEY Key is not allowed |
| 463 |
TPM_RC |
| 464 |
CryptSymKeyValidate( |
| 465 |
TPMT_SYM_DEF_OBJECT *symDef, |
| 466 |
TPM2B_SYM_KEY *key |
| 467 |
) |
| 468 |
{ |
| 469 |
if(key->t.size != BITS_TO_BYTES(symDef->keyBits.sym)) |
| 470 |
return TPM_RCS_KEY_SIZE; |
| 471 |
#if ALG_TDES |
| 472 |
if(symDef->algorithm == TPM_ALG_TDES && !CryptDesValidateKey(key)) |
| 473 |
return TPM_RCS_KEY; |
| 474 |
#endif // ALG_TDES |
| 475 |
return TPM_RC_SUCCESS; |
| 476 |
} |
| 477 |
|
| 478 |
|