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 <syslog.h>
14#include <string.h>
15#include <netdb.h>
16
17#include "trousers/tss.h"
18#include "trousers_types.h"
19#include "tcs_tsp.h"
20#include "tcs_utils.h"
21#include "tcs_int_literals.h"
22#include "capabilities.h"
23#include "tcslog.h"
24#include "tcsd_wrap.h"
25#include "tcsd.h"
26#include "tcs_utils.h"
27#include "rpc_tcstp_tcs.h"
28
29
30TSS_RESULT
31tcs_wrap_GetRandom(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	UINT32 bytesRequested;
35	BYTE *randomBytes = NULL;
36	TSS_RESULT result;
37
38	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
39		return TCSERR(TSS_E_INTERNAL_ERROR);
40
41	if ((result = ctx_verify_context(hContext)))
42		goto done;
43
44	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
45
46	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &bytesRequested, 0, &data->comm))
47		return TCSERR(TSS_E_INTERNAL_ERROR);
48
49	MUTEX_LOCK(tcsp_lock);
50
51	result = TCSP_GetRandom_Internal(hContext, &bytesRequested, &randomBytes);
52
53	MUTEX_UNLOCK(tcsp_lock);
54
55	if (result == TSS_SUCCESS) {
56		initData(&data->comm, 2);
57		if (setData(TCSD_PACKET_TYPE_UINT32, 0, &bytesRequested, 0, &data->comm)) {
58			free(randomBytes);
59			return TCSERR(TSS_E_INTERNAL_ERROR);
60		}
61		if (setData(TCSD_PACKET_TYPE_PBYTE, 1, randomBytes, bytesRequested, &data->comm)) {
62			free(randomBytes);
63			return TCSERR(TSS_E_INTERNAL_ERROR);
64		}
65		free(randomBytes);
66	} else
67done:		initData(&data->comm, 0);
68
69	data->comm.hdr.u.result = result;
70	return TSS_SUCCESS;
71}
72
73TSS_RESULT
74tcs_wrap_StirRandom(struct tcsd_thread_data *data)
75{
76	TCS_CONTEXT_HANDLE hContext;
77	UINT32 inDataSize;
78	BYTE *inData;
79	TSS_RESULT result;
80
81	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
82		return TCSERR(TSS_E_INTERNAL_ERROR);
83
84	if ((result = ctx_verify_context(hContext)))
85		goto done;
86
87	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
88
89	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &inDataSize, 0, &data->comm))
90		return TCSERR(TSS_E_INTERNAL_ERROR);
91
92	inData = calloc(1, inDataSize);
93	if (inData == NULL) {
94		LogError("malloc of %d bytes failed.", inDataSize);
95		return TCSERR(TSS_E_OUTOFMEMORY);
96	}
97	if (getData(TCSD_PACKET_TYPE_PBYTE, 2, inData, inDataSize, &data->comm)) {
98		free(inData);
99		return TCSERR(TSS_E_INTERNAL_ERROR);
100	}
101
102	MUTEX_LOCK(tcsp_lock);
103
104	result = TCSP_StirRandom_Internal(hContext, inDataSize, inData);
105
106	MUTEX_UNLOCK(tcsp_lock);
107	free(inData);
108done:
109	initData(&data->comm, 0);
110	data->comm.hdr.u.result = result;
111
112	return TSS_SUCCESS;
113}
114