PrivateKey
class PrivateKey extends RSA implements PrivateKey (View source)
Raw RSA Key Handler
Traits
Constants
ALGORITHM |
Algorithm Name |
ENCRYPTION_OAEP |
Use {@link http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding Optimal Asymmetric Encryption Padding} (OAEP) for encryption / decryption. Uses sha256 by default |
ENCRYPTION_PKCS1 |
Use PKCS#1 padding. 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. |
ENCRYPTION_NONE |
Do not use any padding 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. |
SIGNATURE_PSS |
Use the Probabilistic Signature Scheme for signing Uses sha256 and 0 as the salt length |
SIGNATURE_RELAXED_PKCS1 |
Use a relaxed version of PKCS#1 padding for signature verification |
SIGNATURE_PKCS1 |
Use PKCS#1 padding for signature verification |
Properties
static protected BigInteger | $zero | Precomputed Zero | from AsymmetricKey |
static protected BigInteger | $one | Precomputed One | from AsymmetricKey |
protected string | $format | Format of the loaded key | from AsymmetricKey |
protected Hash | $hash | Hash function | from AsymmetricKey |
static protected bool[] | $engines | Available Engines | from AsymmetricKey |
protected int | $encryptionPadding | Encryption padding mode | from RSA |
protected int | $signaturePadding | Signature padding mode | from RSA |
protected int | $hLen | Length of hash function output | from RSA |
protected int | $sLen | Length of salt | from RSA |
protected string | $label | Label | from RSA |
protected Hash | $mgfHash | Hash function for the Mask Generation Function | from RSA |
protected int | $mgfHLen | Length of MGF hash function output | from RSA |
protected BigInteger | $modulus | Modulus (ie. n) | from RSA |
protected BigInteger | $k | Modulus length | from RSA |
protected BigInteger | $exponent | Exponent (ie. e or d) | from RSA |
static protected bool | $enableBlinding | Enable Blinding? | from RSA |
static protected ?string | $configFile | OpenSSL configuration file name. | from RSA |
protected array | $primes | Primes for Chinese Remainder Theorem (ie. p and q) | |
protected array | $exponents | Exponents for Chinese Remainder Theorem (ie. dP and dQ) | |
protected array | $coefficients | Coefficients for Chinese Remainder Theorem (ie. qInv) | |
protected mixed | $publicExponent | Public Exponent |
Methods
Loads a private key
Load the key, assuming a specific format
Loads a private key
Loads parameters
Validate Plugin
Flag to use internal engine only (useful for unit testing)
Compute the pseudorandom k for signature generation, using the process specified for deterministic DSA.
Sets the smallest prime number in bits. Used for key generation
EMSA-PKCS1-V1_5-ENCODE (without NULL)
Determines which hashing function should be used for the mask generation function
Create a signature
Decryption
Returns the public key
Returns the private key
Details
in
RSA at line 517
protected
__construct()
Constructor
PublicKey and PrivateKey objects can only be created from abstract RSA class
in
RSA at line 503
static protected
initialize_static_variables()
Initialize static variables
static AsymmetricKey
load(string $key, string $password = false)
Load the key
PrivateKey
loadPrivateKey(string|array $key, string $password = '')
Loads a private key
PublicKey
loadPublicKey(string|array $key)
Loads a public key
AsymmetricKey
loadParameters(string|array $key)
Loads parameters
static AsymmetricKey
loadFormat(string $type, string $key, string $password = false)
Load the key, assuming a specific format
PrivateKey
loadPrivateKeyFormat(string $type, string $key, string $password = false)
Loads a private key
PublicKey
loadPublicKeyFormat(string $type, string $key)
Loads a public key
AsymmetricKey
loadParametersFormat(string $type, string|array $key)
Loads parameters
static protected mixed
validatePlugin(string $format, string $type, string $method = NULL)
Validate Plugin
static array
getSupportedKeyFormats()
Returns a list of supported formats.
static bool
addFileFormat(string $fullname)
Add a fileformat plugin
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.
mixed
getLoadedFormat()
Returns the format of the loaded key.
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.
null|string
getComment()
Returns the key's comment
Not all key formats support comments. If you want to set a comment use toString()
static
useBestEngine()
Tests engine validity
static
useInternalEngine()
Flag to use internal engine only (useful for unit testing)
string
__toString()
__toString() magic method
in
RSA at line 727
withHash(string $hash)
Determines which hashing function should be used
Used with signature production / verification and (if the encryption mode is self::PADDING_OAEP) encryption and decryption.
getHash()
Returns the hash algorithm currently being used
protected string
computek(string $h1)
Compute the pseudorandom k for signature generation, using the process specified for deterministic DSA.
protected BigInteger
bits2int(string $in)
Bit String to Integer
in
RSA at line 284
static
setExponent(int $val)
Sets the public exponent for key generation
This will be 65537 unless changed.
in
RSA at line 297
static
setSmallestPrime(int $val)
Sets the smallest prime number in bits. Used for key generation
This will be 4096 unless changed.
in
RSA at line 310
static
setOpenSSLConfigPath(string $val)
Sets the OpenSSL config file path
Set to the empty string to use the default config file
in
RSA at line 324
static RSA
createKey(int $bits = 2048)
Create a private key
The public key can be extracted from the private key
in
RSA at line 457
static protected bool
onLoad(array $components)
OnLoad Handler
in
RSA at line 536
protected bool|string
i2osp(bool|BigInteger $x, int $xLen)
Integer-to-Octet-String primitive
See {@link http://tools.ietf.org/html/rfc3447#section-4.1 RFC3447#section-4.1}.
in
RSA at line 557
protected BigInteger
os2ip(string $x)
Octet-String-to-Integer primitive
See {@link http://tools.ietf.org/html/rfc3447#section-4.2 RFC3447#section-4.2}.
in
RSA at line 573
protected string
emsa_pkcs1_v1_5_encode(string $m, int $emLen)
EMSA-PKCS1-V1_5-ENCODE
See {@link http://tools.ietf.org/html/rfc3447#section-9.2 RFC3447#section-9.2}.
in
RSA at line 636
protected string
emsa_pkcs1_v1_5_encode_without_null(string $m, int $emLen)
EMSA-PKCS1-V1_5-ENCODE (without NULL)
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"
in
RSA at line 691
protected string
mgf1(string $mgfSeed, int $maskLen)
MGF1
See {@link http://tools.ietf.org/html/rfc3447#appendix-B.2.1 RFC3447#appendix-B.2.1}.
in
RSA at line 713
int
getLength()
Returns the key size
More specifically, this returns the size of the modulo in bits.
in
RSA at line 763
withMGFHash(string $hash)
Determines which hashing function should be used for the mask generation function
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.
in
RSA at line 795
getMGFHash()
Returns the MGF hash algorithm currently being used
in
RSA at line 813
withSaltLength(int $sLen)
Determines the salt length
Used by RSA::PADDING_PSS
To quote from {@link http://tools.ietf.org/html/rfc3447#page-38 RFC3447#page-38}:
Typical salt lengths in octets are hLen (the length of the output of the hash function Hash) and 0.
in
RSA at line 825
getSaltLength()
Returns the salt length currently being used
in
RSA at line 845
withLabel(string $label)
Determines the label
Used by RSA::PADDING_OAEP
To quote from {@link http://tools.ietf.org/html/rfc3447#page-17 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.
in
RSA at line 857
getLabel()
Returns the label currently being used
in
RSA at line 870
withPadding(int $padding)
Determines the padding modes
Example: $key->withPadding(RSA::ENCRYPTION_PKCS1 | RSA::SIGNATURE_PKCS1);
in
RSA at line 919
getPadding()
Returns the padding currently being used
in
RSA at line 937
string
getEngine()
Returns the current engine being used
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
in
RSA at line 949
static
enableBlinding()
Enable RSA Blinding
in
RSA at line 959
static
disableBlinding()
Disable RSA Blinding
withPassword(string|bool $password = false)
Sets the password
Private keys can be encrypted with a password. To unset the password, pass in the empty string or false. Or rather, pass in $password such that empty($password) && !is_string($password) is true.
protected BigInteger
exponentiate(BigInteger $x)
Exponentiate
sign($message)
Create a signature
bool|string
decrypt(string $ciphertext)
Decryption
getPublicKey()
Returns the public key
toString($type, array $options = [])
Returns the private key