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