blob: 422664eb135c2a0195d61d30d22853274c9971a7 [file] [log] [blame]
Balint Dobszayefd67b32024-11-14 17:44:02 +01001From 3e974d38472520577aead8f861e71ac48d2acd5e Mon Sep 17 00:00:00 2001
2From: Balint Dobszay <balint.dobszay@arm.com>
3Date: Wed, 25 Sep 2024 17:41:43 +0200
4Subject: [PATCH 1/3] Add MbedTLS crypto port
5
6Add a new crypto port that uses MbedTLS. The code is based on the crypto
7port code for OpenSSL and WolfSSL found in this repo.
8
9The autotools build system is changed to use MbedTLS by default. This
10makes it possible to simply build the TPM and the simulator for the
11host system, and run e.g. tpm2_tools commands against it. Make sure that
12MbedTLS can be found by pkg-config, i.e. it's either installed to some
13system default location, or pkg-config is configured to find the custom
14installation. Tested with MbedTLS v3.6.0, the older v2.x that comes with
15Ubuntu 22.04 and 24.04 is not compatible.
16
17The build on the host system is done by these commands:
18cd TPMCmd
19./bootstrap
20./configure
21make
22
23To start the simulator, run:
24./TPMCmd/Simulator/src/tpm2-simulator -m
25
26This creates an NVChip file, which is the non-volatile memory of the
27TPM. If something goes wrong and the TPM ends up in a bad state, just
28delete this file and restart the simulator.
29
30To run tpm2_tools commands against the sim, in a separate terminal run:
31export TPM2TOOLS_TCTI=mssim:host=localhost,port=2321
32tpm2_startup -c
33tpm2_getrandom --hex 4
34...
35
36Signed-off-by: Balint Dobszay <balint.dobszay@arm.com>
37Change-Id: I0abfab17918f0b1917e9ca30d30f7fe781cceca2
38---
39 TPMCmd/configure.ac | 8 +-
40 TPMCmd/tpm/include/Mbed/TpmToMbedHash.h | 158 +++++++
41 TPMCmd/tpm/include/Mbed/TpmToMbedMath.h | 87 ++++
42 TPMCmd/tpm/include/Mbed/TpmToMbedSym.h | 98 +++++
43 .../tpm/include/prototypes/TpmToMbedMath_fp.h | 160 +++++++
44 .../include/prototypes/TpmToMbedSupport_fp.h | 44 ++
45 .../tpm/include/prototypes/TpmToMbedSym_fp.h | 51 +++
46 TPMCmd/tpm/src/crypt/mbed/TpmToMbedMath.c | 407 ++++++++++++++++++
47 TPMCmd/tpm/src/crypt/mbed/TpmToMbedSupport.c | 46 ++
48 TPMCmd/tpm/src/crypt/mbed/TpmToMbedSym.c | 94 ++++
49 10 files changed, 1149 insertions(+), 4 deletions(-)
50 create mode 100644 TPMCmd/tpm/include/Mbed/TpmToMbedHash.h
51 create mode 100644 TPMCmd/tpm/include/Mbed/TpmToMbedMath.h
52 create mode 100644 TPMCmd/tpm/include/Mbed/TpmToMbedSym.h
53 create mode 100644 TPMCmd/tpm/include/prototypes/TpmToMbedMath_fp.h
54 create mode 100644 TPMCmd/tpm/include/prototypes/TpmToMbedSupport_fp.h
55 create mode 100644 TPMCmd/tpm/include/prototypes/TpmToMbedSym_fp.h
56 create mode 100644 TPMCmd/tpm/src/crypt/mbed/TpmToMbedMath.c
57 create mode 100644 TPMCmd/tpm/src/crypt/mbed/TpmToMbedSupport.c
58 create mode 100644 TPMCmd/tpm/src/crypt/mbed/TpmToMbedSym.c
59
60diff --git a/TPMCmd/configure.ac b/TPMCmd/configure.ac
61index 58a74b4..d3ebfba 100644
62--- a/TPMCmd/configure.ac
63+++ b/TPMCmd/configure.ac
64@@ -51,16 +51,16 @@ AC_ARG_ENABLE(usedeviceid,
65 AS_HELP_STRING([--enable-usedeviceid],
66 [tpm simulator get seeds derived from hardware parameters. Seeds are not derived from secure hardware source.]))
67
68-PKG_CHECK_MODULES([LIBCRYPTO], [libcrypto])
69+PKG_CHECK_MODULES([LIBCRYPTO], [mbedcrypto])
70 AS_IF([test "x$enable_usedeviceid" = "xyes"], [
71 PKG_CHECK_MODULES([LIBUDEV], [libudev])
72 [ADDITIONAL_LIBS="-ludev"]
73 ])
74 AX_PTHREAD([], [AC_MSG_ERROR([requires pthread])])
75
76-AC_DEFINE([HASH_LIB], [Ossl], [Crypto lib for hash algorithms])
77-AC_DEFINE([SYM_LIB], [Ossl], [Crypto lib for symmetric encryption algorithms])
78-AC_DEFINE([MATH_LIB], [Ossl], [Crypto lib for bignum operations])
79+AC_DEFINE([HASH_LIB], [Mbed], [Crypto lib for hash algorithms])
80+AC_DEFINE([SYM_LIB], [Mbed], [Crypto lib for symmetric encryption algorithms])
81+AC_DEFINE([MATH_LIB], [Mbed], [Crypto lib for bignum operations])
82
83 ADD_COMPILER_FLAG([-std=gnu11])
84 ADD_COMPILER_FLAG([-Werror])
85diff --git a/TPMCmd/tpm/include/Mbed/TpmToMbedHash.h b/TPMCmd/tpm/include/Mbed/TpmToMbedHash.h
86new file mode 100644
87index 0000000..2fec472
88--- /dev/null
89+++ b/TPMCmd/tpm/include/Mbed/TpmToMbedHash.h
90@@ -0,0 +1,158 @@
91+/* Microsoft Reference Implementation for TPM 2.0
92+ *
93+ * The copyright in this software is being made available under the BSD License,
94+ * included below. This software may be subject to other third party and
95+ * contributor rights, including patent rights, and no such rights are granted
96+ * under this license.
97+ *
98+ * Copyright (c) Microsoft Corporation
99+ * Copyright (c) 2024, Arm Limited
100+ *
101+ * All rights reserved.
102+ *
103+ * BSD License
104+ *
105+ * Redistribution and use in source and binary forms, with or without modification,
106+ * are permitted provided that the following conditions are met:
107+ *
108+ * Redistributions of source code must retain the above copyright notice, this list
109+ * of conditions and the following disclaimer.
110+ *
111+ * Redistributions in binary form must reproduce the above copyright notice, this
112+ * list of conditions and the following disclaimer in the documentation and/or
113+ * other materials provided with the distribution.
114+ *
115+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
116+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
117+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
118+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
119+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
120+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
121+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
122+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
123+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
124+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125+ */
126+
127+#ifndef HASH_LIB_DEFINED
128+#define HASH_LIB_DEFINED
129+
130+#define HASH_LIB_MBED
131+
132+#define HASH_ALIGNMENT RADIX_BYTES
133+
134+#if ALG_SM3_256
135+# undef ALG_SM3_256
136+# define ALG_SM3_256 ALG_NO
137+# error "SM3 is not available"
138+#endif
139+
140+#include <mbedtls/sha1.h>
141+#include <mbedtls/sha256.h>
142+#include <mbedtls/sha512.h>
143+
144+#define tpmHashStateSHA1_t mbedtls_sha1_context
145+#define tpmHashStateSHA256_t mbedtls_sha256_context
146+#define tpmHashStateSHA384_t mbedtls_sha512_context
147+#define tpmHashStateSHA512_t mbedtls_sha512_context
148+
149+#ifdef _CRYPT_HASH_C_
150+
151+typedef BYTE* PBYTE;
152+typedef const BYTE* PCBYTE;
153+
154+// Initialize the hash context
155+# define HASH_START_METHOD_DEF void(HASH_START_METHOD)(PANY_HASH_STATE state)
156+# define HASH_START(hashState) ((hashState)->def->method.start)(&(hashState)->state);
157+
158+// Add data to the hash
159+# define HASH_DATA_METHOD_DEF \
160+ void(HASH_DATA_METHOD)(PANY_HASH_STATE state, PCBYTE buffer, size_t size)
161+# define HASH_DATA(hashState, dInSize, dIn) \
162+ ((hashState)->def->method.data)(&(hashState)->state, dIn, dInSize)
163+
164+// Finalize the hash and get the digest
165+# define HASH_END_METHOD_DEF \
166+ void(HASH_END_METHOD)(PANY_HASH_STATE state, BYTE * buffer)
167+# define HASH_END(hashState, buffer) \
168+ ((hashState)->def->method.end)(&(hashState)->state, buffer)
169+
170+// Copy the hash context
171+// Note: For import, export, and copy, memcpy() is used since there is no
172+// reformatting necessary between the internal and external forms.
173+# define HASH_STATE_COPY_METHOD_DEF \
174+ void(HASH_STATE_COPY_METHOD)( \
175+ PANY_HASH_STATE to, PCANY_HASH_STATE from, size_t size)
176+# define HASH_STATE_COPY(hashStateOut, hashStateIn) \
177+ ((hashStateIn)->def->method.copy)(&(hashStateOut)->state, \
178+ &(hashStateIn)->state, \
179+ (hashStateIn)->def->contextSize)
180+
181+// Copy (with reformatting when necessary) an internal hash structure to an
182+// external blob
183+# define HASH_STATE_EXPORT_METHOD_DEF \
184+ void(HASH_STATE_EXPORT_METHOD)(BYTE * to, PCANY_HASH_STATE from, size_t size)
185+# define HASH_STATE_EXPORT(to, hashStateFrom) \
186+ ((hashStateFrom)->def->method.copyOut)( \
187+ &(((BYTE*)(to))[offsetof(HASH_STATE, state)]), \
188+ &(hashStateFrom)->state, \
189+ (hashStateFrom)->def->contextSize)
190+
191+// Copy from an external blob to an internal formate (with reformatting when
192+// necessary
193+# define HASH_STATE_IMPORT_METHOD_DEF \
194+ void(HASH_STATE_IMPORT_METHOD)(PANY_HASH_STATE to, const BYTE* from, size_t size)
195+# define HASH_STATE_IMPORT(hashStateTo, from) \
196+ ((hashStateTo)->def->method.copyIn)( \
197+ &(hashStateTo)->state, \
198+ &(((const BYTE*)(from))[offsetof(HASH_STATE, state)]), \
199+ (hashStateTo)->def->contextSize)
200+
201+static inline int sha256_start(void *hash_state)
202+{
203+ return mbedtls_sha256_starts(hash_state, 0);
204+}
205+
206+static inline int sha384_start(void *hash_state)
207+{
208+ return mbedtls_sha512_starts(hash_state, 1);
209+}
210+
211+static inline int sha512_start(void *hash_state)
212+{
213+ return mbedtls_sha512_starts(hash_state, 0);
214+}
215+
216+// Function aliases. The code in CryptHash.c uses the internal designation for the
217+// functions. These need to be translated to the function names of the library.
218+# define tpmHashStart_SHA1 mbedtls_sha1_starts
219+# define tpmHashData_SHA1 mbedtls_sha1_update
220+# define tpmHashEnd_SHA1 mbedtls_sha1_finish
221+# define tpmHashStateCopy_SHA1 memcpy
222+# define tpmHashStateExport_SHA1 memcpy
223+# define tpmHashStateImport_SHA1 memcpy
224+# define tpmHashStart_SHA256 sha256_start
225+# define tpmHashData_SHA256 mbedtls_sha256_update
226+# define tpmHashEnd_SHA256 mbedtls_sha256_finish
227+# define tpmHashStateCopy_SHA256 memcpy
228+# define tpmHashStateExport_SHA256 memcpy
229+# define tpmHashStateImport_SHA256 memcpy
230+# define tpmHashStart_SHA384 sha384_start
231+# define tpmHashData_SHA384 mbedtls_sha512_update
232+# define tpmHashEnd_SHA384 mbedtls_sha512_finish
233+# define tpmHashStateCopy_SHA384 memcpy
234+# define tpmHashStateExport_SHA384 memcpy
235+# define tpmHashStateImport_SHA384 memcpy
236+# define tpmHashStart_SHA512 sha512_start
237+# define tpmHashData_SHA512 mbedtls_sha512_update
238+# define tpmHashEnd_SHA512 mbedtls_sha512_finish
239+# define tpmHashStateCopy_SHA512 memcpy
240+# define tpmHashStateExport_SHA512 memcpy
241+# define tpmHashStateImport_SHA512 memcpy
242+
243+#endif // _CRYPT_HASH_C_
244+
245+#define LibHashInit()
246+#define HashLibSimulationEnd()
247+
248+#endif // HASH_LIB_DEFINED
249diff --git a/TPMCmd/tpm/include/Mbed/TpmToMbedMath.h b/TPMCmd/tpm/include/Mbed/TpmToMbedMath.h
250new file mode 100644
251index 0000000..59edef8
252--- /dev/null
253+++ b/TPMCmd/tpm/include/Mbed/TpmToMbedMath.h
254@@ -0,0 +1,87 @@
255+/* Microsoft Reference Implementation for TPM 2.0
256+ *
257+ * The copyright in this software is being made available under the BSD License,
258+ * included below. This software may be subject to other third party and
259+ * contributor rights, including patent rights, and no such rights are granted
260+ * under this license.
261+ *
262+ * Copyright (c) Microsoft Corporation
263+ * Copyright (c) 2024, Arm Limited
264+ *
265+ * All rights reserved.
266+ *
267+ * BSD License
268+ *
269+ * Redistribution and use in source and binary forms, with or without modification,
270+ * are permitted provided that the following conditions are met:
271+ *
272+ * Redistributions of source code must retain the above copyright notice, this list
273+ * of conditions and the following disclaimer.
274+ *
275+ * Redistributions in binary form must reproduce the above copyright notice, this
276+ * list of conditions and the following disclaimer in the documentation and/or
277+ * other materials provided with the distribution.
278+ *
279+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
280+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
282+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
283+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
284+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
285+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
286+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
287+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
288+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289+ */
290+
291+#ifndef MATH_LIB_DEFINED
292+#define MATH_LIB_DEFINED
293+
294+#define MATH_LIB_MBED
295+
296+#include <mbedtls/bignum.h>
297+#include <mbedtls/ecp.h>
298+#include <mbedtls/psa_util.h>
299+
300+// Make sure that the library is using the correct size for a crypt word
301+#if (defined MBEDTLS_HAVE_INT32 && (RADIX_BITS != 32)) || \
302+ (defined MBEDTLS_HAVE_INT64 && (RADIX_BITS != 64))
303+# error MbedTLS library is using different radix
304+#endif
305+
306+#define MBEDTLS_OK 0
307+
308+#define MPI_INITIALIZED(name, initializer) \
309+ mbedtls_mpi _##name; \
310+ mbedtls_mpi* name = MpiInitialize(&_##name); \
311+ BnToMbed(name, initializer);
312+
313+#define MPI_DELETE(name) \
314+ mbedtls_mpi_free(&_##name);
315+
316+#define POINT_CREATE(name) \
317+ mbedtls_ecp_point name; \
318+ mbedtls_ecp_point_init(&name);
319+
320+#define POINT_DELETE(name) \
321+ mbedtls_ecp_point_free(&name);
322+
323+typedef struct
324+{
325+ const ECC_CURVE_DATA *C; // the TPM curve values
326+ mbedtls_ecp_group G; // group parameters
327+} MBED_CURVE_DATA;
328+
329+typedef MBED_CURVE_DATA *bigCurve;
330+
331+#define AccessCurveData(E) ((E)->C)
332+
333+#define CURVE_INITIALIZED(name, initializer) \
334+ MBED_CURVE_DATA _##name; \
335+ bigCurve name = BnCurveInitialize(&_##name, initializer)
336+
337+#define CURVE_FREE(name) BnCurveFree(name)
338+
339+#define MathLibSimulationEnd()
340+
341+#endif // MATH_LIB_DEFINED
342diff --git a/TPMCmd/tpm/include/Mbed/TpmToMbedSym.h b/TPMCmd/tpm/include/Mbed/TpmToMbedSym.h
343new file mode 100644
344index 0000000..db697d0
345--- /dev/null
346+++ b/TPMCmd/tpm/include/Mbed/TpmToMbedSym.h
347@@ -0,0 +1,98 @@
348+/* Microsoft Reference Implementation for TPM 2.0
349+ *
350+ * The copyright in this software is being made available under the BSD License,
351+ * included below. This software may be subject to other third party and
352+ * contributor rights, including patent rights, and no such rights are granted
353+ * under this license.
354+ *
355+ * Copyright (c) Microsoft Corporation
356+ * Copyright (c) 2024, Arm Limited
357+ *
358+ * All rights reserved.
359+ *
360+ * BSD License
361+ *
362+ * Redistribution and use in source and binary forms, with or without modification,
363+ * are permitted provided that the following conditions are met:
364+ *
365+ * Redistributions of source code must retain the above copyright notice, this list
366+ * of conditions and the following disclaimer.
367+ *
368+ * Redistributions in binary form must reproduce the above copyright notice, this
369+ * list of conditions and the following disclaimer in the documentation and/or
370+ * other materials provided with the distribution.
371+ *
372+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
373+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
374+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
375+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
376+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
377+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
378+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
379+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
380+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
381+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
382+ */
383+
384+#ifndef SYM_LIB_DEFINED
385+#define SYM_LIB_DEFINED
386+
387+#define SYM_LIB_MBED
388+
389+#include <mbedtls/aes.h>
390+
391+#if ALG_TDES
392+# include <mbedtls/des.h>
393+#endif
394+
395+#if ALG_SM4
396+# undef ALG_SM4
397+# define ALG_SM4 ALG_NO
398+# error "SM4 is not available"
399+#endif
400+
401+#if ALG_CAMELLIA
402+# include <mbedtls/camellia.h>
403+#endif
404+
405+#include "TpmToMbedSym_fp.h"
406+
407+#define SWIZZLE(keySchedule, in, out) \
408+ (const BYTE*)(in), (BYTE*)(out), (void*)(keySchedule)
409+
410+typedef void (*TpmCryptSetSymKeyCall_t)(const BYTE* in, BYTE* out, void* keySchedule);
411+
412+#define SYM_ALIGNMENT RADIX_BYTES
413+
414+#define TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule) \
415+ mbedtls_aes_setkey_enc((tpmKeyScheduleAES*)(schedule), key, keySizeInBits)
416+#define TpmCryptSetDecryptKeyAES(key, keySizeInBits, schedule) \
417+ mbedtls_aes_setkey_dec((tpmKeyScheduleAES*)(schedule), key, keySizeInBits)
418+
419+#define TpmCryptEncryptAES AES_encrypt
420+#define TpmCryptDecryptAES AES_decrypt
421+#define tpmKeyScheduleAES mbedtls_aes_context
422+
423+#define TpmCryptSetEncryptKeyTDES(key, keySizeInBits, schedule) \
424+ TDES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES*)(schedule))
425+#define TpmCryptSetDecryptKeyTDES(key, keySizeInBits, schedule) \
426+ TDES_set_decrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES*)(schedule))
427+
428+#define TpmCryptEncryptTDES(in, out, keySchedule) mbedtls_des3_crypt_ecb((keySchedule), (in), (out))
429+#define TpmCryptDecryptTDES(in, out, keySchedule) mbedtls_des3_crypt_ecb((keySchedule), (in), (out))
430+#define tpmKeyScheduleTDES mbedtls_des3_context
431+
432+#define TpmCryptSetEncryptKeyCAMELLIA(key, keySizeInBits, schedule) \
433+ mbedtls_camellia_setkey_enc((tpmKeyScheduleCAMELLIA*)(schedule), (key), (keySizeInBits))
434+#define TpmCryptSetDecryptKeyCAMELLIA(key, keySizeInBits, schedule) \
435+ mbedtls_camellia_setkey_dec((tpmKeyScheduleCAMELLIA*)(schedule), (key), (keySizeInBits))
436+
437+#define TpmCryptEncryptCAMELLIA CAMELLIA_encrypt
438+#define TpmCryptDecryptCAMELLIA CAMELLIA_decrypt
439+#define tpmKeyScheduleCAMELLIA mbedtls_camellia_context
440+
441+typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t;
442+
443+#define SymLibSimulationEnd()
444+
445+#endif // SYM_LIB_DEFINED
446diff --git a/TPMCmd/tpm/include/prototypes/TpmToMbedMath_fp.h b/TPMCmd/tpm/include/prototypes/TpmToMbedMath_fp.h
447new file mode 100644
448index 0000000..ac03575
449--- /dev/null
450+++ b/TPMCmd/tpm/include/prototypes/TpmToMbedMath_fp.h
451@@ -0,0 +1,160 @@
452+/* Microsoft Reference Implementation for TPM 2.0
453+ *
454+ * The copyright in this software is being made available under the BSD License,
455+ * included below. This software may be subject to other third party and
456+ * contributor rights, including patent rights, and no such rights are granted
457+ * under this license.
458+ *
459+ * Copyright (c) Microsoft Corporation
460+ * Copyright (c) 2024, Arm Limited
461+ *
462+ * All rights reserved.
463+ *
464+ * BSD License
465+ *
466+ * Redistribution and use in source and binary forms, with or without modification,
467+ * are permitted provided that the following conditions are met:
468+ *
469+ * Redistributions of source code must retain the above copyright notice, this list
470+ * of conditions and the following disclaimer.
471+ *
472+ * Redistributions in binary form must reproduce the above copyright notice, this
473+ * list of conditions and the following disclaimer in the documentation and/or
474+ * other materials provided with the distribution.
475+ *
476+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
477+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
478+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
479+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
480+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
481+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
482+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
483+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
484+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
485+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
486+ */
487+
488+#ifndef _TPM_TO_MBED_MATH_FP_H_
489+#define _TPM_TO_MBED_MATH_FP_H_
490+
491+#ifdef MATH_LIB_MBED
492+
493+void BnFromMbed(bigNum bn, mbedtls_mpi *mbedBn);
494+void BnToMbed(mbedtls_mpi *toInit, bigConst initializer);
495+mbedtls_mpi *MpiInitialize(mbedtls_mpi *toInit);
496+
497+# if LIBRARY_COMPATIBILITY_CHECK
498+BOOL MathLibraryCompatibilityCheck(void);
499+# endif
500+
501+//*** BnModMult()
502+// This function does a modular multiply. It first does a multiply and then a divide
503+// and returns the remainder of the divide.
504+// Return Type: BOOL
505+// TRUE(1) success
506+// FALSE(0) failure in operation
507+LIB_EXPORT BOOL BnModMult(bigNum result, bigConst op1, bigConst op2, bigConst modulus);
508+
509+//*** BnMult()
510+// Multiplies two numbers
511+// Return Type: BOOL
512+// TRUE(1) success
513+// FALSE(0) failure in operation
514+LIB_EXPORT BOOL BnMult(bigNum result, bigConst multiplicand, bigConst multiplier);
515+
516+//*** BnDiv()
517+// This function divides two bigNum values. The function returns FALSE if
518+// there is an error in the operation.
519+// Return Type: BOOL
520+// TRUE(1) success
521+// FALSE(0) failure in operation
522+LIB_EXPORT BOOL BnDiv(
523+ bigNum quotient, bigNum remainder, bigConst dividend, bigConst divisor);
524+
525+# if ALG_RSA
526+//*** BnGcd()
527+// Get the greatest common divisor of two numbers
528+// Return Type: BOOL
529+// TRUE(1) success
530+// FALSE(0) failure in operation
531+LIB_EXPORT BOOL BnGcd(bigNum gcd, // OUT: the common divisor
532+ bigConst number1, // IN:
533+ bigConst number2 // IN:
534+);
535+
536+//***BnModExp()
537+// Do modular exponentiation using bigNum values. The conversion from a bignum_t to
538+// a bigNum is trivial as they are based on the same structure
539+// Return Type: BOOL
540+// TRUE(1) success
541+// FALSE(0) failure in operation
542+LIB_EXPORT BOOL BnModExp(bigNum result, // OUT: the result
543+ bigConst number, // IN: number to exponentiate
544+ bigConst exponent, // IN:
545+ bigConst modulus // IN:
546+);
547+
548+//*** BnModInverse()
549+// Modular multiplicative inverse
550+// Return Type: BOOL
551+// TRUE(1) success
552+// FALSE(0) failure in operation
553+LIB_EXPORT BOOL BnModInverse(bigNum result, bigConst number, bigConst modulus);
554+# endif // ALG_RSA
555+# if ALG_ECC
556+
557+//*** BnCurveInitialize()
558+// This function initializes the curve information structure. This
559+// structure points to the TPM-defined values for the curve, to the context for the
560+// number values in the frame, and to the defined group values.
561+// Return Type: bigCurve *
562+// NULL the TPM_ECC_CURVE is not valid or there was a problem in
563+// in initializing the curve data
564+// non-NULL points to 'E'
565+LIB_EXPORT bigCurve BnCurveInitialize(
566+ bigCurve E, // IN: curve structure to initialize
567+ TPM_ECC_CURVE curveId // IN: curve identifier
568+);
569+
570+//*** BnCurveFree()
571+// This function will free the allocated components of the curve and end the
572+// frame in which the curve data exists
573+LIB_EXPORT void BnCurveFree(bigCurve E);
574+
575+//*** BnEccModMult()
576+// This function does a point multiply of the form R = [d]S
577+// Return Type: BOOL
578+// TRUE(1) success
579+// FALSE(0) failure in operation; treat as result being point at infinity
580+LIB_EXPORT BOOL BnEccModMult(bigPoint R, // OUT: computed point
581+ pointConst S, // IN: point to multiply by 'd' (optional)
582+ bigConst d, // IN: scalar for [d]S
583+ bigCurve E);
584+
585+//*** BnEccModMult2()
586+// This function does a point multiply of the form R = [d]G + [u]Q
587+// Return Type: BOOL
588+// TRUE(1) success
589+// FALSE(0) failure in operation; treat as result being point at infinity
590+LIB_EXPORT BOOL BnEccModMult2(bigPoint R, // OUT: computed point
591+ pointConst S, // IN: optional point
592+ bigConst d, // IN: scalar for [d]S or [d]G
593+ pointConst Q, // IN: second point
594+ bigConst u, // IN: second scalar
595+ bigCurve E // IN: curve
596+);
597+
598+//** BnEccAdd()
599+// This function does addition of two points.
600+// Return Type: BOOL
601+// TRUE(1) success
602+// FALSE(0) failure in operation; treat as result being point at infinity
603+LIB_EXPORT BOOL BnEccAdd(bigPoint R, // OUT: computed point
604+ pointConst S, // IN: point to multiply by 'd'
605+ pointConst Q, // IN: second point
606+ bigCurve E // IN: curve
607+);
608+# endif // ALG_ECC
609+#endif // MATHLIB_MBED
610+
611+#endif // _TPM_TO_MBED_MATH_FP_H_
612diff --git a/TPMCmd/tpm/include/prototypes/TpmToMbedSupport_fp.h b/TPMCmd/tpm/include/prototypes/TpmToMbedSupport_fp.h
613new file mode 100644
614index 0000000..72e666e
615--- /dev/null
616+++ b/TPMCmd/tpm/include/prototypes/TpmToMbedSupport_fp.h
617@@ -0,0 +1,44 @@
618+/* Microsoft Reference Implementation for TPM 2.0
619+ *
620+ * The copyright in this software is being made available under the BSD License,
621+ * included below. This software may be subject to other third party and
622+ * contributor rights, including patent rights, and no such rights are granted
623+ * under this license.
624+ *
625+ * Copyright (c) Microsoft Corporation
626+ * Copyright (c) 2024, Arm Limited
627+ *
628+ * All rights reserved.
629+ *
630+ * BSD License
631+ *
632+ * Redistribution and use in source and binary forms, with or without modification,
633+ * are permitted provided that the following conditions are met:
634+ *
635+ * Redistributions of source code must retain the above copyright notice, this list
636+ * of conditions and the following disclaimer.
637+ *
638+ * Redistributions in binary form must reproduce the above copyright notice, this
639+ * list of conditions and the following disclaimer in the documentation and/or
640+ * other materials provided with the distribution.
641+ *
642+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
643+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
644+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
645+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
646+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
647+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
648+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
649+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
650+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
651+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
652+ */
653+
654+#ifndef _TPM_TO_MBED_SUPPORT_FP_H_
655+#define _TPM_TO_MBED_SUPPORT_FP_H_
656+
657+#if defined(HASH_LIB_MBED) || defined(MATH_LIB_MBED) || defined(SYM_LIB_MBED)
658+LIB_EXPORT int SupportLibInit(void);
659+#endif // HASH_LIB_MBED || MATH_LIB_MBED || SYM_LIB_MBED
660+
661+#endif // _TPM_TO_MBED_SUPPORT_FP_H_
662diff --git a/TPMCmd/tpm/include/prototypes/TpmToMbedSym_fp.h b/TPMCmd/tpm/include/prototypes/TpmToMbedSym_fp.h
663new file mode 100644
664index 0000000..7aad912
665--- /dev/null
666+++ b/TPMCmd/tpm/include/prototypes/TpmToMbedSym_fp.h
667@@ -0,0 +1,51 @@
668+/* Microsoft Reference Implementation for TPM 2.0
669+ *
670+ * The copyright in this software is being made available under the BSD License,
671+ * included below. This software may be subject to other third party and
672+ * contributor rights, including patent rights, and no such rights are granted
673+ * under this license.
674+ *
675+ * Copyright (c) Microsoft Corporation
676+ * Copyright (c) 2024, Arm Limited
677+ *
678+ * All rights reserved.
679+ *
680+ * BSD License
681+ *
682+ * Redistribution and use in source and binary forms, with or without modification,
683+ * are permitted provided that the following conditions are met:
684+ *
685+ * Redistributions of source code must retain the above copyright notice, this list
686+ * of conditions and the following disclaimer.
687+ *
688+ * Redistributions in binary form must reproduce the above copyright notice, this
689+ * list of conditions and the following disclaimer in the documentation and/or
690+ * other materials provided with the distribution.
691+ *
692+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
693+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
694+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
695+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
696+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
697+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
698+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
699+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
700+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
701+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
702+ */
703+
704+#ifndef _TPM_TO_MBED_SYM_FP_H_
705+#define _TPM_TO_MBED_SYM_FP_H_
706+
707+#ifdef SYM_LIB_MBED
708+
709+int TDES_set_encrypt_key(const BYTE *key, UINT16 keySizeInBits, void *keySchedule);
710+int TDES_set_decrypt_key(const BYTE *key, UINT16 keySizeInBits, void *keySchedule);
711+int AES_encrypt(const BYTE *in, BYTE *out, void *keySchedule);
712+int AES_decrypt(const BYTE *in, BYTE *out, void *keySchedule);
713+int CAMELLIA_encrypt(const BYTE *in, BYTE *out, void *keySchedule);
714+int CAMELLIA_decrypt(const BYTE *in, BYTE *out, void *keySchedule);
715+
716+#endif // SYM_LIB_MBED
717+
718+#endif // _TPM_TO_MBED_SYM_FP_H_
719diff --git a/TPMCmd/tpm/src/crypt/mbed/TpmToMbedMath.c b/TPMCmd/tpm/src/crypt/mbed/TpmToMbedMath.c
720new file mode 100644
721index 0000000..bad931b
722--- /dev/null
723+++ b/TPMCmd/tpm/src/crypt/mbed/TpmToMbedMath.c
724@@ -0,0 +1,407 @@
725+/* Microsoft Reference Implementation for TPM 2.0
726+ *
727+ * The copyright in this software is being made available under the BSD License,
728+ * included below. This software may be subject to other third party and
729+ * contributor rights, including patent rights, and no such rights are granted
730+ * under this license.
731+ *
732+ * Copyright (c) Microsoft Corporation
733+ * Copyright (c) 2024, Arm Limited
734+ *
735+ * All rights reserved.
736+ *
737+ * BSD License
738+ *
739+ * Redistribution and use in source and binary forms, with or without modification,
740+ * are permitted provided that the following conditions are met:
741+ *
742+ * Redistributions of source code must retain the above copyright notice, this list
743+ * of conditions and the following disclaimer.
744+ *
745+ * Redistributions in binary form must reproduce the above copyright notice, this
746+ * list of conditions and the following disclaimer in the documentation and/or
747+ * other materials provided with the distribution.
748+ *
749+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
750+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
751+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
752+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
753+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
754+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
755+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
756+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
757+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
758+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
759+ */
760+
761+#include "Tpm.h"
762+
763+#ifdef MATH_LIB_MBED
764+# include "BnConvert_fp.h"
765+# include "TpmToMbedMath_fp.h"
766+
767+// This function converts a mbedtls_mpi to a TPM bignum. In this implementation
768+// it is assumed that MbedTLS uses the same format for a big number as does the
769+// TPM -- an array of native-endian words in little-endian order.
770+void BnFromMbed(bigNum bn, mbedtls_mpi *mbedBn)
771+{
772+ uint32_t i;
773+
774+ if(bn != NULL) {
775+ pAssert((unsigned)mbedBn->private_n <= BnGetAllocated(bn));
776+
777+ for(i = 0; i < mbedBn->private_n; i++)
778+ bn->d[i] = mbedBn->private_p[i];
779+
780+ BnSetTop(bn, mbedBn->private_n);
781+ }
782+}
783+
784+// This function converts a TPM bignum to a mbedtls_mpi, and has the same
785+// assumptions as made by BnFromMbed()
786+void BnToMbed(mbedtls_mpi *toInit, bigConst initializer)
787+{
788+ uint32_t i;
789+
790+ if(toInit != NULL && initializer != NULL) {
791+ mbedtls_mpi_grow(toInit, initializer->size);
792+
793+ for(i = 0; i < initializer->size; i++)
794+ toInit->private_p[i] = initializer->d[i];
795+
796+ toInit->private_n = initializer->size;
797+ /* TPM bignums are never negative, we can fix the sign to 1 */
798+ toInit->private_s = 1;
799+ }
800+}
801+
802+mbedtls_mpi *MpiInitialize(mbedtls_mpi *toInit)
803+{
804+ mbedtls_mpi_init(toInit);
805+ return toInit;
806+}
807+
808+# if LIBRARY_COMPATIBILITY_CHECK
809+BOOL MathLibraryCompatibilityCheck(void)
810+{
811+ BN_VAR(tpmTemp, 64 * 8);
812+ crypt_uword_t i;
813+ TPM2B_TYPE(TEST, 16);
814+ TPM2B_TEST test = {{16,
815+ {0x0F,
816+ 0x0E,
817+ 0x0D,
818+ 0x0C,
819+ 0x0B,
820+ 0x0A,
821+ 0x09,
822+ 0x08,
823+ 0x07,
824+ 0x06,
825+ 0x05,
826+ 0x04,
827+ 0x03,
828+ 0x02,
829+ 0x01,
830+ 0x00}}};
831+ // Convert the test TPM2B to a bigNum
832+ BnFrom2B(tpmTemp, &test.b);
833+ MPI_INITIALIZED(mbedTemp, tpmTemp);
834+ // Make sure the values are consistent
835+ VERIFY(mbedTemp->private_n * sizeof(mbedtls_mpi_uint)
836+ == (int)tpmTemp->size * sizeof(crypt_uword_t));
837+ for(i = 0; i < tpmTemp->size; i++)
838+ VERIFY(((crypt_uword_t*)mbedTemp->private_p)[i] == tpmTemp->d[i]);
839+
840+ MPI_DELETE(mbedTemp);
841+ return 1;
842+Error:
843+ return 0;
844+}
845+# endif
846+
847+LIB_EXPORT BOOL BnModMult(bigNum result, bigConst op1, bigConst op2, bigConst modulus)
848+{
849+ BOOL OK;
850+ MPI_INITIALIZED(bnOp1, op1);
851+ MPI_INITIALIZED(bnOp2, op2);
852+ MPI_INITIALIZED(bnTemp, NULL);
853+ BN_VAR(temp, LARGEST_NUMBER_BITS * 2);
854+
855+ pAssert(BnGetAllocated(result) >= BnGetSize(modulus));
856+
857+ OK = (mbedtls_mpi_mul_mpi(bnTemp, bnOp1, bnOp2) == MBEDTLS_OK);
858+ if(OK) {
859+ BnFromMbed(temp, bnTemp);
860+ OK = BnDiv(NULL, result, temp, modulus);
861+ }
862+
863+ MPI_DELETE(bnOp1);
864+ MPI_DELETE(bnOp2);
865+ MPI_DELETE(bnTemp);
866+
867+ return OK;
868+}
869+
870+LIB_EXPORT BOOL BnMult(bigNum result, bigConst multiplicand, bigConst multiplier)
871+{
872+ BOOL OK;
873+ MPI_INITIALIZED(bnTemp, NULL);
874+ MPI_INITIALIZED(bnA, multiplicand);
875+ MPI_INITIALIZED(bnB, multiplier);
876+
877+ pAssert(result->allocated >= (BITS_TO_CRYPT_WORDS(
878+ BnSizeInBits(multiplicand) + BnSizeInBits(multiplier))));
879+
880+ OK = (mbedtls_mpi_mul_mpi(bnTemp, bnA, bnB) == MBEDTLS_OK);
881+ if(OK) {
882+ BnFromMbed(result, bnTemp);
883+ }
884+
885+ MPI_DELETE(bnTemp);
886+ MPI_DELETE(bnA);
887+ MPI_DELETE(bnB);
888+
889+ return OK;
890+}
891+
892+LIB_EXPORT BOOL BnDiv(bigNum quotient, bigNum remainder, bigConst dividend, bigConst divisor)
893+{
894+ BOOL OK;
895+ MPI_INITIALIZED(bnQ, quotient);
896+ MPI_INITIALIZED(bnR, remainder);
897+ MPI_INITIALIZED(bnDend, dividend);
898+ MPI_INITIALIZED(bnSor, divisor);
899+
900+ pAssert(!BnEqualZero(divisor));
901+ if(BnGetSize(dividend) < BnGetSize(divisor)) {
902+ if(quotient)
903+ BnSetWord(quotient, 0);
904+ if(remainder)
905+ BnCopy(remainder, dividend);
906+ OK = TRUE;
907+ } else {
908+ pAssert((quotient == NULL) ||
909+ (quotient->allocated >= (unsigned)(dividend->size - divisor->size)));
910+ pAssert((remainder == NULL) || (remainder->allocated >= divisor->size));
911+ OK = (mbedtls_mpi_div_mpi(bnQ, bnR, bnDend, bnSor) == MBEDTLS_OK);
912+ if(OK) {
913+ BnFromMbed(quotient, bnQ);
914+ BnFromMbed(remainder, bnR);
915+ }
916+ }
917+
918+ MPI_DELETE(bnQ);
919+ MPI_DELETE(bnR);
920+ MPI_DELETE(bnDend);
921+ MPI_DELETE(bnSor);
922+
923+ return OK;
924+}
925+
926+# if ALG_RSA
927+LIB_EXPORT BOOL BnGcd(bigNum gcd,bigConst number1, bigConst number2)
928+{
929+ BOOL OK;
930+ MPI_INITIALIZED(bnGcd, gcd);
931+ MPI_INITIALIZED(bn1, number1);
932+ MPI_INITIALIZED(bn2, number2);
933+
934+ pAssert(gcd != NULL);
935+ OK = (mbedtls_mpi_gcd(bnGcd, bn1, bn2) == MBEDTLS_OK);
936+ if(OK) {
937+ BnFromMbed(gcd, bnGcd);
938+ }
939+
940+ MPI_DELETE(bnGcd);
941+ MPI_DELETE(bn1);
942+ MPI_DELETE(bn2);
943+
944+ return OK;
945+}
946+
947+LIB_EXPORT BOOL BnModExp(bigNum result, bigConst number, bigConst exponent, bigConst modulus)
948+{
949+ BOOL OK;
950+ MPI_INITIALIZED(bnResult, result);
951+ MPI_INITIALIZED(bnN, number);
952+ MPI_INITIALIZED(bnE, exponent);
953+ MPI_INITIALIZED(bnM, modulus);
954+
955+ OK = (mbedtls_mpi_exp_mod(bnResult, bnN, bnE, bnM, NULL) == MBEDTLS_OK);
956+ if(OK) {
957+ BnFromMbed(result, bnResult);
958+ }
959+
960+ MPI_DELETE(bnResult);
961+ MPI_DELETE(bnN);
962+ MPI_DELETE(bnE);
963+ MPI_DELETE(bnM);
964+
965+ return OK;
966+}
967+
968+LIB_EXPORT BOOL BnModInverse(bigNum result, bigConst number, bigConst modulus)
969+{
970+ BOOL OK;
971+ MPI_INITIALIZED(bnResult, result);
972+ MPI_INITIALIZED(bnN, number);
973+ MPI_INITIALIZED(bnM, modulus);
974+
975+ OK = (mbedtls_mpi_inv_mod(bnResult, bnN, bnM) == MBEDTLS_OK);
976+ if(OK) {
977+ BnFromMbed(result, bnResult);
978+ }
979+
980+ MPI_DELETE(bnResult);
981+ MPI_DELETE(bnN);
982+ MPI_DELETE(bnM);
983+
984+ return OK;
985+}
986+# endif // TPM_ALG_RSA
987+
988+# if ALG_ECC
989+
990+void PointFromMbed(bigPoint pOut, mbedtls_ecp_point* pIn)
991+{
992+ BnFromMbed(pOut->x, &pIn->private_X);
993+ BnFromMbed(pOut->y, &pIn->private_Y);
994+ BnFromMbed(pOut->z, &pIn->private_Z);
995+}
996+
997+void PointToMbed(mbedtls_ecp_point* pOut, pointConst pIn)
998+{
999+ BnToMbed(&pOut->private_X, pIn->x);
1000+ BnToMbed(&pOut->private_Y, pIn->y);
1001+ BnToMbed(&pOut->private_Z, pIn->z);
1002+}
1003+
1004+LIB_EXPORT bigCurve BnCurveInitialize(bigCurve E, TPM_ECC_CURVE curveId)
1005+{
1006+ const ECC_CURVE_DATA* C = GetCurveData(curveId);
1007+
1008+ if(C == NULL)
1009+ E = NULL;
1010+
1011+ if(E != NULL) {
1012+ E->C = C;
1013+
1014+ mbedtls_ecp_group_init(&E->G);
1015+ BnToMbed(&E->G.P, C->prime);
1016+ BnToMbed(&E->G.A, C->a);
1017+ BnToMbed(&E->G.B, C->b);
1018+ BnToMbed(&E->G.N, C->order);
1019+
1020+ BnToMbed(&E->G.G.private_X, C->base.x);
1021+ BnToMbed(&E->G.G.private_Y, C->base.y);
1022+
1023+ E->G.pbits = mbedtls_mpi_bitlen(&E->G.P);
1024+ E->G.nbits = mbedtls_mpi_bitlen(&E->G.N);
1025+
1026+ E->G.private_h = 1;
1027+ }
1028+
1029+ return E;
1030+}
1031+
1032+LIB_EXPORT void BnCurveFree(bigCurve E)
1033+{
1034+ if(E)
1035+ mbedtls_ecp_group_free(&E->G);
1036+}
1037+
1038+LIB_EXPORT BOOL BnEccModMult(bigPoint R, pointConst S, bigConst d, bigCurve E)
1039+{
1040+ BOOL OK;
1041+ POINT_CREATE(pR);
1042+ POINT_CREATE(pS);
1043+ MPI_INITIALIZED(bnD, d);
1044+
1045+ if(S == NULL)
1046+ S = CurveGetG(AccessCurveData(E));
1047+
1048+ PointToMbed(&pS, S);
1049+
1050+ OK = (mbedtls_ecp_mul(&E->G, &pR, bnD, &pS, mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE)
1051+ == MBEDTLS_OK);
1052+ if(OK) {
1053+ PointFromMbed(R, &pR);
1054+ }
1055+
1056+ POINT_DELETE(pS);
1057+ POINT_DELETE(pR);
1058+ MPI_DELETE(bnD);
1059+
1060+ return !BnEqualZero(R->z);
1061+}
1062+
1063+LIB_EXPORT BOOL BnEccModMult2(bigPoint R, pointConst S, bigConst d, pointConst Q,
1064+ bigConst u, bigCurve E)
1065+{
1066+ BOOL OK;
1067+ POINT_CREATE(pR);
1068+ POINT_CREATE(pS);
1069+ POINT_CREATE(pQ);
1070+ MPI_INITIALIZED(bnD, d);
1071+ MPI_INITIALIZED(bnU, u);
1072+ MPI_INITIALIZED(bnPrime, CurveGetPrime(AccessCurveData(E)));
1073+ MPI_INITIALIZED(bnA, CurveGet_a(AccessCurveData(E)));
1074+
1075+ PointToMbed(&pQ, Q);
1076+
1077+ if(S == NULL)
1078+ S = CurveGetG(AccessCurveData(E));
1079+
1080+ PointToMbed(&pS, S);
1081+
1082+ OK = (mbedtls_ecp_muladd(&E->G, &pR, bnD, &pS, bnU, &pQ) == MBEDTLS_OK);
1083+ if(OK) {
1084+ PointFromMbed(R, &pR);
1085+ }
1086+
1087+ POINT_DELETE(pS);
1088+ POINT_DELETE(pQ);
1089+ POINT_DELETE(pR);
1090+
1091+ MPI_DELETE(bnD);
1092+ MPI_DELETE(bnU);
1093+ MPI_DELETE(bnPrime);
1094+ MPI_DELETE(bnA);
1095+
1096+ return !BnEqualZero(R->z);
1097+}
1098+
1099+LIB_EXPORT BOOL BnEccAdd(bigPoint R, pointConst S, pointConst Q, bigCurve E)
1100+{
1101+ BOOL OK;
1102+ mbedtls_mpi_uint mp;
1103+ POINT_CREATE(pR);
1104+ POINT_CREATE(pS);
1105+ POINT_CREATE(pQ);
1106+ MPI_INITIALIZED(bnA, CurveGet_a(AccessCurveData(E)));
1107+ MPI_INITIALIZED(bnMod, CurveGetPrime(AccessCurveData(E)));
1108+
1109+ (void)mp;
1110+ PointToMbed(&pS, S);
1111+ PointToMbed(&pQ, Q);
1112+
1113+ pAssert(1 == 0); // cannot implement with mbedtls
1114+
1115+ if(OK) {
1116+ PointFromMbed(R, &pR);
1117+ }
1118+
1119+ POINT_DELETE(pS);
1120+ POINT_DELETE(pQ);
1121+ POINT_DELETE(pR);
1122+
1123+ MPI_DELETE(bnA);
1124+ MPI_DELETE(bnMod);
1125+
1126+ return !BnEqualZero(R->z);
1127+}
1128+
1129+# endif // TPM_ALG_ECC
1130+
1131+#endif // MATH_LIB_MBED
1132\ No newline at end of file
1133diff --git a/TPMCmd/tpm/src/crypt/mbed/TpmToMbedSupport.c b/TPMCmd/tpm/src/crypt/mbed/TpmToMbedSupport.c
1134new file mode 100644
1135index 0000000..c1bdcf1
1136--- /dev/null
1137+++ b/TPMCmd/tpm/src/crypt/mbed/TpmToMbedSupport.c
1138@@ -0,0 +1,46 @@
1139+/* Microsoft Reference Implementation for TPM 2.0
1140+ *
1141+ * The copyright in this software is being made available under the BSD License,
1142+ * included below. This software may be subject to other third party and
1143+ * contributor rights, including patent rights, and no such rights are granted
1144+ * under this license.
1145+ *
1146+ * Copyright (c) Microsoft Corporation
1147+ * Copyright (c) 2024, Arm Limited
1148+ *
1149+ * All rights reserved.
1150+ *
1151+ * BSD License
1152+ *
1153+ * Redistribution and use in source and binary forms, with or without modification,
1154+ * are permitted provided that the following conditions are met:
1155+ *
1156+ * Redistributions of source code must retain the above copyright notice, this list
1157+ * of conditions and the following disclaimer.
1158+ *
1159+ * Redistributions in binary form must reproduce the above copyright notice, this
1160+ * list of conditions and the following disclaimer in the documentation and/or
1161+ * other materials provided with the distribution.
1162+ *
1163+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
1164+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1165+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1166+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
1167+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1168+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1169+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1170+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1171+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1172+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1173+ */
1174+#include "Tpm.h"
1175+#include <psa/crypto.h>
1176+
1177+#if defined(HASH_LIB_MBED) || defined(MATH_LIB_MBED) || defined(SYM_LIB_MBED)
1178+
1179+LIB_EXPORT int SupportLibInit(void)
1180+{
1181+ return psa_crypto_init() == PSA_SUCCESS;
1182+}
1183+
1184+#endif // HASH_LIB_MBED || MATH_LIB_MBED || SYM_LIB_MBED
1185diff --git a/TPMCmd/tpm/src/crypt/mbed/TpmToMbedSym.c b/TPMCmd/tpm/src/crypt/mbed/TpmToMbedSym.c
1186new file mode 100644
1187index 0000000..9f846f9
1188--- /dev/null
1189+++ b/TPMCmd/tpm/src/crypt/mbed/TpmToMbedSym.c
1190@@ -0,0 +1,94 @@
1191+/* Microsoft Reference Implementation for TPM 2.0
1192+ *
1193+ * The copyright in this software is being made available under the BSD License,
1194+ * included below. This software may be subject to other third party and
1195+ * contributor rights, including patent rights, and no such rights are granted
1196+ * under this license.
1197+ *
1198+ * Copyright (c) Microsoft Corporation
1199+ * Copyright (c) 2024, Arm Limited
1200+ *
1201+ * All rights reserved.
1202+ *
1203+ * BSD License
1204+ *
1205+ * Redistribution and use in source and binary forms, with or without modification,
1206+ * are permitted provided that the following conditions are met:
1207+ *
1208+ * Redistributions of source code must retain the above copyright notice, this list
1209+ * of conditions and the following disclaimer.
1210+ *
1211+ * Redistributions in binary form must reproduce the above copyright notice, this
1212+ * list of conditions and the following disclaimer in the documentation and/or
1213+ * other materials provided with the distribution.
1214+ *
1215+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
1216+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1217+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1218+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
1219+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1220+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1221+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1222+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1223+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1224+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1225+ */
1226+
1227+#include "Tpm.h"
1228+
1229+#ifdef SYM_LIB_MBED
1230+# include "TpmToMbedSym_fp.h"
1231+
1232+#if ALG_TDES
1233+#include <mbedtls/des.h>
1234+
1235+int TDES_set_encrypt_key(const BYTE *key, UINT16 keySizeInBits, void *keySchedule)
1236+{
1237+ BYTE key2[MBEDTLS_DES_KEY_SIZE * 2] = {};
1238+ BYTE key3[MBEDTLS_DES_KEY_SIZE * 3] = {};
1239+
1240+ if(keySizeInBits == 128) {
1241+ memcpy(key2, key, MBEDTLS_DES_KEY_SIZE * 2);
1242+ return mbedtls_des3_set2key_enc(keySchedule, key2);
1243+ } else {
1244+ memcpy(key3, key, MBEDTLS_DES_KEY_SIZE * 3);
1245+ return mbedtls_des3_set3key_enc(keySchedule, key3);
1246+ }
1247+}
1248+
1249+int TDES_set_decrypt_key(const BYTE *key, UINT16 keySizeInBits, void *keySchedule)
1250+{
1251+ BYTE key2[MBEDTLS_DES_KEY_SIZE * 2] = {};
1252+ BYTE key3[MBEDTLS_DES_KEY_SIZE * 3] = {};
1253+
1254+ if(keySizeInBits == 128) {
1255+ memcpy(key2, key, MBEDTLS_DES_KEY_SIZE * 2);
1256+ return mbedtls_des3_set2key_dec(keySchedule, key2);
1257+ } else {
1258+ memcpy(key3, key, MBEDTLS_DES_KEY_SIZE * 3);
1259+ return mbedtls_des3_set3key_dec(keySchedule, key3);
1260+ }
1261+}
1262+#endif
1263+
1264+int AES_encrypt(const BYTE *in, BYTE *out, void *keySchedule)
1265+{
1266+ return mbedtls_aes_crypt_ecb(keySchedule, MBEDTLS_AES_ENCRYPT, in, out);
1267+}
1268+
1269+int AES_decrypt(const BYTE *in, BYTE *out, void *keySchedule)
1270+{
1271+ return mbedtls_aes_crypt_ecb(keySchedule, MBEDTLS_AES_DECRYPT, in, out);
1272+}
1273+
1274+int CAMELLIA_encrypt(const BYTE *in, BYTE *out, void *keySchedule)
1275+{
1276+ return mbedtls_camellia_crypt_ecb(keySchedule, MBEDTLS_CAMELLIA_ENCRYPT, in, out);
1277+}
1278+
1279+int CAMELLIA_decrypt(const BYTE *in, BYTE *out, void *keySchedule)
1280+{
1281+ return mbedtls_camellia_crypt_ecb(keySchedule, MBEDTLS_CAMELLIA_DECRYPT, in, out);
1282+}
1283+
1284+#endif // SYM_LIB_MBED
1285--
12862.34.1
1287