xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/include/Wolf/TpmToWolfSym.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 wolfcrypt library into the TPM code.
39 
40 #ifndef SYM_LIB_DEFINED
41 #define SYM_LIB_DEFINED
42 
43 #define SYM_LIB_WOLF
44 
45 #define SYM_ALIGNMENT   RADIX_BYTES
46 
47 #include <wolfssl/wolfcrypt/aes.h>
48 #include <wolfssl/wolfcrypt/des3.h>
49 
50 //***************************************************************
51 //** Links to the wolfCrypt AES code
52 //***************************************************************
53 #if ALG_SM4
54 #undef ALG_SM4
55 #define ALG_SM4 ALG_NO
56 //#error "SM4 is not available"
57 #endif
58 
59 #if ALG_CAMELLIA
60 #undef ALG_CAMELLIA
61 #define ALG_CAMELLIA ALG_NO
62 //#error "Camellia is not available"
63 #endif
64 
65 // Define the order of parameters to the library functions that do block encryption
66 // and decryption.
67 typedef void(*TpmCryptSetSymKeyCall_t)(
68     void        *keySchedule,
69     BYTE        *out,
70     const BYTE  *in
71     );
72 
73 // The Crypt functions that call the block encryption function use the parameters
74 // in the order:
75 //  1) keySchedule
76 //  2) in buffer
77 //  3) out buffer
78 // Since wolfcrypt uses the order in encryptoCall_t above, need to swizzle the
79 // values to the order required by the library.
80 #define SWIZZLE(keySchedule, in, out)                                   \
81     (void *)(keySchedule), (BYTE *)(out), (const BYTE *)(in)
82 
83 // Macros to set up the encryption/decryption key schedules
84 //
85 // AES:
86 #define TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule)            \
87     wc_AesSetKeyDirect((tpmKeyScheduleAES *)(schedule), key, BITS_TO_BYTES(keySizeInBits), 0, AES_ENCRYPTION)
88 #define TpmCryptSetDecryptKeyAES(key, keySizeInBits, schedule)            \
89     wc_AesSetKeyDirect((tpmKeyScheduleAES *)(schedule), key, BITS_TO_BYTES(keySizeInBits), 0, AES_DECRYPTION)
90 
91 // TDES:
92 #define TpmCryptSetEncryptKeyTDES(key, keySizeInBits, schedule)            \
93     TDES_setup_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule))
94 #define TpmCryptSetDecryptKeyTDES(key, keySizeInBits, schedule)            \
95     TDES_setup_decrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule))
96 
97 // Macros to alias encryption calls to specific algorithms. This should be used
98 // sparingly. Currently, only used by CryptRand.c
99 //
100 // When using these calls, to call the AES block encryption code, the caller
101 // should use:
102 //      TpmCryptEncryptAES(SWIZZLE(keySchedule, in, out));
103 #define TpmCryptEncryptAES          wc_AesEncryptDirect
104 #define TpmCryptDecryptAES          wc_AesDecryptDirect
105 #define tpmKeyScheduleAES           Aes
106 
107 #define TpmCryptEncryptTDES         TDES_encrypt
108 #define TpmCryptDecryptTDES         TDES_decrypt
109 #define tpmKeyScheduleTDES          Des3
110 
111 typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t;
112 
113 #if ALG_TDES
114 #include "TpmToWolfDesSupport_fp.h"
115 #endif
116 
117 // This definition would change if there were something to report
118 #define SymLibSimulationEnd()
119 
120 #endif // SYM_LIB_DEFINED
121