TianoCore EDK2 master
Loading...
Searching...
No Matches
Tpm2Session.c
Go to the documentation of this file.
1
13#include <Library/BaseLib.h>
14#include <Library/DebugLib.h>
15
16#pragma pack(1)
17
18typedef struct {
20 TPMI_DH_OBJECT TpmKey;
21 TPMI_DH_ENTITY Bind;
22 TPM2B_NONCE NonceCaller;
24 TPM_SE SessionType;
25 TPMT_SYM_DEF Symmetric;
26 TPMI_ALG_HASH AuthHash;
28
29typedef struct {
31 TPMI_SH_AUTH_SESSION SessionHandle;
32 TPM2B_NONCE NonceTPM;
34
35#pragma pack()
36
55EFIAPI
57 IN TPMI_DH_OBJECT TpmKey,
58 IN TPMI_DH_ENTITY Bind,
59 IN TPM2B_NONCE *NonceCaller,
61 IN TPM_SE SessionType,
62 IN TPMT_SYM_DEF *Symmetric,
63 IN TPMI_ALG_HASH AuthHash,
64 OUT TPMI_SH_AUTH_SESSION *SessionHandle,
65 OUT TPM2B_NONCE *NonceTPM
66 )
67{
68 EFI_STATUS Status;
71 UINT32 SendBufferSize;
72 UINT32 RecvBufferSize;
73 UINT8 *Buffer;
74
75 //
76 // Construct command
77 //
78 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_NO_SESSIONS);
79 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_StartAuthSession);
80
81 SendBuffer.TpmKey = SwapBytes32 (TpmKey);
82 SendBuffer.Bind = SwapBytes32 (Bind);
83 Buffer = (UINT8 *)&SendBuffer.NonceCaller;
84
85 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (NonceCaller->size));
86 Buffer += sizeof (UINT16);
87 CopyMem (Buffer, NonceCaller->buffer, NonceCaller->size);
88 Buffer += NonceCaller->size;
89
90 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Salt->size));
91 Buffer += sizeof (UINT16);
92 CopyMem (Buffer, Salt->secret, Salt->size);
93 Buffer += Salt->size;
94
95 *(TPM_SE *)Buffer = SessionType;
96 Buffer++;
97
98 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Symmetric->algorithm));
99 Buffer += sizeof (UINT16);
100 switch (Symmetric->algorithm) {
101 case TPM_ALG_NULL:
102 break;
103 case TPM_ALG_AES:
104 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Symmetric->keyBits.aes));
105 Buffer += sizeof (UINT16);
106 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Symmetric->mode.aes));
107 Buffer += sizeof (UINT16);
108 break;
109 case TPM_ALG_SM4:
110 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Symmetric->keyBits.SM4));
111 Buffer += sizeof (UINT16);
112 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Symmetric->mode.SM4));
113 Buffer += sizeof (UINT16);
114 break;
115 case TPM_ALG_SYMCIPHER:
116 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Symmetric->keyBits.sym));
117 Buffer += sizeof (UINT16);
118 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Symmetric->mode.sym));
119 Buffer += sizeof (UINT16);
120 break;
121 case TPM_ALG_XOR:
122 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Symmetric->keyBits.xor));
123 Buffer += sizeof (UINT16);
124 break;
125 default:
126 ASSERT (FALSE);
127 DEBUG ((DEBUG_ERROR, "Tpm2StartAuthSession - Symmetric->algorithm - %x\n", Symmetric->algorithm));
128 return EFI_UNSUPPORTED;
129 }
130
131 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthHash));
132 Buffer += sizeof (UINT16);
133
134 SendBufferSize = (UINT32)((UINTN)Buffer - (UINTN)&SendBuffer);
135 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
136
137 //
138 // send Tpm command
139 //
140 RecvBufferSize = sizeof (RecvBuffer);
141 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
142 if (EFI_ERROR (Status)) {
143 return Status;
144 }
145
146 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
147 DEBUG ((DEBUG_ERROR, "Tpm2StartAuthSession - RecvBufferSize Error - %x\n", RecvBufferSize));
148 return EFI_DEVICE_ERROR;
149 }
150
151 if (SwapBytes32 (RecvBuffer.Header.responseCode) != TPM_RC_SUCCESS) {
152 DEBUG ((DEBUG_ERROR, "Tpm2StartAuthSession - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
153 return EFI_DEVICE_ERROR;
154 }
155
156 //
157 // Return the response
158 //
159 *SessionHandle = SwapBytes32 (RecvBuffer.SessionHandle);
160 NonceTPM->size = SwapBytes16 (RecvBuffer.NonceTPM.size);
161 if (NonceTPM->size > sizeof (TPMU_HA)) {
162 DEBUG ((DEBUG_ERROR, "Tpm2StartAuthSession - NonceTPM->size error %x\n", NonceTPM->size));
163 return EFI_DEVICE_ERROR;
164 }
165
166 CopyMem (NonceTPM->buffer, &RecvBuffer.NonceTPM.buffer, NonceTPM->size);
167
168 return EFI_SUCCESS;
169}
UINT64 UINTN
UINT16 EFIAPI SwapBytes16(IN UINT16 Value)
Definition: SwapBytes16.c:25
UINT32 EFIAPI SwapBytes32(IN UINT32 Value)
Definition: SwapBytes32.c:25
UINT16 EFIAPI WriteUnaligned16(OUT UINT16 *Buffer, IN UINT16 Value)
Definition: Unaligned.c:61
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define TPM_ALG_XOR
XOR using the rolling nonces.
Definition: Tpm12.h:371
EFI_STATUS EFIAPI Tpm2SubmitCommand(IN UINT32 InputParameterBlockSize, IN UINT8 *InputParameterBlock, IN OUT UINT32 *OutputParameterBlockSize, IN UINT8 *OutputParameterBlock)
EFI_STATUS EFIAPI Tpm2StartAuthSession(IN TPMI_DH_OBJECT TpmKey, IN TPMI_DH_ENTITY Bind, IN TPM2B_NONCE *NonceCaller, IN TPM2B_ENCRYPTED_SECRET *Salt, IN TPM_SE SessionType, IN TPMT_SYM_DEF *Symmetric, IN TPMI_ALG_HASH AuthHash, OUT TPMI_SH_AUTH_SESSION *SessionHandle, OUT TPM2B_NONCE *NonceTPM)
Definition: Tpm2Session.c:56
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
Definition: Tpm20.h:905