1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************;
3 * Copyright (c) 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_EncryptDecrypt2_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT keyHandle,const TPM2B_MAX_BUFFER * inData,TPMI_YES_NO decrypt,TPMI_ALG_SYM_MODE mode,const TPM2B_IV * ivIn)15 TSS2_RC Tss2_Sys_EncryptDecrypt2_Prepare (
16 TSS2_SYS_CONTEXT *sysContext,
17 TPMI_DH_OBJECT keyHandle,
18 const TPM2B_MAX_BUFFER *inData,
19 TPMI_YES_NO decrypt,
20 TPMI_ALG_SYM_MODE mode,
21 const TPM2B_IV *ivIn)
22 {
23 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
24 TSS2_RC rval;
25
26 if (!ctx)
27 return TSS2_SYS_RC_BAD_REFERENCE;
28
29 rval = CommonPreparePrologue (ctx, TPM2_CC_EncryptDecrypt2);
30 if (rval)
31 return rval;
32
33 rval = Tss2_MU_UINT32_Marshal (keyHandle,
34 ctx->cmdBuffer,
35 ctx->maxCmdSize,
36 &ctx->nextData);
37 if (rval)
38 return rval;
39
40 if (!inData) {
41 rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
42 ctx->maxCmdSize,
43 &ctx->nextData);
44
45 } else {
46
47 rval = Tss2_MU_TPM2B_MAX_BUFFER_Marshal (inData,
48 ctx->cmdBuffer,
49 ctx->maxCmdSize,
50 &ctx->nextData);
51 }
52
53 if (rval)
54 return rval;
55
56 rval = Tss2_MU_UINT8_Marshal (decrypt,
57 ctx->cmdBuffer,
58 ctx->maxCmdSize,
59 &ctx->nextData);
60 if (rval)
61 return rval;
62
63 rval = Tss2_MU_UINT16_Marshal (mode,
64 ctx->cmdBuffer,
65 ctx->maxCmdSize,
66 &ctx->nextData);
67 if (rval)
68 return rval;
69
70 if (!ivIn) {
71 rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
72 ctx->maxCmdSize,
73 &ctx->nextData);
74
75 } else {
76
77 rval = Tss2_MU_TPM2B_IV_Marshal (ivIn,
78 ctx->cmdBuffer,
79 ctx->maxCmdSize,
80 &ctx->nextData);
81 }
82
83 if (rval)
84 return rval;
85
86 ctx->decryptAllowed = 1;
87 ctx->encryptAllowed = 1;
88 ctx->authAllowed = 1;
89
90 return CommonPrepareEpilogue (ctx);
91 }
92
Tss2_Sys_EncryptDecrypt2_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_MAX_BUFFER * outData,TPM2B_IV * ivOut)93 TSS2_RC Tss2_Sys_EncryptDecrypt2_Complete (
94 TSS2_SYS_CONTEXT *sysContext,
95 TPM2B_MAX_BUFFER *outData,
96 TPM2B_IV *ivOut)
97 {
98 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
99 TSS2_RC rval;
100
101 if (!ctx)
102 return TSS2_SYS_RC_BAD_REFERENCE;
103
104 rval = CommonComplete (ctx);
105 if (rval)
106 return rval;
107
108 rval = Tss2_MU_TPM2B_MAX_BUFFER_Unmarshal (ctx->cmdBuffer,
109 ctx->maxCmdSize,
110 &ctx->nextData,
111 outData);
112 if (rval)
113 return rval;
114
115 return Tss2_MU_TPM2B_IV_Unmarshal (ctx->cmdBuffer,
116 ctx->maxCmdSize,
117 &ctx->nextData,
118 ivOut);
119 }
120
Tss2_Sys_EncryptDecrypt2(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT keyHandle,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_MAX_BUFFER * inData,TPMI_YES_NO decrypt,TPMI_ALG_SYM_MODE mode,const TPM2B_IV * ivIn,TPM2B_MAX_BUFFER * outData,TPM2B_IV * ivOut,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)121 TSS2_RC Tss2_Sys_EncryptDecrypt2 (
122 TSS2_SYS_CONTEXT *sysContext,
123 TPMI_DH_OBJECT keyHandle,
124 TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
125 const TPM2B_MAX_BUFFER *inData,
126 TPMI_YES_NO decrypt,
127 TPMI_ALG_SYM_MODE mode,
128 const TPM2B_IV *ivIn,
129 TPM2B_MAX_BUFFER *outData,
130 TPM2B_IV *ivOut,
131 TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
132 {
133 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
134 TSS2_RC rval;
135
136 rval = Tss2_Sys_EncryptDecrypt2_Prepare (sysContext,
137 keyHandle,
138 inData,
139 decrypt,
140 mode,
141 ivIn);
142 if (rval)
143 return rval;
144
145 rval = CommonOneCall (ctx, cmdAuthsArray, rspAuthsArray);
146 if (rval)
147 return rval;
148
149 return Tss2_Sys_EncryptDecrypt2_Complete (sysContext, outData, ivOut);
150 }
151