Git Product home page Git Product logo

pqc.js's Introduction

pqc.js

Overview

This project provides JS bindings and playground of post-quantum asymmetric cipher compiled to WebAssembly using emscripten with a fallback in plain JS.

The available methods are the finalists (and alternate candidates) of NIST Post-Quantum Cryptography Competition:

The C implementations used to create the bindings are the clean versions provided by PQClean.

This project has been inspired by ntru.js that used to provide an NTRU JS binding.

Playground

It is possible to test the bindings, to compare them in real-world conditions on this playground.

How to download pre-built bindings and NPM packages

Pre-built bindings are available on the playground.

Some bindings can directly be downloaded from npmjs.com:

CRYSTALS-Kyber:

npm install @dashlane/pqc-kem-kyber<512|768|1024>-<node|browser>

NTRU-HPS:

npm install @dashlane/pqc-kem-ntruhps<2048509|2048677|4096821>-<node|browser>

NTRU Prime:

npm install @dashlane/pqc-kem-ntrulpr<653|761|857>-<node|browser>

SABER:

npm install @dashlane/pqc-kem-<light||fire>saber-<node|browser>

CRYSTALS-Dilithium:

npm install @dashlane/pqc-sign-dilithium<2|3|5>-<node|browser>

Falcon:

npm install @dashlane/pqc-sign-falcon-<512|1024>-<node|browser>

How to build

  • sudo apt install emscripten
  • npm ci
  • make clean
  • make kem-<the KEM algorithm you want to build> or make sign-<the signature algorithm you want to build>
  • All bindings can be built using make all

The output directory is docs/bin/ and each output binding is made of a JS module and its associated WebAssembly file, a browser package, and a node package.

How to use

Key Encapsulation

import kemBuilder from 'pqc-kem-<algoName>.js'

async function run() {
    const kem = await kemBuilder();
    
    const { publicKey, privateKey } = await kem.keypair();
    const { ciphertext, sharedSecret: sharedSecretA } = await kem.encapsulate(publicKey);
    const { sharedSecret: sharedSecretB } = await kem.decapsulate(ciphertext, privateKey);
    // sharedSecretA === sharedSecretB
}

run();

Signature

import signBuilder from 'pqc-sign-<algoName>.js'

async function run() {
    const sign = await signBuilder();
    
    const message = new Uint8Array([0x44, 0x61, 0x73, 0x68, 0x6c, 0x61, 0x6e, 0x65]);
    
    const { publicKey, privateKey } = await sign.keypair();
    const { signature } = await sign.sign(message, privateKey);
    const validSignature = await sign.verify(signature, message, publicKey);
    // validSignature === true
}

run();

Disable WebAssembly execution

The first optional parameter of the builders is set to true is you want to disable WebAssembly execution.

If it is set to false, the fallback JavaScript may still be used if the WebAssembly fails.

Change the path of the WebAssembly

The second optional parameter of the builders is the path to the WebAssembly file.

Implementation notes

  • Random generation is provided by libsodium that is using crypto.randomBytes.
  • SHA-2 and AES implementations are provided by SubtleCrypto that is an implementation of Web Crypto API. This may slow down the execution of the bindings because of the asynchronous calls to SubtleCrypto, even more when AES-ECB is used because in this case AES-CTR is called for each block.
  • SHA-2 PQClean implementation is still used by sphincs-sha256-* bindings because they are using incremental version of sha256 that do not exist in Web Crypto API.
  • SHA-3 PQClean implementation is still used by NTRU, Kyber, SABER, FrodoKEM and McEliece-Classic because there are no implementations in Web Crypto API.

pqc.js's People

Contributors

jboillot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pqc.js's Issues

fail to implement npm on nodejs

followed the example but failed to use dilithium, is there any nodejs example of the implementation?

Error [ERR_REQUIRE_ESM]: require() of ES Module xxxxxxxxx/node_modules/@dashlane/pqc-sign-dilithium5-node/dist/sign.js from /home/tconrado/genomica/privacy-relay/dist/main.js not supported.
Instead change the require of sign.js in xxxxxxxxxx/dist/main.js to a dynamic import() which is available in all CommonJS modules.
at Object. (xxxxxxxxxxx/dist/main.js:7:36) {
code: 'ERR_REQUIRE_ESM'
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.