1 | /********************************************************************************/
|
---|
2 | /* */
|
---|
3 | /* RSA-related structures and defines */
|
---|
4 | /* Written by Ken Goldman */
|
---|
5 | /* IBM Thomas J. Watson Research Center */
|
---|
6 | /* */
|
---|
7 | /* Licenses and Notices */
|
---|
8 | /* */
|
---|
9 | /* 1. Copyright Licenses: */
|
---|
10 | /* */
|
---|
11 | /* - Trusted Computing Group (TCG) grants to the user of the source code in */
|
---|
12 | /* this specification (the "Source Code") a worldwide, irrevocable, */
|
---|
13 | /* nonexclusive, royalty free, copyright license to reproduce, create */
|
---|
14 | /* derivative works, distribute, display and perform the Source Code and */
|
---|
15 | /* derivative works thereof, and to grant others the rights granted herein. */
|
---|
16 | /* */
|
---|
17 | /* - The TCG grants to the user of the other parts of the specification */
|
---|
18 | /* (other than the Source Code) the rights to reproduce, distribute, */
|
---|
19 | /* display, and perform the specification solely for the purpose of */
|
---|
20 | /* developing products based on such documents. */
|
---|
21 | /* */
|
---|
22 | /* 2. Source Code Distribution Conditions: */
|
---|
23 | /* */
|
---|
24 | /* - Redistributions of Source Code must retain the above copyright licenses, */
|
---|
25 | /* this list of conditions and the following disclaimers. */
|
---|
26 | /* */
|
---|
27 | /* - Redistributions in binary form must reproduce the above copyright */
|
---|
28 | /* licenses, this list of conditions and the following disclaimers in the */
|
---|
29 | /* documentation and/or other materials provided with the distribution. */
|
---|
30 | /* */
|
---|
31 | /* 3. Disclaimers: */
|
---|
32 | /* */
|
---|
33 | /* - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */
|
---|
34 | /* LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */
|
---|
35 | /* RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */
|
---|
36 | /* THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE. */
|
---|
37 | /* Contact TCG Administration ([email protected]) for */
|
---|
38 | /* information on specification licensing rights available through TCG */
|
---|
39 | /* membership agreements. */
|
---|
40 | /* */
|
---|
41 | /* - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED */
|
---|
42 | /* WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR */
|
---|
43 | /* FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR */
|
---|
44 | /* NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY */
|
---|
45 | /* OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE. */
|
---|
46 | /* */
|
---|
47 | /* - Without limitation, TCG and its members and licensors disclaim all */
|
---|
48 | /* liability, including liability for infringement of any proprietary */
|
---|
49 | /* rights, relating to use of information in this specification and to the */
|
---|
50 | /* implementation of this specification, and TCG disclaims all liability for */
|
---|
51 | /* cost of procurement of substitute goods or services, lost profits, loss */
|
---|
52 | /* of use, loss of data or any incidental, consequential, direct, indirect, */
|
---|
53 | /* or special damages, whether under contract, tort, warranty or otherwise, */
|
---|
54 | /* arising in any way out of use or reliance upon this specification or any */
|
---|
55 | /* information herein. */
|
---|
56 | /* */
|
---|
57 | /* (c) Copyright IBM Corp. and others, 2016 - 2023 */
|
---|
58 | /* */
|
---|
59 | /********************************************************************************/
|
---|
60 |
|
---|
61 | // This file contains the RSA-related structures and defines.
|
---|
62 |
|
---|
63 | #ifndef _CRYPT_RSA_H
|
---|
64 | #define _CRYPT_RSA_H
|
---|
65 |
|
---|
66 | // These values are used in the Crypt_Int* representation of various RSA values.
|
---|
67 | // define ci_rsa_t as buffer containing a CRYPT_INT object with space for
|
---|
68 | // (MAX_RSA_KEY_BITS) of actual data.
|
---|
69 | CRYPT_INT_TYPE(rsa, MAX_RSA_KEY_BITS);
|
---|
70 | #define CRYPT_RSA_VAR(name) CRYPT_INT_VAR(name, MAX_RSA_KEY_BITS)
|
---|
71 | #define CRYPT_RSA_INITIALIZED(name, initializer) \
|
---|
72 | CRYPT_INT_INITIALIZED(name, MAX_RSA_KEY_BITS, initializer)
|
---|
73 |
|
---|
74 | #define CRYPT_PRIME_VAR(name) CRYPT_INT_VAR(name, (MAX_RSA_KEY_BITS / 2))
|
---|
75 | // define ci_prime_t as buffer containing a CRYPT_INT object with space for
|
---|
76 | // (MAX_RSA_KEY_BITS/2) of actual data.
|
---|
77 | CRYPT_INT_TYPE(prime, (MAX_RSA_KEY_BITS / 2));
|
---|
78 | #define CRYPT_PRIME_INITIALIZED(name, initializer) \
|
---|
79 | CRYPT_INT_INITIALIZED(name, MAX_RSA_KEY_BITS / 2, initializer)
|
---|
80 |
|
---|
81 | #if !CRT_FORMAT_RSA
|
---|
82 | # error This verson only works with CRT formatted data
|
---|
83 | #endif // !CRT_FORMAT_RSA
|
---|
84 |
|
---|
85 | typedef struct privateExponent
|
---|
86 | {
|
---|
87 | Crypt_Int* P;
|
---|
88 | Crypt_Int* Q;
|
---|
89 | Crypt_Int* dP;
|
---|
90 | Crypt_Int* dQ;
|
---|
91 | Crypt_Int* qInv;
|
---|
92 | ci_prime_t entries[5];
|
---|
93 | } privateExponent;
|
---|
94 |
|
---|
95 | #define NEW_PRIVATE_EXPONENT(X) \
|
---|
96 | privateExponent _##X; \
|
---|
97 | privateExponent* X = RsaInitializeExponent(&(_##X))
|
---|
98 |
|
---|
99 | // libtpms added begin: keep old privateExponent
|
---|
100 | /* The privateExponentOld is part of the OBJECT and we keep it there even though
|
---|
101 | * upstream got rid of it and stores Q, dP, dQ, and qInv by appending them to
|
---|
102 | * P stored in TPMT_SENSITIVE.TPMU_SENSITIVE_COMPOSITE.TPM2B_PRIVATE_KEY_RSA
|
---|
103 | */
|
---|
104 | typedef struct privateExponentOld
|
---|
105 | {
|
---|
106 | ci_prime_t Q;
|
---|
107 | ci_prime_t dP;
|
---|
108 | ci_prime_t dQ;
|
---|
109 | ci_prime_t qInv;
|
---|
110 | } privateExponent_t;
|
---|
111 |
|
---|
112 | #include "BnMemory_fp.h"
|
---|
113 |
|
---|
114 | static inline void RsaInitializeExponentOld(privateExponent_t* pExp)
|
---|
115 | {
|
---|
116 | BN_INIT(pExp->Q);
|
---|
117 | BN_INIT(pExp->dP);
|
---|
118 | BN_INIT(pExp->dQ);
|
---|
119 | BN_INIT(pExp->qInv);
|
---|
120 | }
|
---|
121 |
|
---|
122 | static inline void RsaSetExponentOld(privateExponent_t* pExp, // OUT
|
---|
123 | privateExponent* Z // IN
|
---|
124 | )
|
---|
125 | {
|
---|
126 | // pExp->Q must be set elsewhere
|
---|
127 | ExtMath_Copy((Crypt_Int*)&pExp->dP, Z->dP);
|
---|
128 | ExtMath_Copy((Crypt_Int*)&pExp->dQ, Z->dQ);
|
---|
129 | ExtMath_Copy((Crypt_Int*)&pExp->qInv, Z->qInv);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static inline void RsaSetExponentFromOld(privateExponent* Z, // OUT
|
---|
133 | privateExponent_t* pExp // IN
|
---|
134 | )
|
---|
135 | {
|
---|
136 | ExtMath_Copy(Z->Q, (Crypt_Int*)&pExp->Q);
|
---|
137 | ExtMath_Copy(Z->dP, (Crypt_Int*)&pExp->dP);
|
---|
138 | ExtMath_Copy(Z->dQ, (Crypt_Int*)&pExp->dQ);
|
---|
139 | ExtMath_Copy(Z->qInv, (Crypt_Int*)&pExp->qInv);
|
---|
140 | }
|
---|
141 | // libtpms added end
|
---|
142 | #endif // _CRYPT_RSA_H
|
---|