From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 50D7274003E for ; Wed, 15 Nov 2023 12:03:07 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=XE2W+bThyXlfTyO3C0HpPlbL/1QPN8nniES/PeNb3Nw=; c=relaxed/simple; d=groups.io; h=Subject:To:From:User-Agent:MIME-Version:Date:References:In-Reply-To:Message-ID:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Type; s=20140610; t=1700049786; v=1; b=dH3ZdHCLATZKei72oJQf40/Bt5rflr1X5xVn9Q2DW2/Ir5OHG2CTOyN4neapSZJtq0DMu2EA 8LskF1WcRTmkmU/4UlGqX13uknmUrW+VZqWtCEcuTWPesU4mIVIESFA6T4/I9VCeGARE0Jir9xN OmKmv1WCej57hc/qONEVIV6s= X-Received: by 127.0.0.2 with SMTP id Z3K1YY7687511xkbgFGIO0XY; Wed, 15 Nov 2023 04:03:06 -0800 Subject: Re: [edk2-devel] TPM2 NVM WRITE IN EDK2 To: Laszlo Ersek ,devel@edk2.groups.io From: "Hamit Can Karaca" X-Originating-Location: Ankara, TR (5.46.122.167) X-Originating-Platform: Windows Chrome 119 User-Agent: GROUPS.IO Web Poster MIME-Version: 1.0 Date: Wed, 15 Nov 2023 04:03:05 -0800 References: <2c9a73ee-34e7-3dd8-3ff9-fef6df0336e6@redhat.com> In-Reply-To: <2c9a73ee-34e7-3dd8-3ff9-fef6df0336e6@redhat.com> Message-ID: <2967.1700049785284722966@groups.io> Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,hckaraca99@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: vgAiZpTrBlBWaTVHXycoJbn4x7686176AA= Content-Type: multipart/alternative; boundary="gMNRGCL3ljSZO0f0RZDf" X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=dH3ZdHCL; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io --gMNRGCL3ljSZO0f0RZDf Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Thanks for your Laszlo, I am using the functions that are available in EDK2 TpmCommandLib. I am not= sure where I fail because all the structs that I use are those which are g= iven in EDK2. I will add my code below. It would be very nice If you could = check my code. If you have examples where you use Tpm2DefineSpace and Tpm2N= vWrite functions, can you please share with me? EFI_STATUS EFIAPI DefineSpaceTPM2 ( )=20 { EFI_STATUS Status; UINT32 authSize; ORIG_AUTH_AREA authArea; TPM2_NV_DEFINE_SPACE_COMMAND CmdBuffer; UINT32 CmdBufferSize; TPM2_NV_DEFINESPACE_RESPONSE RecvBuffer; UINT32 RecvBufferSize; ORIG_NV_PUBLIC publicInfo; // Auth Area authArea.sessionHandle =3D SwapBytes32(TPM_RS_PW); authArea.nonceSizeZero =3D SwapBytes16( 0 ); authArea.sessionAttributes.continueSession =3D 0 ; authArea.sessionAttributes.auditExclusive =3D 0 ; authArea.sessionAttributes.auditReset =3D 0 ; authArea.sessionAttributes.reserved3_4 =3D 0 ; authArea.sessionAttributes.decrypt =3D 0 ; authArea.sessionAttributes.encrypt =3D 0 ; authArea.sessionAttributes.audit =3D 0 ; authArea.hmacSizeZero =3D SwapBytes16( 0 ); authSize =3D sizeof (authArea); // publicInfo area publicInfo.nvIndex =3D SwapBytes32(NV_INDEX_FIRST + 0x10 ); publicInfo.nameAlg =3D SwapBytes16(TPM_ALG_SHA256); publicInfo.attributes =3D SwapBytes32( 0x20f500f ); publicInfo.authPolicySizeZero =3D SwapBytes16( 0 ); publicInfo.dataSize =3D SwapBytes16( 16 ); publicInfo.size =3D SwapBytes16( sizeof (publicInfo) - sizeof (publicInfo.= size)); // set parameters CmdBuffer.Header.tag =3D SwapBytes16(TPM_ST_SESSIONS); CmdBuffer.Header.commandCode =3D SwapBytes32(TPM_CC_NV_DefineSpace); CmdBuffer.authHandle =3D SwapBytes32(TPM_RH_OWNER); CmdBuffer.authSize =3D SwapBytes32(authSize); CmdBuffer.authArea =3D authArea; CmdBuffer.authSizeZero =3D SwapBytes16( 0 ); CmdBuffer.publicInfo =3D publicInfo; CmdBufferSize =3D sizeof (CmdBuffer.Header) + sizeof (CmdBuffer.authHandle= ) + sizeof (CmdBuffer.authSize) +=20 sizeof (CmdBuffer.authArea) + sizeof (CmdBuffer.authSizeZe= ro) + sizeof (CmdBuffer.publicInfo); CmdBuffer.Header.paramSize =3D SwapBytes32(CmdBufferSize); // send TPM command DEBUG((DEBUG_INFO, "DefineSpaceTPM2 Sending..\n" )); RecvBufferSize =3D sizeof (RecvBuffer); Status =3D Tpm2SubmitCommand (CmdBufferSize, (UINT8*)&CmdBuffer, &RecvBuff= erSize, (UINT8*)&RecvBuffer); if (Status !=3D EFI_SUCCESS) { DEBUG((DEBUG_INFO, "Code couldn't be submitted\n" )); return Status; } UINT32 res =3D SwapBytes32(RecvBuffer.Header.responseCode); if (res !=3D TPM_RC_SUCCESS) { DEBUG ((EFI_D_ERROR, "DefineSpaceTPM2 - responseCode - %x\n" , res)); } return Status; } EFI_STATUS EFIAPI WriteToDefinedSpace ( ) { EFI_STATUS Status; TPMI_RH_NV_AUTH AuthHandle; TPMS_AUTH_COMMAND *AuthSession; TPM2B_MAX_BUFFER *InData, UINT16 Offset AuthSession =3D (TPMS_AUTH_COMMAND*) AllocateZeroPool ( sizeof (TPMS_AUTH_= COMMAND)); OutData =3D (TPM2B_MAX_BUFFER *) AllocateZeroPool ( sizeof (TPM2B_MAX_BUFF= ER)); InData =3D (TPM2B_MAX_BUFFER *) AllocateZeroPool ( sizeof (TPM2B_MAX_BUFFE= R)); //MAX_DIGEST_BUFFER is default and the value is 1024 OutData->size =3D MAX_DIGEST_BUFFER * sizeof (BYTE); InData->size =3D MAX_DIGEST_BUFFER * sizeof (BYTE); Size =3D 0x2 ; Offset =3D 0x0 ; InData->buffer[ 0 ] =3D 0xC ; InData->buffer[ 1 ] =3D 0xC ; AuthSession->sessionHandle =3D TPM_RS_PW; AuthSession->nonce.size =3D 0 ; CopyMem (AuthSession->nonce.buffer, NULL , 64 * sizeof (BYTE)); AuthSession->sessionAttributes.continueSession =3D 0 ; AuthSession->sessionAttributes.auditExclusive =3D 0 ; AuthSession->sessionAttributes.auditReset =3D 0 ; AuthSession->sessionAttributes.reserved3_4 =3D 0 ; AuthSession->sessionAttributes.decrypt =3D 0 ; AuthSession->sessionAttributes.encrypt =3D 0 ; AuthSession->sessionAttributes.audit =3D 0 ; AuthSession->hmac.size =3D 0 ; CopyMem (AuthSession->hmac.buffer, NULL , 64 * sizeof (BYTE)); AuthHandle =3D TPM_RH_OWNER; Status =3D Tpm2NvWrite (AuthHandle, NV_INDEX_FIRST + 0x10 , AuthSession, I= nData, Offset); if (Status !=3D EFI_SUCCESS){ DEBUG((DEBUG_INFO, "Tpm2NvWrite Status at WriteToDefinedSpace Tpm: %r\n"= , Status)); } return Status; } -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#111268): https://edk2.groups.io/g/devel/message/111268 Mute This Topic: https://groups.io/mt/102510897/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- --gMNRGCL3ljSZO0f0RZDf Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable

Thanks for your Laszlo,

I am using the functions that are available in EDK2 TpmCommandLib. I am = not sure where I fail because all the structs that I use are those which ar= e given in EDK2. I will add my code below. It would be very nice If you cou= ld check my code. If you have examples where you use Tpm2DefineSpace and Tp= m2NvWrite functions, can you please share with me?

EFI_STATUS
EFIAPI
DefineSpac=
eTPM2(
)=20
{
  EFI_STATUS                      Status;
  UINT32                          authSize;
  ORIG_AUTH_AREA                  authArea;
  TPM2_NV_DEFINE_SPACE_COMMAND    CmdBuffer;
  UINT32                          CmdBufferSize;
  TPM2_NV_DEFINESPACE_RESPONSE    RecvBuffer;
  UINT32                          RecvBufferSize;
  ORIG_NV_PUBLIC                  publicInfo;

  // Auth Area
  authArea.sessionHandle =3D SwapBytes32(TPM_RS_PW);
  authArea.nonceSizeZero =3D SwapBytes16(0);
  authArea.sessionAttributes.continueSession =3D 0;
  authArea.sessionAttributes.auditExclusive  =3D 0;
  authArea.sessionAttributes.auditReset      =3D 0;
  authArea.sessionAttributes.reserved3_4     =3D 0;
  authArea.sessionAttributes.decrypt         =3D 0;
  authArea.sessionAttributes.encrypt         =3D 0;
  authArea.sessionAttributes.audit           =3D 0;
  authArea.hmacSizeZero =3D SwapBytes16(0);
  authSize =3D siz=
eof(authArea);

  // publicInfo ar=
ea
  publicInfo.nvIndex =3D SwapBytes32(NV_INDEX_FIRST + 0x10);
  publicInfo.nameAlg =3D SwapBytes16(TPM_ALG_SHA256);
  publicInfo.attributes =3D SwapBytes32(0x20f500f);
  publicInfo.authPolicySizeZero =3D SwapBytes16(0);
  publicInfo.dataSize =3D SwapBytes16(16);
  publicInfo.size =3D SwapBytes16(sizeof(publicInfo) - sizeof(publicInfo.size));


  // set parameter=
s
  CmdBuffer.Header.tag         =3D SwapBytes16(TPM_ST_SESSIONS);
  CmdBuffer.Header.commandCode =3D SwapBytes32(TPM_CC_NV_DefineSpace);
  CmdBuffer.authHandle         =3D SwapBytes32(TPM_RH_OWNER);
  CmdBuffer.authSize           =3D SwapBytes32(authSize);
  CmdBuffer.authArea           =3D authArea;
  CmdBuffer.authSizeZero       =3D SwapBytes16(0);
  CmdBuffer.publicInfo         =3D publicInfo;
  CmdBufferSize =3D sizeof(CmdBuffer.authHandle) + sizeof(CmdBuffer.authSize) +=
=20
                  =
sizeof(CmdBuffer.authArea) + sizeof(CmdBuffer.authSizeZero) + sizeof(CmdBuffer.publicInfo)=
;
  CmdBuffer.Header.paramSize =3D SwapBytes32(CmdBufferSize);

  // send TPM comm=
and
  DEBUG((DEBUG_INFO, "DefineSpaceTPM2 Sending..\n"));
  RecvBufferSize =3D sizeof(RecvBuffer);
  Status =3D Tpm2SubmitCommand (CmdBufferSize, (UINT8*)&CmdBuffer, &=
;RecvBufferSize, (UINT8*)&RecvBuffer);
  if (Statu=
s !=3D EFI_SUCCESS) {
    DEBUG((DEBUG_INFO, "Code couldn't be submitted\n"));
    return =
Status;
  }

  UINT32 res =3D SwapBytes32(RecvBuffer.Header.responseCode);
  if (res !=
=3D TPM_RC_SUCCESS) {
    DEBUG ((EFI_D_ERROR, "DefineSpaceTPM2 - responseCode - %x\n", res));
  }

  return St=
atus;
}

EFI_STATUS
EFIAPI
WriteToDef=
inedSpace(
)
{
  EFI_STATUS                Status;
  TPMI_RH_NV_AUTH           AuthHandle;
  TPMS_AUTH_COMMAND         *AuthSession;
  TPM2B_MAX_BUFFER          *InData,
  UINT16                    Offset

  AuthSession =3D (TPMS_AUTH_COMMAND*) AllocateZeroPool (sizeof(TPMS_AUTH_COMMAND));
  OutData =3D (TPM2B_MAX_BUFFER *) AllocateZeroPool (sizeof(TPM2B_MAX_BUFFER));
  InData =3D (TPM2B_MAX_BUFFER *) AllocateZeroPool (sizeof(TPM2B_MAX_BUFFER));


  //MAX_DIGEST_BUF=
FER is default and the value is 1024
  OutData->size =3D MAX_DIGEST_BUFFER * sizeof(BYTE);
  InData->size =3D MAX_DIGEST_BUFFER * sizeof(BYTE);

  Size =3D 0x2;
  Offset =3D 0x0<=
/span>;

  InData->buffer[0] =3D =
0xC;
  InData->buffer[1] =3D =
0xC;

  AuthSession->sessionHandle =3D TPM_RS_PW;
  AuthSession->nonce.size =3D 0;
  CopyMem (AuthSession->nonce.buffer, NULL, 64 * sizeof(BYTE));
  AuthSession->sessionAttributes.continueSession =3D 0;
  AuthSession->sessionAttributes.auditExclusive  =3D 0;
  AuthSession->sessionAttributes.auditReset      =3D 0;
  AuthSession->sessionAttributes.reserved3_4     =3D 0;
  AuthSession->sessionAttributes.decrypt         =3D 0;
  AuthSession->sessionAttributes.encrypt         =3D 0;
  AuthSession->sessionAttributes.audit           =3D 0;
  AuthSession->hmac.size =3D 0;
  CopyMem (AuthSession->hmac.buffer, NULL, 64 * sizeof(BYTE));

  AuthHandle =3D TPM_RH_OWNER;

  Status =3D Tpm2NvWrite (AuthHandle, NV_INDEX_FIRST + 0x10, AuthSession, InData, Off=
set);
  if (Statu=
s !=3D EFI_SUCCESS){
    DEBUG((DEBUG_INFO, "Tpm2NvWrite Status at WriteToDefinedSpace Tpm: %r\n", Status))=
;
  }

  return St=
atus;
}
_._,_._,_

Groups.io Links:

=20 You receive all messages sent to this group. =20 =20

View/Reply Online (#111268) | =20 | Mute= This Topic | New Topic
Your Subscriptio= n | Contact Group Owner | Unsubscribe [rebecca@openfw.io]

_._,_._,_
--gMNRGCL3ljSZO0f0RZDf--