---
name: "libressl-ECDSA_SIG_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-ECDSA_SIG_new"
---

# libressl-ECDSA\_SIG\_new(3)

Elliptic Curve Digital Signature Algorithm

## NAME

**ECDSA\_SIG\_new**, **ECDSA\_SIG\_free**, **ECDSA\_SIG\_get0**, **ECDSA\_SIG\_get0\_r**, **ECDSA\_SIG\_get0\_s**, **ECDSA\_SIG\_set0**, **i2d\_ECDSA\_SIG**, **d2i\_ECDSA\_SIG**, **ECDSA\_size**, **ECDSA\_sign**, **ECDSA\_verify**, **ECDSA\_do\_sign**, **ECDSA\_do\_verify** – Elliptic Curve Digital Signature Algorithm

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

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

*ECDSA\_SIG* *\**\

**ECDSA\_SIG\_new**(*void*);

*void*\

**ECDSA\_SIG\_free**(*ECDSA\_SIG \*sig*);

*void*\

**ECDSA\_SIG\_get0**(*const ECDSA\_SIG \*sig*, *const BIGNUM \*\*r*, *const BIGNUM \*\*s*);

*const* *BIGNUM* *\**\

**ECDSA\_SIG\_get0\_r**(*const ECDSA\_SIG \*sig*);

*const* *BIGNUM* *\**\

**ECDSA\_SIG\_get0\_s**(*const ECDSA\_SIG \*sig*);

*int*\

**ECDSA\_SIG\_set0**(*ECDSA\_SIG \*sig*, *BIGNUM \*r*, *BIGNUM \*s*);

*int*\

**i2d\_ECDSA\_SIG**(*const ECDSA\_SIG \*sig\_in*, *unsigned char \*\*der\_out*);

*ECDSA\_SIG* *\**\

**d2i\_ECDSA\_SIG**(*ECDSA\_SIG \*\*sig\_out*, *const unsigned char \*\*der\_in*, *long len*);

*int*\

**ECDSA\_size**(*const EC\_KEY \*eckey*);

*int*\

**ECDSA\_sign**(*int type*, *const unsigned char \*dgst*, *int dgstlen*, *unsigned char \*sig*, *unsigned int \*siglen*, *EC\_KEY \*eckey*);

*int*\

**ECDSA\_verify**(*int type*, *const unsigned char \*dgst*, *int dgstlen*, *const unsigned char \*sig*, *int siglen*, *EC\_KEY \*eckey*);

*ECDSA\_SIG* *\**\

**ECDSA\_do\_sign**(*const unsigned char \*dgst*, *int dgst\_len*, *EC\_KEY \*eckey*);

*int*\

**ECDSA\_do\_verify**(*const unsigned char \*dgst*, *int dgst\_len*, *const ECDSA\_SIG \*sig*, *EC\_KEY\* eckey*);

## DESCRIPTION

These functions provide a low level interface to ECDSA. Most applications should use the higher level EVP interface such as EVP\_DigestSignInit(3) or EVP\_DigestVerifyInit(3) instead. Creation of the required *EC\_KEY* objects is described in EC\_KEY\_new(3).

The *ECDSA\_SIG* structure consists of two *BIGNUM*s for the *r* and *s* value of an ECDSA signature (see X9.62 or FIPS 186-2).

```
struct {
	BIGNUM *r;
	BIGNUM *s;
} ECDSA_SIG;
```

**ECDSA\_SIG\_new**() allocates a new *ECDSA\_SIG* structure (note: this function also allocates the *BIGNUM*s) and initializes it.

**ECDSA\_SIG\_free**() frees the *ECDSA\_SIG* structure *sig*.

**ECDSA\_SIG\_get0**() retrieves internal pointers the *r* and *s* values contained in *sig*. The values *r* and *s* can also be retrieved separately by the corresponding function **ECDSA\_SIG\_get0\_r**() and **ECDSA\_SIG\_get0\_s**(), respectively.

**ECDSA\_SIG\_set0**() sets the *r* and *s* values in *sig*. Calling this function transfers the memory management of the values to *sig*. Therefore, the values that have been passed in should not be freed by the caller.

**i2d\_ECDSA\_SIG**() creates the DER encoding of the ECDSA signature *sig\_in* and writes the encoded signature to \**der\_out*. **d2i\_ECDSA\_SIG**() decodes the DER-encoded signature stored in the buffer \**der\_in* which is *len* bytes long into \**sig\_out*. For details about the semantics, examples, caveats, and bugs, see ASN1\_item\_d2i(3).

**ECDSA\_size**() returns the maximum length of a DER-encoded ECDSA signature created with the private EC key *eckey*.

**ECDSA\_sign**() computes a digital signature of the *dgstlen* bytes hash value *dgst* using the private EC key *eckey*. The DER-encoded signature is stored in *sig* and its length is returned in *siglen*. Note: *sig* must point to **ECDSA\_size**() bytes of memory. The parameter *type* is ignored.

**ECDSA\_verify**() verifies that the signature in *sig* of size *siglen* is a valid ECDSA signature of the hash value *dgst* of size *dgstlen* using the public key *eckey*. The parameter *type* is ignored.

**ECDSA\_do\_sign**() computes a digital signature of the *dgst\_len* bytes hash value *dgst* using the private key *eckey*. The signature is returned in a newly allocated *ECDSA\_SIG* structure (or NULL on error).

**ECDSA\_do\_verify**() verifies that the signature *sig* is a valid ECDSA signature of the hash value *dgst* of size *dgst\_len* using the public key *eckey*.

## RETURN VALUES

**ECDSA\_SIG\_new**() returns the new *ECDSA\_SIG* object or NULL if an error occurs.

**i2d\_ECDSA\_SIG**() returns the number of bytes successfully encoded or a negative value if an error occurs.

**d2i\_ECDSA\_SIG**() returns a pointer to the decoded *ECDSA\_SIG* structure or NULL if an error occurs.

**ECDSA\_size**() returns the maximum length signature or 0 on error.

**ECDSA\_SIG\_get0\_r**() and **ECDSA\_SIG\_get0\_s**() return a pointer owned by the *ECDSA\_SIG* object if it has been set or NULL otherwise.

**ECDSA\_SIG\_set0**() and **ECDSA\_sign**() return 1 if successful or 0 on error.

**ECDSA\_do\_sign**() returns a pointer to an allocated *ECDSA\_SIG* structure or NULL on error.

**ECDSA\_verify**() and **ECDSA\_do\_verify**() return 1 for a valid signature, 0 for an invalid signature and -1 on error. The error codes can be obtained by ERR\_get\_error(3).

## EXAMPLES

Creating an ECDSA signature of given SHA-384 hash value using the named curve secp384r1.

First step: create an *EC\_KEY* object. This part is *not* ECDSA specific.

```
int ret;
ECDSA_SIG *sig;
EC_KEY *eckey;
eckey = EC_KEY_new_by_curve_name(NID_secp384r1);
if (eckey == NULL) {
	/* error */
}
if (!EC_KEY_generate_key(eckey)) {
	/* error */
}
```

Second step: compute the ECDSA signature of a SHA-384 hash value using **ECDSA\_do\_sign**()

```
sig = ECDSA_do_sign(digest, SHA384_DIGEST_LENGTH, eckey);
if (sig == NULL) {
	/* error */
}
```

or using **ECDSA\_sign**()

```
unsigned char *buffer, *pp;
int buf_len;
buf_len = ECDSA_size(eckey);
buffer  = malloc(buf_len);
pp = buffer;
if (!ECDSA_sign(0, dgst, dgstlen, pp, &buf_len, eckey) {
	/* error */
}
```

Third step: verify the created ECDSA signature using **ECDSA\_do\_verify**()

```
ret = ECDSA_do_verify(digest, SHA384_DIGEST_LENGTH, sig, eckey);
```

or using **ECDSA\_verify**()

```
ret = ECDSA_verify(0, digest, SHA384_DIGEST_LENGTH, buffer, buf_len, eckey);
```

and finally evaluate the return value:

```
if (ret == -1) {
	/* error */
} else if (ret == 0) {
	/* incorrect signature */
} else {
	/* ret == 1 */
	/* signature ok */
}
```

## SEE ALSO

[crypto(3)](/crypto/3.md), d2i\_ECPKParameters(3), DSA\_new(3), EC\_GROUP\_new\_by\_curve\_name(3), EC\_KEY\_METHOD\_new(3), EC\_KEY\_new(3), EC\_KEY\_set\_ex\_data(3), EVP\_DigestSignInit(3), EVP\_DigestVerifyInit(3), RSA\_new(3)

## STANDARDS

ANSI X9.62, US Federal Information Processing Standard FIPS 186-5 (Digital Signature Standard, DSS)

## HISTORY

**ECDSA\_SIG\_new**(), **ECDSA\_SIG\_free**(), **i2d\_ECDSA\_SIG**(), **d2i\_ECDSA\_SIG**(), **ECDSA\_size**(), **ECDSA\_sign**(), **ECDSA\_verify**(), **ECDSA\_do\_sign**(), and **ECDSA\_do\_verify**() first appeared in OpenSSL 0.9.8 and have been available since OpenBSD 4.5.

**ECDSA\_SIG\_get0**() and **ECDSA\_SIG\_set0**() first appeared in OpenSSL 1.1.0 and have been available since OpenBSD 6.3. **ECDSA\_SIG\_get0\_r**() and **ECDSA\_SIG\_get0\_s**() first appeared in OpenSSL 1.1.1 and have been available since OpenBSD 7.1.

## AUTHORS

Nils Larsch for the OpenSSL project.
