1 // This file was extracted from the TCG Published
2 // Trusted Platform Module Library
3 // Part 3: Commands
4 // Family "2.0"
5 // Level 00 Revision 01.16
6 // October 30, 2014
7 
8 #include "InternalRoutines.h"
9 #include "ECDH_ZGen_fp.h"
10 #ifdef TPM_ALG_ECC
11 //
12 //
13 //     Error Returns                     Meaning
14 //
15 //     TPM_RC_ATTRIBUTES                 key referenced by keyA is restricted or not a decrypt key
16 //     TPM_RC_KEY                        key referenced by keyA is not an ECC key
17 //     TPM_RC_NO_RESULT                  multiplying inPoint resulted in a point at infinity
18 //     TPM_RC_SCHEME                     the scheme of the key referenced by keyA is not TPM_ALG_NULL,
19 //                                       TPM_ALG_ECDH,
20 //
21 TPM_RC
TPM2_ECDH_ZGen(ECDH_ZGen_In * in,ECDH_ZGen_Out * out)22 TPM2_ECDH_ZGen(
23    ECDH_ZGen_In      *in,                  // IN: input parameter list
24    ECDH_ZGen_Out     *out                  // OUT: output parameter list
25    )
26 {
27    TPM_RC                     result;
28    OBJECT                    *eccKey;
29 
30 // Input Validation
31 
32    eccKey = ObjectGet(in->keyHandle);
33 
34    // Input key must be a non-restricted, decrypt ECC key
35    if(   eccKey->publicArea.type != TPM_ALG_ECC)
36        return TPM_RC_KEY + RC_ECDH_ZGen_keyHandle;
37 
38    if(     eccKey->publicArea.objectAttributes.restricted == SET
39       ||   eccKey->publicArea.objectAttributes.decrypt != SET
40      )
41        return TPM_RC_KEY + RC_ECDH_ZGen_keyHandle;
42 
43    // Make sure the scheme allows this use
44    if(     eccKey->publicArea.parameters.eccDetail.scheme.scheme != TPM_ALG_ECDH
45        && eccKey->publicArea.parameters.eccDetail.scheme.scheme != TPM_ALG_NULL)
46        return TPM_RC_SCHEME + RC_ECDH_ZGen_keyHandle;
47 
48 // Command Output
49 
50    // Compute Z. TPM_RC_ECC_POINT or TPM_RC_NO_RESULT may be returned here.
51    result = CryptEccPointMultiply(&out->outPoint.t.point,
52                                   eccKey->publicArea.parameters.eccDetail.curveID,
53                                   &eccKey->sensitive.sensitive.ecc,
54                                   &in->inPoint.t.point);
55    if(result != TPM_RC_SUCCESS)
56        return RcSafeAddToResult(result, RC_ECDH_ZGen_inPoint);
57 
58    out->outPoint.t.size = TPMS_ECC_POINT_Marshal(&out->outPoint.t.point,
59                                                  NULL, NULL);
60 
61    return TPM_RC_SUCCESS;
62 }
63 #endif
64