---
name: "libressl-EC_KEY_new"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-EC_KEY_new"
---

# libressl-EC\_KEY\_new(3)

create, destroy and manipulate EC\_KEY objects

## NAME

**EC\_KEY\_new**, **EC\_KEY\_get\_flags**, **EC\_KEY\_set\_flags**, **EC\_KEY\_clear\_flags**, **EC\_KEY\_new\_by\_curve\_name**, **EC\_KEY\_free**, **EC\_KEY\_copy**, **EC\_KEY\_dup**, **EC\_KEY\_up\_ref**, **EC\_KEY\_get0\_group**, **EC\_KEY\_set\_group**, **EC\_KEY\_get0\_private\_key**, **EC\_KEY\_set\_private\_key**, **EC\_KEY\_get0\_public\_key**, **EC\_KEY\_set\_public\_key**, **EC\_KEY\_get\_enc\_flags**, **EC\_KEY\_set\_enc\_flags**, **EC\_KEY\_get\_conv\_form**, **EC\_KEY\_set\_conv\_form**, **EC\_KEY\_set\_asn1\_flag**, **EC\_KEY\_precompute\_mult**, **EC\_KEY\_generate\_key**, **EC\_KEY\_check\_key**, **EC\_KEY\_set\_public\_key\_affine\_coordinates**, **EC\_KEY\_print**, **EC\_KEY\_print\_fp** – create, destroy and manipulate EC\_KEY objects

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/ec.h\>**

**#include \<openssl/bn.h\>**

*EC\_KEY* *\**\

**EC\_KEY\_new**(*void*);

*int*\

**EC\_KEY\_get\_flags**(*const EC\_KEY \*key*);

*void*\

**EC\_KEY\_set\_flags**(*EC\_KEY \*key*, *int flags*);

*void*\

**EC\_KEY\_clear\_flags**(*EC\_KEY \*key*, *int flags*);

*EC\_KEY* *\**\

**EC\_KEY\_new\_by\_curve\_name**(*int nid*);

*void*\

**EC\_KEY\_free**(*EC\_KEY \*key*);

*EC\_KEY* *\**\

**EC\_KEY\_copy**(*EC\_KEY \*dst*, *const EC\_KEY \*src*);

*EC\_KEY* *\**\

**EC\_KEY\_dup**(*const EC\_KEY \*src*);

*int*\

**EC\_KEY\_up\_ref**(*EC\_KEY \*key*);

*const* *EC\_GROUP* *\**\

**EC\_KEY\_get0\_group**(*const EC\_KEY \*key*);

*int*\

**EC\_KEY\_set\_group**(*EC\_KEY \*key*, *const EC\_GROUP \*group*);

*const* *BIGNUM* *\**\

**EC\_KEY\_get0\_private\_key**(*const EC\_KEY \*key*);

*int*\

**EC\_KEY\_set\_private\_key**(*EC\_KEY \*key*, *const BIGNUM \*prv*);

*const* *EC\_POINT* *\**\

**EC\_KEY\_get0\_public\_key**(*const EC\_KEY \*key*);

*int*\

**EC\_KEY\_set\_public\_key**(*EC\_KEY \*key*, *const EC\_POINT \*pub*);

*unsigned* *int*\

**EC\_KEY\_get\_enc\_flags**(*const EC\_KEY \*key*);

*void*\

**EC\_KEY\_set\_enc\_flags**(*EC\_KEY \*key*, *unsigned int flags*);

*point\_conversion\_form\_t*\

**EC\_KEY\_get\_conv\_form**(*const EC\_KEY \*key*);

*void*\

**EC\_KEY\_set\_conv\_form**(*EC\_KEY \*key*, *point\_conversion\_form\_t cform*);

*void*\

**EC\_KEY\_set\_asn1\_flag**(*EC\_KEY \*key*, *int asn1\_flag*);

*int*\

**EC\_KEY\_precompute\_mult**(*EC\_KEY \*key*, *BN\_CTX \*ctx*);

*int*\

**EC\_KEY\_generate\_key**(*EC\_KEY \*key*);

*int*\

**EC\_KEY\_check\_key**(*const EC\_KEY \*key*);

*int*\

**EC\_KEY\_set\_public\_key\_affine\_coordinates**(*EC\_KEY \*key*, *BIGNUM \*x*, *BIGNUM \*y*);

*int*\

**EC\_KEY\_print**(*BIO \*bp*, *const EC\_KEY \*key*, *int off*);

*int*\

**EC\_KEY\_print\_fp**(*FILE \*fp*, *const EC\_KEY \*key*, *int off*);

## DESCRIPTION

An *EC\_KEY* represents a public key and (optionally) an associated private key. The public key is a point on a curve represented by an *EC\_POINT*, see EC\_POINT\_new(3). The private key is simply a *BIGNUM*, see BN\_new(3).

A new *EC\_KEY* (with no associated curve) can be constructed by calling **EC\_KEY\_new**(). The reference count for the newly created *EC\_KEY* is initially set to 1. A curve can be associated with the *EC\_KEY* by calling **EC\_KEY\_set\_group**().

Alternatively a new *EC\_KEY* can be constructed by calling **EC\_KEY\_new\_by\_curve\_name**() and supplying the *nid* of the associated curve. Refer to EC\_GROUP\_new\_by\_curve\_name(3) for a description of curve names. This function simply wraps calls to **EC\_KEY\_new**() and **EC\_GROUP\_new\_by\_curve\_name**().

Calling **EC\_KEY\_free**() decrements the reference count for the *EC\_KEY* object and, if it has dropped to zero, then frees the memory associated with it. If *key* is a NULL pointer, no action occurs.

**EC\_KEY\_copy**() copies the contents of the *EC\_KEY* in *src* into *dst*.

**EC\_KEY\_dup**() creates a new *EC\_KEY* object and copies *src* into it.

**EC\_KEY\_up\_ref**() increments the reference count associated with the *EC\_KEY* object.

**EC\_KEY\_generate\_key**() generates a new public and private key for the supplied *key* object. *key* must have an *EC\_GROUP* object associated with it before calling this function. The private key is a random integer (0 \< priv\_key \< order, where order is the order of the *EC\_GROUP* object). The public key is an *EC\_POINT* on the curve calculated by multiplying the generator for the curve by the private key.

**EC\_KEY\_check\_key**() performs various sanity checks on the *EC\_KEY* object to confirm that it is valid.

**EC\_KEY\_set\_public\_key\_affine\_coordinates**() sets the public key for *key* based on its affine coordinates, i.e. it constructs an *EC\_POINT* object based on the supplied *x* and *y* values and sets the public key to be this *EC\_POINT*. It also performs certain sanity checks on the key to confirm that it is valid.

The functions **EC\_KEY\_get0\_group**(), **EC\_KEY\_set\_group**(), **EC\_KEY\_get0\_private\_key**(), **EC\_KEY\_set\_private\_key**(), **EC\_KEY\_get0\_public\_key**(), and **EC\_KEY\_set\_public\_key**() get and set the *EC\_GROUP* object, the private key and the *EC\_POINT* public key for the *key*, respectively. The setters copy the group and key objects without sanity checks and it is the caller's responsibility to ensure that the resulting key is valid, for example using **EC\_KEY\_check\_key**().

The functions **EC\_KEY\_get\_enc\_flags**() and **EC\_KEY\_set\_enc\_flags**() get and set the value of the encoding flags for the *key*. There are two encoding flags currently defined: EC\_PKEY\_NO\_PARAMETERS and EC\_PKEY\_NO\_PUBKEY. These flags define the behaviour of how the *key* is converted into ASN.1 in a call to **i2d\_ECPrivateKey**(). If EC\_PKEY\_NO\_PARAMETERS is set then the public parameters for the curve are not encoded along with the private key. If EC\_PKEY\_NO\_PUBKEY is set then the public key is not encoded along with the private key.

The format of the external representation of the public key written by i2d\_ECPrivateKey(3), such as whether it is stored in a compressed form or not, is described by the point\_conversion\_form. See EC\_POINT\_point2oct(3) for a description of point\_conversion\_form.

When reading a private key encoded without an associated public key, for example if EC\_PKEY\_NO\_PUBKEY was used, d2i\_ECPrivateKey(3) generates the missing public key automatically. Private keys encoded without parameters, for example if EC\_PKEY\_NO\_PARAMETERS was used, cannot be loaded using d2i\_ECPrivateKey(3).

The functions **EC\_KEY\_get\_conv\_form**() and **EC\_KEY\_set\_conv\_form**() get and set the point\_conversion\_form for the *key*. For a description of point\_conversion\_form refer to EC\_POINT\_point2oct(3).

**EC\_KEY\_set\_flags**() sets the flags in the *flags* parameter on the *EC\_KEY* object. Any flags that are already set are left set. The currently defined standard flags are EC\_FLAG\_NON\_FIPS\_ALLOW and EC\_FLAG\_FIPS\_CHECKED. In addition there is the ECDH-specific flag EC\_FLAG\_COFACTOR\_ECDH. **EC\_KEY\_get\_flags**() returns the current flags that are set for this *EC\_KEY*. **EC\_KEY\_clear\_flags**() clears the flags indicated by the *flags* parameter. All other flags are left in their existing state.

**EC\_KEY\_set\_asn1\_flag**() sets the asn1\_flag on the underlying *EC\_GROUP* object (if set). Refer to EC\_GROUP\_get\_curve\_name(3) for further information on the asn1\_flag.

**EC\_KEY\_precompute\_mult**() stores multiples of the underlying *EC\_GROUP* generator for faster point multiplication. See also EC\_POINT\_add(3).

**EC\_KEY\_print**() and **EC\_KEY\_print\_fp**() print out the content of *key* to the *BIO* *bp* or to the *FILE* pointer *fp*, respectively. Each line is indented by *indent* spaces.

## RETURN VALUES

**EC\_KEY\_new**(), **EC\_KEY\_new\_by\_curve\_name**(), and **EC\_KEY\_dup**() return a pointer to the newly created *EC\_KEY* *object* or NULL on error.

**EC\_KEY\_get\_flags**() returns the flags associated with the *EC\_KEY* *object*.

**EC\_KEY\_copy**() returns a pointer to the destination key or NULL on error. In the latter case, part of the content may already have been copied.

**EC\_KEY\_up\_ref**(), **EC\_KEY\_set\_group**(), **EC\_KEY\_set\_private\_key**(), **EC\_KEY\_set\_public\_key**(), **EC\_KEY\_precompute\_mult**(), **EC\_KEY\_generate\_key**(), **EC\_KEY\_check\_key**(), **EC\_KEY\_set\_public\_key\_affine\_coordinates**(), **EC\_KEY\_print**(), and **EC\_KEY\_print\_fp**() return 1 on success or 0 on error.

**EC\_KEY\_get0\_group**() returns the *EC\_GROUP* associated with the *EC\_KEY*.

**EC\_KEY\_get0\_private\_key**() and **EC\_KEY\_get0\_public\_key**() return the private or public keys, respectively, associated with the *EC\_KEY*.

**EC\_KEY\_get\_enc\_flags**() returns the value of the current encoding flags for the *EC\_KEY*.

**EC\_KEY\_get\_conv\_form**() returns the point\_conversion\_form for the *EC\_KEY*.

## SEE ALSO

d2i\_ECPKParameters(3), EC\_GROUP\_check(3), EC\_GROUP\_get\_curve\_name(3), EC\_GROUP\_new\_by\_curve\_name(3), EC\_GROUP\_new\_curve\_GFp(3), EC\_KEY\_METHOD\_new(3), EC\_POINT\_add(3), EC\_POINT\_get\_affine\_coordinates(3), EC\_POINT\_point2oct(3), ECDH\_compute\_key(3), ECDSA\_SIG\_new(3), EVP\_PKEY\_set1\_EC\_KEY(3)

## HISTORY

**EC\_KEY\_new**(), **EC\_KEY\_new\_by\_curve\_name**(), **EC\_KEY\_free**(), **EC\_KEY\_copy**(), **EC\_KEY\_dup**(), **EC\_KEY\_up\_ref**(), **EC\_KEY\_get0\_group**(), **EC\_KEY\_set\_group**(), **EC\_KEY\_get0\_private\_key**(), **EC\_KEY\_set\_private\_key**(), **EC\_KEY\_get0\_public\_key**(), **EC\_KEY\_set\_public\_key**(), **EC\_KEY\_get\_enc\_flags**(), **EC\_KEY\_set\_enc\_flags**(), **EC\_KEY\_get\_conv\_form**(), **EC\_KEY\_set\_conv\_form**(), **EC\_KEY\_set\_asn1\_flag**(), **EC\_KEY\_precompute\_mult**(), **EC\_KEY\_generate\_key**(), **EC\_KEY\_check\_key**(), **EC\_KEY\_print**(), and **EC\_KEY\_print\_fp**() first appeared in OpenSSL 0.9.8 and have been available since OpenBSD 4.5.

**EC\_KEY\_get\_flags**(), **EC\_KEY\_set\_flags**(), **EC\_KEY\_clear\_flags**(), and **EC\_KEY\_set\_public\_key\_affine\_coordinates**() first appeared in OpenSSL 1.0.1 and have been available since OpenBSD 5.3.
