SecureLogin
|
Public Member Functions | |
Crypt_RSA () | |
createKey ($bits=1024, $timeout=false, $partial=array()) | |
_convertPrivateKey ($n, $e, $d, $primes, $exponents, $coefficients) | |
_convertPublicKey ($n, $e) | |
_parseKey ($key, $type) | |
loadKey ($key, $type=CRYPT_RSA_PRIVATE_FORMAT_PKCS1) | |
setPassword ($password) | |
setPublicKey ($key, $type=CRYPT_RSA_PUBLIC_FORMAT_PKCS1) | |
getPublicKey ($type=CRYPT_RSA_PUBLIC_FORMAT_PKCS1) | |
_generateMinMax ($bits) | |
_decodeLength (&$string) | |
_encodeLength ($length) | |
_string_shift (&$string, $index=1) | |
setPrivateKeyFormat ($format) | |
setPublicKeyFormat ($format) | |
setHash ($hash) | |
setMGFHash ($hash) | |
setSaltLength ($sLen) | |
_random ($bytes, $nonzero=false) | |
_i2osp ($x, $xLen) | |
_os2ip ($x) | |
_exponentiate ($x) | |
_blind ($x, $r, $i) | |
_rsaep ($m) | |
_rsadp ($c) | |
_rsasp1 ($m) | |
_rsavp1 ($s) | |
_mgf1 ($mgfSeed, $maskLen) | |
_rsaes_oaep_encrypt ($m, $l= '') | |
_rsaes_oaep_decrypt ($c, $l= '') | |
_rsaes_pkcs1_v1_5_encrypt ($m) | |
_rsaes_pkcs1_v1_5_decrypt ($c) | |
_emsa_pss_encode ($m, $emBits) | |
_emsa_pss_verify ($m, $em, $emBits) | |
_rsassa_pss_sign ($m) | |
_rsassa_pss_verify ($m, $s) | |
_emsa_pkcs1_v1_5_encode ($m, $emLen) | |
_rsassa_pkcs1_v1_5_sign ($m) | |
_rsassa_pkcs1_v1_5_verify ($m, $s) | |
setEncryptionMode ($mode) | |
setSignatureMode ($mode) | |
encrypt ($plaintext) | |
decrypt ($ciphertext) | |
sign ($message) | |
verify ($message, $signature) | |
Data Fields | |
$zero | |
$one | |
$privateKeyFormat = CRYPT_RSA_PRIVATE_FORMAT_PKCS1 | |
$publicKeyFormat = CRYPT_RSA_PUBLIC_FORMAT_PKCS1 | |
$modulus | |
$k | |
$exponent | |
$primes | |
$exponents | |
$coefficients | |
$hashName | |
$hash | |
$hLen | |
$sLen | |
$mgfHash | |
$mgfHLen | |
$encryptionMode = CRYPT_RSA_ENCRYPTION_OAEP | |
$signatureMode = CRYPT_RSA_SIGNATURE_PSS | |
$publicExponent = false | |
$password = '' |
_blind | ( | $ | x, |
$ | r, | ||
$ | i | ||
) |
Performs RSA Blinding
Protects against timing attacks by employing RSA Blinding. Returns $x->modPow($this->exponents[$i], $this->primes[$i])
private
Math_BigInteger | $x | |
Math_BigInteger | $r | |
Integer | $i |
_convertPrivateKey | ( | $ | n, |
$ | e, | ||
$ | d, | ||
$ | primes, | ||
$ | exponents, | ||
$ | coefficients | ||
) |
Convert a private key to the appropriate format.
private
String | $RSAPrivateKey |
_convertPublicKey | ( | $ | n, |
$ | e | ||
) |
Convert a public key to the appropriate format
private
String | $RSAPrivateKey |
_decodeLength | ( | &$ | string | ) |
DER-decode the length
DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See X.690 § 8.1.3 for more information.
private
String | $string |
_emsa_pkcs1_v1_5_encode | ( | $ | m, |
$ | emLen | ||
) |
EMSA-PKCS1-V1_5-ENCODE
See RFC3447#section-9.2.
private
String | $m | |
Integer | $emLen |
_emsa_pss_encode | ( | $ | m, |
$ | emBits | ||
) |
_emsa_pss_verify | ( | $ | m, |
$ | em, | ||
$ | emBits | ||
) |
EMSA-PSS-VERIFY
private
String | $m | |
String | $em | |
Integer | $emBits |
_encodeLength | ( | $ | length | ) |
DER-encode the length
DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See X.690 § 8.1.3 for more information.
private
Integer | $length |
_exponentiate | ( | $ | x | ) |
Exponentiate with or without Chinese Remainder Theorem
private
Math_BigInteger | $x |
_generateMinMax | ( | $ | bits | ) |
Generates the smallest and largest numbers requiring $bits bits
private
Integer | $bits |
_i2osp | ( | $ | x, |
$ | xLen | ||
) |
Integer-to-Octet-String primitive
See RFC3447#section-4.1.
private
Math_BigInteger | $x | |
Integer | $xLen |
_mgf1 | ( | $ | mgfSeed, |
$ | maskLen | ||
) |
_os2ip | ( | $ | x | ) |
Octet-String-to-Integer primitive
See RFC3447#section-4.2.
private
String | $x |
_parseKey | ( | $ | key, |
$ | type | ||
) |
Break a public or private key down into its constituant components
private
String | $key | |
Integer | $type |
_random | ( | $ | bytes, |
$ | nonzero = false |
||
) |
Generates a random string x bytes long
public
Integer | $bytes | |
optional | Integer $nonzero |
_rsadp | ( | $ | c | ) |
_rsaep | ( | $ | m | ) |
_rsaes_oaep_decrypt | ( | $ | c, |
$ | l = '' |
||
) |
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.
private
String | $c | |
String | $l |
_rsaes_oaep_encrypt | ( | $ | m, |
$ | l = '' |
||
) |
RSAES-OAEP-ENCRYPT
See RFC3447#section-7.1.1 and {http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding OAES}.
private
String | $m | |
String | $l |
_rsaes_pkcs1_v1_5_decrypt | ( | $ | c | ) |
RSAES-PKCS1-V1_5-DECRYPT
For compatability 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 compatability 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 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.
private
String | $c |
_rsaes_pkcs1_v1_5_encrypt | ( | $ | m | ) |
_rsasp1 | ( | $ | m | ) |
_rsassa_pkcs1_v1_5_sign | ( | $ | m | ) |
_rsassa_pkcs1_v1_5_verify | ( | $ | m, |
$ | s | ||
) |
_rsassa_pss_sign | ( | $ | m | ) |
_rsassa_pss_verify | ( | $ | m, |
$ | s | ||
) |
_rsavp1 | ( | $ | s | ) |
_string_shift | ( | &$ | string, |
$ | index = 1 |
||
) |
String Shift
Inspired by array_shift
String | $string | |
optional | Integer $index |
createKey | ( | $ | bits = 1024 , |
$ | timeout = false , |
||
$ | partial = array() |
||
) |
Create public / private key pair
Returns an array with the following three elements:
public
optional | Integer $bits |
optional | Integer $timeout |
optional | Math_BigInteger $p |
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 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.
decrypt | ( | $ | ciphertext | ) |
encrypt | ( | $ | plaintext | ) |
Encryption
Both CRYPT_RSA_ENCRYPTION_OAEP and CRYPT_RSA_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 |
getPublicKey | ( | $ | type = CRYPT_RSA_PUBLIC_FORMAT_PKCS1 | ) |
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.
String | $key | |
Integer | $type | optional |
loadKey | ( | $ | key, |
$ | type = CRYPT_RSA_PRIVATE_FORMAT_PKCS1 |
||
) |
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)
public
String | $key | |
Integer | $type | optional |
setEncryptionMode | ( | $ | mode | ) |
Set Encryption Mode
Valid values include CRYPT_RSA_ENCRYPTION_OAEP and CRYPT_RSA_ENCRYPTION_PKCS1.
public
Integer | $mode |
setHash | ( | $ | hash | ) |
Determines which hashing function should be used
Used with signature production / verification and (if the encryption mode is CRYPT_RSA_ENCRYPTION_OAEP) encryption and decryption. If $hash isn't supported, sha1 is used.
public
String | $hash |
setMGFHash | ( | $ | hash | ) |
Determines which hashing function should be used for the mask generation function
The mask generation function is used by CRYPT_RSA_ENCRYPTION_OAEP and CRYPT_RSA_SIGNATURE_PSS and although it's best if Hash and MGFHash are set to the same thing this is not a requirement.
public
String | $hash |
setPassword | ( | $ | password | ) |
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 true.
String | $password |
setPrivateKeyFormat | ( | $ | format | ) |
setPublicKey | ( | $ | key, |
$ | type = CRYPT_RSA_PUBLIC_FORMAT_PKCS1 |
||
) |
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.
Do note that when a new key is loaded the index will be cleared.
Returns true on success, false on failure
String | $key | |
Integer | $type | optional |
setPublicKeyFormat | ( | $ | format | ) |
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.
public
Integer | $format |
setSignatureMode | ( | $ | mode | ) |
Set Signature Mode
Valid values include CRYPT_RSA_SIGNATURE_PSS and CRYPT_RSA_SIGNATURE_PKCS1
public
Integer | $mode |
sign | ( | $ | message | ) |
verify | ( | $ | message, |
$ | signature | ||
) |
Verifies a signature
String | $message | |
String | $signature |