JavaScript cryptography
Web Cryptography API [1] is an standardized interface allowing a script to use basic cryptographic operations. However, it exposes a bunch of primitives that are very easy to misuse, or are inherently unsafe. This library demonstrates safe usage of Web Cryptography API. It tries to select safe algorithms and parameters and help you avoid most of the common mistakes. You can either use it as a library, or just copy and paste functions that you need.
Design choices
This library was heavily inspired by George Tankersley's cryptopasta library [2], from which it borrows most of the design choices. You can find more information about selecting right cryptographic primitives at [3] or [4].
Encryption
When encrypting data, you should always use authenticated encryption modes. The only authenticated mode offered by Web Cryptography API is AES-GCM, so that mode was used in this library. Key size is fixed to 256 bits, and nonces are randomly generated (for more info, see the discussion about randomly generated nonces at [10]).
Authentication
Authentication is done using HMAC with SHA-256 hash function. HMAC is the only algorithm that you want to use for authentication, so the only choice was which hash function to use, and SHA-256 is pretty standard choice.
Signing
ECDSA algorithm on P-256 curve was selected for asymmetric signatures. There are better choices here, but none of them is available in Web Cryptography API. RSA-PSS could also have been used, but you should probably stay away from RSA and use elliptic curves instead.
Hashing
Password hashing is not terribly useful in web browser, but it's nevertheless provided here for the sake of completeness. The only available password hashing algorithm in Web Cryptography API is PBKDF2, so that was the logical choice here.
Word of caution
You should never implement crypto yourself (especially in JavaScript). Just use TLS. If you want to know more about why JavaScript cryptography is a bad idea, go to [5], [7] or [8]. That being said, there are some use cases for JavaScript crypto. Read more about that at [6] or [11].
API
Encryption
Promise<CryptoKey> newEncryptionKey();
Promise<ArrayBuffer> encrypt(
ArrayBuffer|ArrayBufferView plaintext,
CryptoKey key
);
Promise<ArrayBuffer> decrypt(
ArrayBuffer|ArrayBufferView ciphertext,
CryptoKey key
);
Authentication
Promise<CryptoKey> newMacKey();
Promise<ArrayBuffer> generateMac(
ArrayBuffer|ArrayBufferView message,
CryptoKey key
);
Promise<Boolean> verifyMac(
ArrayBuffer|ArrayBufferView mac,
ArrayBuffer|ArrayBufferView message,
CryptoKey key
);
Signing
Promise<CryptoKeyPair> newSigningKey();
Promise<ArrayBuffer> sign(
ArrayBuffer|ArrayBufferView message,
CryptoKey privateKey
);
Promise<Boolean> verify(
ArrayBuffer|ArrayBufferView signature,
ArrayBuffer|ArrayBufferView message,
CryptoKey publicKey
);
Hashing
Promise<ArrayBuffer> hashPassword(String password);
Promise<Boolean> verifyPasswordHash(
ArrayBuffer|ArrayBufferView hash,
String password
);
Marshalling
Promise<ArrayBuffer> exportEncryptionKey(CryptoKey key);
Promise<CryptoKey> importEncryptionKey(ArrayBuffer buffer);
Promise<ArrayBuffer> exportPublicKey(CryptoKey publicKey);
Promise<CryptoKey> importPublicKey(ArrayBuffer buffer);
Promise<ArrayBuffer> exportPrivateKey(CryptoKey privateKey);
Promise<CryptoKey> importPrivateKey(ArrayBuffer buffer);
Examples
Encryption
const message = "I'm cooking MC's like a pound of bacon";
const key = window.newEncryptionKey();
const ciphertext = key.then(key => {
const encoded = new TextEncoder('utf-8').encode(message);
return window.encrypt(encoded, key);
});
ciphertext.then(ciphertext => console.log(new Uint8Array(ciphertext)));
const plaintext = ciphertext.then(ciphertext => {
return key.then(key => window.decrypt(ciphertext, key));
}).then(plaintext => {
return new TextDecoder('utf-8').decode(plaintext);
});
plaintext.then(plaintext => console.log(plaintext));
Authentication
const message = "I'm cooking MC's like a pound of bacon";
const encoded = new TextEncoder('utf-8').encode(message);
const key = window.newMacKey();
const mac = key.then(key => window.generateMac(encoded, key))
mac.then(mac => console.log(new Uint8Array(mac)));
const valid = mac.then(mac => key.then(key => window.verifyMac(mac, encoded, key)));
valid.then(valid => console.log(valid));
Signing
const message = "I'm cooking MC's like a pound of bacon";
const encoded = new TextEncoder('utf-8').encode(message);
const key = window.newSigningKey();
const sig = key.then(key => window.sign(encoded, key.privateKey));
sig.then(sig => console.log(new Uint8Array(sig)));
const valid = sig.then(sig => key.then(key => window.verify(sig, encoded, key.publicKey)));
valid.then(valid => console.log(valid));
Hashing
const password = 'password12345';
const hash = window.hashPassword(password);
hash.then(hash => console.log(new Uint8Array(hash)));
const valid = hash.then(hash => window.verifyPasswordHash(hash, password));
valid.then(valid => console.log(valid));
Marshalling