1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2024, Linaro Limited
4  */
5 
6 #ifndef __TPMTOMBEDTLSHASH_H
7 #define __TPMTOMBEDTLSHASH_H
8 
9 #define HASH_ALIGNMENT RADIX_BYTES
10 
11 #include <stdint.h>
12 #include <mbedtls/sha1.h>
13 #include <mbedtls/sha256.h>
14 #include <mbedtls/sha512.h>
15 
16 /*
17  * Define the internal name used for each of the hash state structures to
18  * the name used by the library.
19  * These defines need to be known in all parts of the TPM so that the
20  * structure sizes can be properly computed when needed.
21  */
22 
23 #define tpmHashStateSHA1_t mbedtls_sha1_context
24 #define tpmHashStateSHA256_t mbedtls_sha256_context
25 #define tpmHashStateSHA384_t mbedtls_sha512_context
26 #define tpmHashStateSHA512_t mbedtls_sha512_context
27 
28 /*
29  * The defines below are only needed when compiling CryptHash.c or
30  * CryptSmac.c.
31  */
32 #ifdef _CRYPT_HASH_C_
33 
34 /*
35  * TPMCmd/tpm/src//crypt/CryptHash.c needs this to be defined here.
36  */
37 typedef BYTE *PBYTE;
38 
39 #define HASH_START_METHOD_DEF void(HASH_START_METHOD)(PANY_HASH_STATE state)
40 #define HASH_START(_hs) ((_hs)->def->method.start)(&(_hs)->state);
41 
42 #define HASH_DATA_METHOD_DEF						  \
43 	void(HASH_DATA_METHOD)(PANY_HASH_STATE state, const BYTE *buffer, \
44 			       size_t size)
45 #define HASH_DATA(_hs, dInSize, dIn)					  \
46 	((_hs)->def->method.data)(&(_hs)->state, dIn, dInSize)
47 
48 #define HASH_END_METHOD_DEF						  \
49 	void(HASH_END_METHOD)(PANY_HASH_STATE state, BYTE * buffer)
50 #define HASH_END(_hs, buffer) ((_hs)->def->method.end)(&(_hs)->state, buffer)
51 
52 #define HASH_STATE_COPY_METHOD_DEF					  \
53 	void(HASH_STATE_COPY_METHOD)(PANY_HASH_STATE to,		  \
54 				     PCANY_HASH_STATE from, size_t size)
55 #define HASH_STATE_COPY(hs_out, hs_in)					  \
56 	((hs_in)->def->method.copy)(&(hs_out)->state, &(hs_in)->state,	  \
57 				  (hs_in)->def->contextSize)
58 
59 #define HASH_STATE_EXPORT_METHOD_DEF					  \
60 	void(HASH_STATE_EXPORT_METHOD)(BYTE * to, PCANY_HASH_STATE from,  \
61 				       size_t size)
62 #define HASH_STATE_EXPORT(to, _hs)					  \
63 	((_hs)->def->method.copyOut)(					  \
64 		&(((BYTE *)(to))[offsetof(HASH_STATE, state)]),		  \
65 		&(_hs)->state, (_hs)->def->contextSize)
66 
67 #define HASH_STATE_IMPORT_METHOD_DEF					  \
68 	void(HASH_STATE_IMPORT_METHOD)(PANY_HASH_STATE to, const BYTE *from, \
69 				       size_t size)
70 #define HASH_STATE_IMPORT(_hs, from)					  \
71 	((_hs)->def->method.copyIn)(					  \
72 		&(_hs)->state,						  \
73 		&(((const BYTE *)(from))[offsetof(HASH_STATE, state)]),	  \
74 		(_hs)->def->contextSize)
75 
tpmHashStart_SHA1(mbedtls_sha1_context * ctx)76 static inline int tpmHashStart_SHA1(mbedtls_sha1_context *ctx)
77 {
78 	mbedtls_sha1_init(ctx);
79 	return mbedtls_sha1_starts(ctx);
80 }
81 #define tpmHashData_SHA1 mbedtls_sha1_update
tpmHashEnd_SHA1(mbedtls_sha1_context * ctx,BYTE * buffer)82 static inline int tpmHashEnd_SHA1(mbedtls_sha1_context *ctx, BYTE *buffer)
83 {
84 	int e = mbedtls_sha1_finish(ctx, buffer);
85 
86 	mbedtls_sha1_free(ctx);
87 	return e;
88 }
89 #define tpmHashStateCopy_SHA1 memcpy
90 #define tpmHashStateExport_SHA1 memcpy
91 #define tpmHashStateImport_SHA1 memcpy
92 
tpmHashStart_SHA256(mbedtls_sha256_context * ctx)93 static inline int tpmHashStart_SHA256(mbedtls_sha256_context *ctx)
94 {
95 	mbedtls_sha256_init(ctx);
96 	return mbedtls_sha256_starts(ctx, 0);
97 }
98 #define tpmHashData_SHA256 mbedtls_sha256_update
tpmHashEnd_SHA256(mbedtls_sha256_context * ctx,BYTE * buffer)99 static inline int tpmHashEnd_SHA256(mbedtls_sha256_context *ctx, BYTE *buffer)
100 {
101 	int e = mbedtls_sha256_finish(ctx, buffer);
102 	mbedtls_sha256_free(ctx);
103 	return e;
104 }
105 #define tpmHashStateCopy_SHA256 memcpy
106 #define tpmHashStateExport_SHA256 memcpy
107 #define tpmHashStateImport_SHA256 memcpy
108 
109 /* SHA-384 is implemented using SHA-512, only initialized differently. */
tpmHashStart_SHA384(mbedtls_sha512_context * ctx)110 static inline int tpmHashStart_SHA384(mbedtls_sha512_context *ctx)
111 {
112 	mbedtls_sha512_init(ctx);
113 	return mbedtls_sha512_starts(ctx, 1);
114 }
115 #define tpmHashData_SHA384 mbedtls_sha512_update
116 #define tpmHashEnd_SHA384 tpmHashEnd_SHA512
117 #define tpmHashStateCopy_SHA384 tpmHashStateCopy_SHA512
118 #define tpmHashStateExport_SHA384 tpmHashStateExport_SHA512
119 #define tpmHashStateImport_SHA384 tpmHashStateImport_SHA512
120 
tpmHashStart_SHA512(mbedtls_sha512_context * ctx)121 static inline int tpmHashStart_SHA512(mbedtls_sha512_context *ctx)
122 {
123 	mbedtls_sha512_init(ctx);
124 	return mbedtls_sha512_starts(ctx, 0);
125 }
126 #define tpmHashData_SHA512 mbedtls_sha512_update
tpmHashEnd_SHA512(mbedtls_sha512_context * ctx,BYTE * buffer)127 static inline int tpmHashEnd_SHA512(mbedtls_sha512_context *ctx, BYTE *buffer)
128 {
129 	int e = mbedtls_sha512_finish(ctx, buffer);
130 	mbedtls_sha512_free(ctx);
131 	return e;
132 }
133 #define tpmHashStateCopy_SHA512 memcpy
134 #define tpmHashStateExport_SHA512 memcpy
135 #define tpmHashStateImport_SHA512 memcpy
136 
137 #endif /*_CRYPT_HASH_C_*/
138 
139 #define LibHashInit()
140 #define HashLibSimulationEnd()
141 
142 #endif /*__TPMTOMBEDTLSHASH_H*/
143