This module is not currently maintained. It was known to have faults. It hasn't been tested with test vectors. USE AT YOUR OWN RISK!
Node.js module that statically binds and simplifies the usage of the Crypto++ comprehensive cryptography library. (Written for Node.js v0.10.x)
All the crypto methods could be used in sync/async mode
- node.js, obviously..
Crypto++, that could be installed on Linux rather easily(Not a requirement anymore. v0.2.1 is all about having Crypto++ as a submodule)
On installation, the node-cryptopp module compiles on your computer. Hence Crypto++ needs to be installed.
To install the latest version of this module, run :
npm install git+https://github.com/Mowje/node-cryptopp.git
Or, alternatively :
npm install git+ssh://firstname.lastname@example.org:Mowje/node-cryptopp.git
If you want to use a specific version of this module (let's say v0.2.4), you should add a "#" character and the version number at the end of the URL given. (In our example, that makes :
npm install git+https://github.com/Mowje/node-cryptopp.git#v0.2.4)
NOTE: This module used to be installable from npmjs.org. I struggled to make it work with 0.2.1. So I'm giving up on npm for now and gave you this alternate way to install cryptopp.
CAUTION : Do not use node-cryptopp v0.2.3 and prior to check signatures!!!
Hence, I created the
KeyRing class, that manages keypair generation, saving, loading and clearing in addition to the cryptographic operations where the private key is needed. Also, there is no method in this class that will allow you to extract the private key.
As of now, I kept the unsafe methods from the previous versions of the module, but I highly recommend using the key ring.
Note that I wanted to allow key encryption (ie, when saving then on disk). But it doesn't work as of now. Hence, don't use the
passphrase parameter (or skip it with
undefined like you'd skip any other parameter in cryptopp, as explained below).
- By default, each method described could be given a callback. If no callback is given, the method's result is returned.
- If you want to skip an optional parameter but want to define the parameter that follows it, then the skipped parameter MUST be set to
undefined. Sorry if this seems to totally inconvenient
- This library isn't well written in terms of error management (except the KeyRing class). If the app crashes or throws some strange exception, it is probably because you did something wrong (Thanks Captain Obvious) but in general it won't tell you what it is. Note that if you use a method with a callback, the errors will be thrown exactly like when you use the method without a callback (meaning: not through the callback)
- The different ECC algorithms for which are (or will be) implemented here use standard elliptic curves, defined here. The related methods will have a "curveName" parameter, taken from the previously linked document, like "secp256r1" or "sect233k1". Beware, it is case-sensitive. Each party must use the same curve.
- ECIES keypairs can be used in ECDSA and vice-versa! (as long as you use the same curve in both algorithms) paper that proves it; look for section 4
- You should not use ECDH or ECDSA on binary fields! There is a bug in the related methods that is not yet fixed. (probably in hexStr<->PolynomialMod2 versions, if you are more courageous than me and want to dig in)
- You can choose what hash function want to use in ECDSA and RSA signatures. You can choose either SHA1 (default) or SHA256. Just set the
hashNameparameter to 'sha1' or 'sha256' in the corresponding methods. Note that the default hash function for these algorithms in version prior to v0.2.0 was SHA256.
- Keys, ciphertexts and signatures are all hex encoded. These data types should be kept "as-is" when passed to other methods.
- Crypto++ doesn't do well with fuzzed values (like ciphertexts, keys or signatures). Hence, unless you do value sanitizing of some sort, it seems like a bad idea to use this module in a server to check/validate/decrypt user-provided data (for example).
The test.js script gives example usages for most implemented algorithms. So you can learn from there, in addition to learning from this page.
Before using the
KeyRing, you must construct it. This is how it's done :
var cryptopp = require('cryptopp'); var keyRing = new cryptopp.KeyRing();
Here are the list of methods exposed by the
createKeyPair(algoType, algoOptions, [filename], [passphrase], [callback]):
Generates a keypair the given algorithm. Returns the public key information object (as in the
- algoType : the name of the algorithm for which you want to create a keyPair. Possible values are "rsa", "dsa", "ecies", "ecdsa", "ecdh"
- algoOptions : the keysize when algoType is "rsa" or "dsa", the curve name otherwise
- filename : the path to the file where you want the keypair to saved. Optional parameter
- passphrase : a passphrase used to encrypt the keypair (when you choose to save it). Optional parameter
- callback : a callback function, that will recieve the public key information object as argument. Optional parameter
decrypt(cipherText, [encoding], [callback])
Decrypts the cipherText (optionally encoded)
- cipherText : the ciphertext to decrypt
- encoding : optional, the encoding of the ciphertext. Possible values are : 'hex', 'base64'. Defaults to 'hex'
- callback : optional, receives the plaintext as a parameter
sign(message, [signatureEncoding], [hashName], [callback])
Signs the message with the loaded key ring.
- message : the message to be signed
- signatureEncoding : optional, determines the encoding that should be used for the signature. Possible values : 'hex', 'base64'. Defaults to 'hex'.
- hashName : optional, name of the hash function to be used in the signing process. Possible values are 'sha1', 'sha256'. Defaults to 'sha1'.
- callback : optional. Recieves the signature as a parameter if used
Agrees on a shared secret and returns it (hex encoded)
- pubKey : object containing the keyType, curveName and publicKey attributes for an ECDH key agreement
- callback : receives the shared secret
publicKeyInfo([callback])Returns an object containing public key information from the currently loaded key pair. You can give a callback. The returned object has the following attributes :
- keyType : a string that contains the algo type. Possible values : "rsa", "dsa", "ecdsa", "ecies", "ecdh"
- if (keyType == "rsa") :
- modulus : the RSA modulus
- publicExponent : the RSA public exponent
- if (keyType == "dsa") :
- primeField : the DSA prime field
- divider : the DSA divider
- base : the DSA base
- publicElement : the DSA public key
- if (keyType == "ecdsa" || keyType == "ecies")
- curveName : the standard name of the cruve used
- publicKey.x : x coordinate of the public point
- publicKey.y : y coordinate of the public point
- if (keyType == "ecdh")
- curveName : the standard name of the curve used
- publicKey : the ECDH public key
save(filename, [passphrase], [callback])
Save the keypair to the given filename. DON'T USE THE PASSPHRASE! No paramter passed to the callback
load(filename, [legacy], [passphrase], [callback])
Load the keypair from the given path. Legacy is a boolean, determining whether the file is in the old key file format (prior to v0.2.2) DON'T USE THE PASSPHRASE! The callback receives the public key information object
Deletes the keypair from memory. You MUST call this method once you're done working the keyring.
RSA encryption and signature schemes are supported by this module. For signatures : the default hashing function used here is SHA1, but you can specify the
hashName parameter either to "sha1" or "sha256" (other values will throw an exception)
There are 5 methods for RSA :
- rsa.generateKeyPair(keySize, [callback(keyPair)]) : Generates a RSA keypair with the given key size (in bits). The keysize must be 1024 <= Math.power(2, k) <= 16384 (where k is an integer). The result of the method is an object with 3 attributes : modulus, publicExponent and privateExponent
- rsa.encrypt(plainText, modulus, publicExponent, [callback(cipherText)]) : Returns the ciphertext
- rsa.decrypt(cipherText, modulus, privateExponent, publicExponent, [callback(plainText)]) : Returns the plain text message
- rsa.sign(message, modulus, privateExponent, publicExponent, [hashName], [callback(signature)]) : Signs the message with the given private key
- rsa.verify(message, signature, modulus, publicExponent, [hashName], [callback(isValid)]) : Tells whether the signature for the given message and public key is valid or not
var cryptopp = require('cryptopp'); var rsaKeyPair = cryptopp.rsa.generateKeyPair(2048); var cipher = cryptopp.rsa.encrypt('Testing RSA', rsaKeyPair.modulus, rsaKeyPair.publicExponent); var plaintext = cryptopp.rsa.decrypt(cipher, rsaKeyPair.modulus, rsaKeyPair.privateExponent);
There are 3 methods for DSA. Note that the hashing function used here is SHA1.
- dsa.generateKeyPair(keySize, [callback(keyPair)]) : Generates a DSA keypair with the given key size (in bits). The result is an object with 5 attributes : primeField, divider, base, privateExponent, publicElement
- dsa.sign(message, primeField, divider, base, privateExponent, [callback(signature)]) : Signs the given message using DSA with SHA1
- dsa.verify(message, signature, primeField, divider, base, publicElement, [callback(isValid)]) : Verifies the signature
var cryptopp = require('cryptopp'); var dsaKeyPair = cryptopp.dsa.generateKeyPair(2048); var message = 'Testing DSA'; var signature = cryptopp.dsa.sign(message, dsaKeyPair.primeField, dsaKeyPair.divider, dsaKeyPair.base, dsaKeyPair.privateExponent); var isValid = cryptopp.dsa.verify(message, signature, dsaKeyPair.primeField, dsaKeyPair.divider, dsaKeyPair.base, dsaKeyPair.publicElement);
Bindings have been written for ECIES on prime and binary fields.
The methods are reachable as following cryptopp.ecies.[fieldType].[methodname]
For each of these fields, there are 3 methods available :
- ecies.[fieldType].generateKeyPair(curveName, [callback(keyPair)]) : Returns an object containing the private key, the public key, and curve name. The private and public keys are hex encoded and should be passed in that format to other methods.
- ecies.[fieldType].encrypt(plainText, publicKey, curveName, [callback(cipherText)]) : encrypts the plainText with the given publicKey on the given curve.
- ecies.[fieldType].decrypt(cipherText, privateKey, curveName, [callback(plainText)]) : decrypts the cipherText with the given privateKey on the given curve.
var cryptopp = require('cryptopp'); var keyPair = cryptopp.ecies.prime.generateKeyPair("secp256r1"); var cipher = cryptopp.ecies.prime.encrypt("Testing ECIES", keyPair.publicKey, keyPair.curveName); var plainText = cryptopp.ecies.prime.decrypt(cipher, keyPair.privateKey, keyPair.curveName);
To use ECIES on binary fields, just replace in the code above "prime" by "binary" and the curve name by a "binary curve" one.
Bindings have been written for ECDSA for prime and prime fields. However, as mentioned before in the "General notes" there is a bug somewhere in the binary field version in the signing method.
You can choose which hashing function you want to use by setting the
hashName parameter either to "sha1" or "sha256" (other values will throw an exception). The ECDSA methods are reachable in a manner similar to ECIES. Here are ECDSA's methods :
- ecdsa.[fieldType].generateKeyPair(curveName, [callback(keyPair)]) : Returns an object containing the private key, the public key and the curve name.
- ecdsa.[fieldType].sign(message, privateKey, curveName, [hashName], [callback(signature)]) : Returns the signature for the given message
- ecdsa.[fieldType].verify(message, signature, publicKey, curveName, [hashName], [callback(isValid)]) : A boolean is returned by this method; true when the signature is valid, false when it isn't.
var cryptopp = require('cryptopp'); var keyPair = cryptopp.ecdsa.prime.generateKeyPair("secp256r1"); var message = "Testing ECDSA"; var signature = cryptopp.ecdsa.prime.sign(message, keyPair.privateKey, keyPair.curveName); var isValid = cryptopp.ecdsa.prime.verify(message, signature, keyPair.publicKey, keyPair.curveName);
Binding have been written for ECDH for both type of fields. However, the binary version don't always give the same secret in the "agree" method.
There are only 2 methods per field :
- ecdh.[fieldType].generateKeyPair(curveName, [callback(keyPair)]) : The result is an object with 3 attributes : curveName, privateKey, publicKey
- ecdh.[fieldType].agree(yourPrivateKey, yourCounterpartsPublicKey, curveName, [callback(secret)]) : Returns the common secret.
var cryptopp = require('cryptopp'); var ecdhKeyPair1 = cryptopp.ecdh.prime.generateKeyPair('secp256r1'); var ecdhKeyPair2 = cryptopp.ecdh.prime.generateKeyPair('secp256r1'); var secret1 = cryptopp.ecdh.prime.agree(ecdhKeyPair1.privateKey, ecdhKeyPair2.publicKey, ecdhKeyPair1.curveName); var secret2 = cryptopp.ecdh.prime.agree(ecdhKeyPair2.privateKey, ecdhKeyPair1.publicKey, ecdhKeyPair2.curveName);
Random bytes generation
I found it useful to have a method that gives you random bytes, using the a generator from Crypto++ rather than
Math.random() or whatever
cryptopp.randomBytes(length, [encoding]) :
- length : number of bytes to be generated
- encoding : optional, possible values are 'hex' for hexadecimal and 'base64' for Base64 encoding. Defaults to 'hex'.
Hex and Base64 encodings
Although there are already ways to encode/decode to hex/base64 in Node.js, I wrote bindings to the implementations in Crypto++
hex.encode(text) : Encode the text to hexadecimal
hex.decode(encoded) : Decode the hex encoded text
base64.encode(text) : Encode the text to Base64
base64.decode(encoded) : Decode the Base64 encoded text
Keypair file format
Here is how a keypair file is built. Note that every number is in written in big endian. Note that the format has changed slightly as of v0.2.2 to homogenize it node-sodium's format and to ease the integration of both modules into node-hpka. For reference, here is the old key file format.
- algoType : a byte; 0x00 for ECDSA, 0x01 for RSA, 0x02 for DSA, 0x03 for ECDH, 0x04 for ECIES
- if keyType is ECDSA or ECIES
- curveID : a byte, corresponding to the curve used
- publicKeyX.length : length of the x coordinate of the public point (2 bytes, unsigned integer)
- publicKeyX : x coordinate of the public point
- publicKeyY.length : length of the y coordinate of the public point (2 bytes, unsigned integer)
- publicKeyY : y coordinate of the public point
- privateKey.length : length of the private key (2 bytes, unsigned integer)
- if keyType is RSA
- modulus.length : length of the RSA modulus (2 bytes, unsigned integer)
- modulus : RSA modulus
- publicExponent.length : length of the public exponent (2 bytes, unsigned integer)
- publicExponent : RSA public exponent (or public key)
- privateExponent.length : length of the private exponent (2 bytes, unsigned integer)
- privateExponent : RSA private exponent (or private key)
- if keyType is DSA
- primeField.length : length of the prime field used by the DSA key pair (2 bytes, unsigned integer)
- divider.length : length of the divider (2 bytes, unsigned integer)
- base.length : length of the base (2 bytes, unsigned integer)
- base : DSA base
- publicElement.length : length of the DSA public key (2 bytes, unsigned integer)
- publicElement : DSA public key
- privateExponent.length : length of the DSA private exponent (2 bytes, unsigned integer)
- privateExponent : DSA private exponent (ie, the private key)
- if keyType is ECDH
- curveID : a byte, corresponding to the curve used
- publicKey.length : length of the ECDH public key (2 bytes, unsigned integer)
- publicKey : ECDH public key
- privateKey.length : length of the ECDH private key (2 bytes, unsigned integer)
- privateKey : ECDH private key
CruveName <-> CurveID
This module is licensed under the MIT license.