mirror of
https://github.com/stefanberger/libtpms
synced 2026-01-11 08:25:47 +00:00
rev180: Sync reformatting of BnToOsslMath.c with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
This commit is contained in:
parent
6bfa750639
commit
dc15065e35
@ -1,9 +1,8 @@
|
||||
/********************************************************************************/
|
||||
/* */
|
||||
/* TPM to OpenSSL BigNum Shim Layer */
|
||||
/* */
|
||||
/* Written by Ken Goldman */
|
||||
/* IBM Thomas J. Watson Research Center */
|
||||
/* $Id: TpmToOsslMath.c 1598 2020-03-27 21:59:49Z kgoldman $ */
|
||||
/* */
|
||||
/* Licenses and Notices */
|
||||
/* */
|
||||
@ -55,45 +54,49 @@
|
||||
/* arising in any way out of use or reliance upon this specification or any */
|
||||
/* information herein. */
|
||||
/* */
|
||||
/* (c) Copyright IBM Corp. and others, 2016 - 2020 */
|
||||
/* (c) Copyright IBM Corp. and others, 2023 */
|
||||
/* */
|
||||
/********************************************************************************/
|
||||
|
||||
/* B.2.3.2. TpmToOsslMath.c */
|
||||
/* B.2.3.2.1. Introduction */
|
||||
|
||||
/* The functions in this file provide the low-level interface between the TPM code and the big
|
||||
number and elliptic curve math routines in OpenSSL. */
|
||||
/* Most math on big numbers require a context. The context contains the memory in which OpenSSL
|
||||
creates and manages the big number values. When a OpenSSL math function will be called that
|
||||
modifies a BIGNUM value, that value must be created in an OpenSSL context. The first line of code
|
||||
in such a function must be: OSSL_ENTER(); and the last operation before returning must be
|
||||
OSSL_LEAVE(). OpenSSL variables can then be created with BnNewVariable(). Constant values to be
|
||||
used by OpenSSL are created from the bigNum values passed to the functions in this file. Space
|
||||
for the BIGNUM control block is allocated in the stack of the function and then it is initialized
|
||||
by calling BigInitialized(). That function sets up the values in the BIGNUM structure and sets
|
||||
the data pointer to point to the data in the bignum_t. This is only used when the value is known
|
||||
to be a constant in the called function. */
|
||||
/* Because the allocations of constants is on the local stack and the OSSL_ENTER()/OSSL_LEAVE() pair
|
||||
flushes everything created in OpenSSL memory, there should be no chance of a memory leak. */
|
||||
|
||||
//** Introduction
|
||||
// The functions in this file provide the low-level interface between the TPM code
|
||||
// and the big number and elliptic curve math routines in OpenSSL.
|
||||
//
|
||||
// Most math on big numbers require a context. The context contains the memory in
|
||||
// which OpenSSL creates and manages the big number values. When a OpenSSL math
|
||||
// function will be called that modifies a BIGNUM value, that value must be created in
|
||||
// an OpenSSL context. The first line of code in such a function must be:
|
||||
// OSSL_ENTER(); and the last operation before returning must be OSSL_LEAVE().
|
||||
// OpenSSL variables can then be created with BnNewVariable(). Constant values to be
|
||||
// used by OpenSSL are created from the bigNum values passed to the functions in this
|
||||
// file. Space for the BIGNUM control block is allocated in the stack of the
|
||||
// function and then it is initialized by calling BigInitialized(). That function
|
||||
// sets up the values in the BIGNUM structure and sets the data pointer to point to
|
||||
// the data in the bignum_t. This is only used when the value is known to be a
|
||||
// constant in the called function.
|
||||
//
|
||||
// Because the allocations of constants is on the local stack and the
|
||||
// OSSL_ENTER()/OSSL_LEAVE() pair flushes everything created in OpenSSL memory, there
|
||||
// should be no chance of a memory leak.
|
||||
|
||||
//** Includes and Defines
|
||||
#include "Tpm.h"
|
||||
#ifdef MATH_LIB_OSSL
|
||||
#include "TpmToOsslMath_fp.h"
|
||||
//#include "BnOssl.h"
|
||||
|
||||
/* B.2.3.2.3.1. OsslToTpmBn() */
|
||||
/* This function converts an OpenSSL BIGNUM to a TPM bignum. In this implementation it is assumed
|
||||
that OpenSSL uses a different control structure but the same data layout -- an array of
|
||||
native-endian words in little-endian order. */
|
||||
/* Return Value Meaning */
|
||||
/* TRUE(1) success */
|
||||
/* FALSE(0) failure because value will not fit or OpenSSL variable doesn't exist */
|
||||
BOOL
|
||||
OsslToTpmBn(
|
||||
bigNum bn,
|
||||
const BIGNUM *osslBn // libtpms: added 'const'
|
||||
)
|
||||
#ifdef MATH_LIB_OSSL
|
||||
# include "TpmToOsslMath_fp.h"
|
||||
|
||||
//** Functions
|
||||
|
||||
//*** OsslToTpmBn()
|
||||
// This function converts an OpenSSL BIGNUM to a TPM bigNum. In this implementation
|
||||
// it is assumed that OpenSSL uses a different control structure but the same data
|
||||
// layout -- an array of native-endian words in little-endian order.
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure because value will not fit or OpenSSL variable doesn't
|
||||
// exist
|
||||
BOOL OsslToTpmBn(bigNum bn, const BIGNUM* osslBn) // libtpms: added 'const'
|
||||
{
|
||||
GOTO_ERROR_UNLESS(osslBn != NULL);
|
||||
// If the bn is NULL, it means that an output value pointer was NULL meaning that
|
||||
@ -103,81 +106,74 @@ OsslToTpmBn(
|
||||
|
||||
if(bn != NULL)
|
||||
{
|
||||
#if 1 //libtpms added begin
|
||||
#if 1 // libtpms: added begin
|
||||
int num_bytes;
|
||||
|
||||
num_bytes = BN_num_bytes(osslBn);
|
||||
GOTO_ERROR_UNLESS(num_bytes >= 0 && sizeof(buffer) >= (size_t)num_bytes);
|
||||
buffer_len = BN_bn2bin(osslBn, buffer); /* ossl to bin */
|
||||
BnFromBytes(bn, buffer, buffer_len); /* bin to TPM */
|
||||
#else // libtpms added end
|
||||
int i;
|
||||
#else // libtpms: added end
|
||||
int i;
|
||||
//
|
||||
GOTO_ERROR_UNLESS((unsigned)osslBn->top <= BnGetAllocated(bn));
|
||||
for(i = 0; i < osslBn->top; i++)
|
||||
bn->d[i] = osslBn->d[i];
|
||||
BnSetTop(bn, osslBn->top);
|
||||
#endif // libtpms added
|
||||
#endif // libtpms: added
|
||||
}
|
||||
return TRUE;
|
||||
Error:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* B.2.3.2.3.2. BigInitialized() */
|
||||
/* This function initializes an OSSL BIGNUM from a TPM bigConst. Do not use this for values that are
|
||||
passed to OpenSLL when they are not declared as const in the function prototype. Instead, use
|
||||
BnNewVariable(). */
|
||||
BIGNUM *
|
||||
BigInitialized(
|
||||
BIGNUM *toInit,
|
||||
bigConst initializer
|
||||
)
|
||||
//*** BigInitialized()
|
||||
// This function initializes an OSSL BIGNUM from a TPM bigConst. Do not use this for
|
||||
// values that are passed to OpenSLL when they are not declared as const in the
|
||||
// function prototype. Instead, use BnNewVariable().
|
||||
BIGNUM* BigInitialized(BIGNUM* toInit, bigConst initializer)
|
||||
{
|
||||
#if 1 // libtpms added begin
|
||||
#if 1 // libtpms: added begin
|
||||
BIGNUM *_toInit;
|
||||
unsigned char buffer[LARGEST_NUMBER + 1];
|
||||
NUMBYTES buffer_len = (NUMBYTES )sizeof(buffer);
|
||||
#endif // libtpms added end
|
||||
#endif // libtpms: added end
|
||||
|
||||
if(initializer == NULL)
|
||||
FAIL(FATAL_ERROR_PARAMETER);
|
||||
if(toInit == NULL || initializer == NULL)
|
||||
return NULL;
|
||||
|
||||
#if 1 // libtpms added begin
|
||||
#if 1 // libtpms: added begin
|
||||
BnToBytes(initializer, buffer, &buffer_len); /* TPM to bin */
|
||||
_toInit = BN_bin2bn(buffer, buffer_len, NULL); /* bin to ossl */
|
||||
BN_set_flags(_toInit, BN_FLG_CONSTTIME);
|
||||
BN_copy(toInit, _toInit);
|
||||
BN_clear_free(_toInit);
|
||||
#else // libtpms added end
|
||||
toInit->d = (BN_ULONG *)&initializer->d[0];
|
||||
toInit->dmax = (int)initializer->allocated;
|
||||
toInit->top = (int)initializer->size;
|
||||
toInit->neg = 0;
|
||||
#else // libtpms: added end
|
||||
toInit->d = (BN_ULONG*)&initializer->d[0];
|
||||
toInit->dmax = (int)initializer->allocated;
|
||||
toInit->top = (int)initializer->size;
|
||||
toInit->neg = 0;
|
||||
toInit->flags = 0;
|
||||
#endif
|
||||
#endif // libtpms: added
|
||||
return toInit;
|
||||
}
|
||||
|
||||
#ifndef OSSL_DEBUG
|
||||
# define BIGNUM_PRINT(label, bn, eol)
|
||||
# define DEBUG_PRINT(x)
|
||||
#else
|
||||
# define DEBUG_PRINT(x) printf("%s", x)
|
||||
# define BIGNUM_PRINT(label, bn, eol) BIGNUM_print((label), (bn), (eol))
|
||||
# ifndef OSSL_DEBUG
|
||||
# define BIGNUM_PRINT(label, bn, eol)
|
||||
# define DEBUG_PRINT(x)
|
||||
# else
|
||||
# define DEBUG_PRINT(x) printf("%s", x)
|
||||
# define BIGNUM_PRINT(label, bn, eol) BIGNUM_print((label), (bn), (eol))
|
||||
|
||||
static
|
||||
void BIGNUM_print(
|
||||
const char *label,
|
||||
const BIGNUM *a,
|
||||
BOOL eol
|
||||
)
|
||||
//*** BIGNUM_print()
|
||||
static void BIGNUM_print(const char* label, const BIGNUM* a, BOOL eol)
|
||||
{
|
||||
BN_ULONG *d;
|
||||
int i;
|
||||
int notZero = FALSE;
|
||||
BN_ULONG* d;
|
||||
int i;
|
||||
int notZero = FALSE;
|
||||
|
||||
if(label != NULL)
|
||||
printf("%s", label);
|
||||
if(a == NULL)
|
||||
@ -185,15 +181,15 @@ void BIGNUM_print(
|
||||
printf("NULL");
|
||||
goto done;
|
||||
}
|
||||
if (a->neg)
|
||||
if(a->neg)
|
||||
printf("-");
|
||||
for(i = a->top, d = &a->d[i - 1]; i > 0; i--)
|
||||
{
|
||||
int j;
|
||||
BN_ULONG l = *d--;
|
||||
int j;
|
||||
BN_ULONG l = *d--;
|
||||
for(j = BN_BITS2 - 8; j >= 0; j -= 8)
|
||||
{
|
||||
BYTE b = (BYTE)((l >> j) & 0xFF);
|
||||
BYTE b = (BYTE)((l >> j) & 0xFF);
|
||||
notZero = notZero || (b != 0);
|
||||
if(notZero)
|
||||
printf("%02x", b);
|
||||
@ -206,17 +202,14 @@ void BIGNUM_print(
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
/* B.2.3.2.3.4. BnNewVariable() */
|
||||
/* This function allocates a new variable in the provided context. If the context does not exist or
|
||||
the allocation fails, it is a catastrophic failure. */
|
||||
static BIGNUM *
|
||||
BnNewVariable(
|
||||
BN_CTX *CTX
|
||||
)
|
||||
//*** BnNewVariable()
|
||||
// This function allocates a new variable in the provided context. If the context
|
||||
// does not exist or the allocation fails, it is a catastrophic failure.
|
||||
static BIGNUM* BnNewVariable(BN_CTX* CTX)
|
||||
{
|
||||
BIGNUM *new;
|
||||
BIGNUM* new;
|
||||
//
|
||||
// This check is intended to protect against calling this function without
|
||||
// having initialized the CTX.
|
||||
@ -225,59 +218,52 @@ BnNewVariable(
|
||||
return new;
|
||||
}
|
||||
|
||||
#if LIBRARY_COMPATIBILITY_CHECK
|
||||
# if LIBRARY_COMPATIBILITY_CHECK
|
||||
|
||||
//*** MathLibraryCompatibilityCheck()
|
||||
BOOL BnMathLibraryCompatibilityCheck(void)
|
||||
{
|
||||
OSSL_ENTER();
|
||||
BIGNUM *osslTemp = BnNewVariable(CTX);
|
||||
#if 0
|
||||
crypt_uword_t i;
|
||||
#endif
|
||||
BYTE test[] = {0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x19, 0x18,
|
||||
0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
|
||||
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};
|
||||
BN_VAR(tpmTemp, sizeof(test) * 8); // allocate some space for a test value
|
||||
BIGNUM* osslTemp = BnNewVariable(CTX);
|
||||
#if 0 // libtpms: added
|
||||
crypt_uword_t i;
|
||||
#endif // libtpms: added
|
||||
BYTE test[] = {0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x19, 0x18, 0x17, 0x16, 0x15,
|
||||
0x14, 0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A,
|
||||
0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};
|
||||
BN_VAR(tpmTemp, sizeof(test) * 8); // allocate some space for a test value
|
||||
//
|
||||
// Convert the test data to a bigNum
|
||||
BnFromBytes(tpmTemp, test, sizeof(test));
|
||||
// Convert the test data to an OpenSSL BIGNUM
|
||||
BN_bin2bn(test, sizeof(test), osslTemp);
|
||||
// Make sure the values are consistent
|
||||
#if 0
|
||||
#if 0 // libtpms: added
|
||||
GOTO_ERROR_UNLESS(osslTemp->top == (int)tpmTemp->size);
|
||||
for(i = 0; i < tpmTemp->size; i++)
|
||||
GOTO_ERROR_UNLESS(osslTemp->d[i] == tpmTemp->d[i]);
|
||||
#endif
|
||||
#endif // libtpms: added
|
||||
OSSL_LEAVE();
|
||||
return 1;
|
||||
#if 0
|
||||
#if 0 // libtpms: added
|
||||
Error:
|
||||
return 0;
|
||||
#endif
|
||||
#endif // libtpms: added
|
||||
}
|
||||
# endif
|
||||
|
||||
#endif
|
||||
|
||||
/* B.2.3.2.3.3. BnModMult() */
|
||||
/* Does multiply and divide returning the remainder of the divide. */
|
||||
/* Return Value Meaning */
|
||||
/* TRUE(1) success */
|
||||
/* FALSE(0) failure in operation */
|
||||
|
||||
LIB_EXPORT BOOL
|
||||
BnModMult(
|
||||
bigNum result,
|
||||
bigConst op1,
|
||||
bigConst op2,
|
||||
bigConst modulus
|
||||
)
|
||||
//*** BnModMult()
|
||||
// This function does a modular multiply. It first does a multiply and then a divide
|
||||
// and returns the remainder of the divide.
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation
|
||||
LIB_EXPORT BOOL BnModMult(bigNum result, bigConst op1, bigConst op2, bigConst modulus)
|
||||
{
|
||||
OSSL_ENTER();
|
||||
BOOL OK = TRUE;
|
||||
BIGNUM *bnResult = BN_NEW();
|
||||
BIGNUM *bnTemp = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIGNUM* bnResult = BN_NEW();
|
||||
BIGNUM* bnTemp = BN_NEW();
|
||||
BIG_INITIALIZED(bnOp1, op1);
|
||||
BIG_INITIALIZED(bnOp2, op2);
|
||||
BIG_INITIALIZED(bnMod, modulus);
|
||||
@ -296,22 +282,16 @@ BnModMult(
|
||||
return OK;
|
||||
}
|
||||
|
||||
/* B.2.3.2.3.4. BnMult() */
|
||||
/* Multiplies two numbers */
|
||||
/* Return Value Meaning */
|
||||
/* TRUE(1) success */
|
||||
/* FALSE(0) failure in operation */
|
||||
|
||||
LIB_EXPORT BOOL
|
||||
BnMult(
|
||||
bigNum result,
|
||||
bigConst multiplicand,
|
||||
bigConst multiplier
|
||||
)
|
||||
//*** BnMult()
|
||||
// Multiplies two numbers
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation
|
||||
LIB_EXPORT BOOL BnMult(bigNum result, bigConst multiplicand, bigConst multiplier)
|
||||
{
|
||||
OSSL_ENTER();
|
||||
BIGNUM *bnTemp = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIGNUM* bnTemp = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIG_INITIALIZED(bnA, multiplicand);
|
||||
BIG_INITIALIZED(bnB, multiplier);
|
||||
//
|
||||
@ -327,25 +307,19 @@ BnMult(
|
||||
return OK;
|
||||
}
|
||||
|
||||
/* B.2.3.2.3.5. BnDiv() */
|
||||
/* This function divides two bigNum values. The function returns FALSE if there is an error in the
|
||||
operation. */
|
||||
/* Return Value Meaning */
|
||||
/* TRUE(1) success */
|
||||
/* FALSE(0) failure in operation */
|
||||
|
||||
LIB_EXPORT BOOL
|
||||
BnDiv(
|
||||
bigNum quotient,
|
||||
bigNum remainder,
|
||||
bigConst dividend,
|
||||
bigConst divisor
|
||||
)
|
||||
//*** BnDiv()
|
||||
// This function divides two bigNum values. The function returns FALSE if
|
||||
// there is an error in the operation.
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation
|
||||
LIB_EXPORT BOOL BnDiv(
|
||||
bigNum quotient, bigNum remainder, bigConst dividend, bigConst divisor)
|
||||
{
|
||||
OSSL_ENTER();
|
||||
BIGNUM *bnQ = BN_NEW();
|
||||
BIGNUM *bnR = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIGNUM* bnQ = BN_NEW();
|
||||
BIGNUM* bnR = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIG_INITIALIZED(bnDend, dividend);
|
||||
BIG_INITIALIZED(bnSor, divisor);
|
||||
//
|
||||
@ -369,24 +343,21 @@ BnDiv(
|
||||
return OK;
|
||||
}
|
||||
|
||||
#if ALG_RSA
|
||||
#if !RSA_KEY_SIEVE // libtpms added
|
||||
/* B.2.3.2.3.6. BnGcd() */
|
||||
/* Get the greatest common divisor of two numbers */
|
||||
/* Return Value Meaning */
|
||||
/* TRUE(1) success */
|
||||
/* FALSE(0) failure in operation */
|
||||
|
||||
LIB_EXPORT BOOL
|
||||
BnGcd(
|
||||
bigNum gcd, // OUT: the common divisor
|
||||
bigConst number1, // IN:
|
||||
bigConst number2 // IN:
|
||||
)
|
||||
# if ALG_RSA
|
||||
# if !RSA_KEY_SIEVE // libtpms added
|
||||
//*** BnGcd()
|
||||
// Get the greatest common divisor of two numbers
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation
|
||||
LIB_EXPORT BOOL BnGcd(bigNum gcd, // OUT: the common divisor
|
||||
bigConst number1, // IN:
|
||||
bigConst number2 // IN:
|
||||
)
|
||||
{
|
||||
OSSL_ENTER();
|
||||
BIGNUM *bnGcd = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIGNUM* bnGcd = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIG_INITIALIZED(bn1, number1);
|
||||
BIG_INITIALIZED(bn2, number2);
|
||||
//
|
||||
@ -402,26 +373,23 @@ BnGcd(
|
||||
OSSL_LEAVE();
|
||||
return OK;
|
||||
}
|
||||
#endif // libtpms added
|
||||
# endif // libtpms added
|
||||
|
||||
/* B.2.3.2.3.7. BnModExp() */
|
||||
/* Do modular exponentiation using bigNum values. The conversion from a bignum_t to a bigNum is
|
||||
trivial as they are based on the same structure */
|
||||
/* Return Value Meaning */
|
||||
/* TRUE(1) success */
|
||||
/* FALSE(0) failure in operation */
|
||||
|
||||
LIB_EXPORT BOOL
|
||||
BnModExp(
|
||||
bigNum result, // OUT: the result
|
||||
bigConst number, // IN: number to exponentiate
|
||||
bigConst exponent, // IN:
|
||||
bigConst modulus // IN:
|
||||
)
|
||||
//***BnModExp()
|
||||
// Do modular exponentiation using bigNum values. The conversion from a bignum_t to
|
||||
// a bigNum is trivial as they are based on the same structure
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation
|
||||
LIB_EXPORT BOOL BnModExp(bigNum result, // OUT: the result
|
||||
bigConst number, // IN: number to exponentiate
|
||||
bigConst exponent, // IN:
|
||||
bigConst modulus // IN:
|
||||
)
|
||||
{
|
||||
OSSL_ENTER();
|
||||
BIGNUM *bnResult = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIGNUM* bnResult = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIG_INITIALIZED(bnN, number);
|
||||
BIG_INITIALIZED(bnE, exponent);
|
||||
BIG_INITIALIZED(bnM, modulus);
|
||||
@ -439,23 +407,18 @@ BnModExp(
|
||||
OSSL_LEAVE();
|
||||
return OK;
|
||||
}
|
||||
# endif // ALG_RSA
|
||||
|
||||
/* B.2.3.2.3.8. BnModInverse() */
|
||||
/* Modular multiplicative inverse */
|
||||
/* Return Value Meaning */
|
||||
/* TRUE(1) success */
|
||||
/* FALSE(0) failure in operation */
|
||||
|
||||
LIB_EXPORT BOOL
|
||||
BnModInverse(
|
||||
bigNum result,
|
||||
bigConst number,
|
||||
bigConst modulus
|
||||
)
|
||||
//*** BnModInverse()
|
||||
// Modular multiplicative inverse
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation
|
||||
LIB_EXPORT BOOL BnModInverse(bigNum result, bigConst number, bigConst modulus)
|
||||
{
|
||||
OSSL_ENTER();
|
||||
BIGNUM *bnResult = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIGNUM* bnResult = BN_NEW();
|
||||
BOOL OK = TRUE;
|
||||
BIG_INITIALIZED(bnN, number);
|
||||
BIG_INITIALIZED(bnM, modulus);
|
||||
//
|
||||
@ -472,29 +435,26 @@ BnModInverse(
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif // TPM_ALG_RSA
|
||||
# if ALG_ECC
|
||||
|
||||
#if ALG_ECC
|
||||
|
||||
/* B.2.3.2.3.9. PointFromOssl() */
|
||||
/* Function to copy the point result from an OSSL function to a bigNum */
|
||||
/* Return Value Meaning */
|
||||
/* TRUE(1) success */
|
||||
/* FALSE(0) failure in operation */
|
||||
static BOOL
|
||||
PointFromOssl(
|
||||
bigPoint pOut, // OUT: resulting point
|
||||
EC_POINT *pIn, // IN: the point to return
|
||||
bigCurve E // IN: the curve
|
||||
)
|
||||
//*** PointFromOssl()
|
||||
// Function to copy the point result from an OSSL function to a bigNum
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation
|
||||
static BOOL PointFromOssl(bigPoint pOut, // OUT: resulting point
|
||||
EC_POINT* pIn, // IN: the point to return
|
||||
const bigCurve E // IN: the curve
|
||||
)
|
||||
{
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
BOOL OK;
|
||||
BIGNUM* x = NULL;
|
||||
BIGNUM* y = NULL;
|
||||
BOOL OK;
|
||||
BN_CTX_start(E->CTX);
|
||||
//
|
||||
x = BN_CTX_get(E->CTX);
|
||||
y = BN_CTX_get(E->CTX);
|
||||
|
||||
if(y == NULL)
|
||||
FAIL(FATAL_ERROR_ALLOCATION);
|
||||
// If this returns false, then the point is at infinity
|
||||
@ -515,17 +475,13 @@ PointFromOssl(
|
||||
BN_CTX_end(E->CTX);
|
||||
return OK;
|
||||
}
|
||||
/* B.2.3.2.3.10. EcPointInitialized() */
|
||||
/* Allocate and initialize a point. */
|
||||
|
||||
LIB_EXPORT EC_POINT * // libtpms: exported function
|
||||
EcPointInitialized(
|
||||
pointConst initializer,
|
||||
bigCurve E
|
||||
)
|
||||
//*** EcPointInitialized()
|
||||
// Allocate and initialize a point.
|
||||
LIB_EXPORT EC_POINT* EcPointInitialized(pointConst initializer, bigCurve E) // libtpms: exported function
|
||||
{
|
||||
EC_POINT *P = NULL;
|
||||
|
||||
EC_POINT* P = NULL;
|
||||
|
||||
if(initializer != NULL)
|
||||
{
|
||||
BIG_INITIALIZED(bnX, initializer->x);
|
||||
@ -546,13 +502,14 @@ EcPointInitialized(
|
||||
return P;
|
||||
}
|
||||
|
||||
/* B.2.3.2.3.11. BnCurveInitialize() */
|
||||
/* This function initializes the OpenSSL group definition */
|
||||
/* It is a fatal error if groupContext is not provided. */
|
||||
/* Return Values Meaning */
|
||||
/* NULL the TPM_ECC_CURVE is not valid */
|
||||
/* non-NULL points to a structure in groupContext */
|
||||
|
||||
//*** BnCurveInitialize()
|
||||
// This function initializes the OpenSSL curve information structure. This
|
||||
// structure points to the TPM-defined values for the curve, to the context for the
|
||||
// number values in the frame, and to the OpenSSL-defined group values.
|
||||
// Return Type: bigCurveData*
|
||||
// NULL the TPM_ECC_CURVE is not valid or there was a problem in
|
||||
// in initializing the curve data
|
||||
// non-NULL points to 'E'
|
||||
LIB_EXPORT bigCurve
|
||||
BnCurveInitialize(
|
||||
bigCurve E, // IN: curve structure to initialize
|
||||
@ -566,8 +523,8 @@ BnCurveInitialize(
|
||||
{
|
||||
// This creates the OpenSSL memory context that stays in effect as long as the
|
||||
// curve (E) is defined.
|
||||
OSSL_ENTER(); // if the allocation fails, the TPM fails
|
||||
EC_POINT *P = NULL;
|
||||
OSSL_ENTER(); // if the allocation fails, the TPM fails
|
||||
EC_POINT* P = NULL;
|
||||
BIG_INITIALIZED(bnP, C->prime);
|
||||
BIG_INITIALIZED(bnA, C->a);
|
||||
BIG_INITIALIZED(bnB, C->b);
|
||||
@ -576,20 +533,20 @@ BnCurveInitialize(
|
||||
BIG_INITIALIZED(bnN, C->order);
|
||||
BIG_INITIALIZED(bnH, C->h);
|
||||
//
|
||||
E->C = C;
|
||||
E->C = C;
|
||||
E->CTX = CTX;
|
||||
|
||||
|
||||
// initialize EC group, associate a generator point and initialize the point
|
||||
// from the parameter data
|
||||
// Create a group structure
|
||||
E->G = EC_GROUP_new_curve_GFp(bnP, bnA, bnB, CTX);
|
||||
GOTO_ERROR_UNLESS(E->G != NULL);
|
||||
|
||||
|
||||
// Allocate a point in the group that will be used in setting the
|
||||
// generator. This is not needed after the generator is set.
|
||||
P = EC_POINT_new(E->G);
|
||||
GOTO_ERROR_UNLESS(P != NULL);
|
||||
|
||||
|
||||
// Need to use this in case Montgomery method is being used
|
||||
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
|
||||
!defined(LIBRESSL_VERSION_NUMBER) // libtpms added begin
|
||||
@ -599,7 +556,7 @@ BnCurveInitialize(
|
||||
#endif // libtpms added
|
||||
// Now set the generator
|
||||
GOTO_ERROR_UNLESS(EC_GROUP_set_generator(E->G, P, bnN, bnH));
|
||||
|
||||
|
||||
EC_POINT_free(P);
|
||||
goto Exit_free; // libtpms changed
|
||||
Error:
|
||||
@ -620,13 +577,10 @@ BnCurveInitialize(
|
||||
return E;
|
||||
}
|
||||
|
||||
/* B.2.3.2.3.15. BnCurveFree() */
|
||||
/* This function will free the allocated components of the curve and end the frame in which the
|
||||
curve data exists */
|
||||
LIB_EXPORT void
|
||||
BnCurveFree(
|
||||
bigCurve E
|
||||
)
|
||||
//*** BnCurveFree()
|
||||
// This function will free the allocated components of the curve and end the
|
||||
// frame in which the curve data exists
|
||||
LIB_EXPORT void BnCurveFree(bigCurve E)
|
||||
{
|
||||
if(E)
|
||||
{
|
||||
@ -635,23 +589,20 @@ BnCurveFree(
|
||||
}
|
||||
}
|
||||
|
||||
/* B.2.3.2.3.11. BnEccModMult() */
|
||||
/* This functi2n does a point multiply of the form R = [d]S */
|
||||
/* Return Values Meaning */
|
||||
/* FALSE failure in operation; treat as result being point at infinity */
|
||||
|
||||
LIB_EXPORT BOOL
|
||||
BnEccModMult(
|
||||
bigPoint R, // OUT: computed point
|
||||
pointConst S, // IN: point to multiply by 'd' (optional)
|
||||
bigConst d, // IN: scalar for [d]S
|
||||
bigCurve E
|
||||
)
|
||||
//*** BnEccModMult()
|
||||
// This function does a point multiply of the form R = [d]S
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation; treat as result being point at infinity
|
||||
LIB_EXPORT BOOL BnEccModMult(bigPoint R, // OUT: computed point
|
||||
pointConst S, // IN: point to multiply by 'd' (optional)
|
||||
bigConst d, // IN: scalar for [d]S
|
||||
const bigCurve E)
|
||||
{
|
||||
EC_POINT *pR = EC_POINT_new(E->G);
|
||||
EC_POINT *pS = EcPointInitialized(S, E);
|
||||
EC_POINT* pR = EC_POINT_new(E->G);
|
||||
EC_POINT* pS = EcPointInitialized(S, E);
|
||||
BIG_INITIALIZED(bnD, d);
|
||||
|
||||
|
||||
if(S == NULL)
|
||||
EC_POINT_mul(E->G, pR, bnD, NULL, NULL, E->CTX);
|
||||
else
|
||||
@ -663,27 +614,26 @@ BnEccModMult(
|
||||
return !BnEqualZero(R->z);
|
||||
}
|
||||
|
||||
/* B.2.3.2.3.13. BnEccModMult2() */
|
||||
/* This function does a point multiply of the form R = [d]G + [u]Q */
|
||||
/* FALSE failure in operation; treat as result being point at infinity */
|
||||
|
||||
LIB_EXPORT BOOL
|
||||
BnEccModMult2(
|
||||
bigPoint R, // OUT: computed point
|
||||
pointConst S, // IN: optional point
|
||||
bigConst d, // IN: scalar for [d]S or [d]G
|
||||
pointConst Q, // IN: second point
|
||||
bigConst u, // IN: second scalar
|
||||
bigCurve E // IN: curve
|
||||
)
|
||||
//*** BnEccModMult2()
|
||||
// This function does a point multiply of the form R = [d]G + [u]Q
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation; treat as result being point at infinity
|
||||
LIB_EXPORT BOOL BnEccModMult2(bigPoint R, // OUT: computed point
|
||||
pointConst S, // IN: optional point
|
||||
bigConst d, // IN: scalar for [d]S or [d]G
|
||||
pointConst Q, // IN: second point
|
||||
bigConst u, // IN: second scalar
|
||||
const bigCurve E // IN: curve
|
||||
)
|
||||
{
|
||||
EC_POINT *pR = EC_POINT_new(E->G);
|
||||
EC_POINT *pS = EcPointInitialized(S, E);
|
||||
EC_POINT* pR = EC_POINT_new(E->G);
|
||||
EC_POINT* pS = EcPointInitialized(S, E);
|
||||
BIG_INITIALIZED(bnD, d);
|
||||
EC_POINT *pQ = EcPointInitialized(Q, E);
|
||||
EC_POINT* pQ = EcPointInitialized(Q, E);
|
||||
BIG_INITIALIZED(bnU, u);
|
||||
|
||||
if(S == NULL || S == (pointConst)&(AccessCurveData(E)->base))
|
||||
|
||||
if(S == NULL || S == (pointConst) & (AccessCurveData(E)->base))
|
||||
EC_POINT_mul(E->G, pR, bnD, pQ, bnU, E->CTX);
|
||||
else
|
||||
{
|
||||
@ -701,10 +651,10 @@ BnEccModMult2(
|
||||
EC_POINT_clear_free(pR1);
|
||||
EC_POINT_clear_free(pR2);
|
||||
#else
|
||||
const EC_POINT *points[2];
|
||||
const BIGNUM *scalars[2];
|
||||
points[0] = pS;
|
||||
points[1] = pQ;
|
||||
const EC_POINT* points[2];
|
||||
const BIGNUM* scalars[2];
|
||||
points[0] = pS;
|
||||
points[1] = pQ;
|
||||
scalars[0] = bnD;
|
||||
scalars[1] = bnU;
|
||||
EC_POINTs_mul(E->G, pR, NULL, 2, points, scalars, E->CTX);
|
||||
@ -720,23 +670,23 @@ BnEccModMult2(
|
||||
return !BnEqualZero(R->z);
|
||||
}
|
||||
|
||||
/* B.2.3.2.4. BnEccAdd() */
|
||||
/* This function does addition of two points. */
|
||||
/* Return Values Meaning */
|
||||
/* FALSE failure in operation; treat as result being point at infinity */
|
||||
LIB_EXPORT BOOL
|
||||
BnEccAdd(
|
||||
bigPoint R, // OUT: computed point
|
||||
pointConst S, // IN: point to multiply by 'd'
|
||||
pointConst Q, // IN: second point
|
||||
bigCurve E // IN: curve
|
||||
)
|
||||
//** BnEccAdd()
|
||||
// This function does addition of two points.
|
||||
// Return Type: BOOL
|
||||
// TRUE(1) success
|
||||
// FALSE(0) failure in operation; treat as result being point at infinity
|
||||
LIB_EXPORT BOOL BnEccAdd(bigPoint R, // OUT: computed point
|
||||
pointConst S, // IN: first point to add
|
||||
pointConst Q, // IN: second point
|
||||
const bigCurve E // IN: curve
|
||||
)
|
||||
{
|
||||
EC_POINT *pR = EC_POINT_new(E->G);
|
||||
EC_POINT *pS = EcPointInitialized(S, E);
|
||||
EC_POINT *pQ = EcPointInitialized(Q, E);
|
||||
EC_POINT* pR = EC_POINT_new(E->G);
|
||||
EC_POINT* pS = EcPointInitialized(S, E);
|
||||
EC_POINT* pQ = EcPointInitialized(Q, E);
|
||||
//
|
||||
EC_POINT_add(E->G, pR, pS, pQ, E->CTX);
|
||||
|
||||
PointFromOssl(R, pR, E);
|
||||
EC_POINT_clear_free(pR); // libtpms changed
|
||||
EC_POINT_clear_free(pS); // libtpms changed
|
||||
@ -744,5 +694,6 @@ BnEccAdd(
|
||||
return !BnEqualZero(R->z);
|
||||
}
|
||||
|
||||
#endif // ALG_ECC
|
||||
#endif // MATH_LIB_OSSL
|
||||
# endif // ALG_ECC
|
||||
|
||||
#endif // MATHLIB OSSL
|
||||
|
||||
Loading…
Reference in New Issue
Block a user