xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/src/crypt/CryptEccCrypt.c (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen  *
3*5c591343SA. Cody Schuffelen  *  The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen  *  included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen  *  contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen  *  under this license.
7*5c591343SA. Cody Schuffelen  *
8*5c591343SA. Cody Schuffelen  *  Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen  *
10*5c591343SA. Cody Schuffelen  *  All rights reserved.
11*5c591343SA. Cody Schuffelen  *
12*5c591343SA. Cody Schuffelen  *  BSD License
13*5c591343SA. Cody Schuffelen  *
14*5c591343SA. Cody Schuffelen  *  Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen  *  are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen  *
17*5c591343SA. Cody Schuffelen  *  Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen  *  of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen  *
20*5c591343SA. Cody Schuffelen  *  Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen  *  list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen  *  other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen  *
24*5c591343SA. Cody Schuffelen  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen  */
35*5c591343SA. Cody Schuffelen //** Includes and Defines
36*5c591343SA. Cody Schuffelen #include "Tpm.h"
37*5c591343SA. Cody Schuffelen 
38*5c591343SA. Cody Schuffelen #if CC_ECC_Encrypt || CC_ECC_Encrypt
39*5c591343SA. Cody Schuffelen 
40*5c591343SA. Cody Schuffelen //** Functions
41*5c591343SA. Cody Schuffelen 
42*5c591343SA. Cody Schuffelen //*** CryptEccSelectScheme()
43*5c591343SA. Cody Schuffelen // This function is used by TPM2_ECC_Decrypt and TPM2_ECC_Encrypt.  It sets scheme
44*5c591343SA. Cody Schuffelen // either the input scheme or the key scheme. If they key scheme is not TPM_ALG_NULL
45*5c591343SA. Cody Schuffelen // then the input scheme must be TPM_ALG_NULL or the same as the key scheme. If
46*5c591343SA. Cody Schuffelen // not, then the function returns FALSE.
47*5c591343SA. Cody Schuffelen //  Return Type: BOOL
48*5c591343SA. Cody Schuffelen //      TRUE        'scheme' is set
49*5c591343SA. Cody Schuffelen //      FALSE       'scheme' is not valid (it may have been changed).
50*5c591343SA. Cody Schuffelen BOOL
CryptEccSelectScheme(OBJECT * key,TPMT_KDF_SCHEME * scheme)51*5c591343SA. Cody Schuffelen CryptEccSelectScheme(
52*5c591343SA. Cody Schuffelen     OBJECT              *key,           //IN: key containing default scheme
53*5c591343SA. Cody Schuffelen     TPMT_KDF_SCHEME     *scheme         // IN: a decrypt scheme
54*5c591343SA. Cody Schuffelen )
55*5c591343SA. Cody Schuffelen {
56*5c591343SA. Cody Schuffelen     TPMT_KDF_SCHEME    *keyScheme = &key->publicArea.parameters.eccDetail.kdf;
57*5c591343SA. Cody Schuffelen 
58*5c591343SA. Cody Schuffelen     // Get sign object pointer
59*5c591343SA. Cody Schuffelen     if(scheme->scheme == TPM_ALG_NULL)
60*5c591343SA. Cody Schuffelen         *scheme = *keyScheme;
61*5c591343SA. Cody Schuffelen     if(keyScheme->scheme == TPM_ALG_NULL)
62*5c591343SA. Cody Schuffelen         keyScheme = scheme;
63*5c591343SA. Cody Schuffelen     return (scheme->scheme != TPM_ALG_NULL &&
64*5c591343SA. Cody Schuffelen                 (keyScheme->scheme == scheme->scheme
65*5c591343SA. Cody Schuffelen             && keyScheme->details.anyKdf.hashAlg == scheme->details.anyKdf.hashAlg));
66*5c591343SA. Cody Schuffelen }
67*5c591343SA. Cody Schuffelen 
68*5c591343SA. Cody Schuffelen 
69*5c591343SA. Cody Schuffelen 
70*5c591343SA. Cody Schuffelen //*** CryptEccEncrypt()
71*5c591343SA. Cody Schuffelen //This function performs ECC-based data obfuscation. The only scheme that is currently
72*5c591343SA. Cody Schuffelen // supported is MGF1 based. See Part 1, Annex D for details.
73*5c591343SA. Cody Schuffelen //  Return Type: TPM_RC
74*5c591343SA. Cody Schuffelen //      TPM_RC_CURVE            unsupported curve
75*5c591343SA. Cody Schuffelen //      TPM_RC_HASH             hash not allowed
76*5c591343SA. Cody Schuffelen //      TPM_RC_SCHEME           'scheme' is not supported
77*5c591343SA. Cody Schuffelen //      TPM_RC_NO_RESULT        internal error in big number processing
78*5c591343SA. Cody Schuffelen LIB_EXPORT TPM_RC
CryptEccEncrypt(OBJECT * key,TPMT_KDF_SCHEME * scheme,TPM2B_MAX_BUFFER * plainText,TPMS_ECC_POINT * c1,TPM2B_MAX_BUFFER * c2,TPM2B_DIGEST * c3)79*5c591343SA. Cody Schuffelen CryptEccEncrypt(
80*5c591343SA. Cody Schuffelen     OBJECT                  *key,           // IN: public key of recipient
81*5c591343SA. Cody Schuffelen     TPMT_KDF_SCHEME         *scheme,        // IN: scheme to use.
82*5c591343SA. Cody Schuffelen     TPM2B_MAX_BUFFER        *plainText,     // IN: the text to obfuscate
83*5c591343SA. Cody Schuffelen     TPMS_ECC_POINT          *c1,            // OUT: public ephemeral key
84*5c591343SA. Cody Schuffelen     TPM2B_MAX_BUFFER        *c2,            // OUT: obfuscated text
85*5c591343SA. Cody Schuffelen     TPM2B_DIGEST            *c3             // OUT: digest of ephemeral key
86*5c591343SA. Cody Schuffelen                                             //      and plainText
87*5c591343SA. Cody Schuffelen )
88*5c591343SA. Cody Schuffelen {
89*5c591343SA. Cody Schuffelen     CURVE_INITIALIZED(E, key->publicArea.parameters.eccDetail.curveID);
90*5c591343SA. Cody Schuffelen     POINT_INITIALIZED(PB, &key->publicArea.unique.ecc);
91*5c591343SA. Cody Schuffelen     POINT_VAR(Px, MAX_ECC_KEY_BITS);
92*5c591343SA. Cody Schuffelen     TPMS_ECC_POINT          p2;
93*5c591343SA. Cody Schuffelen     ECC_NUM(D);
94*5c591343SA. Cody Schuffelen     TPM2B_TYPE(2ECC, MAX_ECC_KEY_BYTES * 2);
95*5c591343SA. Cody Schuffelen     TPM2B_2ECC              z;
96*5c591343SA. Cody Schuffelen     int                     i;
97*5c591343SA. Cody Schuffelen     HASH_STATE              hashState;
98*5c591343SA. Cody Schuffelen     TPM_RC                  retVal = TPM_RC_SUCCESS;
99*5c591343SA. Cody Schuffelen     //
100*5c591343SA. Cody Schuffelen #if defined DEBUG_ECC_ENCRYPT && DEBUG_ECC_ENCRYPT == YES
101*5c591343SA. Cody Schuffelen     RND_DEBUG           dbg;
102*5c591343SA. Cody Schuffelen     // This value is one less than the value from the reference so that it
103*5c591343SA. Cody Schuffelen     // will become the correct value after having one added
104*5c591343SA. Cody Schuffelen     TPM2B_ECC_PARAMETER k = {24, {
105*5c591343SA. Cody Schuffelen         0x38, 0x4F, 0x30, 0x35, 0x30, 0x73, 0xAE, 0xEC,
106*5c591343SA. Cody Schuffelen         0xE7, 0xA1, 0x65, 0x43, 0x30, 0xA9, 0x62, 0x04,
107*5c591343SA. Cody Schuffelen         0xD3, 0x79, 0x82, 0xA3, 0xE1, 0x5B, 0x2C, 0xB4}};
108*5c591343SA. Cody Schuffelen     RND_DEBUG_Instantiate(&dbg, &k.b);
109*5c591343SA. Cody Schuffelen #   define RANDOM      (RAND_STATE *)&dbg
110*5c591343SA. Cody Schuffelen 
111*5c591343SA. Cody Schuffelen #else
112*5c591343SA. Cody Schuffelen #   define RANDOM      NULL
113*5c591343SA. Cody Schuffelen #endif
114*5c591343SA. Cody Schuffelen     if (E == NULL)
115*5c591343SA. Cody Schuffelen         ERROR_RETURN(TPM_RC_CURVE);
116*5c591343SA. Cody Schuffelen     if (TPM_ALG_KDF2 != scheme->scheme)
117*5c591343SA. Cody Schuffelen         ERROR_RETURN(TPM_RC_SCHEME);
118*5c591343SA. Cody Schuffelen     // generate an ephemeral key from a random k
119*5c591343SA. Cody Schuffelen     if (!BnEccGenerateKeyPair(D, Px, E, RANDOM)
120*5c591343SA. Cody Schuffelen         // C1 is the public part of the ephemeral key
121*5c591343SA. Cody Schuffelen         || !BnPointTo2B(c1, Px, E)
122*5c591343SA. Cody Schuffelen         // Compute P2
123*5c591343SA. Cody Schuffelen         || (BnPointMult(Px, PB, D, NULL, NULL, E) != TPM_RC_SUCCESS)
124*5c591343SA. Cody Schuffelen         || !BnPointTo2B(&p2, Px, E))
125*5c591343SA. Cody Schuffelen         ERROR_RETURN(TPM_RC_NO_RESULT);
126*5c591343SA. Cody Schuffelen 
127*5c591343SA. Cody Schuffelen     //Compute the C3 value hash(x2 || M || y2)
128*5c591343SA. Cody Schuffelen     if (0 == CryptHashStart(&hashState, scheme->details.mgf1.hashAlg))
129*5c591343SA. Cody Schuffelen         ERROR_RETURN(TPM_RC_HASH);
130*5c591343SA. Cody Schuffelen     CryptDigestUpdate2B(&hashState, &p2.x.b);
131*5c591343SA. Cody Schuffelen     CryptDigestUpdate2B(&hashState, &plainText->b);
132*5c591343SA. Cody Schuffelen     CryptDigestUpdate2B(&hashState, &p2.y.b);
133*5c591343SA. Cody Schuffelen     c3->t.size = CryptHashEnd(&hashState, sizeof(c3->t.buffer), c3->t.buffer);
134*5c591343SA. Cody Schuffelen 
135*5c591343SA. Cody Schuffelen     MemoryCopy2B(&z.b, &p2.x.b, sizeof(z.t.buffer));
136*5c591343SA. Cody Schuffelen     MemoryConcat2B(&z.b, &p2.y.b, sizeof(z.t.buffer));
137*5c591343SA. Cody Schuffelen     // Generate the mask value from MGF1 and put it in the return buffer
138*5c591343SA. Cody Schuffelen     c2->t.size = CryptMGF_KDF(plainText->t.size, c2->t.buffer,
139*5c591343SA. Cody Schuffelen                            scheme->details.mgf1.hashAlg, z.t.size, z.t.buffer, 1);
140*5c591343SA. Cody Schuffelen     // XOR the plainText into the generated mask to create the obfuscated data
141*5c591343SA. Cody Schuffelen     for (i = 0; i < plainText->t.size; i++)
142*5c591343SA. Cody Schuffelen         c2->t.buffer[i] ^= plainText->t.buffer[i];
143*5c591343SA. Cody Schuffelen Exit:
144*5c591343SA. Cody Schuffelen     CURVE_FREE(E);
145*5c591343SA. Cody Schuffelen     return retVal;
146*5c591343SA. Cody Schuffelen }
147*5c591343SA. Cody Schuffelen 
148*5c591343SA. Cody Schuffelen //*** CryptEccDecrypt()
149*5c591343SA. Cody Schuffelen // This function performs ECC decryption and integrity check of the input data.
150*5c591343SA. Cody Schuffelen //  Return Type: TPM_RC
151*5c591343SA. Cody Schuffelen //      TPM_RC_CURVE            unsupported curve
152*5c591343SA. Cody Schuffelen //      TPM_RC_HASH             hash not allowed
153*5c591343SA. Cody Schuffelen //      TPM_RC_SCHEME           'scheme' is not supported
154*5c591343SA. Cody Schuffelen //      TPM_RC_NO_RESULT        internal error in big number processing
155*5c591343SA. Cody Schuffelen //      TPM_RC_VALUE            C3 did not match hash of recovered data
156*5c591343SA. Cody Schuffelen LIB_EXPORT TPM_RC
CryptEccDecrypt(OBJECT * key,TPMT_KDF_SCHEME * scheme,TPM2B_MAX_BUFFER * plainText,TPMS_ECC_POINT * c1,TPM2B_MAX_BUFFER * c2,TPM2B_DIGEST * c3)157*5c591343SA. Cody Schuffelen CryptEccDecrypt(
158*5c591343SA. Cody Schuffelen     OBJECT                  *key,           // IN: key used for data recovery
159*5c591343SA. Cody Schuffelen     TPMT_KDF_SCHEME         *scheme,        // IN: scheme to use.
160*5c591343SA. Cody Schuffelen     TPM2B_MAX_BUFFER        *plainText,     // OUT: the recovered text
161*5c591343SA. Cody Schuffelen     TPMS_ECC_POINT          *c1,            // IN: public ephemeral key
162*5c591343SA. Cody Schuffelen     TPM2B_MAX_BUFFER        *c2,            // IN: obfuscated text
163*5c591343SA. Cody Schuffelen     TPM2B_DIGEST            *c3             // IN: digest of ephemeral key
164*5c591343SA. Cody Schuffelen                                             //      and plainText
165*5c591343SA. Cody Schuffelen )
166*5c591343SA. Cody Schuffelen {
167*5c591343SA. Cody Schuffelen     CURVE_INITIALIZED(E, key->publicArea.parameters.eccDetail.curveID);
168*5c591343SA. Cody Schuffelen     ECC_INITIALIZED(D, &key->sensitive.sensitive.ecc.b);
169*5c591343SA. Cody Schuffelen     POINT_INITIALIZED(C1, c1);
170*5c591343SA. Cody Schuffelen     TPMS_ECC_POINT          p2;
171*5c591343SA. Cody Schuffelen     TPM2B_TYPE(2ECC, MAX_ECC_KEY_BYTES * 2);
172*5c591343SA. Cody Schuffelen     TPM2B_DIGEST            check;
173*5c591343SA. Cody Schuffelen     TPM2B_2ECC              z;
174*5c591343SA. Cody Schuffelen     int                     i;
175*5c591343SA. Cody Schuffelen     HASH_STATE              hashState;
176*5c591343SA. Cody Schuffelen     TPM_RC                  retVal = TPM_RC_SUCCESS;
177*5c591343SA. Cody Schuffelen  //
178*5c591343SA. Cody Schuffelen     if (E == NULL)
179*5c591343SA. Cody Schuffelen         ERROR_RETURN(TPM_RC_CURVE);
180*5c591343SA. Cody Schuffelen     if (TPM_ALG_KDF2 != scheme->scheme)
181*5c591343SA. Cody Schuffelen         ERROR_RETURN(TPM_RC_SCHEME);
182*5c591343SA. Cody Schuffelen     // Generate the Z value
183*5c591343SA. Cody Schuffelen     BnPointMult(C1, C1, D, NULL, NULL, E);
184*5c591343SA. Cody Schuffelen     BnPointTo2B(&p2, C1, E);
185*5c591343SA. Cody Schuffelen 
186*5c591343SA. Cody Schuffelen     // Start the hash to check the algorithm
187*5c591343SA. Cody Schuffelen     if (0 == CryptHashStart(&hashState, scheme->details.mgf1.hashAlg))
188*5c591343SA. Cody Schuffelen         ERROR_RETURN(TPM_RC_HASH);
189*5c591343SA. Cody Schuffelen     CryptDigestUpdate2B(&hashState, &p2.x.b);
190*5c591343SA. Cody Schuffelen 
191*5c591343SA. Cody Schuffelen     MemoryCopy2B(&z.b, &p2.x.b, sizeof(z.t.buffer));
192*5c591343SA. Cody Schuffelen     MemoryConcat2B(&z.b, &p2.y.b, sizeof(z.t.buffer));
193*5c591343SA. Cody Schuffelen 
194*5c591343SA. Cody Schuffelen     // Generate the mask
195*5c591343SA. Cody Schuffelen     plainText->t.size = CryptMGF_KDF(c2->t.size, plainText->t.buffer,
196*5c591343SA. Cody Schuffelen                                   scheme->details.mgf1.hashAlg, z.t.size,
197*5c591343SA. Cody Schuffelen                                   z.t.buffer, 1);
198*5c591343SA. Cody Schuffelen     // XOR the obfuscated data into the generated mask to create the plainText data
199*5c591343SA. Cody Schuffelen     for (i = 0; i < plainText->t.size; i++)
200*5c591343SA. Cody Schuffelen         plainText->t.buffer[i] ^= c2->t.buffer[i];
201*5c591343SA. Cody Schuffelen 
202*5c591343SA. Cody Schuffelen     // Complete the hash and verify the data
203*5c591343SA. Cody Schuffelen     CryptDigestUpdate2B(&hashState, &plainText->b);
204*5c591343SA. Cody Schuffelen     CryptDigestUpdate2B(&hashState, &p2.y.b);
205*5c591343SA. Cody Schuffelen     check.t.size = CryptHashEnd(&hashState, sizeof(check.t.buffer), check.t.buffer);
206*5c591343SA. Cody Schuffelen     if (!MemoryEqual2B(&check.b, &c3->b))
207*5c591343SA. Cody Schuffelen         ERROR_RETURN(TPM_RC_VALUE);
208*5c591343SA. Cody Schuffelen Exit:
209*5c591343SA. Cody Schuffelen     CURVE_FREE(E);
210*5c591343SA. Cody Schuffelen     return retVal;
211*5c591343SA. Cody Schuffelen }
212*5c591343SA. Cody Schuffelen 
213*5c591343SA. Cody Schuffelen 
214*5c591343SA. Cody Schuffelen #endif  // CC_ECC_Encrypt || CC_ECC_Encrypt
215