xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tcs/tcs_req_mgr.c (revision 2d5f7628c5531eb583b9313ac2fd1cf8582b4479)
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 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <syslog.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <unistd.h>
19 
20 #include "trousers/tss.h"
21 #include "tcs_tsp.h"
22 #include "tcs_utils.h"
23 #include "tddl.h"
24 #include "req_mgr.h"
25 #include "tcslog.h"
26 
27 static struct tpm_req_mgr *trm;
28 
29 #ifdef TSS_DEBUG
30 #define TSS_TPM_DEBUG
31 #endif
32 
33 TSS_RESULT
req_mgr_submit_req(BYTE * blob)34 req_mgr_submit_req(BYTE *blob)
35 {
36 	TSS_RESULT result;
37 	BYTE loc_buf[TSS_TPM_TXBLOB_SIZE];
38 	UINT32 size = TSS_TPM_TXBLOB_SIZE;
39 	UINT32 retry = TSS_REQ_MGR_MAX_RETRIES;
40 
41 	MUTEX_LOCK(trm->queue_lock);
42 
43 #ifdef TSS_TPM_DEBUG
44 	LogBlobData("To TPM:", Decode_UINT32(&blob[2]), blob);
45 #endif
46 
47 	do {
48 		result = Tddli_TransmitData(blob, Decode_UINT32(&blob[2]), loc_buf, &size);
49 	} while (!result && (Decode_UINT32(&loc_buf[6]) == TCPA_E_RETRY) && --retry);
50 
51 	if (!result)
52 		memcpy(blob, loc_buf, Decode_UINT32(&loc_buf[2]));
53 
54 #ifdef TSS_TPM_DEBUG
55 	LogBlobData("From TPM:", size, loc_buf);
56 #endif
57 
58 	MUTEX_UNLOCK(trm->queue_lock);
59 
60 	return result;
61 }
62 
63 TSS_RESULT
req_mgr_init()64 req_mgr_init()
65 {
66 	if ((trm = calloc(1, sizeof(struct tpm_req_mgr))) == NULL) {
67 		LogError("malloc of %zd bytes failed.", sizeof(struct tpm_req_mgr));
68 		return TSS_E_OUTOFMEMORY;
69 	}
70 
71 	MUTEX_INIT(trm->queue_lock);
72 
73 	return Tddli_Open();
74 }
75 
76 TSS_RESULT
req_mgr_final()77 req_mgr_final()
78 {
79 	free(trm);
80 
81 	return Tddli_Close();
82 }
83 
84