Skip to content

Commit

Permalink
Merge pull request #209 from dgarske/csharp_add
Browse files Browse the repository at this point in the history
  • Loading branch information
anhu authored May 25, 2022
2 parents f3c8b6c + 0ce7038 commit 160b3e0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ RemoteSystemsTempFiles
*.dep
*.deps
*.libs
*.dSYM
.vs
IDE/IAR-EWARM/settings
wolftpm/options.h

Expand Down Expand Up @@ -96,3 +98,7 @@ ek.pem

# Generated Documentation
docs/html

# Wrapper
wrapper/CSharp/obj
wrapper/CSharp/bin
4 changes: 2 additions & 2 deletions wolftpm/tpm2_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ WOLFTPM_API int wolfTPM2_NVCreateAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent

/*!
\ingroup wolfTPM2_Wrappers
\brief Stores user data to a NV Index, at a given offest
\brief Stores user data to a NV Index, at a given offset
\note User data size should be less or equal to the NV Index maxSize specified using wolfTPM2_CreateAuth
\return TPM_RC_SUCCESS: successful
Expand Down Expand Up @@ -2248,7 +2248,7 @@ WOLFTPM_API int wolfTPM2_CreateAndLoadAIK(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* aikKe
\return TPM_RC_FAILURE: generic failure (check TPM IO and TPM return code)
\return BAD_FUNC_ARG: check the provided arguments
\param aikKey pointer to a WOLFTPM2_KEY structure, containign valid TPM handle of a loaded attestation key
\param aikKey pointer to a WOLFTPM2_KEY structure, containing valid TPM handle of a loaded attestation key
\param getTimeOut pointer to an empty structure of GetTime_Out type, to store the output of the command
\sa wolfTPM2_CreateSRK
Expand Down
52 changes: 46 additions & 6 deletions wrapper/CSharp/wolfTPM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public enum SE : byte
TRIAL = 0x03,
}

public enum SESSION_mask : byte
{
continueSession = 0x01,
auditExclusive = 0x02,
auditReset = 0x04,
decrypt = 0x20,
encrypt = 0x40,
audit = 0x80,
}

public class KeyBlob
{
const string DLLNAME = "wolftpm";
Expand Down Expand Up @@ -320,13 +330,17 @@ public int CreateSRK(Key srkKey,
}

[DllImport(DLLNAME, EntryPoint = "wolfTPM2_StartSession")]
private static extern int wolfTPM2_StartSession(IntPtr dev, IntPtr session,
IntPtr tmpKey, IntPtr bind, byte sesType, int encDecAlg);
private static extern int wolfTPM2_StartSession(IntPtr dev,
IntPtr session,
IntPtr tmpKey,
IntPtr bind,
byte sesType,
int encDecAlg);
public int StartSession(IntPtr session,
Key tmpKey,
IntPtr bind,
byte sesType,
int encDecAlg)
Key tmpKey,
IntPtr bind,
byte sesType,
int encDecAlg)
{
return wolfTPM2_StartSession(device,
session,
Expand All @@ -336,6 +350,21 @@ public int StartSession(IntPtr session,
encDecAlg);
}

[DllImport(DLLNAME, EntryPoint = "wolfTPM2_SetAuthSession")]
private static extern int wolfTPM2_SetAuthSession(IntPtr dev,
int index,
IntPtr tpmSession,
byte sessionAttributes);
public int SetAuthSession(IntPtr session,
int index,
byte sessionAttributes)
{
/* For sessionAttributes suggest using:
* (byte)(SESSION_mask.decrypt | SESSION_mask.encrypt | SESSION_mask.continueSession)
*/
return wolfTPM2_SetAuthSession(device, index, session, sessionAttributes);
}


[DllImport(DLLNAME, EntryPoint = "wolfTPM2_ReadPublicKey")]
private static extern int wolfTPM2_ReadPublicKey(IntPtr dev,
Expand Down Expand Up @@ -382,6 +411,17 @@ public int LoadKey(KeyBlob keyBlob,
return wolfTPM2_LoadKey(device, keyBlob.keyblob, parent.GetHandleRefFromKey());
}


[DllImport(DLLNAME, EntryPoint = "wolfTPM2_NVStoreKey")]
private static extern int wolfTPM2_NVStoreKey(IntPtr dev,
IntPtr primaryHandle, IntPtr key, IntPtr persistentHandle);
public int StoreKey(Key key, IntPtr primaryHandle, IntPtr persistentHandle)
{
return wolfTPM2_NVStoreKey(device, primaryHandle, key.GetHandleRefFromKey(),
persistentHandle);
}


[DllImport(DLLNAME, EntryPoint = "wolfTPM2_ImportRsaPrivateKey")]
private static extern int wolfTPM2_ImportRsaPrivateKey(
IntPtr dev,
Expand Down

0 comments on commit 160b3e0

Please sign in to comment.