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