xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/rpc/tcstp/rpc_random.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 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <assert.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 "hosttable.h"
23 #include "tcsd_wrap.h"
24 #include "obj.h"
25 #include "rpc_tcstp_tsp.h"
26 
27 
28 TSS_RESULT
RPC_GetRandom_TP(struct host_table_entry * hte,UINT32 bytesRequested,BYTE ** randomBytes)29 RPC_GetRandom_TP(struct host_table_entry *hte,
30 		  UINT32 bytesRequested,	/* in */
31 		  BYTE ** randomBytes)	/* out */
32 {
33 	TSS_RESULT result;
34 
35 	initData(&hte->comm, 2);
36 	hte->comm.hdr.u.ordinal = TCSD_ORD_GETRANDOM;
37 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
38 
39 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
40 		return TSPERR(TSS_E_INTERNAL_ERROR);
41 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &bytesRequested, 0, &hte->comm))
42 		return TSPERR(TSS_E_INTERNAL_ERROR);
43 
44 	result = sendTCSDPacket(hte);
45 
46 	if (result == TSS_SUCCESS)
47 		result = hte->comm.hdr.u.result;
48 
49 	if (result == TSS_SUCCESS) {
50 		if (getData(TCSD_PACKET_TYPE_UINT32, 0, &bytesRequested, 0, &hte->comm)) {
51 			result = TSPERR(TSS_E_INTERNAL_ERROR);
52 			goto done;
53 		}
54 		*randomBytes = (BYTE *) malloc(bytesRequested);
55 		if (*randomBytes == NULL) {
56 			LogError("malloc of %u bytes failed.", bytesRequested);
57 			result = TSPERR(TSS_E_OUTOFMEMORY);
58 			goto done;
59 		}
60 		if (getData(TCSD_PACKET_TYPE_PBYTE, 1, *randomBytes, bytesRequested, &hte->comm)) {
61 			free(*randomBytes);
62 			result = TSPERR(TSS_E_INTERNAL_ERROR);
63 		}
64 	}
65 
66 done:
67 	return result;
68 }
69 
70 TSS_RESULT
RPC_StirRandom_TP(struct host_table_entry * hte,UINT32 inDataSize,BYTE * inData)71 RPC_StirRandom_TP(struct host_table_entry *hte,
72 		   UINT32 inDataSize,	/* in */
73 		   BYTE * inData)	/* in */
74 {
75 	TSS_RESULT result;
76 
77 	initData(&hte->comm, 3);
78 	hte->comm.hdr.u.ordinal = TCSD_ORD_STIRRANDOM;
79 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
80 
81 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
82 		return TSPERR(TSS_E_INTERNAL_ERROR);
83 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &inDataSize, 0, &hte->comm))
84 		return TSPERR(TSS_E_INTERNAL_ERROR);
85 	if (setData(TCSD_PACKET_TYPE_PBYTE, 2, inData, inDataSize, &hte->comm))
86 		return TSPERR(TSS_E_INTERNAL_ERROR);
87 
88 	result = sendTCSDPacket(hte);
89 
90 	if (result == TSS_SUCCESS)
91 		result = hte->comm.hdr.u.result;
92 
93 	return result;
94 }
95