Skip to content

Commit 1126c2e

Browse files
Merge pull request #344 from dgarske/nv_auth_policy
Added new API for allowing NV creation with policy
2 parents eede95d + 7a2a566 commit 1126c2e

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/tpm2.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4831,6 +4831,9 @@ TPM_RC TPM2_NV_Write(NV_Write_In* in)
48314831
TPM2_Packet_Init(ctx, &packet);
48324832

48334833
TPM2_Packet_AppendU32(&packet, in->authHandle);
4834+
/* When using an HMAC or Policy session make sure the NV "name" is
4835+
* populated in the TPM2_AUTH_SESSION name.name. This is a computed
4836+
* hash (see TPM2_HashNvPublic) */
48344837
TPM2_Packet_AppendU32(&packet, in->nvIndex);
48354838
TPM2_Packet_AppendAuth(&packet, ctx, &info);
48364839

src/tpm2_wrap.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4258,9 +4258,10 @@ int wolfTPM2_UnloadHandle(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* handle)
42584258

42594259
/* nv is the populated handle and auth */
42604260
/* auth and authSz are optional NV authentication */
4261-
int wolfTPM2_NVCreateAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent,
4261+
/* authPolicy and authPolicySz are optional policy digest */
4262+
int wolfTPM2_NVCreateAuthPolicy(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent,
42624263
WOLFTPM2_NV* nv, word32 nvIndex, word32 nvAttributes, word32 maxSize,
4263-
const byte* auth, int authSz)
4264+
const byte* auth, int authSz, const byte* authPolicy, int authPolicySz)
42644265
{
42654266
int rc, rctmp, alreadyExists = 0;
42664267
NV_DefineSpace_In in;
@@ -4275,7 +4276,7 @@ int wolfTPM2_NVCreateAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent,
42754276

42764277
XMEMSET(&in, 0, sizeof(in));
42774278
in.authHandle = parent->hndl;
4278-
if (auth && authSz > 0) {
4279+
if (auth != NULL && authSz > 0) {
42794280
if (authSz > (int)sizeof(in.auth.buffer))
42804281
authSz = (int)sizeof(in.auth.buffer);
42814282
in.auth.size = authSz;
@@ -4285,6 +4286,14 @@ int wolfTPM2_NVCreateAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent,
42854286
in.publicInfo.nvPublic.nameAlg = WOLFTPM2_WRAP_DIGEST;
42864287
in.publicInfo.nvPublic.attributes = nvAttributes;
42874288
in.publicInfo.nvPublic.dataSize = (UINT16)maxSize;
4289+
if (authPolicy != NULL && authPolicySz > 0) {
4290+
if (authPolicySz > (int)sizeof(in.publicInfo.nvPublic.authPolicy.buffer)) {
4291+
authPolicySz = (int)sizeof(in.publicInfo.nvPublic.authPolicy.buffer);
4292+
}
4293+
in.publicInfo.nvPublic.authPolicy.size = authPolicySz;
4294+
XMEMCPY(in.publicInfo.nvPublic.authPolicy.buffer, authPolicy,
4295+
in.publicInfo.nvPublic.authPolicy.size);
4296+
}
42884297

42894298
rc = TPM2_NV_DefineSpace(&in);
42904299
if (rc == TPM_RC_NV_DEFINED) {
@@ -4321,6 +4330,14 @@ int wolfTPM2_NVCreateAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent,
43214330
return (rc == TPM_RC_SUCCESS && alreadyExists) ? TPM_RC_NV_DEFINED : rc;
43224331
}
43234332

4333+
int wolfTPM2_NVCreateAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent,
4334+
WOLFTPM2_NV* nv, word32 nvIndex, word32 nvAttributes, word32 maxSize,
4335+
const byte* auth, int authSz)
4336+
{
4337+
return wolfTPM2_NVCreateAuthPolicy(dev, parent, nv, nvIndex, nvAttributes,
4338+
maxSize, auth, authSz, NULL, 0);
4339+
}
4340+
43244341
/* older API kept for compatibility, recommend using wolfTPM2_NVCreateAuth */
43254342
int wolfTPM2_NVCreate(WOLFTPM2_DEV* dev, TPM_HANDLE authHandle,
43264343
word32 nvIndex, word32 nvAttributes, word32 maxSize,

wolftpm/tpm2_wrap.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,7 @@ WOLFTPM_API int wolfTPM2_ExtendPCR(WOLFTPM2_DEV* dev, int pcrIndex, int hashAlg,
18621862
\param auth pointer to a string constant, specifying the password authorization for this NV Index
18631863
\param authSz integer value, specifying the size of the password authorization, in bytes
18641864
1865+
\sa wolfTPM2_NVCreateAuthPolicy
18651866
\sa wolfTPM2_NVWriteAuth
18661867
\sa wolfTPM2_NVReadAuth
18671868
\sa wolfTPM2_NVDeleteAuth
@@ -1871,6 +1872,36 @@ WOLFTPM_API int wolfTPM2_NVCreateAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent
18711872
WOLFTPM2_NV* nv, word32 nvIndex, word32 nvAttributes, word32 maxSize,
18721873
const byte* auth, int authSz);
18731874

1875+
/*!
1876+
\ingroup wolfTPM2_Wrappers
1877+
\brief Creates a new NV Index to be later used for storing data into the TPM's NVRAM
1878+
\note This is a wolfTPM2 wrapper around TPM2_NV_DefineSpace
1879+
1880+
\return TPM_RC_SUCCESS: successful
1881+
\return TPM_RC_FAILURE: generic failure (check TPM IO and TPM return code)
1882+
\return BAD_FUNC_ARG: check the provided arguments
1883+
1884+
\param dev pointer to a TPM2_DEV struct
1885+
\param parent pointer to a WOLFTPM2_HANDLE, specifying the TPM hierarchy for the new NV Index
1886+
\param nv pointer to an empty structure of WOLFTPM2_NV type, to hold the new NV Index
1887+
\param nvIndex integer value, holding the NV Index Handle given by the TPM upon success
1888+
\param nvAttributes integer value, use wolfTPM2_GetNvAttributesTemplate to create correct value
1889+
\param maxSize integer value, specifying the maximum number of bytes written at this NV Index
1890+
\param auth pointer to a string constant, specifying the password authorization for this NV Index
1891+
\param authSz integer value, specifying the size of the password authorization, in bytes
1892+
\param authPolicy optional policy for using this key (The policy is computed using the nameAlg of the object)
1893+
\param authPolicySz size of the authPolicy
1894+
1895+
\sa wolfTPM2_NVCreateAuth
1896+
\sa wolfTPM2_NVWriteAuth
1897+
\sa wolfTPM2_NVReadAuth
1898+
\sa wolfTPM2_NVDeleteAuth
1899+
\sa wolfTPM2_NVOpen
1900+
*/
1901+
WOLFTPM_API int wolfTPM2_NVCreateAuthPolicy(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent,
1902+
WOLFTPM2_NV* nv, word32 nvIndex, word32 nvAttributes, word32 maxSize,
1903+
const byte* auth, int authSz, const byte* authPolicy, int authPolicySz);
1904+
18741905
/*!
18751906
\ingroup wolfTPM2_Wrappers
18761907
\brief Stores user data to a NV Index, at a given offset

0 commit comments

Comments
 (0)