ENCRYPTION_OAEP
ENCRYPTION_OAEP
Use {@link http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding Optimal Asymmetric Encryption Padding} (OAEP) for encryption / decryption.
Uses sha1 by default.
Pure-PHP PKCS#1 compliant implementation of RSA.
PUBLIC_FORMAT_PKCS8
PKCS#1 formatted public key (encapsulated)
Used by PHP's openssl_public_encrypt() and openssl's rsautl (when -pubin is set)
Has the following header:
-----BEGIN PUBLIC KEY-----
Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8 is specific to private keys it's basically creating a DER-encoded wrapper for keys. This just extends that same concept to public keys (much like ssh-keygen)
$zero : \phpseclib\Math\BigInteger
Precomputed Zero
$one : \phpseclib\Math\BigInteger
Precomputed One
$modulus : \phpseclib\Math\BigInteger
Modulus (ie. n)
$k : \phpseclib\Math\BigInteger
Modulus length
$exponent : \phpseclib\Math\BigInteger
Exponent (ie. e or d)
$hash : \phpseclib\Crypt\Hash
Hash function
$mgfHash : \phpseclib\Crypt\Hash
Hash function for the Mask Generation Function
__construct() : \phpseclib\Crypt\RSA
The constructor
If you want to make use of the openssl extension, you'll need to set the mode manually, yourself. The reason \phpseclib\Crypt\RSA doesn't do it is because OpenSSL doesn't fail gracefully. openssl_pkey_new(), in particular, requires openssl.cnf be present somewhere and, unfortunately, the only real way to find out is too late.
createKey(integer $bits = 1024, integer $timeout = false, $partial = array())
Create public / private key pair
Returns an array with the following three elements:
integer | $bits | |
integer | $timeout | |
$partial |
loadKey(string|\phpseclib\Crypt\RSA|array $key, boolean|integer $type = false) : boolean
Loads a public or private key
Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
string|\phpseclib\Crypt\RSA|array | $key | |
boolean|integer | $type | optional |
setPublicKey(string $key = false, integer $type = false) : boolean
Defines the public key
Some private key formats define the public exponent and some don't. Those that don't define it are problematic when used in certain contexts. For example, in SSH-2, RSA authentication works by sending the public key along with a message signed by the private key to the server. The SSH-2 server looks the public key up in an index of public keys and if it's present then proceeds to verify the signature. Problem is, if your private key doesn't include the public exponent this won't work unless you manually add the public exponent. phpseclib tries to guess if the key being used is the public key but in the event that it guesses incorrectly you might still want to explicitly set the key as being public.
Do note that when a new key is loaded the index will be cleared.
Returns true on success, false on failure
string | $key | optional |
integer | $type | optional |
setPrivateKey(string $key = false, integer $type = false) : boolean
Defines the private key
If phpseclib guessed a private key was a public key and loaded it as such it might be desirable to force phpseclib to treat the key as a private key. This function will do that.
Do note that when a new key is loaded the index will be cleared.
Returns true on success, false on failure
string | $key | optional |
integer | $type | optional |
getPublicKey(integer $type = self::PUBLIC_FORMAT_PKCS8)
Returns the public key
The public key is only returned under two circumstances - if the private key had the public key embedded within it or if the public key was set via setPublicKey(). If the currently loaded key is supposed to be the public key this function won't return it since this library, for the most part, doesn't distinguish between public and private keys.
integer | $type | optional |
getPublicKeyFingerprint(string $algorithm = 'md5') : mixed
Returns the public key's fingerprint
The public key's fingerprint is returned, which is equivalent to running ssh-keygen -lf rsa.pub
. If there is
no public key currently loaded, false is returned.
Example output (md5): "c1:b1:30:29:d7:b8:de:6c:97:77:10:d7:46:41:63:87" (as specified by RFC 4716)
string | $algorithm | The hashing algorithm to be used. Valid options are 'md5' and 'sha256'. False is returned for invalid values. |
_decodeLength(string $string) : integer
DER-decode the length
DER supports lengths up to (28)127, however, we'll only support lengths up to (28)4. See X.690 paragraph 8.1.3 for more information.
string | $string |
_encodeLength(integer $length) : string
DER-encode the length
DER supports lengths up to (28)127, however, we'll only support lengths up to (28)4. See X.690 paragraph 8.1.3 for more information.
integer | $length |
setMGFHash(string $hash)
Determines which hashing function should be used for the mask generation function
The mask generation function is used by self::ENCRYPTION_OAEP and self::SIGNATURE_PSS and although it's best if Hash and MGFHash are set to the same thing this is not a requirement.
string | $hash |
setSaltLength( $sLen)
Determines the salt length
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.
$sLen |
_i2osp(\phpseclib\Math\BigInteger $x, integer $xLen) : string
Integer-to-Octet-String primitive
See RFC3447#section-4.1.
\phpseclib\Math\BigInteger | $x | |
integer | $xLen |
_os2ip(string $x) : \phpseclib\Math\BigInteger
Octet-String-to-Integer primitive
See RFC3447#section-4.2.
string | $x |
_exponentiate(\phpseclib\Math\BigInteger $x) : \phpseclib\Math\BigInteger
Exponentiate with or without Chinese Remainder Theorem
\phpseclib\Math\BigInteger | $x |
_blind(\phpseclib\Math\BigInteger $x, \phpseclib\Math\BigInteger $r, integer $i) : \phpseclib\Math\BigInteger
Performs RSA Blinding
Protects against timing attacks by employing RSA Blinding. Returns $x->modPow($this->exponents[$i], $this->primes[$i])
\phpseclib\Math\BigInteger | $x | |
\phpseclib\Math\BigInteger | $r | |
integer | $i |
_equals(string $x, string $y) : boolean
Performs blinded RSA equality testing
Protects against a particular type of timing attack described.
See A Lesson In Timing Attacks (or, Don't use MessageDigest.isEquals)
Thanks for the heads up singpolyma!
string | $x | |
string | $y |
_rsaep(\phpseclib\Math\BigInteger $m) : \phpseclib\Math\BigInteger
RSAEP
\phpseclib\Math\BigInteger | $m |
_rsadp(\phpseclib\Math\BigInteger $c) : \phpseclib\Math\BigInteger
RSADP
\phpseclib\Math\BigInteger | $c |
_rsasp1(\phpseclib\Math\BigInteger $m) : \phpseclib\Math\BigInteger
RSASP1
\phpseclib\Math\BigInteger | $m |
_rsavp1(\phpseclib\Math\BigInteger $s) : \phpseclib\Math\BigInteger
RSAVP1
\phpseclib\Math\BigInteger | $s |
_rsaes_oaep_encrypt(string $m, string $l = '') : string
RSAES-OAEP-ENCRYPT
See RFC3447#section-7.1.1 and {http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding OAES}.
string | $m | |
string | $l |
_rsaes_oaep_decrypt(string $c, string $l = '') : string
RSAES-OAEP-DECRYPT
See RFC3447#section-7.1.2. The fact that the error messages aren't distinguishable from one another hinders debugging, but, to quote from RFC3447#section-7.1.2:
Note. Care must be taken to ensure that an opponent cannot distinguish the different error conditions in Step 3.g, whether by error message or timing, or, more generally, learn partial information about the encoded message EM. Otherwise an opponent may be able to obtain useful information about the decryption of the ciphertext C, leading to a chosen-ciphertext attack such as the one observed by Manger [36].
As for $l... 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.
string | $c | |
string | $l |
_rsaes_pkcs1_v1_5_decrypt(string $c) : string
RSAES-PKCS1-V1_5-DECRYPT
For compatibility purposes, this function departs slightly from the description given in RFC3447. The reason being that RFC2313#section-8.1 (PKCS#1 v1.5) states that ciphertext's encrypted by the private key should have the second byte set to either 0 or 1 and that ciphertext's encrypted by the public key should have the second byte set to 2. In RFC3447 (PKCS#1 v2.1), the second byte is supposed to be 2 regardless of which key is used. For compatibility purposes, we'll just check to make sure the second byte is 2 or less. If it is, we'll accept the decrypted string as valid.
As a consequence of this, a private key encrypted ciphertext produced with \phpseclib\Crypt\RSA may not decrypt with a strictly PKCS#1 v1.5 compliant RSA implementation. Public key encrypted ciphertext's should but not private key encrypted ciphertext's.
string | $c |
_emsa_pkcs1_v1_5_encode(string $m, integer $emLen) : string
EMSA-PKCS1-V1_5-ENCODE
See RFC3447#section-9.2.
string | $m | |
integer | $emLen |
encrypt(string $plaintext) : string
Encryption
Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will be concatenated together.
string | $plaintext |