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 header file is used to 'splice' the OpenSSL library into the TPM code. 38 // 39 // The support required of a library are a hash module, a block cipher module and 40 // portions of a big number library. 41 42 // All of the library-dependent headers should have the same guard to that only the 43 // first one gets defined. 44 #ifndef SYM_LIB_DEFINED 45 #define SYM_LIB_DEFINED 46 47 #define SYM_LIB_OSSL 48 49 #include <openssl/aes.h> 50 51 #if ALG_TDES 52 #include <openssl/des.h> 53 #endif 54 55 #if ALG_SM4 56 # if defined(OPENSSL_NO_SM4) || OPENSSL_VERSION_NUMBER < 0x10101010L 57 # undef ALG_SM4 58 # define ALG_SM4 ALG_NO 59 # elif OPENSSL_VERSION_NUMBER >= 0x10200000L 60 # include <openssl/sm4.h> 61 # else 62 // OpenSSL 1.1.1 keeps smX.h headers in the include/crypto directory, 63 // and they do not get installed as part of the libssl package 64 65 # define SM4_KEY_SCHEDULE 32 66 67 typedef struct SM4_KEY_st { 68 uint32_t rk[SM4_KEY_SCHEDULE]; 69 } SM4_KEY; 70 71 int SM4_set_key(const uint8_t *key, SM4_KEY *ks); 72 void SM4_encrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks); 73 void SM4_decrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks); 74 # endif // OpenSSL < 1.2 75 #endif // ALG_SM4 76 77 #if ALG_CAMELLIA 78 #include <openssl/camellia.h> 79 #endif 80 81 #include <openssl/bn.h> 82 #include <openssl/ossl_typ.h> 83 84 //*************************************************************** 85 //** Links to the OpenSSL symmetric algorithms. 86 //*************************************************************** 87 88 // The Crypt functions that call the block encryption function use the parameters 89 // in the order: 90 // 1) keySchedule 91 // 2) in buffer 92 // 3) out buffer 93 // Since open SSL uses the order in encryptoCall_t above, need to swizzle the 94 // values to the order required by the library. 95 #define SWIZZLE(keySchedule, in, out) \ 96 (const BYTE *)(in), (BYTE *)(out), (void *)(keySchedule) 97 98 // Define the order of parameters to the library functions that do block encryption 99 // and decryption. 100 typedef void(*TpmCryptSetSymKeyCall_t)( 101 const BYTE *in, 102 BYTE *out, 103 void *keySchedule 104 ); 105 106 #define SYM_ALIGNMENT RADIX_BYTES 107 108 //*************************************************************** 109 //** Links to the OpenSSL AES code 110 //*************************************************************** 111 // Macros to set up the encryption/decryption key schedules 112 // 113 // AES: 114 #define TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule) \ 115 AES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleAES *)(schedule)) 116 #define TpmCryptSetDecryptKeyAES(key, keySizeInBits, schedule) \ 117 AES_set_decrypt_key((key), (keySizeInBits), (tpmKeyScheduleAES *)(schedule)) 118 119 // Macros to alias encryption calls to specific algorithms. This should be used 120 // sparingly. Currently, only used by CryptSym.c and CryptRand.c 121 // 122 // When using these calls, to call the AES block encryption code, the caller 123 // should use: 124 // TpmCryptEncryptAES(SWIZZLE(keySchedule, in, out)); 125 #define TpmCryptEncryptAES AES_encrypt 126 #define TpmCryptDecryptAES AES_decrypt 127 #define tpmKeyScheduleAES AES_KEY 128 129 130 //*************************************************************** 131 //** Links to the OpenSSL DES code 132 //*************************************************************** 133 #if ALG_TDES 134 #include "TpmToOsslDesSupport_fp.h" 135 #endif 136 137 #define TpmCryptSetEncryptKeyTDES(key, keySizeInBits, schedule) \ 138 TDES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule)) 139 #define TpmCryptSetDecryptKeyTDES(key, keySizeInBits, schedule) \ 140 TDES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule)) 141 142 // Macros to alias encryption calls to specific algorithms. This should be used 143 // sparingly. Currently, only used by CryptRand.c 144 #define TpmCryptEncryptTDES TDES_encrypt 145 #define TpmCryptDecryptTDES TDES_decrypt 146 #define tpmKeyScheduleTDES DES_key_schedule 147 148 149 //*************************************************************** 150 //** Links to the OpenSSL SM4 code 151 //*************************************************************** 152 // Macros to set up the encryption/decryption key schedules 153 #define TpmCryptSetEncryptKeySM4(key, keySizeInBits, schedule) \ 154 SM4_set_key((key), (tpmKeyScheduleSM4 *)(schedule)) 155 #define TpmCryptSetDecryptKeySM4(key, keySizeInBits, schedule) \ 156 SM4_set_key((key), (tpmKeyScheduleSM4 *)(schedule)) 157 158 // Macros to alias encryption calls to specific algorithms. This should be used 159 // sparingly. 160 #define TpmCryptEncryptSM4 SM4_encrypt 161 #define TpmCryptDecryptSM4 SM4_decrypt 162 #define tpmKeyScheduleSM4 SM4_KEY 163 164 165 //*************************************************************** 166 //** Links to the OpenSSL CAMELLIA code 167 //*************************************************************** 168 // Macros to set up the encryption/decryption key schedules 169 #define TpmCryptSetEncryptKeyCAMELLIA(key, keySizeInBits, schedule) \ 170 Camellia_set_key((key), (keySizeInBits), (tpmKeyScheduleCAMELLIA *)(schedule)) 171 #define TpmCryptSetDecryptKeyCAMELLIA(key, keySizeInBits, schedule) \ 172 Camellia_set_key((key), (keySizeInBits), (tpmKeyScheduleCAMELLIA *)(schedule)) 173 174 // Macros to alias encryption calls to specific algorithms. This should be used 175 // sparingly. 176 #define TpmCryptEncryptCAMELLIA Camellia_encrypt 177 #define TpmCryptDecryptCAMELLIA Camellia_decrypt 178 #define tpmKeyScheduleCAMELLIA CAMELLIA_KEY 179 180 // Forward reference 181 182 typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t; 183 184 // This definition would change if there were something to report 185 #define SymLibSimulationEnd() 186 187 #endif // SYM_LIB_DEFINED 188