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