xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/tspi_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 <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_GetRandom(TSS_HTPM hTPM,UINT32 ulRandomDataLength,BYTE ** prgbRandomData)26 Tspi_TPM_GetRandom(TSS_HTPM hTPM,		/* in */
27 		   UINT32 ulRandomDataLength,	/* in */
28 		   BYTE ** prgbRandomData)	/* out */
29 {
30 	TSS_HCONTEXT tspContext;
31 	TSS_RESULT result;
32 
33 	if (prgbRandomData == NULL || ulRandomDataLength > 4096)
34 		return TSPERR(TSS_E_BAD_PARAMETER);
35 
36 	if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
37 		return result;
38 
39 	if (ulRandomDataLength == 0)
40 		return TSS_SUCCESS;
41 
42 	if ((result = TCS_API(tspContext)->GetRandom(tspContext, ulRandomDataLength,
43 						     prgbRandomData)))
44 		return result;
45 
46 	if ((result = __tspi_add_mem_entry(tspContext, *prgbRandomData))) {
47 		free(*prgbRandomData);
48 		*prgbRandomData = NULL;
49 		return result;
50 	}
51 
52 	return TSS_SUCCESS;
53 }
54 
55 TSS_RESULT
Tspi_TPM_StirRandom(TSS_HTPM hTPM,UINT32 ulEntropyDataLength,BYTE * rgbEntropyData)56 Tspi_TPM_StirRandom(TSS_HTPM hTPM,		/* in */
57 		    UINT32 ulEntropyDataLength,	/* in */
58 		    BYTE * rgbEntropyData)	/* in */
59 {
60 	TSS_RESULT result;
61 	TSS_HCONTEXT tspContext;
62 
63 	if (ulEntropyDataLength > 0 && rgbEntropyData == NULL)
64 		return TSPERR(TSS_E_BAD_PARAMETER);
65 
66 	if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
67 		return result;
68 
69 	if ((result = TCS_API(tspContext)->StirRandom(tspContext, ulEntropyDataLength,
70 						      rgbEntropyData)))
71 		return result;
72 
73 	return TSS_SUCCESS;
74 }
75