---
name: "libressl-RSA_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-RSA_new"
---

# libressl-RSA\_new(3)

allocate and free RSA objects

## NAME

**RSA\_new**, **RSAPrivateKey\_dup**, **RSAPublicKey\_dup**, **RSA\_up\_ref**, **RSA\_free** – allocate and free RSA objects

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

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

*RSA* *\**\

**RSA\_new**(*void*);

*RSA* *\**\

**RSAPrivateKey\_dup**(*RSA \*rsa*);

*RSA* *\**\

**RSAPublicKey\_dup**(*RSA \*rsa*);

*int*\

**RSA\_up\_ref**(*RSA \*rsa*);

*void*\

**RSA\_free**(*RSA \*rsa*);

## DESCRIPTION

The RSA functions implement RSA public key encryption and signatures as defined in PKCS #1 v2.0 (RFC 2437).

**RSA\_new**() allocates and initializes an *RSA* structure, setting the reference count to 1. It is equivalent to calling RSA\_new\_method(3) with a NULL argument.

**RSAPrivateKey\_dup**() calls **RSA\_new**() and copies the public and private key components from *rsa* into the new structure. **RSAPublicKey\_dup**() does the same except that it copies the public key components only.

**RSA\_up\_ref**() increments the reference count by 1.

**RSA\_free**() decrements the reference count by 1. If it reaches 0, it calls the optional *finish* function set up with RSA\_meth\_set\_finish(3) and frees the *RSA* structure and its components. The key is erased before the memory is returned to the system. If *rsa* is a NULL pointer, no action occurs.

The *RSA* structure consists of several *BIGNUM* components. It can contain public as well as private RSA keys:

```
typedef struct {
	BIGNUM *n;		// public modulus
	BIGNUM *e;		// public exponent
	BIGNUM *d;		// private exponent
	BIGNUM *p;		// secret prime factor
	BIGNUM *q;		// secret prime factor
	BIGNUM *dmp1;		// d mod (p-1)
	BIGNUM *dmq1;		// d mod (q-1)
	BIGNUM *iqmp;		// q^-1 mod p
	// ...
} RSA;
```

In public keys, the private exponent *d* and the related secret values *p*, *q*, *dmp1*, *dmp2*,,,, and *iqmp* are NULL.

*p*, *q*, *dmp1*, *dmq1*, and *iqmp* may be NULL in private keys, but the RSA operations are much faster when these values are available.

Note that RSA keys may use non-standard *RSA\_METHOD* implementations. In some cases, these *BIGNUM* values will not be used by the implementation or may be used for alternative data storage. For this reason, applications should generally avoid using *RSA* structure elements directly and instead use API functions to query or modify keys.

## RETURN VALUES

**RSA\_new**(), **RSAPrivateKey\_dup**(), and **RSAPublicKey\_dup**() return a pointer to the newly allocated structure, or NULL if an error occurs. An error code can be obtained by ERR\_get\_error(3).

**RSA\_up\_ref**() returns 1 for success or 0 for failure.

## SEE ALSO

BN\_new(3), [crypto(3)](/crypto/3.md), d2i\_RSAPublicKey(3), DH\_new(3), DSA\_new(3), EVP\_PKEY\_set1\_RSA(3), RSA\_blinding\_on(3), RSA\_check\_key(3), RSA\_generate\_key(3), RSA\_get0\_key(3), RSA\_get\_ex\_new\_index(3), RSA\_meth\_new(3), RSA\_padding\_add\_PKCS1\_type\_1(3), RSA\_pkey\_ctx\_ctrl(3), RSA\_print(3), RSA\_private\_encrypt(3), RSA\_PSS\_PARAMS\_new(3), RSA\_public\_encrypt(3), RSA\_security\_bits(3), RSA\_set\_method(3), RSA\_sign(3), RSA\_sign\_ASN1\_OCTET\_STRING(3), RSA\_size(3)

## STANDARDS

SSL, PKCS #1 v2.0

RSA was covered by a US patent which expired in September 2000.

## HISTORY

**RSA\_new**() and **RSA\_free**() appeared in SSLeay 0.4 or earlier. **RSAPrivateKey\_dup**() first appeared in SSLeay 0.5.1 and **RSAPublicKey\_dup**() in SSLeay 0.5.2. These functions have been available since OpenBSD 2.4.

**RSA\_up\_ref**() first appeared in OpenSSL 0.9.7 and has been available since OpenBSD 3.2.
