xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/tsp_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, 2007
8  *
9  */
10 
11 
12 #include <stdlib.h>
13 #include <syslog.h>
14 #include <unistd.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers/trousers.h"
18 #include "trousers_types.h"
19 #include "spi_utils.h"
20 #include "tsplog.h"
21 #include "obj.h"
22 
23 
24 #ifdef TSS_BUILD_TRANSPORT
25 TSS_RESULT
Transport_GetRandom(TSS_HCONTEXT tspContext,UINT32 bytesRequested,BYTE ** randomBytes)26 Transport_GetRandom(TSS_HCONTEXT tspContext,	/* in */
27 		    UINT32 bytesRequested,	/* in */
28 		    BYTE ** randomBytes)	/* out */
29 {
30 	TSS_RESULT result;
31         UINT32 decLen = 0;
32         BYTE *dec = NULL;
33         UINT64 offset;
34 	TCS_HANDLE handlesLen = 0;
35         BYTE data[sizeof(UINT32)];
36 
37 	if ((result = obj_context_transport_init(tspContext)))
38 		return result;
39 
40         LogDebugFn("Executing in a transport session");
41 
42         offset = 0;
43         Trspi_LoadBlob_UINT32(&offset, bytesRequested, data);
44 
45         if ((result = obj_context_transport_execute(tspContext, TPM_ORD_GetRandom, sizeof(data),
46                                                     data, NULL, &handlesLen, NULL, NULL, NULL,
47 						    &decLen, &dec)))
48                 return result;
49 
50 	*randomBytes = dec;
51 
52         return result;
53 
54 }
55 
56 TSS_RESULT
Transport_StirRandom(TSS_HCONTEXT tspContext,UINT32 inDataSize,BYTE * inData)57 Transport_StirRandom(TSS_HCONTEXT tspContext,	/* in */
58 		     UINT32 inDataSize,	/* in */
59 		     BYTE * inData)	/* in */
60 {
61 	TSS_RESULT result;
62         UINT64 offset;
63 	UINT32 dataLen;
64 	TCS_HANDLE handlesLen = 0;
65         BYTE *data;
66 
67 	if ((result = obj_context_transport_init(tspContext)))
68 		return result;
69 
70         LogDebugFn("Executing in a transport session");
71 
72 	dataLen = sizeof(UINT32) + inDataSize;
73 	if ((data = malloc(dataLen)) == NULL) {
74                 LogError("malloc of %u bytes failed", dataLen);
75                 return TSPERR(TSS_E_OUTOFMEMORY);
76 	}
77 
78         offset = 0;
79         Trspi_LoadBlob_UINT32(&offset, inDataSize, data);
80         Trspi_LoadBlob(&offset, inDataSize, data, inData);
81 
82 	result = obj_context_transport_execute(tspContext, TPM_ORD_StirRandom, dataLen, data, NULL,
83 					       &handlesLen, NULL, NULL, NULL, NULL, NULL);
84 	free(data);
85 
86 	return result;
87 }
88 #endif
89 
90