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 //** Introduction
36 // This file contains the structure definitions used for ECC in the OpenSSL
37 // version of the code. These definitions would change, based on the library.
38 // The ECC-related structures that cross the TPM interface are defined
39 // in TpmTypes.h
40 //
41 
42 #ifndef MATH_LIB_DEFINED
43 #define MATH_LIB_DEFINED
44 
45 #define MATH_LIB_OSSL
46 
47 #include <openssl/evp.h>
48 #include <openssl/ec.h>
49 
50 #define SYMMETRIC_ALIGNMENT RADIX_BYTES
51 
52 #include <openssl/bn.h>
53 
54 //** Macros and Defines
55 
56 // Make sure that the library is using the correct size for a crypt word
57 #if    defined THIRTY_TWO_BIT && (RADIX_BITS != 32)  \
58     || ((defined SIXTY_FOUR_BIT_LONG || defined SIXTY_FOUR_BIT) \
59         && (RADIX_BITS != 64))
60 #   error Ossl library is using different radix
61 #endif
62 
63 // Allocate a local BIGNUM value. For the allocation, a bigNum structure is created
64 // as is a local BIGNUM. The bigNum is initialized and then the BIGNUM is
65 // set to reference the local value.
66 #define BIG_VAR(name, bits)                                         \
67     BN_VAR(name##Bn, (bits));                                       \
68     BIGNUM          _##name;                                        \
69     BIGNUM          *name = BigInitialized(&_##name,                \
70                                 BnInit(name##Bn,                    \
71                                 BYTES_TO_CRYPT_WORDS(sizeof(_##name##Bn.d))))
72 
73 // Allocate a BIGNUM and initialize with the values in a bigNum initializer
74 #define BIG_INITIALIZED(name, initializer)                      \
75     BIGNUM           _##name;                                   \
76     BIGNUM          *name = BigInitialized(&_##name, initializer)
77 
78 
79 typedef struct
80 {
81     const ECC_CURVE_DATA    *C;     // the TPM curve values
82     EC_GROUP                *G;     // group parameters
83     BN_CTX                  *CTX;   // the context for the math (this might not be
84                                     // the context in which the curve was created>;
85 } OSSL_CURVE_DATA;
86 
87 typedef OSSL_CURVE_DATA      *bigCurve;
88 
89 #define AccessCurveData(E)      ((E)->C)
90 
91 
92 #include "TpmToOsslSupport_fp.h"
93 
94 // Start and end a context within which the OpenSSL memory management works
95 #define OSSL_ENTER()    BN_CTX          *CTX = OsslContextEnter()
96 #define OSSL_LEAVE()    OsslContextLeave(CTX)
97 
98 // Start and end a context that spans multiple ECC functions. This is used so that
99 // the group for the curve can persist across multiple frames.
100 #define CURVE_INITIALIZED(name, initializer)                        \
101     OSSL_CURVE_DATA     _##name;                                    \
102     bigCurve            name =  BnCurveInitialize(&_##name, initializer)
103 #define CURVE_FREE(name)               BnCurveFree(name)
104 
105 // Start and end a local stack frame within the context of the curve frame
106 #define ECC_ENTER()     BN_CTX         *CTX = OsslPushContext(E->CTX)
107 #define ECC_LEAVE()     OsslPopContext(CTX)
108 
109 #define BN_NEW()        BnNewVariable(CTX)
110 
111 // This definition would change if there were something to report
112 #define MathLibSimulationEnd()
113 
114 #endif // MATH_LIB_DEFINED
115