]> git.proxmox.com Git - libtpms.git/blob - src/tpm2/crypto/openssl/TpmToOsslSym.h
rev162: make union tpmCryptKeySchedule_t a typedef union
[libtpms.git] / src / tpm2 / crypto / openssl / TpmToOsslSym.h
1 /********************************************************************************/
2 /* */
3 /* Splice the OpenSSL() library into the TPM code. */
4 /* Written by Ken Goldman */
5 /* IBM Thomas J. Watson Research Center */
6 /* $Id: TpmToOsslSym.h 1619 2020-05-19 16:51:47Z kgoldman $ */
7 /* */
8 /* Licenses and Notices */
9 /* */
10 /* 1. Copyright Licenses: */
11 /* */
12 /* - Trusted Computing Group (TCG) grants to the user of the source code in */
13 /* this specification (the "Source Code") a worldwide, irrevocable, */
14 /* nonexclusive, royalty free, copyright license to reproduce, create */
15 /* derivative works, distribute, display and perform the Source Code and */
16 /* derivative works thereof, and to grant others the rights granted herein. */
17 /* */
18 /* - The TCG grants to the user of the other parts of the specification */
19 /* (other than the Source Code) the rights to reproduce, distribute, */
20 /* display, and perform the specification solely for the purpose of */
21 /* developing products based on such documents. */
22 /* */
23 /* 2. Source Code Distribution Conditions: */
24 /* */
25 /* - Redistributions of Source Code must retain the above copyright licenses, */
26 /* this list of conditions and the following disclaimers. */
27 /* */
28 /* - Redistributions in binary form must reproduce the above copyright */
29 /* licenses, this list of conditions and the following disclaimers in the */
30 /* documentation and/or other materials provided with the distribution. */
31 /* */
32 /* 3. Disclaimers: */
33 /* */
34 /* - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */
35 /* LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */
36 /* RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */
37 /* THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE. */
38 /* Contact TCG Administration (admin@trustedcomputinggroup.org) for */
39 /* information on specification licensing rights available through TCG */
40 /* membership agreements. */
41 /* */
42 /* - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED */
43 /* WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR */
44 /* FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR */
45 /* NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY */
46 /* OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE. */
47 /* */
48 /* - Without limitation, TCG and its members and licensors disclaim all */
49 /* liability, including liability for infringement of any proprietary */
50 /* rights, relating to use of information in this specification and to the */
51 /* implementation of this specification, and TCG disclaims all liability for */
52 /* cost of procurement of substitute goods or services, lost profits, loss */
53 /* of use, loss of data or any incidental, consequential, direct, indirect, */
54 /* or special damages, whether under contract, tort, warranty or otherwise, */
55 /* arising in any way out of use or reliance upon this specification or any */
56 /* information herein. */
57 /* */
58 /* (c) Copyright IBM Corp. and others, 2016 - 2020 */
59 /* */
60 /********************************************************************************/
61
62 /* B.2.3.2. TpmToOsslSym.h */
63 /* B.2.3.2.1. Introduction */
64 /* This header file is used to splice the OpenSSL() library into the TPM code. */
65 /* The support required of a library are a hash module, a block cipher module and portions of a big
66 number library. All of the library-dependent headers should have the same guard to that only the
67 first one gets defined. */
68
69 #ifndef SYM_LIB_DEFINED
70 #define SYM_LIB_DEFINED
71 #define SYM_LIB_OSSL
72 #include <openssl/aes.h>
73 #if ALG_TDES
74 #include <openssl/des.h>
75 #endif
76
77 #if ALG_SM4
78 # if defined(OPENSSL_NO_SM4) || OPENSSL_VERSION_NUMBER < 0x10101010L
79 # undef ALG_SM4
80 # define ALG_SM4 ALG_NO
81 # elif OPENSSL_VERSION_NUMBER >= 0x10200000L
82 # include <openssl/sm4.h>
83 # else
84 // OpenSSL 1.1.1 keeps smX.h headers in the include/crypto directory,
85 // and they do not get installed as part of the libssl package
86
87 # define SM4_KEY_SCHEDULE 32
88
89 typedef struct SM4_KEY_st {
90 uint32_t rk[SM4_KEY_SCHEDULE];
91 } SM4_KEY;
92
93 int SM4_set_key(const uint8_t *key, SM4_KEY *ks);
94 void SM4_encrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks);
95 void SM4_decrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks);
96 # endif // OpenSSL < 1.2
97 #endif // ALG_SM4
98
99 #if ALG_CAMELLIA
100 #include <openssl/camellia.h>
101 #endif
102 #include <openssl/bn.h>
103 #include <openssl/ossl_typ.h>
104
105 /* B.2.2.3.2. Links to the OpenSSL symmetric algorithms */
106 // The Crypt functions that call the block encryption function use the parameters in the order:
107 // a) keySchedule
108 // b) in buffer
109 // c) out buffer Since open SSL uses the order in encryptoCall_t above, need to swizzle the values
110 // to the order required by the library.
111
112 #define SWIZZLE(keySchedule, in, out) \
113 (const BYTE *)(in), (BYTE *)(out), (void *)(keySchedule)
114
115 // Define the order of parameters to the library functions that do block encryption and decryption.
116
117 typedef void(*TpmCryptSetSymKeyCall_t)(
118 const BYTE *in,
119 BYTE *out,
120 void *keySchedule
121 );
122
123 #define SYM_ALIGNMENT 4 /* libtpms: keep old value */
124
125 /* B.2.2.3.3. Links to the OpenSSL AES code */
126 /* Macros to set up the encryption/decryption key schedules */
127
128 #define TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule) \
129 AES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleAES *)(schedule))
130 #define TpmCryptSetDecryptKeyAES(key, keySizeInBits, schedule) \
131 AES_set_decrypt_key((key), (keySizeInBits), (tpmKeyScheduleAES *)(schedule))
132
133 /* Macros to alias encryption calls to specific algorithms. This should be used
134 sparingly. Currently, only used by CryptSym.c and CryptRand.c */
135 /* When using these calls, to call the AES block encryption code, the caller should use:
136 TpmCryptEncryptAES(SWIZZLE(keySchedule, in, out)); */
137
138 #define TpmCryptEncryptAES AES_encrypt
139 #define TpmCryptDecryptAES AES_decrypt
140 #define tpmKeyScheduleAES AES_KEY
141
142 /* B.2.2.3.4. Links to the OpenSSL DES code */
143
144 #if ALG_TDES && 0 // libtpms changed
145 #include "TpmToOsslDesSupport_fp.h"
146 #endif
147
148 #define TpmCryptSetEncryptKeyTDES(key, keySizeInBits, schedule) \
149 TDES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule))
150 #define TpmCryptSetDecryptKeyTDES(key, keySizeInBits, schedule) \
151 TDES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule))
152
153 /* Macros to alias encryption calls to specific algorithms. This should be used
154 sparingly. Currently, only used by CryptRand.c */
155
156 #define TpmCryptEncryptTDES TDES_encrypt
157 #define TpmCryptDecryptTDES TDES_decrypt
158 #define tpmKeyScheduleTDES DES_key_schedule
159
160 #if ALG_TDES // libtpms added begin
161 #include "TpmToOsslDesSupport_fp.h"
162 #endif // libtpms added end
163
164 /* B.2.2.3.5. Links to the OpenSSL SM4 code */
165 /* Macros to set up the encryption/decryption key schedules */
166
167 #define TpmCryptSetEncryptKeySM4(key, keySizeInBits, schedule) \
168 SM4_set_key((key), (tpmKeyScheduleSM4 *)(schedule))
169 #define TpmCryptSetDecryptKeySM4(key, keySizeInBits, schedule) \
170 SM4_set_key((key), (tpmKeyScheduleSM4 *)(schedule))
171 /* Macros to alias encryption calls to specific algorithms. This should be used sparingly. */
172
173 #define TpmCryptEncryptSM4 SM4_encrypt
174 #define TpmCryptDecryptSM4 SM4_decrypt
175 #define tpmKeyScheduleSM4 SM4_KEY
176
177 /* B.2.2.3.6. Links to the OpenSSL CAMELLIA code */
178 /* Macros to set up the encryption/decryption key schedules */
179
180 #define TpmCryptSetEncryptKeyCAMELLIA(key, keySizeInBits, schedule) \
181 Camellia_set_key((key), (keySizeInBits), (tpmKeyScheduleCAMELLIA *)(schedule))
182 #define TpmCryptSetDecryptKeyCAMELLIA(key, keySizeInBits, schedule) \
183 Camellia_set_key((key), (keySizeInBits), (tpmKeyScheduleCAMELLIA *)(schedule))
184
185 /* Macros to alias encryption calls to specific algorithms. This should be used sparingly. */
186
187 #define TpmCryptEncryptCAMELLIA Camellia_encrypt
188 #define TpmCryptDecryptCAMELLIA Camellia_decrypt
189 #define tpmKeyScheduleCAMELLIA CAMELLIA_KEY
190
191 /* Forward reference */
192
193 // kgold typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t;
194
195 /* This definition would change if there were something to report */
196 #define SymLibSimulationEnd()
197 #endif // SYM_LIB_DEFINED