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_RSA_Decrypt_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT keyHandle,const TPM2B_PUBLIC_KEY_RSA * cipherText,const TPMT_RSA_DECRYPT * inScheme,const TPM2B_DATA * label)15 TSS2_RC Tss2_Sys_RSA_Decrypt_Prepare(
16 TSS2_SYS_CONTEXT *sysContext,
17 TPMI_DH_OBJECT keyHandle,
18 const TPM2B_PUBLIC_KEY_RSA *cipherText,
19 const TPMT_RSA_DECRYPT *inScheme,
20 const TPM2B_DATA *label)
21 {
22 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
23 TSS2_RC rval;
24
25 if (!ctx || !inScheme)
26 return TSS2_SYS_RC_BAD_REFERENCE;
27
28 rval = CommonPreparePrologue(ctx, TPM2_CC_RSA_Decrypt);
29 if (rval)
30 return rval;
31
32 rval = Tss2_MU_UINT32_Marshal(keyHandle, ctx->cmdBuffer,
33 ctx->maxCmdSize,
34 &ctx->nextData);
35 if (rval)
36 return rval;
37
38 if (!cipherText) {
39 ctx->decryptNull = 1;
40
41 rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
42 ctx->maxCmdSize,
43 &ctx->nextData);
44 } else {
45
46 rval = Tss2_MU_TPM2B_PUBLIC_KEY_RSA_Marshal(cipherText,
47 ctx->cmdBuffer,
48 ctx->maxCmdSize,
49 &ctx->nextData);
50 }
51
52 if (rval)
53 return rval;
54
55 rval = Tss2_MU_TPMT_RSA_DECRYPT_Marshal(inScheme, ctx->cmdBuffer,
56 ctx->maxCmdSize,
57 &ctx->nextData);
58 if (rval)
59 return rval;
60
61 if (!label) {
62 rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
63 ctx->maxCmdSize,
64 &ctx->nextData);
65
66 } else {
67
68 rval = Tss2_MU_TPM2B_DATA_Marshal(label, ctx->cmdBuffer,
69 ctx->maxCmdSize,
70 &ctx->nextData);
71 }
72
73 if (rval)
74 return rval;
75
76 ctx->decryptAllowed = 1;
77 ctx->encryptAllowed = 1;
78 ctx->authAllowed = 1;
79
80 return CommonPrepareEpilogue(ctx);
81 }
82
Tss2_Sys_RSA_Decrypt_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_PUBLIC_KEY_RSA * message)83 TSS2_RC Tss2_Sys_RSA_Decrypt_Complete(
84 TSS2_SYS_CONTEXT *sysContext,
85 TPM2B_PUBLIC_KEY_RSA *message)
86 {
87 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
88 TSS2_RC rval;
89
90 if (!ctx)
91 return TSS2_SYS_RC_BAD_REFERENCE;
92
93 rval = CommonComplete(ctx);
94 if (rval)
95 return rval;
96
97 return Tss2_MU_TPM2B_PUBLIC_KEY_RSA_Unmarshal(ctx->cmdBuffer,
98 ctx->maxCmdSize,
99 &ctx->nextData, message);
100 }
101
Tss2_Sys_RSA_Decrypt(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT keyHandle,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_PUBLIC_KEY_RSA * cipherText,const TPMT_RSA_DECRYPT * inScheme,const TPM2B_DATA * label,TPM2B_PUBLIC_KEY_RSA * message,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)102 TSS2_RC Tss2_Sys_RSA_Decrypt(
103 TSS2_SYS_CONTEXT *sysContext,
104 TPMI_DH_OBJECT keyHandle,
105 TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
106 const TPM2B_PUBLIC_KEY_RSA *cipherText,
107 const TPMT_RSA_DECRYPT *inScheme,
108 const TPM2B_DATA *label,
109 TPM2B_PUBLIC_KEY_RSA *message,
110 TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
111 {
112 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
113 TSS2_RC rval;
114
115 if (!inScheme)
116 return TSS2_SYS_RC_BAD_REFERENCE;
117
118 rval = Tss2_Sys_RSA_Decrypt_Prepare(sysContext, keyHandle, cipherText,
119 inScheme, label);
120 if (rval)
121 return rval;
122
123 rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
124 if (rval)
125 return rval;
126
127 return Tss2_Sys_RSA_Decrypt_Complete(sysContext, message);
128 }
129