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_Hash_Prepare(TSS2_SYS_CONTEXT * sysContext,const TPM2B_MAX_BUFFER * data,TPMI_ALG_HASH hashAlg,TPMI_RH_HIERARCHY hierarchy)15 TSS2_RC Tss2_Sys_Hash_Prepare(
16     TSS2_SYS_CONTEXT *sysContext,
17     const TPM2B_MAX_BUFFER *data,
18     TPMI_ALG_HASH hashAlg,
19     TPMI_RH_HIERARCHY hierarchy)
20 {
21     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
22     TSS2_RC rval;
23 
24     if (!ctx)
25         return TSS2_SYS_RC_BAD_REFERENCE;
26 
27     if (IsAlgorithmWeak(hashAlg, 0))
28         return TSS2_SYS_RC_BAD_VALUE;
29 
30     rval = CommonPreparePrologue(ctx, TPM2_CC_Hash);
31     if (rval)
32         return rval;
33 
34     if (!data) {
35         ctx->decryptNull = 1;
36 
37         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
38                                       ctx->maxCmdSize,
39                                       &ctx->nextData);
40     } else {
41 
42         rval = Tss2_MU_TPM2B_MAX_BUFFER_Marshal(data, ctx->cmdBuffer,
43                                                 ctx->maxCmdSize,
44                                                 &ctx->nextData);
45     }
46 
47     if (rval)
48         return rval;
49 
50     rval = Tss2_MU_UINT16_Marshal(hashAlg, ctx->cmdBuffer,
51                                   ctx->maxCmdSize,
52                                   &ctx->nextData);
53     if (rval)
54         return rval;
55 
56     rval = Tss2_MU_UINT32_Marshal(hierarchy, ctx->cmdBuffer,
57                                   ctx->maxCmdSize,
58                                   &ctx->nextData);
59     if (rval)
60         return rval;
61 
62     ctx->decryptAllowed = 1;
63     ctx->encryptAllowed = 1;
64     ctx->authAllowed = 1;
65 
66     return CommonPrepareEpilogue(ctx);
67 }
68 
Tss2_Sys_Hash_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_DIGEST * outHash,TPMT_TK_HASHCHECK * validation)69 TSS2_RC Tss2_Sys_Hash_Complete(
70     TSS2_SYS_CONTEXT *sysContext,
71     TPM2B_DIGEST *outHash,
72     TPMT_TK_HASHCHECK *validation)
73 {
74     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
75     TSS2_RC rval;
76 
77     if (!ctx)
78         return TSS2_SYS_RC_BAD_REFERENCE;
79 
80     rval = CommonComplete(ctx);
81     if (rval)
82         return rval;
83 
84     rval = Tss2_MU_TPM2B_DIGEST_Unmarshal(ctx->cmdBuffer,
85                                           ctx->maxCmdSize,
86                                           &ctx->nextData,
87                                           outHash);
88     if (rval)
89         return rval;
90 
91     return Tss2_MU_TPMT_TK_HASHCHECK_Unmarshal(ctx->cmdBuffer,
92                                                ctx->maxCmdSize,
93                                                &ctx->nextData,
94                                                validation);
95 }
96 
Tss2_Sys_Hash(TSS2_SYS_CONTEXT * sysContext,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_MAX_BUFFER * data,TPMI_ALG_HASH hashAlg,TPMI_RH_HIERARCHY hierarchy,TPM2B_DIGEST * outHash,TPMT_TK_HASHCHECK * validation,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)97 TSS2_RC Tss2_Sys_Hash(
98     TSS2_SYS_CONTEXT *sysContext,
99     TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
100     const TPM2B_MAX_BUFFER *data,
101     TPMI_ALG_HASH hashAlg,
102     TPMI_RH_HIERARCHY hierarchy,
103     TPM2B_DIGEST *outHash,
104     TPMT_TK_HASHCHECK *validation,
105     TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
106 {
107     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
108     TSS2_RC rval;
109 
110     rval = Tss2_Sys_Hash_Prepare(sysContext, data, hashAlg, hierarchy);
111     if (rval)
112         return rval;
113 
114     rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
115     if (rval)
116         return rval;
117 
118     return Tss2_Sys_Hash_Complete(sysContext, outHash, validation);
119 }
120