]>
git.proxmox.com Git - libtpms.git/blob - src/tpm2/crypto/openssl/TpmToOsslSym.h
1 /********************************************************************************/
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 $ */
8 /* Licenses and Notices */
10 /* 1. Copyright Licenses: */
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. */
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. */
23 /* 2. Source Code Distribution Conditions: */
25 /* - Redistributions of Source Code must retain the above copyright licenses, */
26 /* this list of conditions and the following disclaimers. */
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. */
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. */
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. */
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. */
58 /* (c) Copyright IBM Corp. and others, 2016 - 2020 */
60 /********************************************************************************/
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. */
69 #ifndef SYM_LIB_DEFINED
70 #define SYM_LIB_DEFINED
72 #include <openssl/aes.h>
74 #include <openssl/des.h>
78 # if defined(OPENSSL_NO_SM4) || OPENSSL_VERSION_NUMBER < 0x10101010L
80 # define ALG_SM4 ALG_NO
81 # elif OPENSSL_VERSION_NUMBER >= 0x10200000L
82 # include <openssl/sm4.h>
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
87 # define SM4_KEY_SCHEDULE 32
89 typedef struct SM4_KEY_st
{
90 uint32_t rk
[SM4_KEY_SCHEDULE
];
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
100 #include <openssl/camellia.h>
102 #include <openssl/bn.h>
103 #include <openssl/ossl_typ.h>
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:
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.
112 #define SWIZZLE(keySchedule, in, out) \
113 (const BYTE *)(in), (BYTE *)(out), (void *)(keySchedule)
115 // Define the order of parameters to the library functions that do block encryption and decryption.
117 typedef void(*TpmCryptSetSymKeyCall_t
)(
123 #define SYM_ALIGNMENT 4 /* libtpms: keep old value */
125 /* B.2.2.3.3. Links to the OpenSSL AES code */
126 /* Macros to set up the encryption/decryption key schedules */
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))
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)); */
138 #define TpmCryptEncryptAES AES_encrypt
139 #define TpmCryptDecryptAES AES_decrypt
140 #define tpmKeyScheduleAES AES_KEY
142 /* B.2.2.3.4. Links to the OpenSSL DES code */
144 #if ALG_TDES && 0 // libtpms changed
145 #include "TpmToOsslDesSupport_fp.h"
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))
153 /* Macros to alias encryption calls to specific algorithms. This should be used
154 sparingly. Currently, only used by CryptRand.c */
156 #define TpmCryptEncryptTDES TDES_encrypt
157 #define TpmCryptDecryptTDES TDES_decrypt
158 #define tpmKeyScheduleTDES DES_key_schedule
160 #if ALG_TDES // libtpms added begin
161 #include "TpmToOsslDesSupport_fp.h"
162 #endif // libtpms added end
164 /* B.2.2.3.5. Links to the OpenSSL SM4 code */
165 /* Macros to set up the encryption/decryption key schedules */
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. */
173 #define TpmCryptEncryptSM4 SM4_encrypt
174 #define TpmCryptDecryptSM4 SM4_decrypt
175 #define tpmKeyScheduleSM4 SM4_KEY
177 /* B.2.2.3.6. Links to the OpenSSL CAMELLIA code */
178 /* Macros to set up the encryption/decryption key schedules */
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))
185 /* Macros to alias encryption calls to specific algorithms. This should be used sparingly. */
187 #define TpmCryptEncryptCAMELLIA Camellia_encrypt
188 #define TpmCryptDecryptCAMELLIA Camellia_decrypt
189 #define tpmKeyScheduleCAMELLIA CAMELLIA_KEY
191 /* Forward reference */
193 // kgold typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t;
195 /* This definition would change if there were something to report */
196 #define SymLibSimulationEnd()
197 #endif // SYM_LIB_DEFINED