1 /* Microsoft Reference Implementation for TPM 2.0
2 *
3 * The copyright in this software is being made available under the BSD License,
4 * included below. This software may be subject to other third party and
5 * contributor rights, including patent rights, and no such rights are granted
6 * under this license.
7 *
8 * Copyright (c) Microsoft Corporation
9 *
10 * All rights reserved.
11 *
12 * BSD License
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 *
17 * Redistributions of source code must retain the above copyright notice, this list
18 * of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above copyright notice, this
21 * list of conditions and the following disclaimer in the documentation and/or
22 * other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 #include "Tpm.h"
36 #include "RSA_Decrypt_fp.h"
37
38 #if CC_RSA_Decrypt // Conditional expansion of this file
39
40 /*(See part 3 specification)
41 // decrypts the provided data block and removes the padding if applicable
42 */
43 // Return Type: TPM_RC
44 // TPM_RC_ATTRIBUTES 'decrypt' is not SET or if 'restricted' is SET in
45 // the key referenced by 'keyHandle'
46 // TPM_RC_BINDING The public and private parts of the key are not
47 // properly bound
48 // TPM_RC_KEY 'keyHandle' does not reference an unrestricted
49 // decrypt key
50 // TPM_RC_SCHEME incorrect input scheme, or the chosen
51 // 'scheme' is not a valid RSA decrypt scheme
52 // TPM_RC_SIZE 'cipherText' is not the size of the modulus
53 // of key referenced by 'keyHandle'
54 // TPM_RC_VALUE 'label' is not a null terminated string or the value
55 // of 'cipherText' is greater that the modulus of
56 // 'keyHandle' or the encoding of the data is not
57 // valid
58
59 TPM_RC
TPM2_RSA_Decrypt(RSA_Decrypt_In * in,RSA_Decrypt_Out * out)60 TPM2_RSA_Decrypt(
61 RSA_Decrypt_In *in, // IN: input parameter list
62 RSA_Decrypt_Out *out // OUT: output parameter list
63 )
64 {
65 TPM_RC result;
66 OBJECT *rsaKey;
67 TPMT_RSA_DECRYPT *scheme;
68
69 // Input Validation
70
71 rsaKey = HandleToObject(in->keyHandle);
72
73 // The selected key must be an RSA key
74 if(rsaKey->publicArea.type != TPM_ALG_RSA)
75 return TPM_RCS_KEY + RC_RSA_Decrypt_keyHandle;
76
77 // The selected key must be an unrestricted decryption key
78 if(IS_ATTRIBUTE(rsaKey->publicArea.objectAttributes, TPMA_OBJECT, restricted)
79 || !IS_ATTRIBUTE(rsaKey->publicArea.objectAttributes, TPMA_OBJECT, decrypt))
80 return TPM_RCS_ATTRIBUTES + RC_RSA_Decrypt_keyHandle;
81
82 // NOTE: Proper operation of this command requires that the sensitive area
83 // of the key is loaded. This is assured because authorization is required
84 // to use the sensitive area of the key. In order to check the authorization,
85 // the sensitive area has to be loaded, even if authorization is with policy.
86
87 // If label is present, make sure that it is a NULL-terminated string
88 if(!IsLabelProperlyFormatted(&in->label.b))
89 return TPM_RCS_VALUE + RC_RSA_Decrypt_label;
90 // Command Output
91 // Select a scheme for decrypt.
92 scheme = CryptRsaSelectScheme(in->keyHandle, &in->inScheme);
93 if(scheme == NULL)
94 return TPM_RCS_SCHEME + RC_RSA_Decrypt_inScheme;
95
96 // Decryption. TPM_RC_VALUE, TPM_RC_SIZE, and TPM_RC_KEY error may be
97 // returned by CryptRsaDecrypt.
98 // NOTE: CryptRsaDecrypt can also return TPM_RC_ATTRIBUTES or TPM_RC_BINDING
99 // when the key is not a decryption key but that was checked above.
100 out->message.t.size = sizeof(out->message.t.buffer);
101 result = CryptRsaDecrypt(&out->message.b, &in->cipherText.b, rsaKey,
102 scheme, &in->label.b);
103 return result;
104 }
105
106 #endif // CC_RSA_Decrypt