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_Encrypt_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT keyHandle,const TPM2B_PUBLIC_KEY_RSA * message,const TPMT_RSA_DECRYPT * inScheme,const TPM2B_DATA * label)15 TSS2_RC Tss2_Sys_RSA_Encrypt_Prepare(
16 TSS2_SYS_CONTEXT *sysContext,
17 TPMI_DH_OBJECT keyHandle,
18 const TPM2B_PUBLIC_KEY_RSA *message,
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_Encrypt);
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 (!message) {
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(message, ctx->cmdBuffer,
47 ctx->maxCmdSize,
48 &ctx->nextData);
49 }
50
51 if (rval)
52 return rval;
53
54 rval = Tss2_MU_TPMT_RSA_DECRYPT_Marshal(inScheme, ctx->cmdBuffer,
55 ctx->maxCmdSize,
56 &ctx->nextData);
57 if (rval)
58 return rval;
59
60 if (!label) {
61 rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
62 ctx->maxCmdSize,
63 &ctx->nextData);
64
65 } else {
66
67 rval = Tss2_MU_TPM2B_DATA_Marshal(label, ctx->cmdBuffer,
68 ctx->maxCmdSize,
69 &ctx->nextData);
70 }
71
72 if (rval)
73 return rval;
74
75 ctx->decryptAllowed = 1;
76 ctx->encryptAllowed = 1;
77 ctx->authAllowed = 1;
78
79 return CommonPrepareEpilogue(ctx);
80 }
81
Tss2_Sys_RSA_Encrypt_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_PUBLIC_KEY_RSA * outData)82 TSS2_RC Tss2_Sys_RSA_Encrypt_Complete(
83 TSS2_SYS_CONTEXT *sysContext,
84 TPM2B_PUBLIC_KEY_RSA *outData)
85 {
86 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
87 TSS2_RC rval;
88
89 if (!ctx)
90 return TSS2_SYS_RC_BAD_REFERENCE;
91
92 rval = CommonComplete(ctx);
93 if (rval)
94 return rval;
95
96 return Tss2_MU_TPM2B_PUBLIC_KEY_RSA_Unmarshal(ctx->cmdBuffer,
97 ctx->maxCmdSize,
98 &ctx->nextData, outData);
99 }
100
Tss2_Sys_RSA_Encrypt(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT keyHandle,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_PUBLIC_KEY_RSA * message,const TPMT_RSA_DECRYPT * inScheme,const TPM2B_DATA * label,TPM2B_PUBLIC_KEY_RSA * outData,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)101 TSS2_RC Tss2_Sys_RSA_Encrypt(
102 TSS2_SYS_CONTEXT *sysContext,
103 TPMI_DH_OBJECT keyHandle,
104 TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
105 const TPM2B_PUBLIC_KEY_RSA *message,
106 const TPMT_RSA_DECRYPT *inScheme,
107 const TPM2B_DATA *label,
108 TPM2B_PUBLIC_KEY_RSA *outData,
109 TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
110 {
111 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
112 TSS2_RC rval;
113
114 if (!inScheme)
115 return TSS2_SYS_RC_BAD_REFERENCE;
116
117 rval = Tss2_Sys_RSA_Encrypt_Prepare(sysContext, keyHandle, message, inScheme, label);
118 if (rval)
119 return rval;
120
121 rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
122 if (rval)
123 return rval;
124
125 return Tss2_Sys_RSA_Encrypt_Complete(sysContext, outData);
126 }
127