| /** @file | |
| Elliptic Curve and ECDH API implementation based on OpenSSL | |
| Copyright (c) 2022, Intel Corporation. All rights reserved.<BR> | |
| Copyright (c) 2026, Arm Limited. All rights reserved.<BR> | |
| SPDX-License-Identifier: BSD-2-Clause-Patent | |
| **/ | |
| #include "InternalCryptLib.h" | |
| #include "KeyContext.h" | |
| #include <openssl/objects.h> | |
| #include <openssl/bn.h> | |
| #include <openssl/core_names.h> | |
| #include <openssl/ec.h> | |
| // ===================================================================================== | |
| // Basic Elliptic Curve Primitives | |
| // ===================================================================================== | |
| /** | |
| Return the Nid of certain ECC curve. | |
| @param[in] CryptoNid Identifying number for the ECC curve (Defined in | |
| BaseCryptLib.h). | |
| @retval !=-1 On success. | |
| @retval -1 ECC curve not supported. | |
| **/ | |
| STATIC | |
| INT32 | |
| CryptoNidToOpensslNid ( | |
| IN UINTN CryptoNid | |
| ) | |
| { | |
| INT32 Nid; | |
| switch (CryptoNid) { | |
| case CRYPTO_NID_SECP256R1: | |
| Nid = NID_X9_62_prime256v1; | |
| break; | |
| case CRYPTO_NID_SECP384R1: | |
| Nid = NID_secp384r1; | |
| break; | |
| case CRYPTO_NID_SECP521R1: | |
| Nid = NID_secp521r1; | |
| break; | |
| case CRYPTO_NID_BRAINPOOLP512R1: | |
| Nid = NID_brainpoolP512r1; | |
| break; | |
| default: | |
| return -1; | |
| } | |
| return Nid; | |
| } | |
| /** | |
| Return the Nid string of certain ECC curve. | |
| @param[in] CryptoNid Identifying number for the ECC curve (Defined in | |
| BaseCryptLib.h). | |
| @retval !=NULL Nid string | |
| @retval NULL ECC curve not supported. | |
| **/ | |
| STATIC | |
| CONST CHAR8 * | |
| CryptoNidToOpensslSn ( | |
| IN UINTN CryptoNid | |
| ) | |
| { | |
| switch (CryptoNid) { | |
| case CRYPTO_NID_SECP256R1: | |
| return SN_X9_62_prime256v1; | |
| case CRYPTO_NID_SECP384R1: | |
| return SN_secp384r1; | |
| case CRYPTO_NID_SECP521R1: | |
| return SN_secp521r1; | |
| case CRYPTO_NID_BRAINPOOLP512R1: | |
| return SN_brainpoolP512r1; | |
| default: | |
| return NULL; | |
| } | |
| } | |
| /** | |
| Initialize new opaque EcGroup object. This object represents an EC curve and | |
| and is used for calculation within this group. This object should be freed | |
| using EcGroupFree() function. | |
| @param[in] CryptoNid Identifying number for the ECC curve (Defined in | |
| BaseCryptLib.h). | |
| @retval EcGroup object On success. | |
| @retval NULL On failure. | |
| **/ | |
| VOID * | |
| EFIAPI | |
| EcGroupInit ( | |
| IN UINTN CryptoNid | |
| ) | |
| { | |
| INT32 Nid; | |
| Nid = CryptoNidToOpensslNid (CryptoNid); | |
| if (Nid < 0) { | |
| return NULL; | |
| } | |
| return EC_GROUP_new_by_curve_name (Nid); | |
| } | |
| /** | |
| Create a new EC_GROUP over a prime field GF(p). | |
| @param[in] BnPrime Group prime number. | |
| @param[in] BnA A coefficient. | |
| @param[in] BnB B coefficient. | |
| @param[in] BnCtx BN context. | |
| @retval EcGroup object On success. | |
| @retval NULL On failure. | |
| **/ | |
| VOID * | |
| EFIAPI | |
| EcGroupInitGFp ( | |
| IN VOID *BnPrime, | |
| IN VOID *BnA, | |
| IN VOID *BnB, | |
| IN VOID *BnCtx OPTIONAL | |
| ) | |
| { | |
| if ((BnPrime == NULL) || (BnA == NULL) || (BnB == NULL)) { | |
| return NULL; | |
| } | |
| return EC_GROUP_new_curve_GFp (BnPrime, BnA, BnB, BnCtx); | |
| } | |
| /** | |
| Get EC curve parameters. While elliptic curve equation is Y^2 mod P = (X^3 + AX + B) Mod P. | |
| This function will set the provided Big Number objects to the corresponding | |
| values. The caller needs to make sure all the "out" BigNumber parameters | |
| are properly initialized. | |
| @param[in] EcGroup EC group object. | |
| @param[out] BnPrime Group prime number. | |
| @param[out] BnA A coefficient. | |
| @param[out] BnB B coefficient.. | |
| @param[in] BnCtx BN context. | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcGroupGetCurve ( | |
| IN CONST VOID *EcGroup, | |
| OUT VOID *BnPrime, | |
| OUT VOID *BnA, | |
| OUT VOID *BnB, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_GROUP_get_curve (EcGroup, BnPrime, BnA, BnB, BnCtx); | |
| } | |
| /** | |
| Get EC group order. | |
| This function will set the provided Big Number object to the corresponding | |
| value. The caller needs to make sure that the "out" BigNumber parameter | |
| is properly initialized. | |
| @param[in] EcGroup EC group object. | |
| @param[out] BnOrder Group prime number. | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcGroupGetOrder ( | |
| IN VOID *EcGroup, | |
| OUT VOID *BnOrder | |
| ) | |
| { | |
| return (BOOLEAN)EC_GROUP_get_order (EcGroup, BnOrder, NULL); | |
| } | |
| /** | |
| Free previously allocated EC group object using EcGroupInit(). | |
| @param[in] EcGroup EC group object to free. | |
| **/ | |
| VOID | |
| EFIAPI | |
| EcGroupFree ( | |
| IN VOID *EcGroup | |
| ) | |
| { | |
| EC_GROUP_free (EcGroup); | |
| } | |
| /** | |
| Set the generator for an EC_GROUP. | |
| @param[in] EcGroup The EC_GROUP object. | |
| @param[in] EcPointGenerator The generator point G. | |
| @param[in] BnOrder The order of the generator. | |
| @param[in] BnCoFactor The cofactor of the group. | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcGroupSetGenerator ( | |
| IN VOID *EcGroup, | |
| IN CONST VOID *EcPointGenerator, | |
| IN CONST VOID *BnOrder, | |
| IN CONST VOID *BnCoFactor OPTIONAL | |
| ) | |
| { | |
| if ((EcGroup == NULL) || (EcPointGenerator == NULL) || (BnOrder == NULL)) { | |
| return FALSE; | |
| } | |
| return (BOOLEAN)EC_GROUP_set_generator (EcGroup, EcPointGenerator, BnOrder, BnCoFactor); | |
| } | |
| /** | |
| Initialize new opaque EC Point object. This object represents an EC point | |
| within the given EC group (curve). | |
| @param[in] EC Group, properly initialized using EcGroupInit(). | |
| @retval EC Point object On success. | |
| @retval NULL On failure. | |
| **/ | |
| VOID * | |
| EFIAPI | |
| EcPointInit ( | |
| IN CONST VOID *EcGroup | |
| ) | |
| { | |
| return EC_POINT_new (EcGroup); | |
| } | |
| /** | |
| Free previously allocated EC Point object using EcPointInit(). | |
| @param[in] EcPoint EC Point to free. | |
| @param[in] Clear TRUE iff the memory should be cleared. | |
| **/ | |
| VOID | |
| EFIAPI | |
| EcPointDeInit ( | |
| IN VOID *EcPoint, | |
| IN BOOLEAN Clear | |
| ) | |
| { | |
| if (Clear) { | |
| EC_POINT_clear_free (EcPoint); | |
| } else { | |
| EC_POINT_free (EcPoint); | |
| } | |
| } | |
| /** | |
| Get EC point affine (x,y) coordinates. | |
| This function will set the provided Big Number objects to the corresponding | |
| values. The caller needs to make sure all the "out" BigNumber parameters | |
| are properly initialized. | |
| @param[in] EcGroup EC group object. | |
| @param[in] EcPoint EC point object. | |
| @param[out] BnX X coordinate. | |
| @param[out] BnY Y coordinate. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointGetAffineCoordinates ( | |
| IN CONST VOID *EcGroup, | |
| IN CONST VOID *EcPoint, | |
| OUT VOID *BnX, | |
| OUT VOID *BnY, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_POINT_get_affine_coordinates (EcGroup, EcPoint, BnX, BnY, BnCtx); | |
| } | |
| /** | |
| Set EC point affine (x,y) coordinates. | |
| @param[in] EcGroup EC group object. | |
| @param[in] EcPoint EC point object. | |
| @param[in] BnX X coordinate. | |
| @param[in] BnY Y coordinate. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointSetAffineCoordinates ( | |
| IN CONST VOID *EcGroup, | |
| IN VOID *EcPoint, | |
| IN CONST VOID *BnX, | |
| IN CONST VOID *BnY, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_POINT_set_affine_coordinates (EcGroup, EcPoint, BnX, BnY, BnCtx); | |
| } | |
| /** | |
| EC Point addition. EcPointResult = EcPointA + EcPointB. | |
| @param[in] EcGroup EC group object. | |
| @param[out] EcPointResult EC point to hold the result. The point should | |
| be properly initialized. | |
| @param[in] EcPointA EC Point. | |
| @param[in] EcPointB EC Point. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointAdd ( | |
| IN CONST VOID *EcGroup, | |
| OUT VOID *EcPointResult, | |
| IN CONST VOID *EcPointA, | |
| IN CONST VOID *EcPointB, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_POINT_add (EcGroup, EcPointResult, EcPointA, EcPointB, BnCtx); | |
| } | |
| /** | |
| Variable EC point multiplication. EcPointResult = EcPoint * BnPScalar. | |
| @param[in] EcGroup EC group object. | |
| @param[out] EcPointResult EC point to hold the result. The point should | |
| be properly initialized. | |
| @param[in] EcPoint EC Point. | |
| @param[in] BnPScalar P Scalar. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointMul ( | |
| IN CONST VOID *EcGroup, | |
| OUT VOID *EcPointResult, | |
| IN CONST VOID *EcPoint, | |
| IN CONST VOID *BnPScalar, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_POINT_mul (EcGroup, EcPointResult, NULL, EcPoint, BnPScalar, BnCtx); | |
| } | |
| /** | |
| Variable EC point multiplication. EcPointResult = BnGScalar * G + EcPoint * BnPScalar. | |
| @param[in] EcGroup EC group object. | |
| @param[out] EcPointResult EC point to hold the result. The point should | |
| be properly initialized. | |
| @param[in] BnGScalar G Scalar. | |
| @param[in] EcPoint EC Point. | |
| @param[in] BnPScalar P Scalar. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointMul2 ( | |
| IN CONST VOID *EcGroup, | |
| OUT VOID *EcPointResult, | |
| IN CONST VOID *BnGScalar, | |
| IN CONST VOID *EcPoint, | |
| IN CONST VOID *BnPScalar, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_POINT_mul (EcGroup, EcPointResult, BnGScalar, EcPoint, BnPScalar, BnCtx); | |
| } | |
| /** | |
| Compute a linear combination of multiple EC points. | |
| This function calculates: | |
| EcPointResult = BnGScalar * G + Σ (EcPoints[i] * BnPScalars[i]) | |
| where G is the generator of the EC_GROUP, and Q[i] are arbitrary points. | |
| @param[in] EcGroup EC group object. | |
| @param[out] EcPointResult EC point to hold the result. The point should | |
| be properly initialized. | |
| @param[in] BnGScalar G Scalar. | |
| May be NULL if not used. | |
| @param[in] Count The number of EcPoints and P scalars. | |
| @param[in] EcPoints EC Points. | |
| @param[in] BnPScalars P Scalars. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointsMul ( | |
| IN CONST VOID *EcGroup, | |
| OUT VOID *EcPointResult, | |
| IN CONST VOID *BnGScalar, | |
| UINTN Count, | |
| IN CONST VOID *EcPoints[], | |
| IN CONST VOID *BnPScalars[], | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_POINTs_mul (EcGroup, EcPointResult, BnGScalar, Count, (CONST EC_POINT **)EcPoints, (CONST BIGNUM **)BnPScalars, BnCtx); | |
| } | |
| /** | |
| Calculate the inverse of the supplied EC point. | |
| @param[in] EcGroup EC group object. | |
| @param[in,out] EcPoint EC point to invert. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointInvert ( | |
| IN CONST VOID *EcGroup, | |
| IN OUT VOID *EcPoint, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_POINT_invert (EcGroup, EcPoint, BnCtx); | |
| } | |
| /** | |
| Check if the supplied point is on EC curve. | |
| @param[in] EcGroup EC group object. | |
| @param[in] EcPoint EC point to check. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On curve. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointIsOnCurve ( | |
| IN CONST VOID *EcGroup, | |
| IN CONST VOID *EcPoint, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return EC_POINT_is_on_curve (EcGroup, EcPoint, BnCtx) == 1; | |
| } | |
| /** | |
| Check if the supplied point is at infinity. | |
| @param[in] EcGroup EC group object. | |
| @param[in] EcPoint EC point to check. | |
| @retval TRUE At infinity. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointIsAtInfinity ( | |
| IN CONST VOID *EcGroup, | |
| IN CONST VOID *EcPoint | |
| ) | |
| { | |
| return EC_POINT_is_at_infinity (EcGroup, EcPoint) == 1; | |
| } | |
| /** | |
| Check if EC points are equal. | |
| @param[in] EcGroup EC group object. | |
| @param[in] EcPointA EC point A. | |
| @param[in] EcPointB EC point B. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE A == B. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointEqual ( | |
| IN CONST VOID *EcGroup, | |
| IN CONST VOID *EcPointA, | |
| IN CONST VOID *EcPointB, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return EC_POINT_cmp (EcGroup, EcPointA, EcPointB, BnCtx) == 0; | |
| } | |
| /** | |
| Set EC point compressed coordinates. Points can be described in terms of | |
| their compressed coordinates. For a point (x, y), for any given value for x | |
| such that the point is on the curve there will only ever be two possible | |
| values for y. Therefore, a point can be set using this function where BnX is | |
| the x coordinate and YBit is a value 0 or 1 to identify which of the two | |
| possible values for y should be used. | |
| @param[in] EcGroup EC group object. | |
| @param[in] EcPoint EC Point. | |
| @param[in] BnX X coordinate. | |
| @param[in] YBit 0 or 1 to identify which Y value is used. | |
| @param[in] BnCtx BN context, created with BigNumNewContext(). | |
| @retval TRUE On success. | |
| @retval FALSE Otherwise. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcPointSetCompressedCoordinates ( | |
| IN CONST VOID *EcGroup, | |
| IN VOID *EcPoint, | |
| IN CONST VOID *BnX, | |
| IN UINT8 YBit, | |
| IN VOID *BnCtx | |
| ) | |
| { | |
| return (BOOLEAN)EC_POINT_set_compressed_coordinates (EcGroup, EcPoint, BnX, YBit, BnCtx); | |
| } | |
| // ===================================================================================== | |
| // Elliptic Curve Diffie Hellman Primitives | |
| // ===================================================================================== | |
| /** | |
| Allocates and Initializes one Elliptic Curve Context for subsequent use | |
| with the NID. | |
| @param[in] Nid Identifying number for the ECC curve (Defined in | |
| BaseCryptLib.h). | |
| @return Pointer to the Elliptic Curve Context that has been initialized. | |
| If the allocations fails, EcNewByNid() returns NULL. | |
| **/ | |
| VOID * | |
| EFIAPI | |
| EcNewByNid ( | |
| IN UINTN Nid | |
| ) | |
| { | |
| CONST CHAR8 *OpenSslSn; | |
| EC_CONTEXT *EcCtx; | |
| EVP_PKEY_CTX *EvpCtx; | |
| OpenSslSn = CryptoNidToOpensslSn (Nid); | |
| if (OpenSslSn == NULL) { | |
| return NULL; | |
| } | |
| EcCtx = (EC_CONTEXT *)OPENSSL_zalloc (sizeof (EC_CONTEXT)); | |
| if (EcCtx == NULL) { | |
| return NULL; | |
| } | |
| EvpCtx = EVP_PKEY_CTX_new_from_name (NULL, "EC", NULL); | |
| if (EvpCtx == NULL) { | |
| goto ErrorHandler; | |
| } | |
| if (EVP_PKEY_paramgen_init (EvpCtx) != 1) { | |
| goto ErrorHandler1; | |
| } | |
| if (EVP_PKEY_CTX_set_group_name (EvpCtx, OpenSslSn) != 1) { | |
| goto ErrorHandler1; | |
| } | |
| if (EVP_PKEY_paramgen (EvpCtx, &EcCtx->EvpParam) != 1) { | |
| goto ErrorHandler1; | |
| } | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| return (VOID *)EcCtx; | |
| ErrorHandler1: | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| ErrorHandler: | |
| OPENSSL_free (EcCtx); | |
| return NULL; | |
| } | |
| /** | |
| Release the specified EC context. | |
| @param[in] EcContext Pointer to the EC context to be released. | |
| **/ | |
| VOID | |
| EFIAPI | |
| EcFree ( | |
| IN VOID *EcContext | |
| ) | |
| { | |
| EC_CONTEXT *EcCtx; | |
| EcCtx = (EC_CONTEXT *)EcContext; | |
| if (EcCtx != NULL) { | |
| if (EcCtx->EvpPkey != NULL) { | |
| EVP_PKEY_free (EcCtx->EvpPkey); | |
| } | |
| if (EcCtx->EvpParam != NULL) { | |
| EVP_PKEY_free (EcCtx->EvpParam); | |
| } | |
| OPENSSL_free (EcCtx); | |
| } | |
| } | |
| /** | |
| Return the NID for the Elliptic Curve Context. | |
| @param[in] EcContext Pointer to the EC context. | |
| @param[out] Nid Identifying number for the ECC curve (Defined in | |
| BaseCryptLib.h). | |
| @retval TRUE The NID for the EC key component was retrieved successfully. | |
| @retval FALSE Invalid EC key component or Nid is NULL. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcGetCurveNid ( | |
| IN VOID *EcContext, | |
| OUT UINTN *Nid | |
| ) | |
| { | |
| EC_CONTEXT *EcCtx; | |
| EVP_PKEY *EvpPkey; | |
| CHAR8 GroupName[20]; | |
| if ((EcContext == NULL) || (Nid == NULL)) { | |
| return FALSE; | |
| } | |
| EcCtx = (EC_CONTEXT *)EcContext; | |
| // | |
| // Prefer the generated/imported key when present. Otherwise, use the | |
| // parameter-only EVP_PKEY created by EcNewByNid(), which still carries | |
| // the EC group name. | |
| // | |
| EvpPkey = (EcCtx->EvpPkey != NULL) ? EcCtx->EvpPkey : EcCtx->EvpParam; | |
| if (EvpPkey == NULL) { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_get_utf8_string_param ( | |
| EvpPkey, | |
| OSSL_PKEY_PARAM_GROUP_NAME, | |
| GroupName, | |
| sizeof (GroupName), | |
| NULL | |
| ) != 1) | |
| { | |
| return FALSE; | |
| } | |
| if (AsciiStrCmp (GroupName, SN_X9_62_prime256v1) == 0) { | |
| *Nid = CRYPTO_NID_SECP256R1; | |
| } else if (AsciiStrCmp (GroupName, SN_secp384r1) == 0) { | |
| *Nid = CRYPTO_NID_SECP384R1; | |
| } else if (AsciiStrCmp (GroupName, SN_secp521r1) == 0) { | |
| *Nid = CRYPTO_NID_SECP521R1; | |
| } else if (AsciiStrCmp (GroupName, SN_brainpoolP512r1) == 0) { | |
| *Nid = CRYPTO_NID_BRAINPOOLP512R1; | |
| } else { | |
| return FALSE; | |
| } | |
| return TRUE; | |
| } | |
| /** | |
| Generates EC key and returns EC public key (X, Y), Please note, this function uses | |
| pseudo random number generator. The caller must make sure RandomSeed() | |
| function was properly called before. | |
| The Ec context should be correctly initialized by EcNewByNid. | |
| This function generates random secret, and computes the public key (X, Y), which is | |
| returned via parameter Public, PublicSize. | |
| X is the first half of Public with size being PublicSize / 2, | |
| Y is the second half of Public with size being PublicSize / 2. | |
| EC context is updated accordingly. | |
| If the Public buffer is too small to hold the public X, Y, FALSE is returned and | |
| PublicSize is set to the required buffer size to obtain the public X, Y. | |
| For P-256, the PublicSize is 64. First 32-byte is X, Second 32-byte is Y. | |
| For P-384, the PublicSize is 96. First 48-byte is X, Second 48-byte is Y. | |
| For P-521, the PublicSize is 132. First 66-byte is X, Second 66-byte is Y. | |
| If EcContext is NULL, then return FALSE. | |
| If PublicSize is NULL, then return FALSE. | |
| If PublicSize is large enough but Public is NULL, then return FALSE. | |
| @param[in, out] EcContext Pointer to the EC context. | |
| @param[out] PublicKey Pointer to t buffer to receive generated public X,Y. | |
| @param[in, out] PublicKeySize On input, the size of Public buffer in bytes. | |
| On output, the size of data returned in Public buffer in bytes. | |
| @retval TRUE EC public X,Y generation succeeded. | |
| @retval FALSE EC public X,Y generation failed. | |
| @retval FALSE PublicKeySize is not large enough. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcGenerateKey ( | |
| IN OUT VOID *EcContext, | |
| OUT UINT8 *PublicKey, | |
| IN OUT UINTN *PublicKeySize | |
| ) | |
| { | |
| EC_CONTEXT *EcCtx; | |
| EVP_PKEY_CTX *EvpCtx; | |
| EVP_PKEY *NewPkey; | |
| BOOLEAN RetVal; | |
| BIGNUM *BnX; | |
| BIGNUM *BnY; | |
| UINTN HalfSize; | |
| INTN XSize; | |
| INTN YSize; | |
| INT32 Bits; | |
| if ((EcContext == NULL) || (PublicKeySize == NULL)) { | |
| return FALSE; | |
| } | |
| if ((PublicKey == NULL) && (*PublicKeySize != 0)) { | |
| return FALSE; | |
| } | |
| EcCtx = (EC_CONTEXT *)EcContext; | |
| if (EcCtx->EvpParam == NULL) { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_get_int_param (EcCtx->EvpParam, OSSL_PKEY_PARAM_BITS, &Bits) != 1) { | |
| return FALSE; | |
| } | |
| HalfSize = (Bits + 7) / 8; | |
| if (*PublicKeySize < HalfSize * 2) { | |
| *PublicKeySize = HalfSize * 2; | |
| return FALSE; | |
| } | |
| *PublicKeySize = HalfSize * 2; | |
| EvpCtx = EVP_PKEY_CTX_new (EcCtx->EvpParam, NULL); | |
| if (EvpCtx == NULL) { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_keygen_init (EvpCtx) != 1) { | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| return FALSE; | |
| } | |
| // Generate a new ephemeral EvpKey. | |
| NewPkey = NULL; | |
| // Assume RAND_seed was called | |
| if (EVP_PKEY_keygen (EvpCtx, &NewPkey) != 1) { | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| return FALSE; | |
| } | |
| // Now that we have a new PKey free the EvpCtx as it is no longer needed. | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| EvpCtx = NULL; | |
| RetVal = FALSE; | |
| BnX = NULL; | |
| BnY = NULL; | |
| if (EVP_PKEY_get_bn_param (NewPkey, OSSL_PKEY_PARAM_EC_PUB_X, &BnX) != 1) { | |
| goto fail; | |
| } | |
| if (EVP_PKEY_get_bn_param (NewPkey, OSSL_PKEY_PARAM_EC_PUB_Y, &BnY) != 1) { | |
| goto fail1; | |
| } | |
| XSize = BN_num_bytes (BnX); | |
| YSize = BN_num_bytes (BnY); | |
| if ((XSize <= 0) || (YSize <= 0)) { | |
| goto fail2; | |
| } | |
| ASSERT ((UINTN)XSize <= HalfSize && (UINTN)YSize <= HalfSize); | |
| ZeroMem (PublicKey, *PublicKeySize); | |
| BN_bn2bin (BnX, &PublicKey[0 + HalfSize - XSize]); | |
| BN_bn2bin (BnY, &PublicKey[HalfSize + HalfSize - YSize]); | |
| // Free the old EVP Key so that the new ephemeral EvpKey can be set. | |
| if (EcCtx->EvpPkey != NULL) { | |
| EVP_PKEY_free (EcCtx->EvpPkey); | |
| } | |
| EcCtx->EvpPkey = NewPkey; | |
| NewPkey = NULL; | |
| RetVal = TRUE; | |
| fail2: | |
| if (BnY != NULL) { | |
| BN_free (BnY); | |
| } | |
| fail1: | |
| if (BnX != NULL) { | |
| BN_free (BnX); | |
| } | |
| fail: | |
| if (!RetVal && (NewPkey != NULL)) { | |
| EVP_PKEY_free (NewPkey); | |
| NewPkey = NULL; | |
| } | |
| return RetVal; | |
| } | |
| /** | |
| Gets the public key component from the established EC context. | |
| The Ec context should be correctly initialized by EcNewByNid, and successfully | |
| generate key pair from EcGenerateKey(). | |
| For P-256, the PublicSize is 64. First 32-byte is X, Second 32-byte is Y. | |
| For P-384, the PublicSize is 96. First 48-byte is X, Second 48-byte is Y. | |
| For P-521, the PublicSize is 132. First 66-byte is X, Second 66-byte is Y. | |
| @param[in, out] EcContext Pointer to EC context being set. | |
| @param[out] PublicKey Pointer to t buffer to receive generated public X,Y. | |
| @param[in, out] PublicKeySize On input, the size of Public buffer in bytes. | |
| On output, the size of data returned in Public buffer in bytes. | |
| @retval TRUE EC key component was retrieved successfully. | |
| @retval FALSE Invalid EC key component. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcGetPubKey ( | |
| IN OUT VOID *EcContext, | |
| OUT UINT8 *PublicKey, | |
| IN OUT UINTN *PublicKeySize | |
| ) | |
| { | |
| EC_CONTEXT *EcCtx; | |
| BIGNUM *BnX; | |
| BIGNUM *BnY; | |
| UINTN HalfSize; | |
| INTN XSize; | |
| INTN YSize; | |
| BOOLEAN RetVal; | |
| INT32 Bits; | |
| if ((EcContext == NULL) || (PublicKeySize == NULL)) { | |
| return FALSE; | |
| } | |
| if ((PublicKey == NULL) && (*PublicKeySize != 0)) { | |
| return FALSE; | |
| } | |
| EcCtx = (EC_CONTEXT *)EcContext; | |
| if (EcCtx->EvpPkey == NULL) { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_get_int_param (EcCtx->EvpPkey, OSSL_PKEY_PARAM_BITS, &Bits) != 1) { | |
| return FALSE; | |
| } | |
| HalfSize = (Bits + 7) / 8; | |
| if (*PublicKeySize < HalfSize * 2) { | |
| *PublicKeySize = HalfSize * 2; | |
| return FALSE; | |
| } | |
| *PublicKeySize = HalfSize * 2; | |
| RetVal = FALSE; | |
| BnX = NULL; | |
| BnY = NULL; | |
| if (EVP_PKEY_get_bn_param (EcCtx->EvpPkey, OSSL_PKEY_PARAM_EC_PUB_X, &BnX) != 1) { | |
| goto fail; | |
| } | |
| if (EVP_PKEY_get_bn_param (EcCtx->EvpPkey, OSSL_PKEY_PARAM_EC_PUB_Y, &BnY) != 1) { | |
| goto fail1; | |
| } | |
| XSize = BN_num_bytes (BnX); | |
| YSize = BN_num_bytes (BnY); | |
| if ((XSize <= 0) || (YSize <= 0)) { | |
| goto fail2; | |
| } | |
| ASSERT ((UINTN)XSize <= HalfSize && (UINTN)YSize <= HalfSize); | |
| if (PublicKey != NULL) { | |
| ZeroMem (PublicKey, *PublicKeySize); | |
| BN_bn2bin (BnX, &PublicKey[0 + HalfSize - XSize]); | |
| BN_bn2bin (BnY, &PublicKey[HalfSize + HalfSize - YSize]); | |
| } | |
| RetVal = TRUE; | |
| fail2: | |
| if (BnY != NULL) { | |
| BN_free (BnY); | |
| } | |
| fail1: | |
| if (BnX != NULL) { | |
| BN_free (BnX); | |
| } | |
| fail: | |
| return RetVal; | |
| } | |
| /** | |
| Computes exchanged common key. | |
| Given peer's public key (X, Y), this function computes the exchanged common key, | |
| based on its own context including value of curve parameter and random secret. | |
| X is the first half of PeerPublic with size being PeerPublicSize / 2, | |
| Y is the second half of PeerPublic with size being PeerPublicSize / 2. | |
| If public key is compressed, the PeerPublic will only contain half key (X). | |
| If EcContext is NULL, then return FALSE. | |
| If PeerPublic is NULL, then return FALSE. | |
| If PeerPublicSize is 0, then return FALSE. | |
| If Key is NULL, then return FALSE. | |
| If KeySize is not large enough, then return FALSE. | |
| For P-256, the PeerPublicSize is 64. First 32-byte is X, Second 32-byte is Y. | |
| For P-384, the PeerPublicSize is 96. First 48-byte is X, Second 48-byte is Y. | |
| For P-521, the PeerPublicSize is 132. First 66-byte is X, Second 66-byte is Y. | |
| @param[in, out] EcContext Pointer to the EC context. | |
| @param[in] PeerPublic Pointer to the peer's public X,Y. | |
| @param[in] PeerPublicSize Size of peer's public X,Y in bytes. | |
| @param[in] CompressFlag Flag of PeerPublic is compressed or not. | |
| @param[out] Key Pointer to the buffer to receive generated key. | |
| @param[in, out] KeySize On input, the size of Key buffer in bytes. | |
| On output, the size of data returned in Key buffer in bytes. | |
| @retval TRUE EC exchanged key generation succeeded. | |
| @retval FALSE EC exchanged key generation failed. | |
| @retval FALSE KeySize is not large enough. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcDhComputeKey ( | |
| IN OUT VOID *EcContext, | |
| IN CONST UINT8 *PeerPublic, | |
| IN UINTN PeerPublicSize, | |
| IN CONST INT32 *CompressFlag, | |
| OUT UINT8 *Key, | |
| IN OUT UINTN *KeySize | |
| ) | |
| { | |
| EC_CONTEXT *EcCtx; | |
| EVP_PKEY_CTX *EvpCtx; | |
| EVP_PKEY *PeerEvpPkey; | |
| CHAR8 GroupName[20]; | |
| BOOLEAN RetVal; | |
| UINTN HalfSize; | |
| OSSL_PARAM Params[3]; | |
| INT32 Bits; | |
| UINT8 *EncodedPoint; | |
| UINTN EncodedPointSize; | |
| if ((EcContext == NULL) || (PeerPublic == NULL) || (KeySize == NULL)) { | |
| return FALSE; | |
| } | |
| if ((Key == NULL) && (*KeySize != 0)) { | |
| return FALSE; | |
| } | |
| if (PeerPublicSize > INT_MAX) { | |
| return FALSE; | |
| } | |
| EcCtx = (EC_CONTEXT *)EcContext; | |
| if (EcCtx->EvpPkey == NULL) { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_get_utf8_string_param ( | |
| EcCtx->EvpPkey, | |
| OSSL_PKEY_PARAM_GROUP_NAME, | |
| GroupName, | |
| sizeof (GroupName), | |
| NULL | |
| ) != 1) | |
| { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_get_int_param (EcCtx->EvpPkey, OSSL_PKEY_PARAM_BITS, &Bits) != 1) { | |
| return FALSE; | |
| } | |
| HalfSize = (Bits + 7) / 8; | |
| if ((CompressFlag == NULL) && (PeerPublicSize != HalfSize * 2)) { | |
| return FALSE; | |
| } | |
| if ((CompressFlag != NULL) && (PeerPublicSize != HalfSize)) { | |
| return FALSE; | |
| } | |
| if (*KeySize < HalfSize) { | |
| *KeySize = HalfSize; | |
| return FALSE; | |
| } | |
| *KeySize = HalfSize; | |
| RetVal = FALSE; | |
| EvpCtx = NULL; | |
| // Prepare SEC1 encoded point. | |
| EncodedPointSize = 1 + PeerPublicSize; | |
| EncodedPoint = OPENSSL_zalloc (EncodedPointSize); | |
| if (EncodedPoint == NULL) { | |
| return FALSE; | |
| } | |
| if (CompressFlag == NULL) { | |
| EncodedPoint[0] = POINT_CONVERSION_UNCOMPRESSED; | |
| } else { | |
| EncodedPoint[0] = (POINT_CONVERSION_COMPRESSED | ((*CompressFlag) & 1)); | |
| } | |
| CopyMem (EncodedPoint + 1, PeerPublic, PeerPublicSize); | |
| // Prepare parameters for EVP_PKEY_from_data() | |
| Params[0] = OSSL_PARAM_construct_utf8_string ( | |
| OSSL_PKEY_PARAM_GROUP_NAME, | |
| GroupName, | |
| 0 | |
| ); | |
| Params[1] = OSSL_PARAM_construct_octet_string ( | |
| OSSL_PKEY_PARAM_PUB_KEY, | |
| EncodedPoint, | |
| EncodedPointSize | |
| ); | |
| Params[2] = OSSL_PARAM_construct_end (); | |
| PeerEvpPkey = NULL; | |
| // Create EVP_PKEY from PeerPublic | |
| EvpCtx = EVP_PKEY_CTX_new_from_name (NULL, "EC", NULL); | |
| if (EvpCtx == NULL) { | |
| goto fail; | |
| } | |
| if (EVP_PKEY_fromdata_init (EvpCtx) != 1) { | |
| goto fail; | |
| } | |
| if (EVP_PKEY_fromdata ( | |
| EvpCtx, | |
| &PeerEvpPkey, | |
| EVP_PKEY_PUBLIC_KEY, | |
| Params | |
| ) != 1) | |
| { | |
| goto fail; | |
| } | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| // Validate NIST ECDH public key | |
| EvpCtx = EVP_PKEY_CTX_new (PeerEvpPkey, NULL); | |
| if (EvpCtx == NULL) { | |
| goto fail; | |
| } | |
| if (EVP_PKEY_public_check (EvpCtx) != 1) { | |
| goto fail; | |
| } | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| // Compute shared secret | |
| EvpCtx = EVP_PKEY_CTX_new (EcCtx->EvpPkey, NULL); | |
| if (EvpCtx == NULL) { | |
| goto fail; | |
| } | |
| if (EVP_PKEY_derive_init (EvpCtx) != 1) { | |
| goto fail; | |
| } | |
| if (EVP_PKEY_derive_set_peer (EvpCtx, PeerEvpPkey) != 1) { | |
| goto fail; | |
| } | |
| if (EVP_PKEY_derive (EvpCtx, Key, KeySize) != 1) { | |
| goto fail; | |
| } | |
| RetVal = TRUE; | |
| fail: | |
| if (PeerEvpPkey != NULL) { | |
| EVP_PKEY_free (PeerEvpPkey); | |
| } | |
| if (EvpCtx != NULL) { | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| } | |
| OPENSSL_free (EncodedPoint); | |
| return RetVal; | |
| } | |
| /** | |
| Carries out the EC-DSA signature. | |
| This function carries out the EC-DSA signature. | |
| If the Signature buffer is too small to hold the contents of signature, FALSE | |
| is returned and SigSize is set to the required buffer size to obtain the signature. | |
| If EcContext is NULL, then return FALSE. | |
| If MessageHash is NULL, then return FALSE. | |
| If HashSize need match the HashNid. HashNid could be SHA256, SHA384, SHA512, SHA3_256, SHA3_384, SHA3_512. | |
| If SigSize is large enough but Signature is NULL, then return FALSE. | |
| For P-256, the SigSize is 64. First 32-byte is R, Second 32-byte is S. | |
| For P-384, the SigSize is 96. First 48-byte is R, Second 48-byte is S. | |
| For P-521, the SigSize is 132. First 66-byte is R, Second 66-byte is S. | |
| @param[in] EcContext Pointer to EC context for signature generation. | |
| @param[in] HashNid hash NID | |
| @param[in] MessageHash Pointer to octet message hash to be signed. | |
| @param[in] HashSize Size of the message hash in bytes. | |
| @param[out] Signature Pointer to buffer to receive EC-DSA signature. | |
| @param[in, out] SigSize On input, the size of Signature buffer in bytes. | |
| On output, the size of data returned in Signature buffer in bytes. | |
| @retval TRUE Signature successfully generated in EC-DSA. | |
| @retval FALSE Signature generation failed. | |
| @retval FALSE SigSize is too small. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcDsaSign ( | |
| IN VOID *EcContext, | |
| IN UINTN HashNid, | |
| IN CONST UINT8 *MessageHash, | |
| IN UINTN HashSize, | |
| OUT UINT8 *Signature, | |
| IN OUT UINTN *SigSize | |
| ) | |
| { | |
| EC_CONTEXT *EcCtx; | |
| EVP_PKEY_CTX *EvpCtx; | |
| ECDSA_SIG *EcDsaSig; | |
| UINT8 HalfSize; | |
| BIGNUM *R; | |
| BIGNUM *S; | |
| INTN RSize; | |
| INTN SSize; | |
| INT32 Bits; | |
| UINT8 *DerSig; | |
| CONST UINT8 *SigPtr; | |
| UINTN DerSize; | |
| BOOLEAN RetVal; | |
| if ((EcContext == NULL) || (MessageHash == NULL) || (Signature == NULL)) { | |
| return FALSE; | |
| } | |
| EcCtx = (EC_CONTEXT *)EcContext; | |
| if (EcCtx->EvpPkey == NULL) { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_get_int_param (EcCtx->EvpPkey, OSSL_PKEY_PARAM_BITS, &Bits) != 1) { | |
| return FALSE; | |
| } | |
| // Max value of HalfSize is expected to be 66 for P-521. | |
| HalfSize = (UINT8)((Bits + 7) / 8); | |
| if (*SigSize < (UINTN)(HalfSize * 2)) { | |
| *SigSize = HalfSize * 2; | |
| return FALSE; | |
| } | |
| *SigSize = HalfSize * 2; | |
| ZeroMem (Signature, *SigSize); | |
| switch (HashNid) { | |
| case CRYPTO_NID_SHA256: | |
| if (HashSize != SHA256_DIGEST_SIZE) { | |
| return FALSE; | |
| } | |
| break; | |
| case CRYPTO_NID_SHA384: | |
| if (HashSize != SHA384_DIGEST_SIZE) { | |
| return FALSE; | |
| } | |
| break; | |
| case CRYPTO_NID_SHA512: | |
| if (HashSize != SHA512_DIGEST_SIZE) { | |
| return FALSE; | |
| } | |
| break; | |
| default: | |
| return FALSE; | |
| } | |
| RetVal = FALSE; | |
| DerSig = NULL; | |
| EcDsaSig = NULL; | |
| EvpCtx = EVP_PKEY_CTX_new (EcCtx->EvpPkey, NULL); | |
| if (EvpCtx == NULL) { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_sign_init (EvpCtx) != 1) { | |
| goto ErrorHandler; | |
| } | |
| if (EVP_PKEY_sign (EvpCtx, NULL, &DerSize, MessageHash, (UINT32)HashSize) != 1) { | |
| goto ErrorHandler; | |
| } | |
| DerSig = OPENSSL_zalloc (DerSize); | |
| if (DerSig == NULL) { | |
| goto ErrorHandler; | |
| } | |
| if (EVP_PKEY_sign (EvpCtx, DerSig, &DerSize, MessageHash, (UINT32)HashSize) != 1) { | |
| goto ErrorHandler; | |
| } | |
| SigPtr = DerSig; | |
| EcDsaSig = d2i_ECDSA_SIG (NULL, &SigPtr, (UINT32)DerSize); | |
| if (EcDsaSig == NULL) { | |
| goto ErrorHandler; | |
| } | |
| ECDSA_SIG_get0 (EcDsaSig, (CONST BIGNUM **)&R, (CONST BIGNUM **)&S); | |
| RSize = BN_num_bytes (R); | |
| SSize = BN_num_bytes (S); | |
| if ((RSize <= 0) || (SSize <= 0)) { | |
| goto ErrorHandler; | |
| } | |
| ASSERT ((UINTN)RSize <= HalfSize && (UINTN)SSize <= HalfSize); | |
| BN_bn2bin (R, &Signature[0 + HalfSize - RSize]); | |
| BN_bn2bin (S, &Signature[HalfSize + HalfSize - SSize]); | |
| RetVal = TRUE; | |
| ErrorHandler: | |
| if (EcDsaSig != NULL) { | |
| ECDSA_SIG_free (EcDsaSig); | |
| } | |
| if (DerSig != NULL) { | |
| OPENSSL_free (DerSig); | |
| } | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| return RetVal; | |
| } | |
| /** | |
| Verifies the EC-DSA signature. | |
| If EcContext is NULL, then return FALSE. | |
| If MessageHash is NULL, then return FALSE. | |
| If Signature is NULL, then return FALSE. | |
| If HashSize need match the HashNid. HashNid could be SHA256, SHA384, SHA512, SHA3_256, SHA3_384, SHA3_512. | |
| For P-256, the SigSize is 64. First 32-byte is R, Second 32-byte is S. | |
| For P-384, the SigSize is 96. First 48-byte is R, Second 48-byte is S. | |
| For P-521, the SigSize is 132. First 66-byte is R, Second 66-byte is S. | |
| @param[in] EcContext Pointer to EC context for signature verification. | |
| @param[in] HashNid hash NID | |
| @param[in] MessageHash Pointer to octet message hash to be checked. | |
| @param[in] HashSize Size of the message hash in bytes. | |
| @param[in] Signature Pointer to EC-DSA signature to be verified. | |
| @param[in] SigSize Size of signature in bytes. | |
| @retval TRUE Valid signature encoded in EC-DSA. | |
| @retval FALSE Invalid signature or invalid EC context. | |
| **/ | |
| BOOLEAN | |
| EFIAPI | |
| EcDsaVerify ( | |
| IN VOID *EcContext, | |
| IN UINTN HashNid, | |
| IN CONST UINT8 *MessageHash, | |
| IN UINTN HashSize, | |
| IN CONST UINT8 *Signature, | |
| IN UINTN SigSize | |
| ) | |
| { | |
| EC_CONTEXT *EcCtx; | |
| EVP_PKEY_CTX *EvpCtx; | |
| ECDSA_SIG *EcDsaSig; | |
| UINT8 HalfSize; | |
| BIGNUM *R; | |
| BIGNUM *S; | |
| INT32 Bits; | |
| UINT8 *DerSig; | |
| UINT8 *SigPtr; | |
| INT32 DerSize; | |
| BOOLEAN RetVal; | |
| if ((EcContext == NULL) || (MessageHash == NULL) || (Signature == NULL)) { | |
| return FALSE; | |
| } | |
| if ((SigSize > INT_MAX) || (SigSize == 0)) { | |
| return FALSE; | |
| } | |
| EcCtx = (EC_CONTEXT *)EcContext; | |
| if (EcCtx->EvpPkey == NULL) { | |
| return FALSE; | |
| } | |
| if (EVP_PKEY_get_int_param (EcCtx->EvpPkey, OSSL_PKEY_PARAM_BITS, &Bits) != 1) { | |
| return FALSE; | |
| } | |
| // Max value of HalfSize is expected to be 66 for P-521. | |
| HalfSize = (UINT8)((Bits + 7) / 8); | |
| if (SigSize != (UINTN)(HalfSize * 2)) { | |
| return FALSE; | |
| } | |
| switch (HashNid) { | |
| case CRYPTO_NID_SHA256: | |
| if (HashSize != SHA256_DIGEST_SIZE) { | |
| return FALSE; | |
| } | |
| break; | |
| case CRYPTO_NID_SHA384: | |
| if (HashSize != SHA384_DIGEST_SIZE) { | |
| return FALSE; | |
| } | |
| break; | |
| case CRYPTO_NID_SHA512: | |
| if (HashSize != SHA512_DIGEST_SIZE) { | |
| return FALSE; | |
| } | |
| break; | |
| default: | |
| return FALSE; | |
| } | |
| RetVal = FALSE; | |
| R = NULL; | |
| S = NULL; | |
| EcDsaSig = NULL; | |
| DerSig = NULL; | |
| EvpCtx = NULL; | |
| R = BN_bin2bn (Signature, (UINT32)HalfSize, NULL); | |
| S = BN_bin2bn (Signature + HalfSize, (UINT32)HalfSize, NULL); | |
| if ((R == NULL) || (S == NULL)) { | |
| goto ErrorHandler; | |
| } | |
| EcDsaSig = ECDSA_SIG_new (); | |
| if (EcDsaSig == NULL) { | |
| goto ErrorHandler; | |
| } | |
| if (ECDSA_SIG_set0 (EcDsaSig, R, S) != 1) { | |
| goto ErrorHandler; | |
| } | |
| /* | |
| * Ownership of R and S moved to EcDsaSig. | |
| */ | |
| R = NULL; | |
| S = NULL; | |
| DerSize = i2d_ECDSA_SIG (EcDsaSig, NULL); | |
| if (DerSize <= 0) { | |
| goto ErrorHandler; | |
| } | |
| DerSig = OPENSSL_zalloc (DerSize); | |
| if (DerSig == NULL) { | |
| goto ErrorHandler; | |
| } | |
| SigPtr = DerSig; | |
| if (i2d_ECDSA_SIG (EcDsaSig, &SigPtr) != DerSize) { | |
| goto ErrorHandler; | |
| } | |
| EvpCtx = EVP_PKEY_CTX_new (EcCtx->EvpPkey, NULL); | |
| if (EvpCtx == NULL) { | |
| goto ErrorHandler; | |
| } | |
| if (EVP_PKEY_verify_init (EvpCtx) != 1) { | |
| goto ErrorHandler; | |
| } | |
| if (EVP_PKEY_verify (EvpCtx, DerSig, DerSize, MessageHash, HashSize) != 1) { | |
| goto ErrorHandler; | |
| } | |
| RetVal = TRUE; | |
| ErrorHandler: | |
| if (EvpCtx != NULL) { | |
| EVP_PKEY_CTX_free (EvpCtx); | |
| } | |
| if (DerSig != NULL) { | |
| OPENSSL_free (DerSig); | |
| } | |
| if (EcDsaSig != NULL) { | |
| ECDSA_SIG_free (EcDsaSig); | |
| } | |
| if (S != NULL) { | |
| BN_free (S); | |
| } | |
| if (R != NULL) { | |
| BN_free (R); | |
| } | |
| return RetVal; | |
| } |