xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/include/Ltc/TpmToLtcSym.h (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
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 TPM to the LTC symmetric cipher code.
39 
40 #ifndef SYM_LIB_DEFINED
41 #define SYM_LIB_DEFINED
42 
43 #define SYM_LIB_LTC
44 
45 #define SYM_ALIGNMENT   RADIX_BYTES
46 
47 // Avoid pulling in the MPA math if not doing asymmetric with LTC
48 #if !(defined MATH_LIB_LTC)
49 #  define LTC_NO_ASYMMETRIC
50 #endif
51 
52 #include "LtcSettings.h"
53 
54 //***************************************************************
55 //******** Linking to the TomCrypt AES code *********************
56 //***************************************************************
57 
58 #if ALG_SM4
59 #error "SM4 is not available"
60 #endif
61 
62 #if ALG_CAMELLIA
63 #error "Camellia is not available"
64 #endif
65 
66 // Define the order of parameters to the functions that do block encryption and
67 // decryption.
68 typedef void(*TpmCryptSetSymKeyCall_t)(
69     const void      *in,
70     void            *out,
71     void            *keySchedule
72     );
73 
74 // Macro to put the parameters in the order required by the library
75 #define SWIZZLE(keySchedule, in, out)                                               \
76     (const void *)(in), (void *)(out), (void *)(keySchedule)
77 
78 // Macros to set up the encryption/decryption key schedules
79 //
80 // AES:
81 # define TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule)                     \
82     aes_setup((key), BITS_TO_BYTES(keySizeInBits), 0, (symmetric_key *)(schedule))
83 # define TpmCryptSetDecryptKeyAES(key, keySizeInBits, schedule)                     \
84     aes_setup((key), BITS_TO_BYTES(keySizeInBits), 0, (symmetric_key *)(schedule))
85 
86 // TDES:
87 # define TpmCryptSetEncryptKeyTDES(key, keySizeInBits, schedule)                    \
88     TDES_setup((key), (keySizeInBits), (symmetric_key *)(schedule))
89 # define TpmCryptSetDecryptKeyTDES(key, keySizeInBits, schedule)                    \
90     TDES_setup((key), (keySizeInBits), (symmetric_key *)(schedule))
91 
92 
93 // Macros to alias encrypt and decrypt function calls to library-specific values
94 // sparingly. These should be used sparingly. Currently, they are only used by
95 // CryptRand.c in the AES version of the DRBG.
96 #define TpmCryptEncryptAES      aes_ecb_encrypt
97 #define TpmCryptDecryptAES      aes_ecb_decrypt
98 #define tpmKeyScheduleAES       struct rijndael_key
99 //
100 #define TpmCryptEncryptTDES     des3_ecb_encrypt
101 #define TpmCryptDecryptTDES     des3_ecb_decrypt
102 #define tpmKeyScheduleTDES      struct des3_key
103 
104 typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t;
105 
106 #include "TpmToLtcDesSupport_fp.h"
107 
108 // This is used to trigger printing of simulation statistics
109 
110 #define SymLibSimulationEnd()
111 
112 #endif // SYM_LIB_DEFINED
113