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_HMAC_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT handle,const TPM2B_MAX_BUFFER * buffer,TPMI_ALG_HASH hashAlg)15 TSS2_RC Tss2_Sys_HMAC_Prepare(
16 TSS2_SYS_CONTEXT *sysContext,
17 TPMI_DH_OBJECT handle,
18 const TPM2B_MAX_BUFFER *buffer,
19 TPMI_ALG_HASH hashAlg)
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_HMAC);
31 if (rval)
32 return rval;
33
34 rval = Tss2_MU_UINT32_Marshal(handle, ctx->cmdBuffer,
35 ctx->maxCmdSize,
36 &ctx->nextData);
37 if (rval)
38 return rval;
39
40 if (!buffer) {
41 ctx->decryptNull = 1;
42
43 rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
44 ctx->maxCmdSize,
45 &ctx->nextData);
46 } else {
47
48 rval = Tss2_MU_TPM2B_MAX_BUFFER_Marshal(buffer, ctx->cmdBuffer,
49 ctx->maxCmdSize,
50 &ctx->nextData);
51 }
52
53 if (rval)
54 return rval;
55
56 rval = Tss2_MU_UINT16_Marshal(hashAlg, 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_HMAC_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_DIGEST * outHMAC)69 TSS2_RC Tss2_Sys_HMAC_Complete(
70 TSS2_SYS_CONTEXT *sysContext,
71 TPM2B_DIGEST *outHMAC)
72 {
73 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
74 TSS2_RC rval;
75
76 if (!ctx)
77 return TSS2_SYS_RC_BAD_REFERENCE;
78
79 rval = CommonComplete(ctx);
80 if (rval)
81 return rval;
82
83 return Tss2_MU_TPM2B_DIGEST_Unmarshal(ctx->cmdBuffer,
84 ctx->maxCmdSize,
85 &ctx->nextData,
86 outHMAC);
87 }
88
Tss2_Sys_HMAC(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT handle,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_MAX_BUFFER * buffer,TPMI_ALG_HASH hashAlg,TPM2B_DIGEST * outHMAC,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)89 TSS2_RC Tss2_Sys_HMAC(
90 TSS2_SYS_CONTEXT *sysContext,
91 TPMI_DH_OBJECT handle,
92 TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
93 const TPM2B_MAX_BUFFER *buffer,
94 TPMI_ALG_HASH hashAlg,
95 TPM2B_DIGEST *outHMAC,
96 TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
97 {
98 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
99 TSS2_RC rval;
100
101 rval = Tss2_Sys_HMAC_Prepare(sysContext, handle, buffer, hashAlg);
102 if (rval)
103 return rval;
104
105 rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
106 if (rval)
107 return rval;
108
109 return Tss2_Sys_HMAC_Complete(sysContext, outHMAC);
110 }
111