RSA

Extends\phpseclib3\Crypt\Common\AsymmetricKey

Pure-PHP PKCS#1 compliant implementation of RSA.

author

Jim Wigginton terrafrost@php.net

package

Default

Methods

The constructor

__construct()
inherited

__toString() magic method

__toString(): string
inherited

Response

string

Add a fileformat plugin

addFileFormat(string $fullname): boolean
inheritedstatic

The plugin needs to either already be loaded or be auto-loadable. Loading a plugin whose shortname overwrite an existing shortname will overwrite the old plugin.

see \phpseclib3\Crypt\Common\AsymmetricKey::load()

Arguments

$fullname

string

Response

boolean

Bit String to Integer

bits2int(string $in): \phpseclib3\Math\BigInteger
inherited

Arguments

$in

string

Response

\phpseclib3\Math\BigInteger

Bit String to Octet String

bits2octets(string $in): string
inherited

Arguments

$in

string

Response

string

Compute the pseudorandom k for signature generation, using the process specified for deterministic DSA.

computek(string $h1): string
inherited

Arguments

$h1

string

Response

string

Create a private key

createKey(integer $bits = 2048): \phpseclib3\Crypt\RSA\PrivateKey
static

The public key can be extracted from the private key

Arguments

$bits

integer

Response

\phpseclib3\Crypt\RSA\PrivateKey

Disable RSA Blinding

disableBlinding()
static

EMSA-PKCS1-V1_5-ENCODE

emsa_pkcs1_v1_5_encode(string $m,integer $emLen): string
Throws
\LengthException

if the intended encoded message length is too short

Arguments

$m

string

$emLen

integer

Response

string

EMSA-PKCS1-V1_5-ENCODE (without NULL)

emsa_pkcs1_v1_5_encode_without_null(string $m,integer $emLen): string

Quoting https://tools.ietf.org/html/rfc8017#page-65,

"The parameters field associated with id-sha1, id-sha224, id-sha256, id-sha384, id-sha512, id-sha512/224, and id-sha512/256 should generally be omitted, but if present, it shall have a value of type NULL"

Arguments

$m

string

$emLen

integer

Response

string

Enable RSA Blinding

enableBlinding()
static

Returns the key's comment

getComment(): null|string
inherited

Not all key formats support comments. If you want to set a comment use toString()

Response

null|string

Returns the current engine being used

getEngine(): string

OpenSSL is only used in this class (and it's subclasses) for key generation Even then it depends on the parameters you're using. It's not used for multi-prime RSA nor is it used if the key length is outside of the range supported by OpenSSL

see self::useInternalEngine()self::useBestEngine()

Response

string

Returns the hash algorithm currently being used

getHash()
inherited

Returns the label currently being used

getLabel()

Returns the key size

getLength(): integer

More specifically, this returns the size of the modulo in bits.

Response

integer

Returns the format of the loaded key.

getLoadedFormat(): mixed
inherited

If the key that was loaded wasn't in a valid or if the key was auto-generated with RSA::createKey() then this will throw an exception.

see \phpseclib3\Crypt\Common\AsymmetricKey::load()

Response

mixed

Returns the MGF hash algorithm currently being used

getMGFHash()

Returns the padding currently being used

getPadding()

Returns the salt length currently being used

getSaltLength()

Returns a list of supported formats.

getSupportedKeyFormats(): array
inheritedstatic

Response

array

Integer-to-Octet-String primitive

i2osp(boolean|\phpseclib3\Math\BigInteger $x,integer $xLen): boolean|string

Arguments

$xLen

integer

Response

boolean|string

Initialize static variables

initialize_static_variables()
inheritedstatic

Integer to Octet String

int2octets(\phpseclib3\Math\BigInteger $v): string
inherited

Arguments

Response

string

Load the key

load(string $key,string $password = false): \phpseclib3\Crypt\Common\AsymmetricKey
inheritedstatic

Arguments

$key

string

$password

string

optional

Response

\phpseclib3\Crypt\Common\AsymmetricKey

Load the key, assuming a specific format

loadFormat(string $type,string $key,string $password = false): static
inheritedstatic

Arguments

$type

string

$key

string

$password

string

optional

Response

static

Loads parameters

loadParameters(string|array $key): \phpseclib3\Crypt\Common\AsymmetricKey
inheritedstatic

Arguments

$key

string|array

Response

\phpseclib3\Crypt\Common\AsymmetricKey

Loads parameters

loadParametersFormat(string $type,string|array $key): \phpseclib3\Crypt\Common\AsymmetricKey
inheritedstatic

Arguments

$type

string

$key

string|array

Response

\phpseclib3\Crypt\Common\AsymmetricKey

Load Plugins

loadPlugins(string $format)
inheritedstatic

Arguments

$format

string

Loads a private key

loadPrivateKey(string|array $key,string $password = ''): \phpseclib3\Crypt\Common\PrivateKey
inheritedstatic

Arguments

$key

string|array

$password

string

optional

Response

\phpseclib3\Crypt\Common\PrivateKey

Loads a private key

loadPrivateKeyFormat(string $type,string $key,string $password = false): \phpseclib3\Crypt\Common\PrivateKey
inheritedstatic

Arguments

$type

string

$key

string

$password

string

optional

Response

\phpseclib3\Crypt\Common\PrivateKey

Loads a public key

loadPublicKey(string|array $key): \phpseclib3\Crypt\Common\PublicKey
inheritedstatic

Arguments

$key

string|array

Response

\phpseclib3\Crypt\Common\PublicKey

Loads a public key

loadPublicKeyFormat(string $type,string $key): \phpseclib3\Crypt\Common\PublicKey
inheritedstatic

Arguments

$type

string

$key

string

Response

\phpseclib3\Crypt\Common\PublicKey

MGF1

mgf1(string $mgfSeed,integer $maskLen): string

Arguments

$mgfSeed

string

$maskLen

integer

Response

string

OnLoad Handler

onLoad(array $components): boolean
static

Arguments

$components

array

Response

boolean

Octet-String-to-Integer primitive

os2ip(string $x): \phpseclib3\Math\BigInteger

Arguments

$x

string

Response

\phpseclib3\Math\BigInteger

Sets the public exponent for key generation

setExponent(integer $val)
static

This will be 65537 unless changed.

Arguments

$val

integer

Sets the OpenSSL config file path

setOpenSSLConfigPath(string $val)
static

Set to the empty string to use the default config file

Arguments

$val

string

Sets the smallest prime number in bits. Used for key generation

setSmallestPrime(integer $val)
static

This will be 4096 unless changed.

Arguments

$val

integer

toString

toString(string $type,array $options = array()): string
inheritedabstract

Arguments

$type

string

$options

array

Response

string

Tests engine validity

useBestEngine()
inheritedstatic

Flag to use internal engine only (useful for unit testing)

useInternalEngine()
inheritedstatic

Validate Plugin

validatePlugin(string $format,string $type,string $method = null): mixed
inheritedstatic

Arguments

$format

string

$type

string

$method

string

optional

Response

mixed

Determines which hashing function should be used

withHash(string $hash)
inherited

Arguments

$hash

string

Determines the label

withLabel(string $label)

Used by RSA::PADDING_OAEP

To quote from RFC3447#page-17:

Both the encryption and the decryption operations of RSAES-OAEP take the value of a label L as input. In this version of PKCS #1, L is the empty string; other uses of the label are outside the scope of this document.

Arguments

$label

string

Determines which hashing function should be used for the mask generation function

withMGFHash(string $hash)

The mask generation function is used by self::PADDING_OAEP and self::PADDING_PSS and although it's best if Hash and MGFHash are set to the same thing this is not a requirement.

Arguments

$hash

string

Determines the padding modes

withPadding(integer $padding)

Example: $key->withPadding(RSA::ENCRYPTION_PKCS1 | RSA::SIGNATURE_PKCS1);

Arguments

$padding

integer

Determines the salt length

withSaltLength(integer $sLen)

Used by RSA::PADDING_PSS

To quote from RFC3447#page-38:

Typical salt lengths in octets are hLen (the length of the output of the hash function Hash) and 0.

Arguments

$sLen

integer

Constants

Algorithm Name

ALGORITHM
var

Use {@link http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding Optimal Asymmetric Encryption Padding} (OAEP) for encryption / decryption.

ENCRYPTION_OAEP

Uses sha256 by default

see

Use PKCS#1 padding.

ENCRYPTION_PKCS1

Although self::PADDING_OAEP / self::PADDING_PSS offers more security, including PKCS#1 padding is necessary for purposes of backwards compatibility with protocols (like SSH-1) written before OAEP's introduction.

see

Do not use any padding

ENCRYPTION_NONE

Although this method is not recommended it can none-the-less sometimes be useful if you're trying to decrypt some legacy stuff, if you're trying to diagnose why an encrypted message isn't decrypting, etc.

see

Use the Probabilistic Signature Scheme for signing

SIGNATURE_PSS

Uses sha256 and 0 as the salt length

see

Use a relaxed version of PKCS#1 padding for signature verification

SIGNATURE_RELAXED_PKCS1
see

Use PKCS#1 padding for signature verification

SIGNATURE_PKCS1
see

Properties

Encryption padding mode

encryptionPadding :integer
var

Type(s)

integer

Signature padding mode

signaturePadding :integer
var

Type(s)

integer

Length of hash function output

hLen :integer
var

Type(s)

integer

Length of salt

sLen :integer
var

Type(s)

integer

Label

label :string
var

Type(s)

string

Hash function for the Mask Generation Function

mgfHash :\phpseclib3\Crypt\Hash

Length of MGF hash function output

mgfHLen :integer
var

Type(s)

integer

Modulus (ie. n)

modulus :\phpseclib3\Math\BigInteger

Modulus length

k :\phpseclib3\Math\BigInteger

Exponent (ie. e or d)

exponent :\phpseclib3\Math\BigInteger

Default public exponent

defaultExponent :integer
static

Enable Blinding?

enableBlinding :boolean
static
var

Type(s)

boolean

OpenSSL configuration file name.

configFile :\phpseclib3\Crypt\?string
static
see
var

Type(s)

\phpseclib3\Crypt\?string

Smallest Prime

smallestPrime :integer
static

Per http://cseweb.ucsd.edu/~hovav/dist/survey.pdf#page=5, this number ought not result in primes smaller than 256 bits. As a consequence if the key you're trying to create is 1024 bits and you've set smallestPrime to 384 bits then you're going to get a 384 bit prime and a 640 bit prime (384 + 1024 % 384). At least if engine is set to self::ENGINE_INTERNAL. If Engine is set to self::ENGINE_OPENSSL then smallest Prime is ignored (ie. multi-prime RSA support is more intended as a way to speed up RSA key generation when there's a chance neither gmp nor OpenSSL are installed)

var

Type(s)

integer

Public Exponent

publicExponent :\phpseclib3\Math\BigInteger

Precomputed Zero

zero :\phpseclib3\Math\BigInteger
inheritedstatic

Precomputed One

one :\phpseclib3\Math\BigInteger
inheritedstatic

Format of the loaded key

format :string
inherited
var

Type(s)

string

Hash function

hash :\phpseclib3\Crypt\Hash
inherited

HMAC function

hmac :\phpseclib3\Crypt\Hash
inherited

Supported plugins (lower case)

plugins :array
inheritedstatic
see
var

Type(s)

array

Invisible plugins

invisiblePlugins :array
inheritedstatic
see
var

Type(s)

array

Available Engines

engines :array<mixed,boolean>
inheritedstatic
var

Type(s)

array<mixed,boolean>

Key Comment

comment :null|string
inherited
var

Type(s)

null|string