Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/include/BnValues.h

raw
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
37 // This file contains the definitions needed for defining the internal BIGNUM
38 // structure.
39
40 // A BIGNUM is a pointer to a structure. The structure has three fields. The
41 // last field is and array (d) of crypt_uword_t. Each word is in machine format
42 // (big- or little-endian) with the words in ascending significance (i.e. words
43 // in little-endian order). This is the order that seems to be used in every
44 // big number library in the worlds, so...
45 //
46 // The first field in the structure (allocated) is the number of words in 'd'.
47 // This is the upper limit on the size of the number that can be held in the
48 // structure. This differs from libraries like OpenSSL as this is not intended
49 // to deal with numbers of arbitrary size; just numbers that are needed to deal
50 // with the algorithms that are defined in the TPM implementation.
51 //
52 // The second field in the structure (size) is the number of significant words
53 // in 'n'. When this number is zero, the number is zero. The word at used-1 should
54 // never be zero. All words between d[size] and d[allocated-1] should be zero.
55
56 //** Defines
57
58 #ifndef _BN_NUMBERS_H
59 #define _BN_NUMBERS_H
60
61 #if RADIX_BITS == 64
62 # define RADIX_LOG2 6
63 #elif RADIX_BITS == 32
64 #define RADIX_LOG2 5
65 #else
66 # error "Unsupported radix"
67 #endif
68
69 #define RADIX_MOD(x) ((x) & ((1 << RADIX_LOG2) - 1))
70 #define RADIX_DIV(x) ((x) >> RADIX_LOG2)
71 #define RADIX_MASK ((((crypt_uword_t)1) << RADIX_LOG2) - 1)
72
73 #define BITS_TO_CRYPT_WORDS(bits) RADIX_DIV((bits) + (RADIX_BITS - 1))
74 #define BYTES_TO_CRYPT_WORDS(bytes) BITS_TO_CRYPT_WORDS(bytes * 8)
75 #define SIZE_IN_CRYPT_WORDS(thing) BYTES_TO_CRYPT_WORDS(sizeof(thing))
76
77 #if RADIX_BITS == 64
78 #define SWAP_CRYPT_WORD(x) REVERSE_ENDIAN_64(x)
79 typedef uint64_t crypt_uword_t;
80 typedef int64_t crypt_word_t;
81 # define TO_CRYPT_WORD_64 BIG_ENDIAN_BYTES_TO_UINT64
82 # define TO_CRYPT_WORD_32(a, b, c, d) TO_CRYPT_WORD_64(0, 0, 0, 0, a, b, c, d)
83 #elif RADIX_BITS == 32
84 # define SWAP_CRYPT_WORD(x) REVERSE_ENDIAN_32((x))
85 typedef uint32_t crypt_uword_t;
86 typedef int32_t crypt_word_t;
87 # define TO_CRYPT_WORD_64(a, b, c, d, e, f, g, h) \
88 BIG_ENDIAN_BYTES_TO_UINT32(e, f, g, h), \
89 BIG_ENDIAN_BYTES_TO_UINT32(a, b, c, d)
90 #endif
91
92 #define MAX_CRYPT_UWORD (~((crypt_uword_t)0))
93 #define MAX_CRYPT_WORD ((crypt_word_t)(MAX_CRYPT_UWORD >> 1))
94 #define MIN_CRYPT_WORD (~MAX_CRYPT_WORD)
95
96 #define LARGEST_NUMBER (MAX((ALG_RSA * MAX_RSA_KEY_BYTES), \
97 MAX((ALG_ECC * MAX_ECC_KEY_BYTES), MAX_DIGEST_SIZE)))
98 #define LARGEST_NUMBER_BITS (LARGEST_NUMBER * 8)
99
100 #define MAX_ECC_PARAMETER_BYTES (MAX_ECC_KEY_BYTES * ALG_ECC)
101
102 // These are the basic big number formats. This is convertible to the library-
103 // specific format without to much difficulty. For the math performed using
104 // these numbers, the value is always positive.
105 #define BN_STRUCT_DEF(count) struct { \
106 crypt_uword_t allocated; \
107 crypt_uword_t size; \
108 crypt_uword_t d[count]; \
109 }
110
111 typedef BN_STRUCT_DEF(1) bignum_t;
112 #ifndef bigNum
113 typedef bignum_t *bigNum;
114 typedef const bignum_t *bigConst;
115 #endif
116
117 extern const bignum_t BnConstZero;
118
119 // The Functions to access the properties of a big number.
120 // Get number of allocated words
121 #define BnGetAllocated(x) (unsigned)((x)->allocated)
122
123 // Get number of words used
124 #define BnGetSize(x) ((x)->size)
125
126 // Get a pointer to the data array
127 #define BnGetArray(x) ((crypt_uword_t *)&((x)->d[0]))
128
129 // Get the nth word of a BIGNUM (zero-based)
130 #define BnGetWord(x, i) (crypt_uword_t)((x)->d[i])
131
132 // Some things that are done often.
133
134 // Test to see if a bignum_t is equal to zero
135 #define BnEqualZero(bn) (BnGetSize(bn) == 0)
136
137 // Test to see if a bignum_t is equal to a word type
138 #define BnEqualWord(bn, word) \
139 ((BnGetSize(bn) == 1) && (BnGetWord(bn, 0) == (crypt_uword_t)word))
140
141 // Determine if a BIGNUM is even. A zero is even. Although the
142 // indication that a number is zero is that it's size is zero,
143 // all words of the number are 0 so this test works on zero.
144 #define BnIsEven(n) ((BnGetWord(n, 0) & 1) == 0)
145
146 // The macros below are used to define BIGNUM values of the required
147 // size. The values are allocated on the stack so they can be
148 // treated like simple local values.
149
150 // This will call the initialization function for a defined bignum_t.
151 // This sets the allocated and used fields and clears the words of 'n'.
152 #define BN_INIT(name) \
153 (bigNum)BnInit((bigNum)&(name), \
154 BYTES_TO_CRYPT_WORDS(sizeof(name.d)))
155
156 // In some cases, a function will need the address of the structure
157 // associated with a variable. The structure for a BIGNUM variable
158 // of 'name' is 'name_'. Generally, when the structure is created, it
159 // is initialized and a parameter is created with a pointer to the
160 // structure. The pointer has the 'name' and the structure it points
161 // to is 'name_'
162 #define BN_ADDRESS(name) (bigNum)&name##_
163
164 #define BN_STRUCT_ALLOCATION(bits) (BITS_TO_CRYPT_WORDS(bits) + 1)
165
166 // Create a structure of the correct size.
167 #define BN_STRUCT(bits) \
168 BN_STRUCT_DEF(BN_STRUCT_ALLOCATION(bits))
169
170 // Define a BIGNUM type with a specific allocation
171 #define BN_TYPE(name, bits) \
172 typedef BN_STRUCT(bits) bn_##name##_t
173
174 // This creates a local BIGNUM variable of a specific size and
175 // initializes it from a TPM2B input parameter.
176 #define BN_INITIALIZED(name, bits, initializer) \
177 BN_STRUCT(bits) name##_; \
178 bigNum name = BnFrom2B(BN_INIT(name##_), \
179 (const TPM2B *)initializer)
180
181 // Create a local variable that can hold a number with 'bits'
182 #define BN_VAR(name, bits) \
183 BN_STRUCT(bits) _##name; \
184 bigNum name = BN_INIT(_##name)
185
186 // Create a type that can hold the largest number defined by the
187 // implementation.
188 #define BN_MAX(name) BN_VAR(name, LARGEST_NUMBER_BITS)
189 #define BN_MAX_INITIALIZED(name, initializer) \
190 BN_INITIALIZED(name, LARGEST_NUMBER_BITS, initializer)
191
192 // A word size value is useful
193 #define BN_WORD(name) BN_VAR(name, RADIX_BITS)
194
195 // This is used to created a word-size BIGNUM and initialize it with
196 // an input parameter to a function.
197 #define BN_WORD_INITIALIZED(name, initial) \
198 BN_STRUCT(RADIX_BITS) name##_; \
199 bigNum name = BnInitializeWord((bigNum)&name##_, \
200 BN_STRUCT_ALLOCATION(RADIX_BITS), initial)
201
202 // ECC-Specific Values
203
204 // This is the format for a point. It is always in affine format. The Z value is
205 // carried as part of the point, primarily to simplify the interface to the support
206 // library. Rather than have the interface layer have to create space for the
207 // point each time it is used...
208 // The x, y, and z values are pointers to bigNum values and not in-line versions of
209 // the numbers. This is a relic of the days when there was no standard TPM format
210 // for the numbers
211 typedef struct _bn_point_t
212 {
213 bigNum x;
214 bigNum y;
215 bigNum z;
216 } bn_point_t;
217
218 typedef bn_point_t *bigPoint;
219 typedef const bn_point_t *pointConst;
220
221 typedef struct constant_point_t
222 {
223 bigConst x;
224 bigConst y;
225 bigConst z;
226 } constant_point_t;
227
228 #define ECC_BITS (MAX_ECC_KEY_BYTES * 8)
229 BN_TYPE(ecc, ECC_BITS);
230 #define ECC_NUM(name) BN_VAR(name, ECC_BITS)
231 #define ECC_INITIALIZED(name, initializer) \
232 BN_INITIALIZED(name, ECC_BITS, initializer)
233
234 #define POINT_INSTANCE(name, bits) \
235 BN_STRUCT (bits) name##_x = \
236 {BITS_TO_CRYPT_WORDS ( bits ), 0,{0}}; \
237 BN_STRUCT ( bits ) name##_y = \
238 {BITS_TO_CRYPT_WORDS ( bits ), 0,{0}}; \
239 BN_STRUCT ( bits ) name##_z = \
240 {BITS_TO_CRYPT_WORDS ( bits ), 0,{0}}; \
241 bn_point_t name##_
242
243 #define POINT_INITIALIZER(name) \
244 BnInitializePoint(&name##_, (bigNum)&name##_x, \
245 (bigNum)&name##_y, (bigNum)&name##_z)
246
247 #define POINT_INITIALIZED(name, initValue) \
248 POINT_INSTANCE(name, MAX_ECC_KEY_BITS); \
249 bigPoint name = BnPointFrom2B( \
250 POINT_INITIALIZER(name), \
251 initValue)
252
253 #define POINT_VAR(name, bits) \
254 POINT_INSTANCE (name, bits); \
255 bigPoint name = POINT_INITIALIZER(name)
256
257 #define POINT(name) POINT_VAR(name, MAX_ECC_KEY_BITS)
258
259 // Structure for the curve parameters. This is an analog to the
260 // TPMS_ALGORITHM_DETAIL_ECC
261 typedef struct
262 {
263 bigConst prime; // a prime number
264 bigConst order; // the order of the curve
265 bigConst h; // cofactor
266 bigConst a; // linear coefficient
267 bigConst b; // constant term
268 constant_point_t base; // base point
269 } ECC_CURVE_DATA;
270
271 // Access macros for the ECC_CURVE structure. The parameter 'C' is a pointer
272 // to an ECC_CURVE_DATA structure. In some libraries, the curve structure contains
273 // a pointer to an ECC_CURVE_DATA structure as well as some other bits. For those
274 // cases, the AccessCurveData macro is used in the code to first get the pointer
275 // to the ECC_CURVE_DATA for access. In some cases, the macro does noting.
276 #define CurveGetPrime(C) ((C)->prime)
277 #define CurveGetOrder(C) ((C)->order)
278 #define CurveGetCofactor(C) ((C)->h)
279 #define CurveGet_a(C) ((C)->a)
280 #define CurveGet_b(C) ((C)->b)
281 #define CurveGetG(C) ((pointConst)&((C)->base))
282 #define CurveGetGx(C) ((C)->base.x)
283 #define CurveGetGy(C) ((C)->base.y)
284
285
286 // Convert bytes in initializers according to the endianess of the system.
287 // This is used for CryptEccData.c.
288 #define BIG_ENDIAN_BYTES_TO_UINT32(a, b, c, d) \
289 ( ((UINT32)(a) << 24) \
290 + ((UINT32)(b) << 16) \
291 + ((UINT32)(c) << 8) \
292 + ((UINT32)(d)) \
293 )
294
295 #define BIG_ENDIAN_BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
296 ( ((UINT64)(a) << 56) \
297 + ((UINT64)(b) << 48) \
298 + ((UINT64)(c) << 40) \
299 + ((UINT64)(d) << 32) \
300 + ((UINT64)(e) << 24) \
301 + ((UINT64)(f) << 16) \
302 + ((UINT64)(g) << 8) \
303 + ((UINT64)(h)) \
304 )
305
306 #ifndef RADIX_BYTES
307 # if RADIX_BITS == 32
308 # define RADIX_BYTES 4
309 # elif RADIX_BITS == 64
310 # define RADIX_BYTES 8
311 # else
312 # error "RADIX_BITS must either be 32 or 64"
313 # endif
314 #endif
315
316 // Add implementation dependent definitions for other ECC Values and for linkages.
317 #include LIB_INCLUDE(MATH_LIB, Math)
318
319
320 #endif // _BN_NUMBERS_H