xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/tspi_dir.c (revision 1023804e3833a0bd94414f2545512128f6502c74)
1 
2 /*
3  * Licensed Materials - Property of IBM
4  *
5  * trousers - An open source TCG Software Stack
6  *
7  * (C) Copyright International Business Machines Corp. 2004-2006
8  *
9  */
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <inttypes.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers/trousers.h"
18 #include "trousers_types.h"
19 #include "spi_utils.h"
20 #include "capabilities.h"
21 #include "tsplog.h"
22 #include "obj.h"
23 
24 
25 TSS_RESULT
Tspi_TPM_DirWrite(TSS_HTPM hTPM,UINT32 ulDirIndex,UINT32 ulDirDataLength,BYTE * rgbDirData)26 Tspi_TPM_DirWrite(TSS_HTPM hTPM,		/* in */
27 		  UINT32 ulDirIndex,		/* in */
28 		  UINT32 ulDirDataLength,	/* in */
29 		  BYTE * rgbDirData)		/* in */
30 {
31 	TSS_HCONTEXT tspContext;
32 	TCPA_RESULT result;
33 	TPM_AUTH auth;
34 	TCPA_DIGEST hashDigest;
35 	TSS_HPOLICY hPolicy;
36 	TCPA_DIRVALUE dirValue = { { 0 } };
37 	Trspi_HashCtx hashCtx;
38 
39 	if (rgbDirData == NULL || ulDirDataLength == 0)
40 		return TSPERR(TSS_E_BAD_PARAMETER);
41 
42 	if (ulDirDataLength > (UINT32)sizeof(TCPA_DIRVALUE))
43 		return TSPERR(TSS_E_BAD_PARAMETER);
44 
45 	if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
46 		return result;
47 
48 	if ((result = obj_tpm_get_policy(hTPM, TSS_POLICY_USAGE, &hPolicy)))
49 		return result;
50 
51 	memcpy((BYTE *)&dirValue, rgbDirData, ulDirDataLength);
52 
53 	/* hash to be used for the OIAP calc */
54 	result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
55 	result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_DirWriteAuth);
56 	result |= Trspi_Hash_UINT32(&hashCtx, ulDirIndex);
57 	result |= Trspi_HashUpdate(&hashCtx, (UINT32)sizeof(TCPA_DIRVALUE), (BYTE *)&dirValue);
58 	if ((result |= Trspi_HashFinal(&hashCtx, hashDigest.digest)))
59 		return result;
60 
61 	/* hashDigest now has the hash result */
62 	if ((result = secret_PerformAuth_OIAP(hTPM, TPM_ORD_DirWriteAuth, hPolicy, FALSE,
63 					      &hashDigest, &auth)))
64 		return result;
65 
66 	if ((result = TCS_API(tspContext)->DirWriteAuth(tspContext, ulDirIndex, &dirValue, &auth)))
67 		return result;
68 
69 	result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
70 	result |= Trspi_Hash_UINT32(&hashCtx, result);
71 	result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_DirWriteAuth);
72 	if ((result |= Trspi_HashFinal(&hashCtx, hashDigest.digest)))
73 		return result;
74 
75 	return obj_policy_validate_auth_oiap(hPolicy, &hashDigest, &auth);
76 }
77 
78 TSS_RESULT
Tspi_TPM_DirRead(TSS_HTPM hTPM,UINT32 ulDirIndex,UINT32 * pulDirDataLength,BYTE ** prgbDirData)79 Tspi_TPM_DirRead(TSS_HTPM hTPM,			/* in */
80 		 UINT32 ulDirIndex,		/* in */
81 		 UINT32 * pulDirDataLength,	/* out */
82 		 BYTE ** prgbDirData)		/* out */
83 {
84 	TCPA_DIRVALUE dirValue;
85 	TSS_RESULT result;
86 	TSS_HCONTEXT tspContext;
87 
88 	if (pulDirDataLength == NULL || prgbDirData == NULL)
89 		return TSPERR(TSS_E_BAD_PARAMETER);
90 
91 	if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
92 		return result;
93 
94 	if ((result = TCS_API(tspContext)->DirRead(tspContext, ulDirIndex, &dirValue)))
95 		return result;
96 
97 	*pulDirDataLength = 20;
98 	*prgbDirData = calloc_tspi(tspContext, *pulDirDataLength);
99 	if (*prgbDirData == NULL) {
100 		LogError("malloc of %d bytes failed.", *pulDirDataLength);
101 		return TSPERR(TSS_E_OUTOFMEMORY);
102 	}
103 	memcpy(*prgbDirData, dirValue.digest, *pulDirDataLength);
104 	return TSS_SUCCESS;
105 }
106