1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0 2*5c591343SA. Cody Schuffelen * 3*5c591343SA. Cody Schuffelen * The copyright in this software is being made available under the BSD License, 4*5c591343SA. Cody Schuffelen * included below. This software may be subject to other third party and 5*5c591343SA. Cody Schuffelen * contributor rights, including patent rights, and no such rights are granted 6*5c591343SA. Cody Schuffelen * under this license. 7*5c591343SA. Cody Schuffelen * 8*5c591343SA. Cody Schuffelen * Copyright (c) Microsoft Corporation 9*5c591343SA. Cody Schuffelen * 10*5c591343SA. Cody Schuffelen * All rights reserved. 11*5c591343SA. Cody Schuffelen * 12*5c591343SA. Cody Schuffelen * BSD License 13*5c591343SA. Cody Schuffelen * 14*5c591343SA. Cody Schuffelen * Redistribution and use in source and binary forms, with or without modification, 15*5c591343SA. Cody Schuffelen * are permitted provided that the following conditions are met: 16*5c591343SA. Cody Schuffelen * 17*5c591343SA. Cody Schuffelen * Redistributions of source code must retain the above copyright notice, this list 18*5c591343SA. Cody Schuffelen * of conditions and the following disclaimer. 19*5c591343SA. Cody Schuffelen * 20*5c591343SA. Cody Schuffelen * Redistributions in binary form must reproduce the above copyright notice, this 21*5c591343SA. Cody Schuffelen * list of conditions and the following disclaimer in the documentation and/or 22*5c591343SA. Cody Schuffelen * other materials provided with the distribution. 23*5c591343SA. Cody Schuffelen * 24*5c591343SA. Cody Schuffelen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" 25*5c591343SA. Cody Schuffelen * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26*5c591343SA. Cody Schuffelen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27*5c591343SA. Cody Schuffelen * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 28*5c591343SA. Cody Schuffelen * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29*5c591343SA. Cody Schuffelen * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30*5c591343SA. Cody Schuffelen * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 31*5c591343SA. Cody Schuffelen * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32*5c591343SA. Cody Schuffelen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33*5c591343SA. Cody Schuffelen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34*5c591343SA. Cody Schuffelen */ 35*5c591343SA. Cody Schuffelen //** Introduction 36*5c591343SA. Cody Schuffelen // 37*5c591343SA. Cody Schuffelen // This file contains the implementation of the symmetric block cipher modes 38*5c591343SA. Cody Schuffelen // allowed for a TPM. These functions only use the single block encryption functions 39*5c591343SA. Cody Schuffelen // of the selected symmetric cryptographic library. 40*5c591343SA. Cody Schuffelen 41*5c591343SA. Cody Schuffelen //** Includes, Defines, and Typedefs 42*5c591343SA. Cody Schuffelen #ifndef CRYPT_SYM_H 43*5c591343SA. Cody Schuffelen #define CRYPT_SYM_H 44*5c591343SA. Cody Schuffelen 45*5c591343SA. Cody Schuffelen #if ALG_AES 46*5c591343SA. Cody Schuffelen # define IF_IMPLEMENTED_AES(op) op(AES, aes) 47*5c591343SA. Cody Schuffelen #else 48*5c591343SA. Cody Schuffelen # define IF_IMPLEMENTED_AES(op) 49*5c591343SA. Cody Schuffelen #endif 50*5c591343SA. Cody Schuffelen #if ALG_SM4 51*5c591343SA. Cody Schuffelen # define IF_IMPLEMENTED_SM4(op) op(SM4, sm4) 52*5c591343SA. Cody Schuffelen #else 53*5c591343SA. Cody Schuffelen # define IF_IMPLEMENTED_SM4(op) 54*5c591343SA. Cody Schuffelen #endif 55*5c591343SA. Cody Schuffelen #if ALG_CAMELLIA 56*5c591343SA. Cody Schuffelen # define IF_IMPLEMENTED_CAMELLIA(op) op(CAMELLIA, camellia) 57*5c591343SA. Cody Schuffelen #else 58*5c591343SA. Cody Schuffelen # define IF_IMPLEMENTED_CAMELLIA(op) 59*5c591343SA. Cody Schuffelen #endif 60*5c591343SA. Cody Schuffelen #if ALG_TDES 61*5c591343SA. Cody Schuffelen # define IF_IMPLEMENTED_TDES(op) op(TDES, tdes) 62*5c591343SA. Cody Schuffelen #else 63*5c591343SA. Cody Schuffelen # define IF_IMPLEMENTED_TDES(op) 64*5c591343SA. Cody Schuffelen #endif 65*5c591343SA. Cody Schuffelen 66*5c591343SA. Cody Schuffelen #define FOR_EACH_SYM(op) \ 67*5c591343SA. Cody Schuffelen IF_IMPLEMENTED_AES(op) \ 68*5c591343SA. Cody Schuffelen IF_IMPLEMENTED_SM4(op) \ 69*5c591343SA. Cody Schuffelen IF_IMPLEMENTED_CAMELLIA(op) \ 70*5c591343SA. Cody Schuffelen IF_IMPLEMENTED_TDES(op) 71*5c591343SA. Cody Schuffelen 72*5c591343SA. Cody Schuffelen // Macros for creating the key schedule union 73*5c591343SA. Cody Schuffelen #define KEY_SCHEDULE(SYM, sym) tpmKeySchedule##SYM sym; 74*5c591343SA. Cody Schuffelen #define TDES DES[3] 75*5c591343SA. Cody Schuffelen typedef union tpmCryptKeySchedule_t { 76*5c591343SA. Cody Schuffelen FOR_EACH_SYM(KEY_SCHEDULE) 77*5c591343SA. Cody Schuffelen 78*5c591343SA. Cody Schuffelen #if SYMMETRIC_ALIGNMENT == 8 79*5c591343SA. Cody Schuffelen uint64_t alignment; 80*5c591343SA. Cody Schuffelen #else 81*5c591343SA. Cody Schuffelen uint32_t alignment; 82*5c591343SA. Cody Schuffelen #endif 83*5c591343SA. Cody Schuffelen } tpmCryptKeySchedule_t; 84*5c591343SA. Cody Schuffelen 85*5c591343SA. Cody Schuffelen 86*5c591343SA. Cody Schuffelen // Each block cipher within a library is expected to conform to the same calling 87*5c591343SA. Cody Schuffelen // conventions with three parameters ('keySchedule', 'in', and 'out') in the same 88*5c591343SA. Cody Schuffelen // order. That means that all algorithms would use the same order of the same 89*5c591343SA. Cody Schuffelen // parameters. The code is written assuming the ('keySchedule', 'in', and 'out') 90*5c591343SA. Cody Schuffelen // order. However, if the library uses a different order, the order can be changed 91*5c591343SA. Cody Schuffelen // with a SWIZZLE macro that puts the parameters in the correct order. 92*5c591343SA. Cody Schuffelen // Note that all algorithms have to use the same order and number of parameters 93*5c591343SA. Cody Schuffelen // because the code to build the calling list is common for each call to encrypt 94*5c591343SA. Cody Schuffelen // or decrypt with the algorithm chosen by setting a function pointer to select 95*5c591343SA. Cody Schuffelen // the algorithm that is used. 96*5c591343SA. Cody Schuffelen 97*5c591343SA. Cody Schuffelen # define ENCRYPT(keySchedule, in, out) \ 98*5c591343SA. Cody Schuffelen encrypt(SWIZZLE(keySchedule, in, out)) 99*5c591343SA. Cody Schuffelen 100*5c591343SA. Cody Schuffelen # define DECRYPT(keySchedule, in, out) \ 101*5c591343SA. Cody Schuffelen decrypt(SWIZZLE(keySchedule, in, out)) 102*5c591343SA. Cody Schuffelen 103*5c591343SA. Cody Schuffelen // Note that the macros rely on 'encrypt' as local values in the 104*5c591343SA. Cody Schuffelen // functions that use these macros. Those parameters are set by the macro that 105*5c591343SA. Cody Schuffelen // set the key schedule to be used for the call. 106*5c591343SA. Cody Schuffelen 107*5c591343SA. Cody Schuffelen #define ENCRYPT_CASE(ALG, alg) \ 108*5c591343SA. Cody Schuffelen case TPM_ALG_##ALG: \ 109*5c591343SA. Cody Schuffelen TpmCryptSetEncryptKey##ALG(key, keySizeInBits, &keySchedule.alg); \ 110*5c591343SA. Cody Schuffelen encrypt = (TpmCryptSetSymKeyCall_t)TpmCryptEncrypt##ALG; \ 111*5c591343SA. Cody Schuffelen break; 112*5c591343SA. Cody Schuffelen #define DECRYPT_CASE(ALG, alg) \ 113*5c591343SA. Cody Schuffelen case TPM_ALG_##ALG: \ 114*5c591343SA. Cody Schuffelen TpmCryptSetDecryptKey##ALG(key, keySizeInBits, &keySchedule.alg); \ 115*5c591343SA. Cody Schuffelen decrypt = (TpmCryptSetSymKeyCall_t)TpmCryptDecrypt##ALG; \ 116*5c591343SA. Cody Schuffelen break; 117*5c591343SA. Cody Schuffelen 118*5c591343SA. Cody Schuffelen #endif // CRYPT_SYM_H