/** Copy TPML_DIGEST_VALUES into a buffer @param[in,out] Buffer Buffer to hold TPML_DIGEST_VALUES. @param[in] DigestList TPML_DIGEST_VALUES to be copied. @return The end of buffer to hold TPML_DIGEST_VALUES. **/ VOID * CopyDigestListToBuffer ( IN OUT VOID *Buffer, IN TPML_DIGEST_VALUES *DigestList ) { UINTN Index; UINT16 DigestSize; CopyMem (Buffer, &DigestList->count, sizeof(DigestList->count)); Buffer = (UINT8 *)Buffer + sizeof(DigestList->count); for (Index = 0; Index < DigestList->count; Index++) { if (!IsHashAlgSupportedInPcrBank (DigestList->digests[Index].hashAlg)) { DEBUG ((EFI_D_ERROR, "WARNING: TPM2 Event log has HashAlg unsupported by PCR bank (0x%x)\n", DigestList->digests[Index].hashAlg)); continue; } CopyMem (Buffer, &DigestList->digests[Index].hashAlg, sizeof(DigestList->digests[Index].hashAlg)); Buffer = (UINT8 *)Buffer + sizeof(DigestList->digests[Index].hashAlg); DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg); CopyMem (Buffer, &DigestList->digests[Index].digest, DigestSize); Buffer = (UINT8 *)Buffer + DigestSize; } return Buffer; }
/** Get TPML_DIGEST_VALUES data size. @param[in] DigestList TPML_DIGEST_VALUES data. @return TPML_DIGEST_VALUES data size. **/ UINT32 GetDigestListSize ( IN TPML_DIGEST_VALUES *DigestList ) { UINTN Index; UINT16 DigestSize; UINT32 TotalSize; TotalSize = sizeof(DigestList->count); for (Index = 0; Index < DigestList->count; Index++) { DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg); TotalSize += sizeof(DigestList->digests[Index].hashAlg) + DigestSize; } return TotalSize; }
/** This function get digest from digest list. @param HashAlg digest algorithm @param DigestList digest list @param Digest digest @retval EFI_SUCCESS Sha1Digest is found and returned. @retval EFI_NOT_FOUND Sha1Digest is not found. **/ EFI_STATUS Tpm2GetDigestFromDigestList ( IN TPMI_ALG_HASH HashAlg, IN TPML_DIGEST_VALUES *DigestList, IN VOID *Digest ) { UINTN Index; UINT16 DigestSize; DigestSize = GetHashSizeFromAlgo (HashAlg); for (Index = 0; Index < DigestList->count; Index++) { if (DigestList->digests[Index].hashAlg == HashAlg) { CopyMem ( Digest, &DigestList->digests[Index].digest, DigestSize ); return EFI_SUCCESS; } } return EFI_NOT_FOUND; }
/** This command is used to cause an update to the indicated PCR. The digests parameter contains one or more tagged digest value identified by an algorithm ID. For each digest, the PCR associated with pcrHandle is Extended into the bank identified by the tag (hashAlg). @param[in] PcrHandle Handle of the PCR @param[in] Digests List of tagged digest values to be extended @retval EFI_SUCCESS Operation completed successfully. @retval EFI_DEVICE_ERROR Unexpected device behavior. **/ EFI_STATUS EFIAPI Tpm2PcrExtend ( IN TPMI_DH_PCR PcrHandle, IN TPML_DIGEST_VALUES *Digests ) { EFI_STATUS Status; TPM2_PCR_EXTEND_COMMAND Cmd; TPM2_PCR_EXTEND_RESPONSE Res; UINT32 CmdSize; UINT32 RespSize; UINT32 ResultBufSize; UINT8 *Buffer; UINTN Index; UINT32 SessionInfoSize; UINT16 DigestSize; Cmd.Header.tag = SwapBytes16(TPM_ST_SESSIONS); Cmd.Header.commandCode = SwapBytes32(TPM_CC_PCR_Extend); Cmd.PcrHandle = SwapBytes32(PcrHandle); // // Add in Auth session // Buffer = (UINT8 *)&Cmd.AuthSessionPcr; // sessionInfoSize SessionInfoSize = CopyAuthSessionCommand (NULL, Buffer); Buffer += SessionInfoSize; Cmd.AuthorizationSize = SwapBytes32(SessionInfoSize); //Digest Count WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32(Digests->count)); Buffer += sizeof(UINT32); //Digest for (Index = 0; Index < Digests->count; Index++) { WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16(Digests->digests[Index].hashAlg)); Buffer += sizeof(UINT16); DigestSize = GetHashSizeFromAlgo (Digests->digests[Index].hashAlg); if (DigestSize == 0) { DEBUG ((EFI_D_ERROR, "Unknown hash algorithm %d\r\n", Digests->digests[Index].hashAlg)); return EFI_DEVICE_ERROR; } CopyMem( Buffer, &Digests->digests[Index].digest, DigestSize ); Buffer += DigestSize; } CmdSize = (UINT32)((UINTN)Buffer - (UINTN)&Cmd); Cmd.Header.paramSize = SwapBytes32(CmdSize); ResultBufSize = sizeof(Res); Status = Tpm2SubmitCommand (CmdSize, (UINT8 *)&Cmd, &ResultBufSize, (UINT8 *)&Res); if (EFI_ERROR(Status)) { return Status; } if (ResultBufSize > sizeof(Res)) { DEBUG ((EFI_D_ERROR, "Tpm2PcrExtend: Failed ExecuteCommand: Buffer Too Small\r\n")); return EFI_BUFFER_TOO_SMALL; } // // Validate response headers // RespSize = SwapBytes32(Res.Header.paramSize); if (RespSize > sizeof(Res)) { DEBUG ((EFI_D_ERROR, "Tpm2PcrExtend: Response size too large! %d\r\n", RespSize)); return EFI_BUFFER_TOO_SMALL; } // // Fail if command failed // if (SwapBytes32(Res.Header.responseCode) != TPM_RC_SUCCESS) { DEBUG ((EFI_D_ERROR, "Tpm2PcrExtend: Response Code error! 0x%08x\r\n", SwapBytes32(Res.Header.responseCode))); return EFI_DEVICE_ERROR; } // // Unmarshal the response // // None return EFI_SUCCESS; }