| 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 |
// This file contains the structure definitions used for ECC in the LibTomCrypt |
| 37 |
// version of the code. These definitions would change, based on the library. |
| 38 |
// The ECC-related structures that cross the TPM interface are defined |
| 39 |
// in TpmTypes.h |
| 40 |
// |
| 41 |
|
| 42 |
#ifndef MATH_LIB_DEFINED |
| 43 |
#define MATH_LIB_DEFINED |
| 44 |
|
| 45 |
#define MATH_LIB_OSSL |
| 46 |
|
| 47 |
#include <openssl/evp.h> |
| 48 |
#include <openssl/ec.h> |
| 49 |
#if 0 // OPENSSL_VERSION_NUMBER >= 0x10200000L |
| 50 |
// Check the bignum_st definition in crypto/bn/bn_lcl.h and either update the |
| 51 |
// version check or provide the new definition for this version. |
| 52 |
# error Untested OpenSSL version |
| 53 |
#elif OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 54 |
// from crypto/bn/bn_lcl.h |
| 55 |
struct bignum_st { |
| 56 |
BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit |
| 57 |
* chunks. */ |
| 58 |
int top; /* Index of last used d +1. */ |
| 59 |
/* The next are internal book keeping for bn_expand. */ |
| 60 |
int dmax; /* Size of the d array. */ |
| 61 |
int neg; /* one if the number is negative */ |
| 62 |
int flags; |
| 63 |
}; |
| 64 |
#endif // OPENSSL_VERSION_NUMBER |
| 65 |
#include <openssl/bn.h> |
| 66 |
|
| 67 |
//** Macros and Defines |
| 68 |
|
| 69 |
// Make sure that the library is using the correct size for a crypt word |
| 70 |
#if defined THIRTY_TWO_BIT && (RADIX_BITS != 32) \ |
| 71 |
|| ((defined SIXTY_FOUR_BIT_LONG || defined SIXTY_FOUR_BIT) \ |
| 72 |
&& (RADIX_BITS != 64)) |
| 73 |
# error Ossl library is using different radix |
| 74 |
#endif |
| 75 |
|
| 76 |
// Allocate a local BIGNUM value. For the allocation, a bigNum structure is created |
| 77 |
// as is a local BIGNUM. The bigNum is initialized and then the BIGNUM is |
| 78 |
// set to reference the local value. |
| 79 |
#define BIG_VAR(name, bits) \ |
| 80 |
BN_VAR(name##Bn, (bits)); \ |
| 81 |
BIGNUM _##name; \ |
| 82 |
BIGNUM *name = BigInitialized(&_##name, \ |
| 83 |
BnInit(name##Bn, \ |
| 84 |
BYTES_TO_CRYPT_WORDS(sizeof(_##name##Bn.d)))) |
| 85 |
|
| 86 |
// Allocate a BIGNUM and initialize with the values in a bigNum initializer |
| 87 |
#define BIG_INITIALIZED(name, initializer) \ |
| 88 |
BIGNUM _##name; \ |
| 89 |
BIGNUM *name = BigInitialized(&_##name, initializer) |
| 90 |
|
| 91 |
|
| 92 |
typedef struct |
| 93 |
{ |
| 94 |
const ECC_CURVE_DATA *C; // the TPM curve values |
| 95 |
EC_GROUP *G; // group parameters |
| 96 |
BN_CTX *CTX; // the context for the math (this might not be |
| 97 |
// the context in which the curve was created>; |
| 98 |
} OSSL_CURVE_DATA; |
| 99 |
|
| 100 |
typedef OSSL_CURVE_DATA *bigCurve; |
| 101 |
|
| 102 |
#define AccessCurveData(E) ((E)->C) |
| 103 |
|
| 104 |
|
| 105 |
#include "TpmToOsslSupport_fp.h" |
| 106 |
|
| 107 |
// Start and end a context within which the OpenSSL memory management works |
| 108 |
#define OSSL_ENTER() BN_CTX *CTX = OsslContextEnter() |
| 109 |
#define OSSL_LEAVE() OsslContextLeave(CTX) |
| 110 |
|
| 111 |
// Start and end a context that spans multiple ECC functions. This is used so that |
| 112 |
// the group for the curve can persist across multiple frames. |
| 113 |
#define CURVE_INITIALIZED(name, initializer) \ |
| 114 |
OSSL_CURVE_DATA _##name; \ |
| 115 |
bigCurve name = BnCurveInitialize(&_##name, initializer) |
| 116 |
#define CURVE_FREE(name) BnCurveFree(name) |
| 117 |
|
| 118 |
// Start and end a local stack frame within the context of the curve frame |
| 119 |
#define ECC_ENTER() BN_CTX *CTX = OsslPushContext(E->CTX) |
| 120 |
#define ECC_LEAVE() OsslPopContext(CTX) |
| 121 |
|
| 122 |
#define BN_NEW() BnNewVariable(CTX) |
| 123 |
|
| 124 |
// This definition would change if there were something to report |
| 125 |
#define MathLibSimulationEnd() |
| 126 |
|
| 127 |
#endif // MATH_LIB_DEFINED |