mesur-io / dilithium

https://mesur-io.github.io/dilithium/spec
Other
0 stars 1 forks source link

sign and verify interface #4

Open OR13 opened 2 years ago

OR13 commented 2 years ago

currently:

      const privateKeyJwk = await api.generate();
      const message = "hello";
      const signature = await api.sign(message, privateKeyJwk);
      const verified = await api.verify(message, signature, privateKeyJwk);

in the future:

message needs to be a Uint8Array. publicKey and privateKey also need to be Uint8Array

publicKey = base64url.decode(privateKeyJwk.x) privateKey = base64url.decode(privateKeyJwk.d)

lowest level crypto should avoid passing around string encoded keys, signatures... serialization to string should happen only at an app boundary.

ideally these interface would also be type checked.

OR13 commented 2 years ago

for example:

const dilithium = require("../../util/api");

export const signer = (privateKeyJwk: any) => {
  return {
    async sign({ data }: { data: Uint8Array }) {
      const api = await dilithium.init();
      const signature = await api.sign(data, privateKeyJwk);
      return Uint8Array.from(Buffer.from(signature, "base64"));
    },
  };
};

export const verifier = (publicKeyJwk: any) => {
  return {
    async verify({
      data,
      signature,
    }: {
      data: Uint8Array;
      signature: Uint8Array;
    }) {
      let verified = false;
      try {
        const api = await dilithium.init();
        const sig = Buffer.from(signature).toString("base64");
        verified = await api.verify(data, sig, publicKeyJwk);
      } catch (e) {
        // console.error('An error occurred when verifying signature: ', e);
      }
      return verified;
    },
  };
};