Skip to content

Commit 37db857

Browse files
authored
Merge pull request #203 from anhu/csharp_wrap
Adding a C# wrapper; limitted functionality.
2 parents efc85df + 915df85 commit 37db857

12 files changed

Lines changed: 1234 additions & 3 deletions

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ include IDE/include.am
3939
include certs/include.am
4040
include tests/include.am
4141
include docs/include.am
42+
include wrapper/include.am
4243

4344
EXTRA_DIST+= README.md
4445
EXTRA_DIST+= ChangeLog.md

src/tpm2_param_enc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int TPM2_KDFa(
173173
copyLen = keySz - pos;
174174
}
175175

176-
memcpy(keyStream, hash, copyLen);
176+
XMEMCPY(keyStream, hash, copyLen);
177177
keyStream += copyLen;
178178
}
179179
ret = keySz;

src/tpm2_wrap.c

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,273 @@ int wolfTPM2_Init(WOLFTPM2_DEV* dev, TPM2HalIoCb ioCb, void* userCtx)
172172
return rc;
173173
}
174174

175+
#ifndef WOLFTPM2_NO_HEAP
176+
WOLFTPM2_DEV *wolfTPM2_New(void)
177+
{
178+
WOLFTPM2_DEV *dev = NULL;
179+
180+
dev = (WOLFTPM2_DEV *) XMALLOC(sizeof(WOLFTPM2_DEV), NULL,
181+
DYNAMIC_TYPE_TMP_BUFFER);
182+
if (dev == NULL) {
183+
return NULL;
184+
}
185+
186+
if (wolfTPM2_Init(dev, NULL, NULL) != TPM_RC_SUCCESS) {
187+
XFREE(dev, NULL, DYNAMIC_TYPE_TMP_BUFFER);
188+
return NULL;
189+
}
190+
191+
return dev;
192+
}
193+
194+
int wolfTPM2_Free(WOLFTPM2_DEV *dev)
195+
{
196+
if (dev != NULL) {
197+
wolfTPM2_Cleanup(dev);
198+
XFREE(dev, NULL, DYNAMIC_TYPE_TMP_BUFFER);
199+
}
200+
return TPM_RC_SUCCESS;
201+
}
202+
203+
WOLFTPM2_KEYBLOB* wolfTPM2_NewKeyBlob(void)
204+
{
205+
WOLFTPM2_KEYBLOB* blob = NULL;
206+
207+
blob = (WOLFTPM2_KEYBLOB *) XMALLOC(sizeof(WOLFTPM2_KEYBLOB), NULL,
208+
DYNAMIC_TYPE_TMP_BUFFER);
209+
if (blob == NULL) {
210+
return NULL;
211+
}
212+
213+
XMEMSET(blob, 0, sizeof(WOLFTPM2_KEYBLOB));
214+
return blob;
215+
}
216+
217+
int wolfTPM2_FreeKeyBlob(WOLFTPM2_KEYBLOB* blob)
218+
{
219+
if (blob != NULL) {
220+
XFREE(blob, NULL, DYNAMIC_TYPE_TMP_BUFFER);
221+
}
222+
return TPM_RC_SUCCESS;
223+
}
224+
225+
TPMT_PUBLIC* wolfTPM2_NewPublicTemplate(void)
226+
{
227+
TPMT_PUBLIC* template = NULL;
228+
229+
template = (TPMT_PUBLIC *) XMALLOC(sizeof(TPMT_PUBLIC), NULL,
230+
DYNAMIC_TYPE_TMP_BUFFER);
231+
if (template == NULL) {
232+
return NULL;
233+
}
234+
235+
XMEMSET(template, 0, sizeof(TPMT_PUBLIC));
236+
return template;
237+
}
238+
239+
int wolfTPM2_FreePublicTemplate(TPMT_PUBLIC* template)
240+
{
241+
if (template != NULL) {
242+
XFREE(template, NULL, DYNAMIC_TYPE_TMP_BUFFER);
243+
}
244+
return TPM_RC_SUCCESS;
245+
}
246+
247+
WOLFTPM2_KEY* wolfTPM2_NewKey(void)
248+
{
249+
WOLFTPM2_KEY* key = NULL;
250+
251+
key = (WOLFTPM2_KEY *) XMALLOC(sizeof(WOLFTPM2_KEY), NULL,
252+
DYNAMIC_TYPE_TMP_BUFFER);
253+
if (key == NULL) {
254+
return NULL;
255+
}
256+
257+
XMEMSET(key, 0, sizeof(WOLFTPM2_KEY));
258+
return key;
259+
}
260+
261+
int wolfTPM2_FreeKey(WOLFTPM2_KEY* key)
262+
{
263+
if (key != NULL) {
264+
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
265+
}
266+
return TPM_RC_SUCCESS;
267+
}
268+
269+
WOLFTPM2_SESSION* wolfTPM2_NewSession(void)
270+
{
271+
WOLFTPM2_SESSION* session = NULL;
272+
273+
session = (WOLFTPM2_SESSION *) XMALLOC(sizeof(WOLFTPM2_SESSION), NULL,
274+
DYNAMIC_TYPE_TMP_BUFFER);
275+
if (session == NULL) {
276+
return NULL;
277+
}
278+
279+
XMEMSET(session, 0, sizeof(WOLFTPM2_SESSION));
280+
return session;
281+
}
282+
283+
int wolfTPM2_FreeSession(WOLFTPM2_SESSION* session)
284+
{
285+
if (session != NULL) {
286+
XFREE(session, NULL, DYNAMIC_TYPE_TMP_BUFFER);
287+
}
288+
return TPM_RC_SUCCESS;
289+
}
290+
#endif /* WOLFTPM2_NO_HEAP */
291+
292+
WOLFTPM2_HANDLE* wolfTPM2_GetHandleRefFromKey(WOLFTPM2_KEY* key)
293+
{
294+
if (key == NULL) {
295+
return NULL;
296+
}
297+
return &(key->handle);
298+
}
299+
300+
int wolfTPM2_GetKeyBlobAsBuffer(byte *buffer, word32 bufferSz,
301+
WOLFTPM2_KEYBLOB* key)
302+
{
303+
int rc = 0;
304+
int sz = 0;
305+
byte pubAreaBuffer[sizeof(TPM2B_PUBLIC)];
306+
int pubAreaSize;
307+
308+
if ((buffer == NULL) || (bufferSz <= 0) || (key == NULL)) {
309+
return BAD_FUNC_ARG;
310+
}
311+
312+
/* publicArea is encoded format. Eliminates empty fields, saves space. */
313+
rc = TPM2_AppendPublic(pubAreaBuffer, (word32)sizeof(pubAreaBuffer),
314+
&pubAreaSize, &key->pub);
315+
if (rc != TPM_RC_SUCCESS) {
316+
return rc;
317+
}
318+
319+
if (pubAreaSize != (key->pub.size + (int)sizeof(key->pub.size))) {
320+
#ifdef DEBUG_WOLFTPM
321+
printf("Sanity check for publicArea size failed\n");
322+
#endif
323+
return BUFFER_E;
324+
}
325+
326+
if (bufferSz < sizeof(key->pub.size) + sizeof(UINT16) + key->pub.size +
327+
sizeof(UINT16) + key->priv.size) {
328+
return BUFFER_E;
329+
}
330+
331+
/* Write size marker for the public part */
332+
XMEMCPY(buffer + sz, &key->pub.size, sizeof(key->pub.size));
333+
sz += sizeof(key->pub.size);
334+
335+
/* Write the public part with bytes aligned */
336+
XMEMCPY(buffer + sz, pubAreaBuffer, sizeof(UINT16) + key->pub.size);
337+
sz += sizeof(UINT16) + key->pub.size;
338+
339+
/* Write the private part, size marker is included */
340+
XMEMCPY(buffer + sz, &key->priv, sizeof(UINT16) + key->priv.size);
341+
sz += sizeof(UINT16) + key->priv.size;
342+
343+
#ifdef WOLFTPM_DEBUG_VERBOSE
344+
TPM2_PrintBin(buffer, sz);
345+
printf("Getting %d bytes\n", (int)sz);
346+
#endif
347+
348+
return sz;
349+
}
350+
351+
int wolfTPM2_SetKeyBlobFromBuffer(WOLFTPM2_KEYBLOB* key, byte *buffer,
352+
word32 bufferSz)
353+
{
354+
int rc = 0;
355+
byte pubAreaBuffer[sizeof(TPM2B_PUBLIC)];
356+
int pubAreaSize;
357+
byte *runner = buffer;
358+
size_t done_reading = 0;
359+
360+
if ((key == NULL) || (buffer == NULL) || (bufferSz <= 0)) {
361+
return BAD_FUNC_ARG;
362+
}
363+
364+
XMEMSET(key, 0, sizeof(WOLFTPM2_KEYBLOB));
365+
366+
#ifdef WOLFTPM_DEBUG_VERBOSE
367+
TPM2_PrintBin(buffer, bufferSz);
368+
printf("Setting %d bytes\n", (int)bufferSz);
369+
#endif
370+
371+
if (bufferSz < done_reading + sizeof(key->pub.size)) {
372+
#ifdef DEBUG_WOLFTPM
373+
printf("Buffer size check failed (%d)\n", bufferSz);
374+
#endif
375+
return BUFFER_E;
376+
}
377+
378+
XMEMCPY(&key->pub.size, runner, sizeof(key->pub.size));
379+
runner += sizeof(key->pub.size);
380+
done_reading += sizeof(key->pub.size);
381+
382+
if (bufferSz < done_reading + sizeof(UINT16) + key->pub.size) {
383+
#ifdef DEBUG_WOLFTPM
384+
printf("Buffer size check failed (%d)\n", bufferSz);
385+
#endif
386+
return BUFFER_E;
387+
}
388+
389+
XMEMCPY(pubAreaBuffer, runner, sizeof(UINT16) + key->pub.size);
390+
runner += sizeof(UINT16) + key->pub.size;
391+
done_reading += sizeof(UINT16) + key->pub.size;
392+
393+
/* Decode the byte stream into a publicArea structure ready for use */
394+
rc = TPM2_ParsePublic(&key->pub, pubAreaBuffer,
395+
(word32)sizeof(pubAreaBuffer), &pubAreaSize);
396+
if (rc != TPM_RC_SUCCESS) {
397+
return rc;
398+
}
399+
400+
if (bufferSz < done_reading + sizeof(key->priv.size)) {
401+
#ifdef DEBUG_WOLFTPM
402+
printf("Buffer size check failed (%d)\n", bufferSz);
403+
#endif
404+
return BUFFER_E;
405+
}
406+
407+
XMEMCPY(&key->priv.size, runner, sizeof(key->priv.size));
408+
runner += sizeof(key->priv.size);
409+
done_reading += sizeof(key->priv.size);
410+
411+
if (bufferSz < done_reading + key->priv.size) {
412+
#ifdef DEBUG_WOLFTPM
413+
printf("Buffer size check failed (%d)\n", bufferSz);
414+
#endif
415+
return BUFFER_E;
416+
}
417+
418+
XMEMCPY(key->priv.buffer, runner, key->priv.size);
419+
runner += key->priv.size;
420+
done_reading += key->priv.size;
421+
422+
return TPM_RC_SUCCESS;
423+
}
424+
425+
int wolfTPM2_SetKeyAuthPassword(WOLFTPM2_KEY *key, const byte* auth,
426+
int authSz)
427+
{
428+
if ((key == NULL) || (authSz < 0)) {
429+
return BAD_FUNC_ARG;
430+
}
431+
432+
if ((auth != NULL) && (authSz == 0)) {
433+
return BAD_FUNC_ARG;
434+
}
435+
436+
/* specify auth password for storage key */
437+
key->handle.auth.size = authSz;
438+
XMEMCPY(key->handle.auth.buffer, auth, authSz);
439+
return TPM_RC_SUCCESS;
440+
}
441+
175442
/* Access already started TPM module */
176443
int wolfTPM2_OpenExisting(WOLFTPM2_DEV* dev, TPM2HalIoCb ioCb, void* userCtx)
177444
{

wolftpm/tpm2_wrap.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,8 @@ WOLFTPM_API int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
569569
\sa wolfTPM2_CreatePrimaryKey
570570
*/
571571
WOLFTPM_API int wolfTPM2_CreateKey(WOLFTPM2_DEV* dev,
572-
WOLFTPM2_KEYBLOB* keyBlob, WOLFTPM2_HANDLE* parent, TPMT_PUBLIC* publicTemplate,
573-
const byte* auth, int authSz);
572+
WOLFTPM2_KEYBLOB* keyBlob, WOLFTPM2_HANDLE* parent,
573+
TPMT_PUBLIC* publicTemplate, const byte* auth, int authSz);
574574

575575
/*!
576576
\ingroup wolfTPM2_Wrappers
@@ -2348,6 +2348,28 @@ WOLFTPM_API int wolfTPM2_ClearCryptoDevCb(WOLFTPM2_DEV* dev, int devId);
23482348

23492349
#endif /* WOLF_CRYPTO_CB */
23502350

2351+
#ifndef WOLFTPM2_NO_HEAP
2352+
WOLFTPM_API WOLFTPM2_DEV *wolfTPM2_New(void);
2353+
WOLFTPM_API int wolfTPM2_Free(WOLFTPM2_DEV *dev);
2354+
WOLFTPM_API WOLFTPM2_KEYBLOB* wolfTPM2_NewKeyBlob(void);
2355+
WOLFTPM_API int wolfTPM2_FreeKeyBlob(WOLFTPM2_KEYBLOB* blob);
2356+
WOLFTPM_API TPMT_PUBLIC* wolfTPM2_NewPublicTemplate(void);
2357+
WOLFTPM_API int wolfTPM2_FreePublicTemplate(TPMT_PUBLIC* template);
2358+
WOLFTPM_API WOLFTPM2_KEY* wolfTPM2_NewKey(void);
2359+
WOLFTPM_API int wolfTPM2_FreeKey(WOLFTPM2_KEY* key);
2360+
WOLFTPM_API WOLFTPM2_SESSION* wolfTPM2_NewSession(void);
2361+
WOLFTPM_API int wolfTPM2_FreeSession(WOLFTPM2_SESSION* session);
2362+
#endif
2363+
2364+
WOLFTPM_API int wolfTPM2_OpenExistingDev(WOLFTPM2_DEV* dev);
2365+
WOLFTPM_API WOLFTPM2_HANDLE* wolfTPM2_GetHandleRefFromKey(WOLFTPM2_KEY* key);
2366+
WOLFTPM_API int wolfTPM2_SetKeyAuthPassword(WOLFTPM2_KEY *key, const byte* auth,
2367+
int authSz);
2368+
WOLFTPM_API int wolfTPM2_GetKeyBlobAsBuffer(byte *buffer, word32 bufferSz,
2369+
WOLFTPM2_KEYBLOB* key);
2370+
WOLFTPM_API int wolfTPM2_SetKeyBlobFromBuffer(WOLFTPM2_KEYBLOB* key,
2371+
byte *buffer, word32 bufferSz);
2372+
23512373
#ifdef __cplusplus
23522374
} /* extern "C" */
23532375
#endif

wrapper/CSharp/.runsettings

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<RunConfiguration>
4+
<EnvironmentVariables>
5+
<!-- update to path to local vcpkg install
6+
<PATH>%PATH%;c:\vcpkg\installed\x64-windows\bin</PATH>
7+
-->
8+
<!--
9+
if wolfTPM cmake solution is built using visual studio,
10+
we need to back out several(5) directories `wrapper\CSharp\bin\Debug\netcoreapp3.1`
11+
-->
12+
<PATH>%PATH%;..\..\..\..\..\out\build\x64-Debug\bin</PATH>
13+
</EnvironmentVariables>
14+
</RunConfiguration>
15+
</RunSettings>

0 commit comments

Comments
 (0)