1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************;
3  * Copyright (c) 2015 - 2017, Intel Corporation
4  * All rights reserved.
5  ***********************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include "tss2_tpm2_types.h"
12 #include "tss2_mu.h"
13 #include "sysapi_util.h"
14 
Tss2_Sys_GetRandom_Prepare(TSS2_SYS_CONTEXT * sysContext,UINT16 bytesRequested)15 TSS2_RC Tss2_Sys_GetRandom_Prepare(
16     TSS2_SYS_CONTEXT *sysContext,
17     UINT16 bytesRequested)
18 {
19     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
20     TSS2_RC rval;
21 
22     if (!ctx)
23         return TSS2_SYS_RC_BAD_REFERENCE;
24 
25     rval = CommonPreparePrologue(ctx, TPM2_CC_GetRandom);
26     if (rval)
27         return rval;
28     rval = Tss2_MU_UINT16_Marshal(bytesRequested, ctx->cmdBuffer,
29                                   ctx->maxCmdSize,
30                                   &ctx->nextData);
31     if (rval)
32         return rval;
33 
34     ctx->decryptAllowed = 0;
35     ctx->encryptAllowed = 1;
36     ctx->authAllowed = 1;
37 
38     return CommonPrepareEpilogue(ctx);
39 }
40 
Tss2_Sys_GetRandom_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_DIGEST * randomBytes)41 TSS2_RC Tss2_Sys_GetRandom_Complete(
42     TSS2_SYS_CONTEXT *sysContext,
43     TPM2B_DIGEST *randomBytes)
44 {
45     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
46     TSS2_RC rval;
47 
48     if (!ctx)
49         return TSS2_SYS_RC_BAD_REFERENCE;
50 
51     rval = CommonComplete(ctx);
52     if (rval)
53         return rval;
54 
55     return Tss2_MU_TPM2B_DIGEST_Unmarshal(ctx->cmdBuffer,
56                                           ctx->maxCmdSize,
57                                           &ctx->nextData, randomBytes);
58 }
59 
Tss2_Sys_GetRandom(TSS2_SYS_CONTEXT * sysContext,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,UINT16 bytesRequested,TPM2B_DIGEST * randomBytes,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)60 TSS2_RC Tss2_Sys_GetRandom(
61     TSS2_SYS_CONTEXT *sysContext,
62     TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
63     UINT16 bytesRequested,
64     TPM2B_DIGEST *randomBytes,
65     TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
66 {
67     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
68     TSS2_RC rval;
69 
70     rval = Tss2_Sys_GetRandom_Prepare(sysContext, bytesRequested);
71     if (rval)
72         return rval;
73 
74     rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
75     if (rval)
76         return rval;
77 
78     return Tss2_Sys_GetRandom_Complete(sysContext, randomBytes);
79 }
80