dchest / tweetnacl-js

Port of TweetNaCl cryptographic library to JavaScript
https://tweetnacl.js.org
The Unlicense
1.75k stars 292 forks source link

tweetnacl for signal protocol keypairs? #254

Closed timothyerwin closed 11 months ago

timothyerwin commented 11 months ago

I'm trying to use this library to generate the keypairs for signal protocol as it's quite fast...however, the resulting keys don't seem to work properly and fail when attempting to run the protocol which previously worked albeit with a slower key generation (about 5x slower?)

below is the relevant code...if anyone code explain what I'm doing wrong? thanks!

function processKeys(raw_keys) {
  // prepend version byte
  var origPub = new Uint8Array(raw_keys.pubKey);
  var pub = new Uint8Array(33);
  pub.set(origPub, 1);
  pub[0] = 5;

  return { pubKey: pub.buffer, privKey: raw_keys.privKey };
}

function wrapCurve25519(curve25519) {
  return {
    // Curve 25519 crypto
    createKeyPair: function(privKey) {
      validatePrivKey(privKey);

      return new Promise((resolve, reject) => {
        try {
          const { publicKey, secretKey } = privKey ? nacl.box.keyPair.fromSecretKey(new Uint8Array(privKey)) : nacl.box.keyPair();

          // Convert the public key to 33 bytes by prepending a 0x05 flag
          // const publicWithFlag = new Uint8Array(33);
          // publicWithFlag.set([0x05], 0);
          // publicWithFlag.set(publicKey, 1);

          resolve(processKeys({ pubKey: publicKey.buffer, privKey: secretKey.buffer }));
        } catch (error) {
          reject(error);
        }
      });
      // var raw_keys = curve25519.keyPair(privKey);
      // if (raw_keys instanceof Promise) {
      //   return raw_keys.then(processKeys);
      // } else {
      //   return processKeys(raw_keys);
      // }
    },