TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptEc.c File Reference
#include "InternalCryptLib.h"
#include <openssl/objects.h>
#include <openssl/bn.h>
#include <openssl/ec.h>

Go to the source code of this file.

Functions

STATIC INT32 CryptoNidToOpensslNid (IN UINTN CryptoNid)
 
VOID *EFIAPI EcGroupInit (IN UINTN CryptoNid)
 
BOOLEAN EFIAPI EcGroupGetCurve (IN CONST VOID *EcGroup, OUT VOID *BnPrime, OUT VOID *BnA, OUT VOID *BnB, IN VOID *BnCtx)
 
BOOLEAN EFIAPI EcGroupGetOrder (IN VOID *EcGroup, OUT VOID *BnOrder)
 
VOID EFIAPI EcGroupFree (IN VOID *EcGroup)
 
VOID *EFIAPI EcPointInit (IN CONST VOID *EcGroup)
 
VOID EFIAPI EcPointDeInit (IN VOID *EcPoint, IN BOOLEAN Clear)
 
BOOLEAN EFIAPI EcPointGetAffineCoordinates (IN CONST VOID *EcGroup, IN CONST VOID *EcPoint, OUT VOID *BnX, OUT VOID *BnY, IN VOID *BnCtx)
 
BOOLEAN EFIAPI EcPointSetAffineCoordinates (IN CONST VOID *EcGroup, IN VOID *EcPoint, IN CONST VOID *BnX, IN CONST VOID *BnY, IN VOID *BnCtx)
 
BOOLEAN EFIAPI EcPointAdd (IN CONST VOID *EcGroup, OUT VOID *EcPointResult, IN CONST VOID *EcPointA, IN CONST VOID *EcPointB, IN VOID *BnCtx)
 
BOOLEAN EFIAPI EcPointMul (IN CONST VOID *EcGroup, OUT VOID *EcPointResult, IN CONST VOID *EcPoint, IN CONST VOID *BnPScalar, IN VOID *BnCtx)
 
BOOLEAN EFIAPI EcPointInvert (IN CONST VOID *EcGroup, IN OUT VOID *EcPoint, IN VOID *BnCtx)
 
BOOLEAN EFIAPI EcPointIsOnCurve (IN CONST VOID *EcGroup, IN CONST VOID *EcPoint, IN VOID *BnCtx)
 
BOOLEAN EFIAPI EcPointIsAtInfinity (IN CONST VOID *EcGroup, IN CONST VOID *EcPoint)
 
BOOLEAN EFIAPI EcPointEqual (IN CONST VOID *EcGroup, IN CONST VOID *EcPointA, IN CONST VOID *EcPointB, IN VOID *BnCtx)
 
BOOLEAN EFIAPI EcPointSetCompressedCoordinates (IN CONST VOID *EcGroup, IN VOID *EcPoint, IN CONST VOID *BnX, IN UINT8 YBit, IN VOID *BnCtx)
 
VOID *EFIAPI EcNewByNid (IN UINTN Nid)
 
VOID EFIAPI EcFree (IN VOID *EcContext)
 
BOOLEAN EFIAPI EcGenerateKey (IN OUT VOID *EcContext, OUT UINT8 *PublicKey, IN OUT UINTN *PublicKeySize)
 
BOOLEAN EFIAPI EcGetPubKey (IN OUT VOID *EcContext, OUT UINT8 *PublicKey, IN OUT UINTN *PublicKeySize)
 
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)
 
BOOLEAN EFIAPI EcDsaSign (IN VOID *EcContext, IN UINTN HashNid, IN CONST UINT8 *MessageHash, IN UINTN HashSize, OUT UINT8 *Signature, IN OUT UINTN *SigSize)
 
BOOLEAN EFIAPI EcDsaVerify (IN VOID *EcContext, IN UINTN HashNid, IN CONST UINT8 *MessageHash, IN UINTN HashSize, IN CONST UINT8 *Signature, IN UINTN SigSize)
 

Detailed Description

Elliptic Curve and ECDH API implementation based on OpenSSL

Copyright (c) 2022, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file CryptEc.c.

Function Documentation

◆ CryptoNidToOpensslNid()

STATIC INT32 CryptoNidToOpensslNid ( IN UINTN  CryptoNid)

Return the Nid of certain ECC curve.

Parameters
[in]CryptoNidIdentifying number for the ECC curve (Defined in BaseCryptLib.h).
Return values
!=-1On success.
-1ECC curve not supported.

Definition at line 29 of file CryptEc.c.

◆ EcDhComputeKey()

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 
)

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.

Parameters
[in,out]EcContextPointer to the EC context.
[in]PeerPublicPointer to the peer's public X,Y.
[in]PeerPublicSizeSize of peer's public X,Y in bytes.
[in]CompressFlagFlag of PeerPublic is compressed or not.
[out]KeyPointer to the buffer to receive generated key.
[in,out]KeySizeOn input, the size of Key buffer in bytes. On output, the size of data returned in Key buffer in bytes.
Return values
TRUEEC exchanged key generation succeeded.
FALSEEC exchanged key generation failed.
FALSEKeySize is not large enough.

Definition at line 667 of file CryptEc.c.

◆ EcDsaSign()

BOOLEAN EFIAPI EcDsaSign ( IN VOID *  EcContext,
IN UINTN  HashNid,
IN CONST UINT8 *  MessageHash,
IN UINTN  HashSize,
OUT UINT8 *  Signature,
IN OUT UINTN SigSize 
)

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.

Parameters
[in]EcContextPointer to EC context for signature generation.
[in]HashNidhash NID
[in]MessageHashPointer to octet message hash to be signed.
[in]HashSizeSize of the message hash in bytes.
[out]SignaturePointer to buffer to receive EC-DSA signature.
[in,out]SigSizeOn input, the size of Signature buffer in bytes. On output, the size of data returned in Signature buffer in bytes.
Return values
TRUESignature successfully generated in EC-DSA.
FALSESignature generation failed.
FALSESigSize is too small.

Definition at line 801 of file CryptEc.c.

◆ EcDsaVerify()

BOOLEAN EFIAPI EcDsaVerify ( IN VOID *  EcContext,
IN UINTN  HashNid,
IN CONST UINT8 *  MessageHash,
IN UINTN  HashSize,
IN CONST UINT8 *  Signature,
IN UINTN  SigSize 
)

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.

Parameters
[in]EcContextPointer to EC context for signature verification.
[in]HashNidhash NID
[in]MessageHashPointer to octet message hash to be checked.
[in]HashSizeSize of the message hash in bytes.
[in]SignaturePointer to EC-DSA signature to be verified.
[in]SigSizeSize of signature in bytes.
Return values
TRUEValid signature encoded in EC-DSA.
FALSEInvalid signature or invalid EC context.

Definition at line 933 of file CryptEc.c.

◆ EcFree()

VOID EFIAPI EcFree ( IN VOID *  EcContext)

Release the specified EC context.

Parameters
[in]EcContextPointer to the EC context to be released.

Definition at line 440 of file CryptEc.c.

◆ EcGenerateKey()

BOOLEAN EFIAPI EcGenerateKey ( IN OUT VOID *  EcContext,
OUT UINT8 *  PublicKey,
IN OUT UINTN PublicKeySize 
)

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.

Parameters
[in,out]EcContextPointer to the EC context.
[out]PublicKeyPointer to t buffer to receive generated public X,Y.
[in,out]PublicKeySizeOn input, the size of Public buffer in bytes. On output, the size of data returned in Public buffer in bytes.
Return values
TRUEEC public X,Y generation succeeded.
FALSEEC public X,Y generation failed.
FALSEPublicKeySize is not large enough.

Definition at line 475 of file CryptEc.c.

◆ EcGetPubKey()

BOOLEAN EFIAPI EcGetPubKey ( IN OUT VOID *  EcContext,
OUT UINT8 *  PublicKey,
IN OUT UINTN PublicKeySize 
)

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.

Parameters
[in,out]EcContextPointer to EC context being set.
[out]PublicKeyPointer to t buffer to receive generated public X,Y.
[in,out]PublicKeySizeOn input, the size of Public buffer in bytes. On output, the size of data returned in Public buffer in bytes.
Return values
TRUEEC key component was retrieved successfully.
FALSEInvalid EC key component.

Definition at line 567 of file CryptEc.c.

◆ EcGroupFree()

VOID EFIAPI EcGroupFree ( IN VOID *  EcGroup)

Free previously allocated EC group object using EcGroupInit().

Parameters
[in]EcGroupEC group object to free.

Definition at line 140 of file CryptEc.c.

◆ EcGroupGetCurve()

BOOLEAN EFIAPI EcGroupGetCurve ( IN CONST VOID *  EcGroup,
OUT VOID *  BnPrime,
OUT VOID *  BnA,
OUT VOID *  BnB,
IN VOID *  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.

Parameters
[in]EcGroupEC group object.
[out]BnPrimeGroup prime number.
[out]BnAA coefficient.
[out]BnBB coefficient..
[in]BnCtxBN context.
Return values
TRUEOn success.
FALSEOtherwise.

Definition at line 100 of file CryptEc.c.

◆ EcGroupGetOrder()

BOOLEAN EFIAPI EcGroupGetOrder ( IN VOID *  EcGroup,
OUT VOID *  BnOrder 
)

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.

Parameters
[in]EcGroupEC group object.
[out]BnOrderGroup prime number.
Return values
TRUEOn success.
FALSEOtherwise.

Definition at line 125 of file CryptEc.c.

◆ EcGroupInit()

VOID *EFIAPI EcGroupInit ( IN UINTN  CryptoNid)

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.

Parameters
[in]CryptoNidIdentifying number for the ECC curve (Defined in BaseCryptLib.h).
Return values
EcGroupobject On success.
NULLOn failure.

Definition at line 68 of file CryptEc.c.

◆ EcNewByNid()

VOID *EFIAPI EcNewByNid ( IN UINTN  Nid)

Allocates and Initializes one Elliptic Curve Context for subsequent use with the NID.

Parameters
[in]NidIdentifying number for the ECC curve (Defined in BaseCryptLib.h).
Returns
Pointer to the Elliptic Curve Context that has been initialized. If the allocations fails, EcNewByNid() returns NULL.

Definition at line 419 of file CryptEc.c.

◆ EcPointAdd()

BOOLEAN EFIAPI EcPointAdd ( IN CONST VOID *  EcGroup,
OUT VOID *  EcPointResult,
IN CONST VOID *  EcPointA,
IN CONST VOID *  EcPointB,
IN VOID *  BnCtx 
)

EC Point addition. EcPointResult = EcPointA + EcPointB.

Parameters
[in]EcGroupEC group object.
[out]EcPointResultEC point to hold the result. The point should be properly initialized.
[in]EcPointAEC Point.
[in]EcPointBEC Point.
[in]BnCtxBN context, created with BigNumNewContext().
Return values
TRUEOn success.
FALSEOtherwise.

Definition at line 253 of file CryptEc.c.

◆ EcPointDeInit()

VOID EFIAPI EcPointDeInit ( IN VOID *  EcPoint,
IN BOOLEAN  Clear 
)

Free previously allocated EC Point object using EcPointInit().

Parameters
[in]EcPointEC Point to free.
[in]ClearTRUE iff the memory should be cleared.

Definition at line 173 of file CryptEc.c.

◆ EcPointEqual()

BOOLEAN EFIAPI EcPointEqual ( IN CONST VOID *  EcGroup,
IN CONST VOID *  EcPointA,
IN CONST VOID *  EcPointB,
IN VOID *  BnCtx 
)

Check if EC points are equal.

Parameters
[in]EcGroupEC group object.
[in]EcPointAEC point A.
[in]EcPointBEC point B.
[in]BnCtxBN context, created with BigNumNewContext().
Return values
TRUEA == B.
FALSEOtherwise.

Definition at line 364 of file CryptEc.c.

◆ EcPointGetAffineCoordinates()

BOOLEAN EFIAPI EcPointGetAffineCoordinates ( IN CONST VOID *  EcGroup,
IN CONST VOID *  EcPoint,
OUT VOID *  BnX,
OUT VOID *  BnY,
IN VOID *  BnCtx 
)

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.

Parameters
[in]EcGroupEC group object.
[in]EcPointEC point object.
[out]BnXX coordinate.
[out]BnYY coordinate.
[in]BnCtxBN context, created with BigNumNewContext().
Return values
TRUEOn success.
FALSEOtherwise.

Definition at line 202 of file CryptEc.c.

◆ EcPointInit()

VOID *EFIAPI EcPointInit ( IN CONST VOID *  EcGroup)

Initialize new opaque EC Point object. This object represents an EC point within the given EC group (curve).

Parameters
[in]ECGroup, properly initialized using EcGroupInit().
Return values
ECPoint object On success.
NULLOn failure.

Definition at line 158 of file CryptEc.c.

◆ EcPointInvert()

BOOLEAN EFIAPI EcPointInvert ( IN CONST VOID *  EcGroup,
IN OUT VOID *  EcPoint,
IN VOID *  BnCtx 
)

Calculate the inverse of the supplied EC point.

Parameters
[in]EcGroupEC group object.
[in,out]EcPointEC point to invert.
[in]BnCtxBN context, created with BigNumNewContext().
Return values
TRUEOn success.
FALSEOtherwise.

Definition at line 302 of file CryptEc.c.

◆ EcPointIsAtInfinity()

BOOLEAN EFIAPI EcPointIsAtInfinity ( IN CONST VOID *  EcGroup,
IN CONST VOID *  EcPoint 
)

Check if the supplied point is at infinity.

Parameters
[in]EcGroupEC group object.
[in]EcPointEC point to check.
Return values
TRUEAt infinity.
FALSEOtherwise.

Definition at line 343 of file CryptEc.c.

◆ EcPointIsOnCurve()

BOOLEAN EFIAPI EcPointIsOnCurve ( IN CONST VOID *  EcGroup,
IN CONST VOID *  EcPoint,
IN VOID *  BnCtx 
)

Check if the supplied point is on EC curve.

Parameters
[in]EcGroupEC group object.
[in]EcPointEC point to check.
[in]BnCtxBN context, created with BigNumNewContext().
Return values
TRUEOn curve.
FALSEOtherwise.

Definition at line 323 of file CryptEc.c.

◆ EcPointMul()

BOOLEAN EFIAPI EcPointMul ( IN CONST VOID *  EcGroup,
OUT VOID *  EcPointResult,
IN CONST VOID *  EcPoint,
IN CONST VOID *  BnPScalar,
IN VOID *  BnCtx 
)

Variable EC point multiplication. EcPointResult = EcPoint * BnPScalar.

Parameters
[in]EcGroupEC group object.
[out]EcPointResultEC point to hold the result. The point should be properly initialized.
[in]EcPointEC Point.
[in]BnPScalarP Scalar.
[in]BnCtxBN context, created with BigNumNewContext().
Return values
TRUEOn success.
FALSEOtherwise.

Definition at line 279 of file CryptEc.c.

◆ EcPointSetAffineCoordinates()

BOOLEAN EFIAPI EcPointSetAffineCoordinates ( IN CONST VOID *  EcGroup,
IN VOID *  EcPoint,
IN CONST VOID *  BnX,
IN CONST VOID *  BnY,
IN VOID *  BnCtx 
)

Set EC point affine (x,y) coordinates.

Parameters
[in]EcGroupEC group object.
[in]EcPointEC point object.
[in]BnXX coordinate.
[in]BnYY coordinate.
[in]BnCtxBN context, created with BigNumNewContext().
Return values
TRUEOn success.
FALSEOtherwise.

Definition at line 227 of file CryptEc.c.

◆ EcPointSetCompressedCoordinates()

BOOLEAN EFIAPI EcPointSetCompressedCoordinates ( IN CONST VOID *  EcGroup,
IN VOID *  EcPoint,
IN CONST VOID *  BnX,
IN UINT8  YBit,
IN VOID *  BnCtx 
)

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.

Parameters
[in]EcGroupEC group object.
[in]EcPointEC Point.
[in]BnXX coordinate.
[in]YBit0 or 1 to identify which Y value is used.
[in]BnCtxBN context, created with BigNumNewContext().
Return values
TRUEOn success.
FALSEOtherwise.

Definition at line 393 of file CryptEc.c.