]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLibNull/Pk/CryptEcNull.c
2 Elliptic Curve and ECDH API implementation based on OpenSSL
4 Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/BaseCryptLib.h>
10 #include <Library/DebugLib.h>
13 Initialize new opaque EcGroup object. This object represents an EC curve and
14 and is used for calculation within this group. This object should be freed
15 using EcGroupFree() function.
17 @param[in] CryptoNid Identifying number for the ECC curve (Defined in
20 @retval EcGroup object On success.
21 @retval NULL On failure.
34 Get EC curve parameters. While elliptic curve equation is Y^2 mod P = (X^3 + AX + B) Mod P.
35 This function will set the provided Big Number objects to the corresponding
36 values. The caller needs to make sure all the "out" BigNumber parameters
37 are properly initialized.
39 @param[in] EcGroup EC group object.
40 @param[out] BnPrime Group prime number.
41 @param[out] BnA A coefficient.
42 @param[out] BnB B coefficient..
43 @param[in] BnCtx BN context.
45 @retval TRUE On success.
46 @retval FALSE Otherwise.
51 IN CONST VOID
*EcGroup
,
64 This function will set the provided Big Number object to the corresponding
65 value. The caller needs to make sure that the "out" BigNumber parameter
66 is properly initialized.
68 @param[in] EcGroup EC group object.
69 @param[out] BnOrder Group prime number.
71 @retval TRUE On success.
72 @retval FALSE Otherwise.
86 Free previously allocated EC group object using EcGroupInit().
88 @param[in] EcGroup EC group object to free.
100 Initialize new opaque EC Point object. This object represents an EC point
101 within the given EC group (curve).
103 @param[in] EC Group, properly initialized using EcGroupInit().
105 @retval EC Point object On success.
106 @retval NULL On failure.
111 IN CONST VOID
*EcGroup
119 Free previously allocated EC Point object using EcPointInit().
121 @param[in] EcPoint EC Point to free.
122 @param[in] Clear TRUE iff the memory should be cleared.
135 Get EC point affine (x,y) coordinates.
136 This function will set the provided Big Number objects to the corresponding
137 values. The caller needs to make sure all the "out" BigNumber parameters
138 are properly initialized.
140 @param[in] EcGroup EC group object.
141 @param[in] EcPoint EC point object.
142 @param[out] BnX X coordinate.
143 @param[out] BnY Y coordinate.
144 @param[in] BnCtx BN context, created with BigNumNewContext().
146 @retval TRUE On success.
147 @retval FALSE Otherwise.
151 EcPointGetAffineCoordinates (
152 IN CONST VOID
*EcGroup
,
153 IN CONST VOID
*EcPoint
,
164 Set EC point affine (x,y) coordinates.
166 @param[in] EcGroup EC group object.
167 @param[in] EcPoint EC point object.
168 @param[in] BnX X coordinate.
169 @param[in] BnY Y coordinate.
170 @param[in] BnCtx BN context, created with BigNumNewContext().
172 @retval TRUE On success.
173 @retval FALSE Otherwise.
177 EcPointSetAffineCoordinates (
178 IN CONST VOID
*EcGroup
,
190 EC Point addition. EcPointResult = EcPointA + EcPointB.
192 @param[in] EcGroup EC group object.
193 @param[out] EcPointResult EC point to hold the result. The point should
194 be properly initialized.
195 @param[in] EcPointA EC Point.
196 @param[in] EcPointB EC Point.
197 @param[in] BnCtx BN context, created with BigNumNewContext().
199 @retval TRUE On success.
200 @retval FALSE Otherwise.
205 IN CONST VOID
*EcGroup
,
206 OUT VOID
*EcPointResult
,
207 IN CONST VOID
*EcPointA
,
208 IN CONST VOID
*EcPointB
,
217 Variable EC point multiplication. EcPointResult = EcPoint * BnPScalar.
219 @param[in] EcGroup EC group object.
220 @param[out] EcPointResult EC point to hold the result. The point should
221 be properly initialized.
222 @param[in] EcPoint EC Point.
223 @param[in] BnPScalar P Scalar.
224 @param[in] BnCtx BN context, created with BigNumNewContext().
226 @retval TRUE On success.
227 @retval FALSE Otherwise.
232 IN CONST VOID
*EcGroup
,
233 OUT VOID
*EcPointResult
,
234 IN CONST VOID
*EcPoint
,
235 IN CONST VOID
*BnPScalar
,
244 Calculate the inverse of the supplied EC point.
246 @param[in] EcGroup EC group object.
247 @param[in,out] EcPoint EC point to invert.
248 @param[in] BnCtx BN context, created with BigNumNewContext().
250 @retval TRUE On success.
251 @retval FALSE Otherwise.
256 IN CONST VOID
*EcGroup
,
257 IN OUT VOID
*EcPoint
,
266 Check if the supplied point is on EC curve.
268 @param[in] EcGroup EC group object.
269 @param[in] EcPoint EC point to check.
270 @param[in] BnCtx BN context, created with BigNumNewContext().
272 @retval TRUE On curve.
273 @retval FALSE Otherwise.
278 IN CONST VOID
*EcGroup
,
279 IN CONST VOID
*EcPoint
,
288 Check if the supplied point is at infinity.
290 @param[in] EcGroup EC group object.
291 @param[in] EcPoint EC point to check.
293 @retval TRUE At infinity.
294 @retval FALSE Otherwise.
298 EcPointIsAtInfinity (
299 IN CONST VOID
*EcGroup
,
300 IN CONST VOID
*EcPoint
308 Check if EC points are equal.
310 @param[in] EcGroup EC group object.
311 @param[in] EcPointA EC point A.
312 @param[in] EcPointB EC point B.
313 @param[in] BnCtx BN context, created with BigNumNewContext().
316 @retval FALSE Otherwise.
321 IN CONST VOID
*EcGroup
,
322 IN CONST VOID
*EcPointA
,
323 IN CONST VOID
*EcPointB
,
332 Set EC point compressed coordinates. Points can be described in terms of
333 their compressed coordinates. For a point (x, y), for any given value for x
334 such that the point is on the curve there will only ever be two possible
335 values for y. Therefore, a point can be set using this function where BnX is
336 the x coordinate and YBit is a value 0 or 1 to identify which of the two
337 possible values for y should be used.
339 @param[in] EcGroup EC group object.
340 @param[in] EcPoint EC Point.
341 @param[in] BnX X coordinate.
342 @param[in] YBit 0 or 1 to identify which Y value is used.
343 @param[in] BnCtx BN context, created with BigNumNewContext().
345 @retval TRUE On success.
346 @retval FALSE Otherwise.
350 EcPointSetCompressedCoordinates (
351 IN CONST VOID
*EcGroup
,
363 Allocates and Initializes one Elliptic Curve Context for subsequent use
366 @param[in] Nid cipher NID
367 @return Pointer to the Elliptic Curve Context that has been initialized.
368 If the allocations fails, EcNewByNid() returns NULL.
381 Release the specified EC context.
383 @param[in] EcContext Pointer to the EC context to be released.
395 Generates EC key and returns EC public key (X, Y), Please note, this function uses
396 pseudo random number generator. The caller must make sure RandomSeed()
397 function was properly called before.
398 The Ec context should be correctly initialized by EcNewByNid.
399 This function generates random secret, and computes the public key (X, Y), which is
400 returned via parameter Public, PublicSize.
401 X is the first half of Public with size being PublicSize / 2,
402 Y is the second half of Public with size being PublicSize / 2.
403 EC context is updated accordingly.
404 If the Public buffer is too small to hold the public X, Y, FALSE is returned and
405 PublicSize is set to the required buffer size to obtain the public X, Y.
406 For P-256, the PublicSize is 64. First 32-byte is X, Second 32-byte is Y.
407 For P-384, the PublicSize is 96. First 48-byte is X, Second 48-byte is Y.
408 For P-521, the PublicSize is 132. First 66-byte is X, Second 66-byte is Y.
409 If EcContext is NULL, then return FALSE.
410 If PublicSize is NULL, then return FALSE.
411 If PublicSize is large enough but Public is NULL, then return FALSE.
412 @param[in, out] EcContext Pointer to the EC context.
413 @param[out] PublicKey Pointer to t buffer to receive generated public X,Y.
414 @param[in, out] PublicKeySize On input, the size of Public buffer in bytes.
415 On output, the size of data returned in Public buffer in bytes.
416 @retval TRUE EC public X,Y generation succeeded.
417 @retval FALSE EC public X,Y generation failed.
418 @retval FALSE PublicKeySize is not large enough.
423 IN OUT VOID
*EcContext
,
424 OUT UINT8
*PublicKey
,
425 IN OUT UINTN
*PublicKeySize
433 Gets the public key component from the established EC context.
434 The Ec context should be correctly initialized by EcNewByNid, and successfully
435 generate key pair from EcGenerateKey().
436 For P-256, the PublicSize is 64. First 32-byte is X, Second 32-byte is Y.
437 For P-384, the PublicSize is 96. First 48-byte is X, Second 48-byte is Y.
438 For P-521, the PublicSize is 132. First 66-byte is X, Second 66-byte is Y.
439 @param[in, out] EcContext Pointer to EC context being set.
440 @param[out] PublicKey Pointer to t buffer to receive generated public X,Y.
441 @param[in, out] PublicKeySize On input, the size of Public buffer in bytes.
442 On output, the size of data returned in Public buffer in bytes.
443 @retval TRUE EC key component was retrieved successfully.
444 @retval FALSE Invalid EC key component.
449 IN OUT VOID
*EcContext
,
450 OUT UINT8
*PublicKey
,
451 IN OUT UINTN
*PublicKeySize
459 Computes exchanged common key.
460 Given peer's public key (X, Y), this function computes the exchanged common key,
461 based on its own context including value of curve parameter and random secret.
462 X is the first half of PeerPublic with size being PeerPublicSize / 2,
463 Y is the second half of PeerPublic with size being PeerPublicSize / 2.
464 If EcContext is NULL, then return FALSE.
465 If PeerPublic is NULL, then return FALSE.
466 If PeerPublicSize is 0, then return FALSE.
467 If Key is NULL, then return FALSE.
468 If KeySize is not large enough, then return FALSE.
469 For P-256, the PeerPublicSize is 64. First 32-byte is X, Second 32-byte is Y.
470 For P-384, the PeerPublicSize is 96. First 48-byte is X, Second 48-byte is Y.
471 For P-521, the PeerPublicSize is 132. First 66-byte is X, Second 66-byte is Y.
472 @param[in, out] EcContext Pointer to the EC context.
473 @param[in] PeerPublic Pointer to the peer's public X,Y.
474 @param[in] PeerPublicSize Size of peer's public X,Y in bytes.
475 @param[in] CompressFlag Flag of PeerPublic is compressed or not.
476 @param[out] Key Pointer to the buffer to receive generated key.
477 @param[in, out] KeySize On input, the size of Key buffer in bytes.
478 On output, the size of data returned in Key buffer in bytes.
479 @retval TRUE EC exchanged key generation succeeded.
480 @retval FALSE EC exchanged key generation failed.
481 @retval FALSE KeySize is not large enough.
486 IN OUT VOID
*EcContext
,
487 IN CONST UINT8
*PeerPublic
,
488 IN UINTN PeerPublicSize
,
489 IN CONST INT32
*CompressFlag
,
491 IN OUT UINTN
*KeySize
499 Carries out the EC-DSA signature.
501 This function carries out the EC-DSA signature.
502 If the Signature buffer is too small to hold the contents of signature, FALSE
503 is returned and SigSize is set to the required buffer size to obtain the signature.
505 If EcContext is NULL, then return FALSE.
506 If MessageHash is NULL, then return FALSE.
507 If HashSize need match the HashNid. HashNid could be SHA256, SHA384, SHA512, SHA3_256, SHA3_384, SHA3_512.
508 If SigSize is large enough but Signature is NULL, then return FALSE.
510 For P-256, the SigSize is 64. First 32-byte is R, Second 32-byte is S.
511 For P-384, the SigSize is 96. First 48-byte is R, Second 48-byte is S.
512 For P-521, the SigSize is 132. First 66-byte is R, Second 66-byte is S.
514 @param[in] EcContext Pointer to EC context for signature generation.
515 @param[in] HashNid hash NID
516 @param[in] MessageHash Pointer to octet message hash to be signed.
517 @param[in] HashSize Size of the message hash in bytes.
518 @param[out] Signature Pointer to buffer to receive EC-DSA signature.
519 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
520 On output, the size of data returned in Signature buffer in bytes.
522 @retval TRUE Signature successfully generated in EC-DSA.
523 @retval FALSE Signature generation failed.
524 @retval FALSE SigSize is too small.
532 IN CONST UINT8
*MessageHash
,
534 OUT UINT8
*Signature
,
535 IN OUT UINTN
*SigSize
543 Verifies the EC-DSA signature.
545 If EcContext is NULL, then return FALSE.
546 If MessageHash is NULL, then return FALSE.
547 If Signature is NULL, then return FALSE.
548 If HashSize need match the HashNid. HashNid could be SHA256, SHA384, SHA512, SHA3_256, SHA3_384, SHA3_512.
550 For P-256, the SigSize is 64. First 32-byte is R, Second 32-byte is S.
551 For P-384, the SigSize is 96. First 48-byte is R, Second 48-byte is S.
552 For P-521, the SigSize is 132. First 66-byte is R, Second 66-byte is S.
554 @param[in] EcContext Pointer to EC context for signature verification.
555 @param[in] HashNid hash NID
556 @param[in] MessageHash Pointer to octet message hash to be checked.
557 @param[in] HashSize Size of the message hash in bytes.
558 @param[in] Signature Pointer to EC-DSA signature to be verified.
559 @param[in] SigSize Size of signature in bytes.
561 @retval TRUE Valid signature encoded in EC-DSA.
562 @retval FALSE Invalid signature or invalid EC context.
570 IN CONST UINT8
*MessageHash
,
572 IN CONST UINT8
*Signature
,