ec.c File Reference

ECC (Elliptic Curve Cryptography) More...

#include "core/crypto.h"
#include "ecc/ec.h"
#include "ecc/ec_misc.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 

Functions

void ecInitPublicKey (EcPublicKey *key)
 Initialize an EC public key. More...
 
void ecFreePublicKey (EcPublicKey *key)
 Release an EC public key. More...
 
void ecInitPrivateKey (EcPrivateKey *key)
 Initialize an EC private key. More...
 
void ecFreePrivateKey (EcPrivateKey *key)
 Release an EC private key. More...
 
__weak_func error_t ecGenerateKeyPair (const PrngAlgo *prngAlgo, void *prngContext, const EcCurve *curve, EcPrivateKey *privateKey, EcPublicKey *publicKey)
 EC key pair generation. More...
 
error_t ecGeneratePrivateKey (const PrngAlgo *prngAlgo, void *prngContext, const EcCurve *curve, EcPrivateKey *privateKey)
 EC private key generation. More...
 
error_t ecGeneratePublicKey (const EcPrivateKey *privateKey, EcPublicKey *publicKey)
 Derive the public key from an EC private key. More...
 
error_t ecImportPublicKey (EcPublicKey *key, const EcCurve *curve, const uint8_t *data, size_t length, EcPublicKeyFormat format)
 Import an EC public key. More...
 
error_t ecExportPublicKey (const EcPublicKey *key, uint8_t *data, size_t *length, EcPublicKeyFormat format)
 Export an EC public key. More...
 
error_t ecImportPrivateKey (EcPrivateKey *key, const EcCurve *curve, const uint8_t *data, size_t length)
 Import an EC private key. More...
 
error_t ecExportPrivateKey (const EcPrivateKey *key, uint8_t *data, size_t *length)
 Export an EC private key. More...
 
error_t ecImportPoint (const EcCurve *curve, EcPoint *r, const uint8_t *data, size_t length)
 Convert an octet string to an EC point. More...
 
error_t ecExportPoint (const EcCurve *curve, const EcPoint *a, uint8_t *data, size_t *length)
 Convert an EC point to an octet string. More...
 
void ecProjectify (const EcCurve *curve, EcPoint3 *r, const EcPoint *s)
 Compute projective representation. More...
 
__weak_func error_t ecAffinify (const EcCurve *curve, EcPoint3 *r, const EcPoint3 *s)
 Recover affine representation. More...
 
__weak_func bool_t ecIsPointAffine (const EcCurve *curve, const EcPoint *s)
 Check whether the affine point S is on the curve. More...
 
void ecDouble (EcState *state, EcPoint3 *r, const EcPoint3 *s)
 Point doubling. More...
 
void ecAdd (EcState *state, EcPoint3 *r, const EcPoint3 *s, const EcPoint3 *t)
 Point addition (helper routine) More...
 
void ecFullAdd (EcState *state, EcPoint3 *r, const EcPoint3 *s, const EcPoint3 *t)
 Point addition. More...
 
void ecFullSub (EcState *state, EcPoint3 *r, const EcPoint3 *s, const EcPoint3 *t)
 Point subtraction. More...
 
__weak_func error_t ecMulFast (const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
 Scalar multiplication (fast calculation) More...
 
__weak_func error_t ecMulRegular (const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
 Scalar multiplication (regular calculation) More...
 
__weak_func error_t ecTwinMul (const EcCurve *curve, EcPoint3 *r, const uint32_t *d0, const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t)
 Twin multiplication. More...
 

Variables

const uint8_t EC_PUBLIC_KEY_OID [7] = {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01}
 

Detailed Description

ECC (Elliptic Curve Cryptography)

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneCRYPTO Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.5.0

Definition in file ec.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 32 of file ec.c.

Function Documentation

◆ ecAdd()

void ecAdd ( EcState state,
EcPoint3 r,
const EcPoint3 s,
const EcPoint3 t 
)

Point addition (helper routine)

Parameters
[in]statePointer to the working state
[out]rResulting point R = S + T
[in]sFirst operand
[in]tSecond operand

Definition at line 957 of file ec.c.

◆ ecAffinify()

__weak_func error_t ecAffinify ( const EcCurve curve,
EcPoint3 r,
const EcPoint3 s 
)

Recover affine representation.

Parameters
[in]curveElliptic curve parameters
[out]rAffine representation of the point
[in]sProjective representation of the point
Returns
Error code

Definition at line 749 of file ec.c.

◆ ecDouble()

void ecDouble ( EcState state,
EcPoint3 r,
const EcPoint3 s 
)

Point doubling.

Parameters
[in]statePointer to the working state
[out]rResulting point R = 2S
[in]sPoint S

Definition at line 848 of file ec.c.

◆ ecExportPoint()

error_t ecExportPoint ( const EcCurve curve,
const EcPoint a,
uint8_t *  data,
size_t *  length 
)

Convert an EC point to an octet string.

Parameters
[in]curveElliptic curve parameters
[in]aEC point to be converted
[out]dataPointer to the octet string
[out]lengthLength of the resulting octet string
Returns
Error code

Definition at line 675 of file ec.c.

◆ ecExportPrivateKey()

error_t ecExportPrivateKey ( const EcPrivateKey key,
uint8_t *  data,
size_t *  length 
)

Export an EC private key.

Parameters
[in]keyEC private key
[out]dataPointer to the octet string
[out]lengthLength of the octet string, in bytes
Returns
Error code

Definition at line 554 of file ec.c.

◆ ecExportPublicKey()

error_t ecExportPublicKey ( const EcPublicKey key,
uint8_t *  data,
size_t *  length,
EcPublicKeyFormat  format 
)

Export an EC public key.

Parameters
[in]keyEC public key
[out]dataPointer to the octet string
[out]lengthLength of the octet string, in bytes
[in]formatEC public key format (X9.63 or raw format)
Returns
Error code

Definition at line 378 of file ec.c.

◆ ecFreePrivateKey()

void ecFreePrivateKey ( EcPrivateKey key)

Release an EC private key.

Parameters
[in]keyPointer to the EC public key to free

Definition at line 100 of file ec.c.

◆ ecFreePublicKey()

void ecFreePublicKey ( EcPublicKey key)

Release an EC public key.

Parameters
[in]keyPointer to the EC public key to free

Definition at line 68 of file ec.c.

◆ ecFullAdd()

void ecFullAdd ( EcState state,
EcPoint3 r,
const EcPoint3 s,
const EcPoint3 t 
)

Point addition.

Parameters
[in]statePointer to the working state
[out]rResulting point R = S + T
[in]sFirst operand
[in]tSecond operand

Definition at line 1094 of file ec.c.

◆ ecFullSub()

void ecFullSub ( EcState state,
EcPoint3 r,
const EcPoint3 s,
const EcPoint3 t 
)

Point subtraction.

Parameters
[in]statePointer to the working state
[out]rResulting point R = S - T
[in]sFirst operand
[in]tSecond operand

Definition at line 1151 of file ec.c.

◆ ecGenerateKeyPair()

__weak_func error_t ecGenerateKeyPair ( const PrngAlgo prngAlgo,
void *  prngContext,
const EcCurve curve,
EcPrivateKey privateKey,
EcPublicKey publicKey 
)

EC key pair generation.

Parameters
[in]prngAlgoPRNG algorithm
[in]prngContextPointer to the PRNG context
[in]curveElliptic curve parameters
[out]privateKeyEC private key
[out]publicKeyEC public key (optional parameter)
Returns
Error code

Definition at line 117 of file ec.c.

◆ ecGeneratePrivateKey()

error_t ecGeneratePrivateKey ( const PrngAlgo prngAlgo,
void *  prngContext,
const EcCurve curve,
EcPrivateKey privateKey 
)

EC private key generation.

Parameters
[in]prngAlgoPRNG algorithm
[in]prngContextPointer to the PRNG context
[in]curveElliptic curve parameters
[out]privateKeyEC private key
Returns
Error code

Definition at line 158 of file ec.c.

◆ ecGeneratePublicKey()

error_t ecGeneratePublicKey ( const EcPrivateKey privateKey,
EcPublicKey publicKey 
)

Derive the public key from an EC private key.

Parameters
[in]privateKeyEC private key
[out]publicKeyEC public key
Returns
Error code

Definition at line 200 of file ec.c.

◆ ecImportPoint()

error_t ecImportPoint ( const EcCurve curve,
EcPoint r,
const uint8_t *  data,
size_t  length 
)

Convert an octet string to an EC point.

Parameters
[in]curveElliptic curve parameters
[out]rEC point resulting from the conversion
[in]dataPointer to the octet string
[in]lengthLength of the octet string
Returns
Error code

Definition at line 626 of file ec.c.

◆ ecImportPrivateKey()

error_t ecImportPrivateKey ( EcPrivateKey key,
const EcCurve curve,
const uint8_t *  data,
size_t  length 
)

Import an EC private key.

Parameters
[out]keyEC private key
[in]dataPointer to the octet string
[in]lengthLength of the octet string, in bytes
Returns
Error code

Definition at line 490 of file ec.c.

◆ ecImportPublicKey()

error_t ecImportPublicKey ( EcPublicKey key,
const EcCurve curve,
const uint8_t *  data,
size_t  length,
EcPublicKeyFormat  format 
)

Import an EC public key.

Parameters
[out]keyEC public key
[in]curveElliptic curve parameters
[in]dataPointer to the octet string
[in]lengthLength of the octet string, in bytes
[in]formatEC public key format (X9.63 or raw format)
Returns
Error code

Definition at line 263 of file ec.c.

◆ ecInitPrivateKey()

void ecInitPrivateKey ( EcPrivateKey key)

Initialize an EC private key.

Parameters
[in]keyPointer to the EC private key to initialize

Definition at line 80 of file ec.c.

◆ ecInitPublicKey()

void ecInitPublicKey ( EcPublicKey key)

Initialize an EC public key.

Parameters
[in]keyPointer to the EC public key to initialize

Definition at line 52 of file ec.c.

◆ ecIsPointAffine()

__weak_func bool_t ecIsPointAffine ( const EcCurve curve,
const EcPoint s 
)

Check whether the affine point S is on the curve.

Parameters
[in]curveElliptic curve parameters
[in]sAffine representation of the point
Returns
TRUE if the affine point S is on the curve, else FALSE

Definition at line 798 of file ec.c.

◆ ecMulFast()

__weak_func error_t ecMulFast ( const EcCurve curve,
EcPoint3 r,
const uint32_t *  d,
const EcPoint3 s 
)

Scalar multiplication (fast calculation)

Parameters
[in]curveElliptic curve parameters
[out]rResulting point R = d.S
[in]dAn integer d such as 0 <= d < q
[in]sEC point
Returns
Error code

Definition at line 1181 of file ec.c.

◆ ecMulRegular()

__weak_func error_t ecMulRegular ( const EcCurve curve,
EcPoint3 r,
const uint32_t *  d,
const EcPoint3 s 
)

Scalar multiplication (regular calculation)

Parameters
[in]curveElliptic curve parameters
[out]rResulting point R = d.S
[in]dAn integer d such as 0 <= d < q
[in]sEC point
Returns
Error code

Definition at line 1312 of file ec.c.

◆ ecProjectify()

void ecProjectify ( const EcCurve curve,
EcPoint3 r,
const EcPoint s 
)

Compute projective representation.

Parameters
[in]curveElliptic curve parameters
[out]rProjective representation of the point
[in]sAffine representation of the point

Definition at line 720 of file ec.c.

◆ ecTwinMul()

__weak_func error_t ecTwinMul ( const EcCurve curve,
EcPoint3 r,
const uint32_t *  d0,
const EcPoint3 s,
const uint32_t *  d1,
const EcPoint3 t 
)

Twin multiplication.

Parameters
[in]curveElliptic curve parameters
[out]rResulting point R = d0.S + d1.T
[in]d0An integer d such as 0 <= d0 < q
[in]sEC point
[in]d1An integer d such as 0 <= d1 < q
[in]tEC point
Returns
Error code

Definition at line 1418 of file ec.c.

Variable Documentation

◆ EC_PUBLIC_KEY_OID

const uint8_t EC_PUBLIC_KEY_OID[7] = {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01}

Definition at line 44 of file ec.c.