def write_to_nv_index()

in cvm-attestation/tpm_wrapper.py [0:0]


  def write_to_nv_index(self, index, user_data):
    tpm = Tpm()
    tpm.connect()

    attributes =  TPMA_NV.OWNERWRITE | TPMA_NV.OWNERREAD
    attributes |=  TPMA_NV.AUTHWRITE | TPMA_NV.AUTHREAD

    auth = TPM_HANDLE(TPM_RH.OWNER)
    nvIndex = TPM_HANDLE(int(index, 16))
    handle = TPMS_NV_PUBLIC(nvIndex, TPM_ALG_ID.SHA256, attributes, None, 64)

    # undefine nv space if its defined
    try:
      tpm.NV_UndefineSpace(auth, nvIndex)
    except:
      self.log.info('Index is not defined yet')

    # define the nv idex to write the user data
    tpm.NV_DefineSpace(auth, None, handle)
    tpm.NV_Write(auth, nvIndex, user_data, 0)

    data = tpm.NV_Read(auth, nvIndex , 64, 0)
    if len(data) != 0:
      self.log.info('Wrote data successfully')

    tpm.close()