xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/include/prototypes/BnMath_fp.h (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
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    _BN_MATH_FP_H_
41 #define    _BN_MATH_FP_H_
42 
43 //*** BnAdd()
44 // This function adds two bigNum values. This function always returns TRUE.
45 LIB_EXPORT BOOL
46 BnAdd(
47     bigNum           result,
48     bigConst         op1,
49     bigConst         op2
50 );
51 
52 //*** BnAddWord()
53 // This function adds a word value to a bigNum. This function always returns TRUE.
54 LIB_EXPORT BOOL
55 BnAddWord(
56     bigNum           result,
57     bigConst         op,
58     crypt_uword_t    word
59 );
60 
61 //*** BnSub()
62 // This function does subtraction of two bigNum values and returns result = op1 - op2
63 // when op1 is greater than op2. If op2 is greater than op1, then a fault is
64 // generated. This function always returns TRUE.
65 LIB_EXPORT BOOL
66 BnSub(
67     bigNum           result,
68     bigConst         op1,
69     bigConst         op2
70 );
71 
72 //*** BnSubWord()
73 // This function subtracts a word value from a bigNum. This function always
74 // returns TRUE.
75 LIB_EXPORT BOOL
76 BnSubWord(
77     bigNum           result,
78     bigConst         op,
79     crypt_uword_t    word
80 );
81 
82 //*** BnUnsignedCmp()
83 // This function performs a comparison of op1 to op2. The compare is approximately
84 // constant time if the size of the values used in the compare is consistent
85 // across calls (from the same line in the calling code).
86 //  Return Type: int
87 //      < 0             op1 is less than op2
88 //      0               op1 is equal to op2
89 //      > 0             op1 is greater than op2
90 LIB_EXPORT int
91 BnUnsignedCmp(
92     bigConst               op1,
93     bigConst               op2
94 );
95 
96 //*** BnUnsignedCmpWord()
97 // Compare a bigNum to a crypt_uword_t.
98 //  Return Type: int
99 //      -1              op1 is less that word
100 //      0               op1 is equal to word
101 //      1               op1 is greater than word
102 LIB_EXPORT int
103 BnUnsignedCmpWord(
104     bigConst             op1,
105     crypt_uword_t        word
106 );
107 
108 //*** BnModWord()
109 // This function does modular division of a big number when the modulus is a
110 // word value.
111 LIB_EXPORT crypt_word_t
112 BnModWord(
113     bigConst         numerator,
114     crypt_word_t     modulus
115 );
116 
117 //*** Msb()
118 // This function returns the bit number of the most significant bit of a
119 // crypt_uword_t. The number for the least significant bit of any bigNum value is 0.
120 // The maximum return value is RADIX_BITS - 1,
121 //  Return Type: int
122 //      -1              the word was zero
123 //      n               the bit number of the most significant bit in the word
124 LIB_EXPORT int
125 Msb(
126     crypt_uword_t           word
127 );
128 
129 //*** BnMsb()
130 // This function returns the number of the MSb of a bigNum value.
131 //  Return Type: int
132 //      -1              the word was zero or 'bn' was NULL
133 //      n               the bit number of the most significant bit in the word
134 LIB_EXPORT int
135 BnMsb(
136     bigConst            bn
137 );
138 
139 //*** BnSizeInBits()
140 // This function returns the number of bits required to hold a number. It is one
141 // greater than the Msb.
142 //
143 LIB_EXPORT unsigned
144 BnSizeInBits(
145     bigConst                 n
146 );
147 
148 //*** BnSetWord()
149 // Change the value of a bignum_t to a word value.
150 LIB_EXPORT bigNum
151 BnSetWord(
152     bigNum               n,
153     crypt_uword_t        w
154 );
155 
156 //*** BnSetBit()
157 // This function will SET a bit in a bigNum. Bit 0 is the least-significant bit in
158 // the 0th digit_t. The function always return TRUE
159 LIB_EXPORT BOOL
160 BnSetBit(
161     bigNum           bn,        // IN/OUT: big number to modify
162     unsigned int     bitNum     // IN: Bit number to SET
163 );
164 
165 //*** BnTestBit()
166 // This function is used to check to see if a bit is SET in a bignum_t. The 0th bit
167 // is the LSb of d[0].
168 //  Return Type: BOOL
169 //      TRUE(1)         the bit is set
170 //      FALSE(0)        the bit is not set or the number is out of range
171 LIB_EXPORT BOOL
172 BnTestBit(
173     bigNum               bn,        // IN: number to check
174     unsigned int         bitNum     // IN: bit to test
175 );
176 
177 //***BnMaskBits()
178 // This function is used to mask off high order bits of a big number.
179 // The returned value will have no more than 'maskBit' bits
180 // set.
181 // Note: There is a requirement that unused words of a bignum_t are set to zero.
182 //  Return Type: BOOL
183 //      TRUE(1)         result masked
184 //      FALSE(0)        the input was not as large as the mask
185 LIB_EXPORT BOOL
186 BnMaskBits(
187     bigNum           bn,        // IN/OUT: number to mask
188     crypt_uword_t    maskBit    // IN: the bit number for the mask.
189 );
190 
191 //*** BnShiftRight()
192 // This function will shift a bigNum to the right by the shiftAmount.
193 // This function always returns TRUE.
194 LIB_EXPORT BOOL
195 BnShiftRight(
196     bigNum           result,
197     bigConst         toShift,
198     uint32_t         shiftAmount
199 );
200 
201 //*** BnGetRandomBits()
202 // This function gets random bits for use in various places. To make sure that the
203 // number is generated in a portable format, it is created as a TPM2B and then
204 // converted to the internal format.
205 //
206 // One consequence of the generation scheme is that, if the number of bits requested
207 // is not a multiple of 8, then the high-order bits are set to zero. This would come
208 // into play when generating a 521-bit ECC key. A 66-byte (528-bit) value is
209 // generated an the high order 7 bits are masked off (CLEAR).
210 //  Return Type: BOOL
211 //      TRUE(1)         success
212 //      FALSE(0)        failure
213 LIB_EXPORT BOOL
214 BnGetRandomBits(
215     bigNum           n,
216     size_t           bits,
217     RAND_STATE      *rand
218 );
219 
220 //*** BnGenerateRandomInRange()
221 // This function is used to generate a random number r in the range 1 <= r < limit.
222 // The function gets a random number of bits that is the size of limit. There is some
223 // some probability that the returned number is going to be greater than or equal
224 // to the limit. If it is, try again. There is no more than 50% chance that the
225 // next number is also greater, so try again. We keep trying until we get a
226 // value that meets the criteria. Since limit is very often a number with a LOT of
227 // high order ones, this rarely would need a second try.
228 //  Return Type: BOOL
229 //      TRUE(1)         success
230 //      FALSE(0)        failure ('limit' is too small)
231 LIB_EXPORT BOOL
232 BnGenerateRandomInRange(
233     bigNum           dest,
234     bigConst         limit,
235     RAND_STATE      *rand
236 );
237 
238 #endif  // _BN_MATH_FP_H_
239