Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/src/crypt/wolf/TpmToWolfMath.c

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 other
22 * 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
36 //** Introduction
37 //
38 // This file contains the math functions that are not implemented in the BnMath
39 // library (yet). These math functions will call the wolfcrypt library to execute
40 // the operations. There is a difference between the internal format and the
41 // wolfcrypt format. To call the wolfcrypt function, a mp_int structure is created
42 // for each passed variable. We define USE_FAST_MATH wolfcrypt option, which allocates
43 // mp_int on the stack. We must copy each word to the new structure, and set the used
44 // size.
45 //
46 // Not using USE_FAST_MATH would allow for a simple pointer swap for the big integer
47 // buffer 'd', however wolfcrypt expects to manage this memory, and will swap out
48 // the pointer to and from temporary variables and free the reference underneath us.
49 // Using USE_FAST_MATH also instructs wolfcrypt to use the stack for all these
50 // intermediate variables
51
52
53 //** Includes and Defines
54 #include "Tpm.h"
55
56 #ifdef MATH_LIB_WOLF
57 #include "BnConvert_fp.h"
58 #include "TpmToWolfMath_fp.h"
59
60 #define WOLF_HALF_RADIX (RADIX_BITS == 64 && !defined(FP_64BIT))
61
62 //** Functions
63
64 //*** BnFromWolf()
65 // This function converts a wolfcrypt mp_int to a TPM bignum. In this implementation
66 // it is assumed that wolfcrypt used the same format for a big number as does the
67 // TPM -- an array of native-endian words in little-endian order.
68 void
69 BnFromWolf(
70 bigNum bn,
71 mp_int *wolfBn
72 )
73 {
74 if(bn != NULL)
75 {
76 int i;
77 #if WOLF_HALF_RADIX
78 pAssert((unsigned)wolfBn->used <= 2 * BnGetAllocated(bn));
79 #else
80 pAssert((unsigned)wolfBn->used <= BnGetAllocated(bn));
81 #endif
82 for (i = 0; i < wolfBn->used; i++)
83 {
84 #if WOLF_HALF_RADIX
85 if (i & 1)
86 bn->d[i/2] |= (crypt_uword_t)wolfBn->dp[i] << 32;
87 else
88 bn->d[i/2] = wolfBn->dp[i];
89 #else
90 bn->d[i] = wolfBn->dp[i];
91 #endif
92 }
93
94 #if WOLF_HALF_RADIX
95 BnSetTop(bn, (wolfBn->used + 1)/2);
96 #else
97 BnSetTop(bn, wolfBn->used);
98 #endif
99 }
100 }
101
102 //*** BnToWolf()
103 // This function converts a TPM bignum to a wolfcrypt mp_init, and has the same
104 // assumptions as made by BnFromWolf()
105 void
106 BnToWolf(
107 mp_int *toInit,
108 bigConst initializer
109 )
110 {
111 uint32_t i;
112 if (toInit != NULL && initializer != NULL)
113 {
114 for (i = 0; i < initializer->size; i++)
115 {
116 #if WOLF_HALF_RADIX
117 toInit->dp[2 * i] = (fp_digit)initializer->d[i];
118 toInit->dp[2 * i + 1] = (fp_digit)(initializer->d[i] >> 32);
119 #else
120 toInit->dp[i] = initializer->d[i];
121 #endif
122 }
123
124 #if WOLF_HALF_RADIX
125 toInit->used = (int)initializer->size * 2;
126 if (toInit->dp[toInit->used - 1] == 0 && toInit->dp[toInit->used - 2] != 0)
127 --toInit->used;
128 #else
129 toInit->used = (int)initializer->size;
130 #endif
131 toInit->sign = 0;
132 }
133 }
134
135 //*** MpInitialize()
136 // This function initializes an wolfcrypt mp_int.
137 mp_int *
138 MpInitialize(
139 mp_int *toInit
140 )
141 {
142 mp_init( toInit );
143 return toInit;
144 }
145
146 #if LIBRARY_COMPATIBILITY_CHECK
147 //** MathLibraryCompatibililtyCheck()
148 // This function is only used during development to make sure that the library
149 // that is being referenced is using the same size of data structures as the TPM.
150 void
151 MathLibraryCompatibilityCheck(
152 void
153 )
154 {
155 BN_VAR(tpmTemp, 64 * 8); // allocate some space for a test value
156 crypt_uword_t i;
157 TPM2B_TYPE(TEST, 16);
158 TPM2B_TEST test = {{16, {0x0F, 0x0E, 0x0D, 0x0C,
159 0x0B, 0x0A, 0x09, 0x08,
160 0x07, 0x06, 0x05, 0x04,
161 0x03, 0x02, 0x01, 0x00}}};
162 // Convert the test TPM2B to a bigNum
163 BnFrom2B(tpmTemp, &test.b);
164 MP_INITIALIZED(wolfTemp, tpmTemp);
165 (wolfTemp); // compiler warning
166 // Make sure the values are consistent
167 cAssert(wolfTemp->used == (int)tpmTemp->size);
168 for(i = 0; i < tpmTemp->size; i++)
169 cAssert(wolfTemp->dp[i] == tpmTemp->d[i]);
170 }
171 #endif
172
173 //*** BnModMult()
174 // Does multiply and divide returning the remainder of the divide.
175 LIB_EXPORT BOOL
176 BnModMult(
177 bigNum result,
178 bigConst op1,
179 bigConst op2,
180 bigConst modulus
181 )
182 {
183 WOLF_ENTER();
184 BOOL OK;
185 MP_INITIALIZED(bnOp1, op1);
186 MP_INITIALIZED(bnOp2, op2);
187 MP_INITIALIZED(bnTemp, NULL);
188 BN_VAR(temp, LARGEST_NUMBER_BITS * 2);
189
190 pAssert(BnGetAllocated(result) >= BnGetSize(modulus));
191
192 OK = (mp_mul( bnOp1, bnOp2, bnTemp ) == MP_OKAY);
193 if(OK)
194 {
195 BnFromWolf(temp, bnTemp);
196 OK = BnDiv(NULL, result, temp, modulus);
197 }
198
199 WOLF_LEAVE();
200 return OK;
201 }
202
203 //*** BnMult()
204 // Multiplies two numbers
205 LIB_EXPORT BOOL
206 BnMult(
207 bigNum result,
208 bigConst multiplicand,
209 bigConst multiplier
210 )
211 {
212 WOLF_ENTER();
213 BOOL OK;
214 MP_INITIALIZED(bnTemp, NULL);
215 MP_INITIALIZED(bnA, multiplicand);
216 MP_INITIALIZED(bnB, multiplier);
217
218 pAssert(result->allocated >=
219 (BITS_TO_CRYPT_WORDS(BnSizeInBits(multiplicand)
220 + BnSizeInBits(multiplier))));
221
222 OK = (mp_mul( bnA, bnB, bnTemp ) == MP_OKAY);
223 if(OK)
224 {
225 BnFromWolf(result, bnTemp);
226 }
227
228 WOLF_LEAVE();
229 return OK;
230 }
231
232 //*** BnDiv()
233 // This function divides two bigNum values. The function returns FALSE if
234 // there is an error in the operation.
235 LIB_EXPORT BOOL
236 BnDiv(
237 bigNum quotient,
238 bigNum remainder,
239 bigConst dividend,
240 bigConst divisor
241 )
242 {
243 WOLF_ENTER();
244 BOOL OK;
245 MP_INITIALIZED(bnQ, quotient);
246 MP_INITIALIZED(bnR, remainder);
247 MP_INITIALIZED(bnDend, dividend);
248 MP_INITIALIZED(bnSor, divisor);
249 pAssert(!BnEqualZero(divisor));
250 if(BnGetSize(dividend) < BnGetSize(divisor))
251 {
252 if(quotient)
253 BnSetWord(quotient, 0);
254 if(remainder)
255 BnCopy(remainder, dividend);
256 OK = TRUE;
257 }
258 else
259 {
260 pAssert((quotient == NULL)
261 || (quotient->allocated >= (unsigned)(dividend->size
262 - divisor->size)));
263 pAssert((remainder == NULL)
264 || (remainder->allocated >= divisor->size));
265 OK = (mp_div(bnDend , bnSor, bnQ, bnR) == MP_OKAY);
266 if(OK)
267 {
268 BnFromWolf(quotient, bnQ);
269 BnFromWolf(remainder, bnR);
270 }
271 }
272
273 WOLF_LEAVE();
274 return OK;
275 }
276
277 #if ALG_RSA
278 //*** BnGcd()
279 // Get the greatest common divisor of two numbers
280 LIB_EXPORT BOOL
281 BnGcd(
282 bigNum gcd, // OUT: the common divisor
283 bigConst number1, // IN:
284 bigConst number2 // IN:
285 )
286 {
287 WOLF_ENTER();
288 BOOL OK;
289 MP_INITIALIZED(bnGcd, gcd);
290 MP_INITIALIZED(bn1, number1);
291 MP_INITIALIZED(bn2, number2);
292 pAssert(gcd != NULL);
293 OK = (mp_gcd( bn1, bn2, bnGcd ) == MP_OKAY);
294 if(OK)
295 {
296 BnFromWolf(gcd, bnGcd);
297 }
298 WOLF_LEAVE();
299 return OK;
300 }
301
302 //***BnModExp()
303 // Do modular exponentiation using bigNum values. The conversion from a mp_int to
304 // a bigNum is trivial as they are based on the same structure
305 LIB_EXPORT BOOL
306 BnModExp(
307 bigNum result, // OUT: the result
308 bigConst number, // IN: number to exponentiate
309 bigConst exponent, // IN:
310 bigConst modulus // IN:
311 )
312 {
313 WOLF_ENTER();
314 BOOL OK;
315 MP_INITIALIZED(bnResult, result);
316 MP_INITIALIZED(bnN, number);
317 MP_INITIALIZED(bnE, exponent);
318 MP_INITIALIZED(bnM, modulus);
319 OK = (mp_exptmod( bnN, bnE, bnM, bnResult ) == MP_OKAY);
320 if(OK)
321 {
322 BnFromWolf(result, bnResult);
323 }
324
325 WOLF_LEAVE();
326 return OK;
327 }
328
329 //*** BnModInverse()
330 // Modular multiplicative inverse
331 LIB_EXPORT BOOL
332 BnModInverse(
333 bigNum result,
334 bigConst number,
335 bigConst modulus
336 )
337 {
338 WOLF_ENTER();
339 BOOL OK;
340 MP_INITIALIZED(bnResult, result);
341 MP_INITIALIZED(bnN, number);
342 MP_INITIALIZED(bnM, modulus);
343
344 OK = (mp_invmod(bnN, bnM, bnResult) == MP_OKAY);
345 if(OK)
346 {
347 BnFromWolf(result, bnResult);
348 }
349
350 WOLF_LEAVE();
351 return OK;
352 }
353 #endif // TPM_ALG_RSA
354
355 #if ALG_ECC
356
357 //*** PointFromWolf()
358 // Function to copy the point result from a wolf ecc_point to a bigNum
359 void
360 PointFromWolf(
361 bigPoint pOut, // OUT: resulting point
362 ecc_point *pIn // IN: the point to return
363 )
364 {
365 BnFromWolf(pOut->x, pIn->x);
366 BnFromWolf(pOut->y, pIn->y);
367 BnFromWolf(pOut->z, pIn->z);
368 }
369
370 //*** PointToWolf()
371 // Function to copy the point result from a bigNum to a wolf ecc_point
372 void
373 PointToWolf(
374 ecc_point *pOut, // OUT: resulting point
375 pointConst pIn // IN: the point to return
376 )
377 {
378 BnToWolf(pOut->x, pIn->x);
379 BnToWolf(pOut->y, pIn->y);
380 BnToWolf(pOut->z, pIn->z);
381 }
382
383 //*** EcPointInitialized()
384 // Allocate and initialize a point.
385 static ecc_point *
386 EcPointInitialized(
387 pointConst initializer
388 )
389 {
390 ecc_point *P;
391
392 P = wc_ecc_new_point();
393 pAssert(P != NULL);
394 // mp_int x,y,z are stack allocated.
395 // initializer is not required
396 if (P != NULL && initializer != NULL)
397 {
398 PointToWolf( P, initializer );
399 }
400
401 return P;
402 }
403
404 //*** BnEccModMult()
405 // This function does a point multiply of the form R = [d]S
406 // return type: BOOL
407 // FALSE failure in operation; treat as result being point at infinity
408 LIB_EXPORT BOOL
409 BnEccModMult(
410 bigPoint R, // OUT: computed point
411 pointConst S, // IN: point to multiply by 'd' (optional)
412 bigConst d, // IN: scalar for [d]S
413 bigCurve E
414 )
415 {
416 WOLF_ENTER();
417 BOOL OK;
418 MP_INITIALIZED(bnD, d);
419 MP_INITIALIZED(bnPrime, CurveGetPrime(E));
420 POINT_CREATE(pS, NULL);
421 POINT_CREATE(pR, NULL);
422
423 if(S == NULL)
424 S = CurveGetG(AccessCurveData(E));
425
426 PointToWolf(pS, S);
427
428 OK = (wc_ecc_mulmod(bnD, pS, pR, NULL, bnPrime, 1 ) == MP_OKAY);
429 if(OK)
430 {
431 PointFromWolf(R, pR);
432 }
433
434 POINT_DELETE(pR);
435 POINT_DELETE(pS);
436
437 WOLF_LEAVE();
438 return !BnEqualZero(R->z);
439 }
440
441 //*** BnEccModMult2()
442 // This function does a point multiply of the form R = [d]G + [u]Q
443 // return type: BOOL
444 // FALSE failure in operation; treat as result being point at infinity
445 LIB_EXPORT BOOL
446 BnEccModMult2(
447 bigPoint R, // OUT: computed point
448 pointConst S, // IN: optional point
449 bigConst d, // IN: scalar for [d]S or [d]G
450 pointConst Q, // IN: second point
451 bigConst u, // IN: second scalar
452 bigCurve E // IN: curve
453 )
454 {
455 WOLF_ENTER();
456 BOOL OK;
457 POINT_CREATE(pR, NULL);
458 POINT_CREATE(pS, NULL);
459 POINT_CREATE(pQ, Q);
460 MP_INITIALIZED(bnD, d);
461 MP_INITIALIZED(bnU, u);
462 MP_INITIALIZED(bnPrime, CurveGetPrime(E));
463 MP_INITIALIZED(bnA, CurveGet_a(E));
464
465 if(S == NULL)
466 S = CurveGetG(AccessCurveData(E));
467 PointToWolf( pS, S );
468
469 OK = (ecc_mul2add(pS, bnD, pQ, bnU, pR, bnA, bnPrime, NULL) == MP_OKAY);
470 if(OK)
471 {
472 PointFromWolf(R, pR);
473 }
474
475 POINT_DELETE(pS);
476 POINT_DELETE(pQ);
477 POINT_DELETE(pR);
478
479 WOLF_LEAVE();
480 return !BnEqualZero(R->z);
481 }
482
483 //** BnEccAdd()
484 // This function does addition of two points.
485 // return type: BOOL
486 // FALSE failure in operation; treat as result being point at infinity
487 LIB_EXPORT BOOL
488 BnEccAdd(
489 bigPoint R, // OUT: computed point
490 pointConst S, // IN: point to multiply by 'd'
491 pointConst Q, // IN: second point
492 bigCurve E // IN: curve
493 )
494 {
495 WOLF_ENTER();
496 BOOL OK;
497 mp_digit mp;
498 POINT_CREATE(pR, NULL);
499 POINT_CREATE(pS, S);
500 POINT_CREATE(pQ, Q);
501 MP_INITIALIZED(bnA, CurveGet_a(E));
502 MP_INITIALIZED(bnMod, CurveGetPrime(E));
503 //
504 OK = (mp_montgomery_setup(bnMod, &mp) == MP_OKAY);
505 OK = OK && (ecc_projective_add_point(pS, pQ, pR, bnA, bnMod, mp ) == MP_OKAY);
506 if(OK)
507 {
508 PointFromWolf(R, pR);
509 }
510
511 POINT_DELETE(pS);
512 POINT_DELETE(pQ);
513 POINT_DELETE(pR);
514
515 WOLF_LEAVE();
516 return !BnEqualZero(R->z);
517 }
518
519 #endif // TPM_ALG_ECC
520
521 #endif // MATH_LIB_WOLF