1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2024, Siemens AG
4  * All rights reserved.
5  * Copyright (c) 2024, Linaro Limited
6  *
7  * Based on the original code by Microsoft. Modified to support using
8  * TEE functions to provide cryptographic functionality.
9  *
10  * Portions Copyright Microsoft Corporation, see below for details:
11  *
12  * The copyright in this software is being made available under the BSD
13  * License, included below. This software may be subject to other third
14  * party and contributor rights, including patent rights, and no such
15  * rights are granted under this license.
16  *
17  * Copyright (c) 2018 Microsoft Corporation
18  *
19  * All rights reserved.
20  *
21  * BSD License
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions are
25  * met:
26  *
27  * Redistributions of source code must retain the above copyright notice,
28  * this list of conditions and the following disclaimer.
29  *
30  * Redistributions in binary form must reproduce the above copyright
31  * notice, this list of conditions and the following disclaimer in the
32  * documentation and/or other materials provided with the distribution.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
40  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  */
46 
47 /*
48  * This header file is used to 'splice' the TEE sym code into the TPM code.
49  */
50 
51 #ifndef SYM_LIB_DEFINED
52 #define SYM_LIB_DEFINED
53 
54 #define SYM_LIB_TEE
55 
56 #define SYM_ALIGNMENT RADIX_BYTES
57 
58 #include <tee_internal_api.h>
59 #include <stdint.h>
60 
61 /*
62  * The TEE does not export a key schedule, so these structs do not not
63  * really represent a key schedule but rather a copy of the key.
64  */
65 typedef struct {
66 	uint16_t keySizeInBytes;
67 	uint8_t key[32];
68 } tpmKeyScheduleAES;
69 
70 typedef struct {
71 	uint16_t keySizeInBytes;
72 	uint8_t key[24];
73 } tpmKeyScheduleTDES;
74 
75 typedef struct {
76 	uint16_t keySizeInBytes;
77 	uint8_t key[16];
78 } tpmKeyScheduleSM4;
79 
80 int TEE_SetKeyAES(tpmKeyScheduleAES *key_schedule, const uint8_t *key,
81 		  uint16_t keySizeInBytes);
82 int TEE_SetKeyTDES(tpmKeyScheduleTDES *key_schedule, const uint8_t *key,
83 		   uint16_t keySizeInBytes);
84 int TEE_SetKeySM4(tpmKeyScheduleSM4 *key_schedule, const uint8_t *key,
85 		  uint16_t keySizeInBytes);
86 
87 void TEE_AESEncrypt(uint8_t *out, const tpmKeyScheduleAES *key_schedule,
88 		    const uint8_t *in);
89 void TEE_AESDecrypt(uint8_t *out, const tpmKeyScheduleAES *key_schedule,
90 		    const uint8_t *in);
91 void TEE_TDESEncrypt(uint8_t *out, const tpmKeyScheduleTDES *key_schedule,
92 		     const uint8_t *in);
93 void TEE_TDESDecrypt(uint8_t *out, const tpmKeyScheduleTDES *key_schedule,
94 		     const uint8_t *in);
95 void TEE_SM4Encrypt(uint8_t *out, const tpmKeyScheduleSM4 *key_schedule,
96 		    const uint8_t *in);
97 void TEE_SM4Decrypt(uint8_t *out, const tpmKeyScheduleSM4 *key_schedule,
98 		    const uint8_t *in);
99 
100 /*
101  * Links to the TEE sym code
102  */
103 
104 #if ALG_CAMELLIA
105 #  undef ALG_CAMELLIA
106 #  define ALG_CAMELLIA ALG_NO
107 #endif
108 
109 /*
110  * Define the order of parameters to the library functions that do block
111  * encryption and decryption.
112  */
113 typedef void (*TpmCryptSetSymKeyCall_t)(void* keySchedule, BYTE* out, const BYTE* in);
114 
115 /*
116  * The Crypt functions that call the block encryption function use the
117  * parameters in the order:
118  *  1) keySchedule
119  *  2) in buffer
120  *  3) out buffer
121  * Since the functions TEE_Encrypt* uses a different order, we need to
122  * swizzle the values to the order required by the library.
123  */
124 #define SWIZZLE(keySchedule, in, out) \
125 	(BYTE *)(out), (void *)(keySchedule), (const BYTE *)(in)
126 
127 /*
128  * Macros to set up the encryption/decryption key schedules
129  */
130 /* AES */
131 #define TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule) \
132 	TEE_SetKeyAES((tpmKeyScheduleAES *)(schedule), key,    \
133 		      BITS_TO_BYTES(keySizeInBits))
134 #define TpmCryptSetDecryptKeyAES(key, keySizeInBits, schedule) \
135 	TEE_SetKeyAES((tpmKeyScheduleAES *)(schedule), key,    \
136 		      BITS_TO_BYTES(keySizeInBits))
137 
138 /* TDES */
139 #define TpmCryptSetEncryptKeyTDES(key, keySizeInBits, schedule) \
140 	TEE_SetKeyTDES((tpmKeyScheduleTDES *)(schedule), (key), \
141 		       BITS_TO_BYTES(keySizeInBits))
142 #define TpmCryptSetDecryptKeyTDES(key, keySizeInBits, schedule) \
143 	TEE_SetKeyTDES((tpmKeyScheduleTDES *)(schedule), (key), \
144 		       BITS_TO_BYTES(keySizeInBits))
145 
146 /* SM4 */
147 #define TpmCryptSetEncryptKeySM4(key, keySizeInBits, schedule) \
148 	TEE_SetKeySM4((tpmKeyScheduleSM4 *)(schedule), (key),  \
149 		      BITS_TO_BYTES(keySizeInBits))
150 #define TpmCryptSetDecryptKeySM4(key, keySizeInBits, schedule) \
151 	TEE_SetKeySM4((tpmKeyScheduleSM4 *)(schedule), (key),  \
152 		      BITS_TO_BYTES(keySizeInBits))
153 /*
154  * Macros to alias encryption calls to specific algorithms. This should be
155  * used sparingly. Currently, only used by CryptRand.c
156  *
157  * When using these calls, to call the AES block encryption code, the
158  * caller should use:
159  *      TpmCryptEncryptAES(SWIZZLE(keySchedule, in, out));
160  */
161 #define TpmCryptEncryptAES TEE_AESEncrypt
162 #define TpmCryptDecryptAES TEE_AESDecrypt
163 
164 #define TpmCryptEncryptTDES TEE_TDESEncrypt
165 #define TpmCryptDecryptTDES TEE_TDESDecrypt
166 
167 #define TpmCryptEncryptSM4 TEE_SM4Encrypt
168 #define TpmCryptDecryptSM4 TEE_SM4Decrypt
169 
170 typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t;
171 
172 /* This definition would change if there were something to report */
173 #define SymLibSimulationEnd()
174 
175 #endif /*SYM_LIB_DEFINED*/
176