4 Copyright (c) 2013, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <IndustryStandard/UefiTcgPlatform.h>
16 #include <Library/Tpm2CommandLib.h>
17 #include <Library/Tpm2DeviceLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
23 TPMI_ALG_HASH HashAlgo
;
27 STATIC INTERNAL_HASH_INFO mHashInfo
[] = {
28 {TPM_ALG_SHA1
, SHA1_DIGEST_SIZE
},
29 {TPM_ALG_SHA256
, SHA256_DIGEST_SIZE
},
30 {TPM_ALG_SM3_256
, SM3_256_DIGEST_SIZE
},
31 {TPM_ALG_SHA384
, SHA384_DIGEST_SIZE
},
32 {TPM_ALG_SHA512
, SHA512_DIGEST_SIZE
},
36 Return size of digest.
38 @param[in] HashAlgo Hash algorithm
40 @return size of digest
45 IN TPMI_ALG_HASH HashAlgo
50 for (Index
= 0; Index
< sizeof(mHashInfo
)/sizeof(mHashInfo
[0]); Index
++) {
51 if (mHashInfo
[Index
].HashAlgo
== HashAlgo
) {
52 return mHashInfo
[Index
].HashSize
;
59 Copy AuthSessionIn to TPM2 command buffer.
61 @param [in] AuthSessionIn Input AuthSession data
62 @param [out] AuthSessionOut Output AuthSession data in TPM2 command buffer
64 @return AuthSession size
68 CopyAuthSessionCommand (
69 IN TPMS_AUTH_COMMAND
*AuthSessionIn
, OPTIONAL
70 OUT UINT8
*AuthSessionOut
75 Buffer
= (UINT8
*)AuthSessionOut
;
78 // Add in Auth session
80 if (AuthSessionIn
!= NULL
) {
82 WriteUnaligned32 ((UINT32
*)Buffer
, SwapBytes32(AuthSessionIn
->sessionHandle
));
83 Buffer
+= sizeof(UINT32
);
86 WriteUnaligned16 ((UINT16
*)Buffer
, SwapBytes16 (AuthSessionIn
->nonce
.size
));
87 Buffer
+= sizeof(UINT16
);
89 CopyMem (Buffer
, AuthSessionIn
->nonce
.buffer
, AuthSessionIn
->nonce
.size
);
90 Buffer
+= AuthSessionIn
->nonce
.size
;
93 *(UINT8
*)Buffer
= *(UINT8
*)&AuthSessionIn
->sessionAttributes
;
94 Buffer
+= sizeof(UINT8
);
97 WriteUnaligned16 ((UINT16
*)Buffer
, SwapBytes16 (AuthSessionIn
->hmac
.size
));
98 Buffer
+= sizeof(UINT16
);
100 CopyMem (Buffer
, AuthSessionIn
->hmac
.buffer
, AuthSessionIn
->hmac
.size
);
101 Buffer
+= AuthSessionIn
->hmac
.size
;
104 WriteUnaligned32 ((UINT32
*)Buffer
, SwapBytes32(TPM_RS_PW
));
105 Buffer
+= sizeof(UINT32
);
108 WriteUnaligned16 ((UINT16
*)Buffer
, SwapBytes16(0));
109 Buffer
+= sizeof(UINT16
);
111 // sessionAttributes = 0
112 *(UINT8
*)Buffer
= 0x00;
113 Buffer
+= sizeof(UINT8
);
116 WriteUnaligned16 ((UINT16
*)Buffer
, SwapBytes16(0));
117 Buffer
+= sizeof(UINT16
);
120 return (UINT32
)(UINTN
)(Buffer
- (UINT8
*)AuthSessionOut
);
124 Copy AuthSessionIn from TPM2 response buffer.
126 @param [in] AuthSessionIn Input AuthSession data in TPM2 response buffer
127 @param [out] AuthSessionOut Output AuthSession data
129 @return AuthSession size
133 CopyAuthSessionResponse (
134 IN UINT8
*AuthSessionIn
,
135 OUT TPMS_AUTH_RESPONSE
*AuthSessionOut OPTIONAL
139 TPMS_AUTH_RESPONSE LocalAuthSessionOut
;
141 if (AuthSessionOut
== NULL
) {
142 AuthSessionOut
= &LocalAuthSessionOut
;
145 Buffer
= (UINT8
*)AuthSessionIn
;
148 AuthSessionOut
->nonce
.size
= SwapBytes16 (ReadUnaligned16 ((UINT16
*)Buffer
));
149 Buffer
+= sizeof(UINT16
);
151 CopyMem (AuthSessionOut
->nonce
.buffer
, Buffer
, AuthSessionOut
->nonce
.size
);
152 Buffer
+= AuthSessionOut
->nonce
.size
;
155 *(UINT8
*)&AuthSessionOut
->sessionAttributes
= *(UINT8
*)Buffer
;
156 Buffer
+= sizeof(UINT8
);
159 AuthSessionOut
->hmac
.size
= SwapBytes16 (ReadUnaligned16 ((UINT16
*)Buffer
));
160 Buffer
+= sizeof(UINT16
);
162 CopyMem (AuthSessionOut
->hmac
.buffer
, Buffer
, AuthSessionOut
->hmac
.size
);
163 Buffer
+= AuthSessionOut
->hmac
.size
;
165 return (UINT32
)(UINTN
)(Buffer
- (UINT8
*)AuthSessionIn
);