What is curve25519-js?
The curve25519-js npm package provides JavaScript implementations of the Curve25519 elliptic curve cryptography functions. It is primarily used for key exchange and digital signatures, offering a high level of security and performance.
What are curve25519-js's main functionalities?
Key Pair Generation
This feature allows you to generate a public and private key pair using the Curve25519 algorithm. The generated keys can be used for secure communication.
const curve = require('curve25519-js');
const keyPair = curve.generateKeyPair();
console.log(keyPair);
Shared Secret Computation
This feature allows you to compute a shared secret between two parties using their private and public keys. This shared secret can be used for encrypted communication.
const curve = require('curve25519-js');
const aliceKeyPair = curve.generateKeyPair();
const bobKeyPair = curve.generateKeyPair();
const sharedSecret = curve.sharedKey(aliceKeyPair.private, bobKeyPair.public);
console.log(sharedSecret);
Signature Generation
This feature allows you to generate a digital signature for a given message using a private key. The signature can be used to verify the authenticity of the message.
const curve = require('curve25519-js');
const keyPair = curve.generateKeyPair();
const message = 'Hello, World!';
const signature = curve.sign(keyPair.private, message);
console.log(signature);
Signature Verification
This feature allows you to verify a digital signature using the corresponding public key. It ensures that the message has not been tampered with and is from the expected sender.
const curve = require('curve25519-js');
const keyPair = curve.generateKeyPair();
const message = 'Hello, World!';
const signature = curve.sign(keyPair.private, message);
const isValid = curve.verify(keyPair.public, message, signature);
console.log(isValid);
Other packages similar to curve25519-js
tweetnacl
TweetNaCl is a cryptographic library that provides high-level cryptographic functions, including public-key encryption, secret-key encryption, and digital signatures. It is known for its simplicity and high performance. Compared to curve25519-js, TweetNaCl offers a broader range of cryptographic functions and is widely used in the community.
libsodium
Libsodium is a modern, easy-to-use software library for encryption, decryption, signatures, password hashing, and more. It is a portable, cross-compilable, installable, and packageable fork of NaCl, with a compatible API. Libsodium provides a more comprehensive set of cryptographic tools compared to curve25519-js and is highly regarded for its security and performance.
elliptic
Elliptic is a JavaScript library for elliptic curve cryptography. It supports a variety of elliptic curves, including Curve25519, and provides functions for key generation, signing, and verification. Compared to curve25519-js, Elliptic offers support for multiple curves and is more versatile in terms of cryptographic operations.
curve25519-js
Curve25519 signatures with X25519 keys.
Installation
npm i curve25519-js
Usage
import { sharedKey } from 'curve25519-js';
const ALICE_PRIV = '77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a';
const BOB_PUB = 'de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f';
const alicePriv = Uint8Array.from(Buffer.from(ALICE_PRIV, 'hex'));
const bobPub = Uint8Array.from(Buffer.from(BOB_PUB, 'hex'));
const secret = sharedKey(alicePriv, bobPub);
console.log('Secret:', Buffer.from(secret).toString('hex'))
Functions
generateKeyPair
Generates a new key pair from the given 32-byte secret seed (which should be generated with a CSPRNG) and returns it as object:
generateKeyPair(seed: Uint8Array(32)): {
private: Uint8Array(32);
public: Uint8Array(32);
}
The returned keys can be used for signing and key agreement.
sign
Signs the given message using the private key and returns signature.
sign(secretKey: Uint8Array(32), message: any, [random: Uint8Array(64)]): Uint8Array(64)
Optional random data argument (which must have 64 random bytes) turns on
hash separation and randomization to make signatures non-deterministic.
verify
Verifies the given signature for the message using the given private key.
Returns true
if the signature is valid, false
otherwise.
verify(publicKey: Uint8Array(32), message: any, signature: Uint8Array(64)): boolean
signMessage
Signs the given message using the private key and returns
a signed message (signature concatenated with the message copy).
signMessage(secretKey: Uint8Array(32), message: any, [random: Uint8Array(64)]): any
Optional random data argument (which must have 64 random bytes) turns on
hash separation and randomization to make signatures non-deterministic.
openMessage
Verifies signed message with the public key and returns the original message
without signature if it's correct or null
if verification fails.
openMessage(publicKey: Uint8Array(32), signedMessage: any): Message | null
sharedKey
Returns a raw shared key between own private key and peer's public key (in other words, this is an ECC Diffie-Hellman function X25519, performing scalar multiplication).
The result should not be used directly as a key, but should be processed with a one-way function (e.g. HSalsa20 as in NaCl, or any secure cryptographic hash function, such as SHA-256, or key derivation function, such as HKDF).
sharedKey(privateKey: Uint8Array(32), publicKey: Uint8Array(32)): Uint8Array(32)
How is it different from Ed25519?
Axlsign allows calculating key agreement and signing using just a single X25519 key instead of two different X25519 and Ed25519 keys.
It uses keys in X25519 format (Montgomery), while Ed25519 uses keys in a different representation (Twisted Edwards). Internally, it converts keys to the correct format, but since such conversion would lose a sign bit, it also embeds the sign bit from public key into signature during signing, and puts it back into the key during verification.
Note: if signing and performing key agreement with a single key is needed, but using keys in X25519 format is not a requrement, a better choice is to use Ed25519 keys, and then convert them to X25519 keys for key agreement (e.g. using https://github.com/dchest/ed2curve-js). This allows using only an external conversion functions without changing signature algorithms and formats.
Credits
Re-written in 2019 with TypeScript support by Harvey Connor.
Written in 2016 by Dmitry Chestnykh.
You can use it under MIT or CC0 license.
Curve25519 signatures idea and math by Trevor Perrin
https://moderncrypto.org/mail-archive/curves/2014/000205.html
Derived from TweetNaCl.js https://tweetnacl.js.org.
Ported in 2014 by Dmitry Chestnykh and Devi Mandiri. Public domain.
Implementation derived from TweetNaCl version 20140427
http://tweetnacl.cr.yp.to