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_ECDH_ZGen_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT keyHandle,const TPM2B_ECC_POINT * inPoint)15 TSS2_RC Tss2_Sys_ECDH_ZGen_Prepare(
16     TSS2_SYS_CONTEXT *sysContext,
17     TPMI_DH_OBJECT keyHandle,
18     const TPM2B_ECC_POINT *inPoint)
19 {
20     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
21     TSS2_RC rval;
22 
23     if (!ctx)
24         return TSS2_SYS_RC_BAD_REFERENCE;
25 
26     rval = CommonPreparePrologue(ctx, TPM2_CC_ECDH_ZGen);
27     if (rval)
28         return rval;
29 
30     rval = Tss2_MU_UINT32_Marshal(keyHandle, ctx->cmdBuffer,
31                                   ctx->maxCmdSize,
32                                   &ctx->nextData);
33     if (rval)
34         return rval;
35 
36     if (!inPoint) {
37         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
38                                       ctx->maxCmdSize,
39                                       &ctx->nextData);
40 
41     } else {
42 
43         rval = Tss2_MU_TPM2B_ECC_POINT_Marshal(inPoint, ctx->cmdBuffer,
44                                                ctx->maxCmdSize,
45                                                &ctx->nextData);
46     }
47 
48     if (rval)
49         return rval;
50 
51     ctx->decryptAllowed = 1;
52     ctx->encryptAllowed = 1;
53     ctx->authAllowed = 1;
54 
55     return CommonPrepareEpilogue(ctx);
56 }
57 
Tss2_Sys_ECDH_ZGen_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_ECC_POINT * outPoint)58 TSS2_RC Tss2_Sys_ECDH_ZGen_Complete(
59     TSS2_SYS_CONTEXT *sysContext,
60     TPM2B_ECC_POINT *outPoint)
61 {
62     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
63     TSS2_RC rval;
64 
65     if (!ctx)
66         return TSS2_SYS_RC_BAD_REFERENCE;
67 
68     rval = CommonComplete(ctx);
69     if (rval)
70         return rval;
71 
72     return Tss2_MU_TPM2B_ECC_POINT_Unmarshal(ctx->cmdBuffer,
73                                              ctx->maxCmdSize,
74                                              &ctx->nextData,
75                                              outPoint);
76 }
77 
Tss2_Sys_ECDH_ZGen(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT keyHandle,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_ECC_POINT * inPoint,TPM2B_ECC_POINT * outPoint,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)78 TSS2_RC Tss2_Sys_ECDH_ZGen(
79     TSS2_SYS_CONTEXT *sysContext,
80     TPMI_DH_OBJECT keyHandle,
81     TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
82     const TPM2B_ECC_POINT *inPoint,
83     TPM2B_ECC_POINT *outPoint,
84     TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
85 {
86     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
87     TSS2_RC rval;
88 
89     rval = Tss2_Sys_ECDH_ZGen_Prepare(sysContext, keyHandle, inPoint);
90     if (rval)
91         return rval;
92 
93     rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
94     if (rval)
95         return rval;
96 
97     return Tss2_Sys_ECDH_ZGen_Complete(sysContext, outPoint);
98 }
99