| 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 other |
| 22 |
* 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 |
|
| 36 |
//** Introduction |
| 37 |
// |
| 38 |
// This header file is used to 'splice' the wolfcrypt library into the TPM code. |
| 39 |
|
| 40 |
#ifndef SYM_LIB_DEFINED |
| 41 |
#define SYM_LIB_DEFINED |
| 42 |
|
| 43 |
#define SYM_LIB_WOLF |
| 44 |
|
| 45 |
#include <wolfssl/wolfcrypt/aes.h> |
| 46 |
#include <wolfssl/wolfcrypt/des3.h> |
| 47 |
|
| 48 |
//*************************************************************** |
| 49 |
//** Links to the wolfCrypt AES code |
| 50 |
//*************************************************************** |
| 51 |
|
| 52 |
#if ALG_SM4 |
| 53 |
#error "SM4 is not available" |
| 54 |
#endif |
| 55 |
|
| 56 |
#if ALG_CAMELLIA |
| 57 |
#error "Camellia is not available" |
| 58 |
#endif |
| 59 |
|
| 60 |
// Define the order of parameters to the library functions that do block encryption |
| 61 |
// and decryption. |
| 62 |
typedef void(*TpmCryptSetSymKeyCall_t)( |
| 63 |
void *keySchedule, |
| 64 |
BYTE *out, |
| 65 |
const BYTE *in |
| 66 |
); |
| 67 |
|
| 68 |
// The Crypt functions that call the block encryption function use the parameters |
| 69 |
// in the order: |
| 70 |
// 1) keySchedule |
| 71 |
// 2) in buffer |
| 72 |
// 3) out buffer |
| 73 |
// Since wolfcrypt uses the order in encryptoCall_t above, need to swizzle the |
| 74 |
// values to the order required by the library. |
| 75 |
#define SWIZZLE(keySchedule, in, out) \ |
| 76 |
(void *)(keySchedule), (BYTE *)(out), (const BYTE *)(in) |
| 77 |
|
| 78 |
// Macros to set up the encryption/decryption key schedules |
| 79 |
// |
| 80 |
// AES: |
| 81 |
#define TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule) \ |
| 82 |
wc_AesSetKeyDirect((tpmKeyScheduleAES *)(schedule), key, BITS_TO_BYTES(keySizeInBits), 0, AES_ENCRYPTION) |
| 83 |
#define TpmCryptSetDecryptKeyAES(key, keySizeInBits, schedule) \ |
| 84 |
wc_AesSetKeyDirect((tpmKeyScheduleAES *)(schedule), key, BITS_TO_BYTES(keySizeInBits), 0, AES_DECRYPTION) |
| 85 |
|
| 86 |
// TDES: |
| 87 |
#define TpmCryptSetEncryptKeyTDES(key, keySizeInBits, schedule) \ |
| 88 |
TDES_setup_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule)) |
| 89 |
#define TpmCryptSetDecryptKeyTDES(key, keySizeInBits, schedule) \ |
| 90 |
TDES_setup_decrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule)) |
| 91 |
|
| 92 |
// Macros to alias encryption calls to specific algorithms. This should be used |
| 93 |
// sparingly. Currently, only used by CryptRand.c |
| 94 |
// |
| 95 |
// When using these calls, to call the AES block encryption code, the caller |
| 96 |
// should use: |
| 97 |
// TpmCryptEncryptAES(SWIZZLE(keySchedule, in, out)); |
| 98 |
#define TpmCryptEncryptAES wc_AesEncryptDirect |
| 99 |
#define TpmCryptDecryptAES wc_AesDecryptDirect |
| 100 |
#define tpmKeyScheduleAES Aes |
| 101 |
|
| 102 |
#define TpmCryptEncryptTDES TDES_encrypt |
| 103 |
#define TpmCryptDecryptTDES TDES_decrypt |
| 104 |
#define tpmKeyScheduleTDES Des3 |
| 105 |
|
| 106 |
typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t; |
| 107 |
|
| 108 |
#if ALG_TDES |
| 109 |
#include "TpmToWolfDesSupport_fp.h" |
| 110 |
#endif |
| 111 |
|
| 112 |
// This definition would change if there were something to report |
| 113 |
#define SymLibSimulationEnd() |
| 114 |
|
| 115 |
#endif // SYM_LIB_DEFINED |