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 /*(Auto-generated)
36  *  Created by TpmPrototypes; Version 3.0 July 18, 2017
37  *  Date: Aug 30, 2019  Time: 02:11:54PM
38  */
39 
40 #ifndef    _TPM_TO_WOLF_MATH_FP_H_
41 #define    _TPM_TO_WOLF_MATH_FP_H_
42 
43 #ifdef MATH_LIB_WOLF
44 
45 //*** BnFromWolf()
46 // This function converts a wolfcrypt mp_int to a TPM bignum. In this implementation
47 // it is assumed that wolfcrypt used the same format for a big number as does the
48 // TPM -- an array of native-endian words in little-endian order.
49 void
50 BnFromWolf(
51     bigNum          bn,
52     mp_int          *wolfBn
53 );
54 
55 //*** BnToWolf()
56 // This function converts a TPM bignum to a wolfcrypt mp_init, and has the same
57 // assumptions as made by BnFromWolf()
58 void
59 BnToWolf(
60     mp_int              *toInit,
61     bigConst            initializer
62 );
63 
64 //*** MpInitialize()
65 // This function initializes an wolfcrypt mp_int.
66 mp_int *
67 MpInitialize(
68     mp_int              *toInit
69 );
70 
71 #if LIBRARY_COMPATIBILITY_CHECK
72 //** MathLibraryCompatibililtyCheck()
73 // This function is only used during development to make sure that the library
74 // that is being referenced is using the same size of data structures as the TPM.
75 BOOL
76 MathLibraryCompatibilityCheck(
77     void
78 );
79 #endif
80 
81 //*** BnModMult()
82 // Does multiply and divide returning the remainder of the divide.
83 LIB_EXPORT BOOL
84 BnModMult(
85     bigNum              result,
86     bigConst            op1,
87     bigConst            op2,
88     bigConst            modulus
89 );
90 
91 //*** BnMult()
92 // Multiplies two numbers
93 LIB_EXPORT BOOL
94 BnMult(
95     bigNum               result,
96     bigConst             multiplicand,
97     bigConst             multiplier
98 );
99 
100 //*** BnDiv()
101 // This function divides two bigNum values. The function returns FALSE if
102 // there is an error in the operation.
103 LIB_EXPORT BOOL
104 BnDiv(
105     bigNum               quotient,
106     bigNum               remainder,
107     bigConst             dividend,
108     bigConst             divisor
109 );
110 
111 #if ALG_RSA
112 //*** BnGcd()
113 // Get the greatest common divisor of two numbers
114 LIB_EXPORT BOOL
115 BnGcd(
116     bigNum      gcd,            // OUT: the common divisor
117     bigConst    number1,        // IN:
118     bigConst    number2         // IN:
119 );
120 
121 //***BnModExp()
122 // Do modular exponentiation using bigNum values. The conversion from a mp_int to
123 // a bigNum is trivial as they are based on the same structure
124 LIB_EXPORT BOOL
125 BnModExp(
126     bigNum               result,         // OUT: the result
127     bigConst             number,         // IN: number to exponentiate
128     bigConst             exponent,       // IN:
129     bigConst             modulus         // IN:
130 );
131 
132 //*** BnModInverse()
133 // Modular multiplicative inverse
134 LIB_EXPORT BOOL
135 BnModInverse(
136     bigNum               result,
137     bigConst             number,
138     bigConst             modulus
139 );
140 #endif // TPM_ALG_RSA
141 #if ALG_ECC
142 
143 //*** PointFromWolf()
144 // Function to copy the point result from a wolf ecc_point to a bigNum
145 void
146 PointFromWolf(
147     bigPoint         pOut,      // OUT: resulting point
148     ecc_point       *pIn       // IN: the point to return
149 );
150 
151 //*** PointToWolf()
152 // Function to copy the point result from a bigNum to a wolf ecc_point
153 void
154 PointToWolf(
155     ecc_point      *pOut,      // OUT: resulting point
156     pointConst      pIn       // IN: the point to return
157 );
158 
159 //*** BnEccModMult()
160 // This function does a point multiply of the form R = [d]S
161 // return type: BOOL
162 //  FALSE       failure in operation; treat as result being point at infinity
163 LIB_EXPORT BOOL
164 BnEccModMult(
165     bigPoint             R,         // OUT: computed point
166     pointConst           S,         // IN: point to multiply by 'd' (optional)
167     bigConst             d,         // IN: scalar for [d]S
168     bigCurve             E
169 );
170 
171 //*** BnEccModMult2()
172 // This function does a point multiply of the form R = [d]G + [u]Q
173 // return type: BOOL
174 //  FALSE       failure in operation; treat as result being point at infinity
175 LIB_EXPORT BOOL
176 BnEccModMult2(
177     bigPoint             R,         // OUT: computed point
178     pointConst           S,         // IN: optional point
179     bigConst             d,         // IN: scalar for [d]S or [d]G
180     pointConst           Q,         // IN: second point
181     bigConst             u,         // IN: second scalar
182     bigCurve             E          // IN: curve
183 );
184 
185 //** BnEccAdd()
186 // This function does addition of two points.
187 // return type: BOOL
188 //  FALSE       failure in operation; treat as result being point at infinity
189 LIB_EXPORT BOOL
190 BnEccAdd(
191     bigPoint             R,         // OUT: computed point
192     pointConst           S,         // IN: point to multiply by 'd'
193     pointConst           Q,         // IN: second point
194     bigCurve             E          // IN: curve
195 );
196 #endif // TPM_ALG_ECC
197 #endif // MATH_LIB_WOLF
198 
199 #endif  // _TPM_TO_WOLF_MATH_FP_H_
200