From: Chris Duncan Date: Sat, 23 Nov 2024 09:47:23 +0000 (-0800) Subject: Start rewriting tweetnacl as module with exports. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=bfdd51a7419d2f0582cf896280bdbecfcc18538e;p=libnemo.git Start rewriting tweetnacl as module with exports. --- diff --git a/src/lib/workers/nano25519.ts b/src/lib/workers/nano25519.ts index fe633b7..de2536c 100644 --- a/src/lib/workers/nano25519.ts +++ b/src/lib/workers/nano25519.ts @@ -3,7 +3,6 @@ //@ts-nocheck import blake2b from 'blake2b' -(function(nacl) { 'use strict'; // Ported in 2014 by Dmitry Chestnykh and Devi Mandiri. @@ -2071,7 +2070,7 @@ import blake2b from 'blake2b' crypto_sign_SEEDBYTES = 32, crypto_hash_BYTES = 64; - nacl.lowlevel = { + export const lowlevel = { crypto_core_hsalsa20: crypto_core_hsalsa20, crypto_stream_xor: crypto_stream_xor, crypto_stream: crypto_stream, @@ -2153,38 +2152,13 @@ import blake2b from 'blake2b' for (var i = 0; i < arr.length; i++) arr[i] = 0; } - nacl.randomBytes = function(n) { + const randomBytes = function(n) { var b = new Uint8Array(n); randombytes(b, n); return b; }; - nacl.secretbox = function(msg, nonce, key) { - checkArrayTypes(msg, nonce, key); - checkLengths(key, nonce); - var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length); - var c = new Uint8Array(m.length); - for (var i = 0; i < msg.length; i++) m[i+crypto_secretbox_ZEROBYTES] = msg[i]; - crypto_secretbox(c, m, m.length, nonce, key); - return c.subarray(crypto_secretbox_BOXZEROBYTES); - }; - - nacl.secretbox.open = function(box, nonce, key) { - checkArrayTypes(box, nonce, key); - checkLengths(key, nonce); - var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length); - var m = new Uint8Array(c.length); - for (var i = 0; i < box.length; i++) c[i+crypto_secretbox_BOXZEROBYTES] = box[i]; - if (c.length < 32) return null; - if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null; - return m.subarray(crypto_secretbox_ZEROBYTES); - }; - - nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES; - nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES; - nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES; - - nacl.scalarMult = function(n, p) { + const scalarMult = function(n, p) { checkArrayTypes(n, p); if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size'); if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size'); @@ -2193,7 +2167,7 @@ import blake2b from 'blake2b' return q; }; - nacl.scalarMult.base = function(n) { + scalarMult.base = function(n) { checkArrayTypes(n); if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size'); var q = new Uint8Array(crypto_scalarmult_BYTES); @@ -2201,137 +2175,97 @@ import blake2b from 'blake2b' return q; }; - nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES; - nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES; - - nacl.box = function(msg, nonce, publicKey, secretKey) { - var k = nacl.box.before(publicKey, secretKey); - return nacl.secretbox(msg, nonce, k); - }; - - nacl.box.before = function(publicKey, secretKey) { - checkArrayTypes(publicKey, secretKey); - checkBoxLengths(publicKey, secretKey); - var k = new Uint8Array(crypto_box_BEFORENMBYTES); - crypto_box_beforenm(k, publicKey, secretKey); - return k; - }; - - nacl.box.after = nacl.secretbox; - - nacl.box.open = function(msg, nonce, publicKey, secretKey) { - var k = nacl.box.before(publicKey, secretKey); - return nacl.secretbox.open(msg, nonce, k); - }; - - nacl.box.open.after = nacl.secretbox.open; - - nacl.box.keyPair = function() { - var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES); - var sk = new Uint8Array(crypto_box_SECRETKEYBYTES); - crypto_box_keypair(pk, sk); - return {publicKey: pk, secretKey: sk}; - }; - - nacl.box.keyPair.fromSecretKey = function(secretKey) { - checkArrayTypes(secretKey); - if (secretKey.length !== crypto_box_SECRETKEYBYTES) - throw new Error('bad secret key size'); - var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES); - crypto_scalarmult_base(pk, secretKey); - return {publicKey: pk, secretKey: new Uint8Array(secretKey)}; - }; - - nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES; - nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES; - nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES; - nacl.box.nonceLength = crypto_box_NONCEBYTES; - nacl.box.overheadLength = nacl.secretbox.overheadLength; - - nacl.sign = function(msg, secretKey) { - checkArrayTypes(msg, secretKey); - if (secretKey.length !== crypto_sign_SECRETKEYBYTES) - throw new Error('bad secret key size'); - var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length); - crypto_sign(signedMsg, msg, msg.length, secretKey); - return signedMsg; - }; - - nacl.sign.open = function(signedMsg, publicKey) { - checkArrayTypes(signedMsg, publicKey); - if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) - throw new Error('bad public key size'); - var tmp = new Uint8Array(signedMsg.length); - var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey); - if (mlen < 0) return null; - var m = new Uint8Array(mlen); - for (var i = 0; i < m.length; i++) m[i] = tmp[i]; - return m; - }; - - nacl.sign.detached = function(msg, secretKey) { - var signedMsg = nacl.sign(msg, secretKey); - var sig = new Uint8Array(crypto_sign_BYTES); - for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i]; - return sig; - }; - - nacl.sign.detached.verify = function(msg, sig, publicKey) { - checkArrayTypes(msg, sig, publicKey); - if (sig.length !== crypto_sign_BYTES) - throw new Error('bad signature size'); - if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) - throw new Error('bad public key size'); - var sm = new Uint8Array(crypto_sign_BYTES + msg.length); - var m = new Uint8Array(crypto_sign_BYTES + msg.length); - var i; - for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i]; - for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i]; - return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0); - }; - - nacl.sign.keyPair = function() { - var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); - var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES); - crypto_sign_keypair(pk, sk); - return {publicKey: pk, secretKey: sk}; - }; - - nacl.sign.keyPair.fromSecretKey = function(secretKey) { - checkArrayTypes(secretKey); - if (secretKey.length !== crypto_sign_SECRETKEYBYTES) - throw new Error('bad secret key size'); - var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); - for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i]; - return {publicKey: pk, secretKey: new Uint8Array(secretKey)}; - }; + scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES; + scalarMult.groupElementLength = crypto_scalarmult_BYTES; + + const message = { + sign: (msg, secretKey) => { + checkArrayTypes(msg, secretKey); + if (secretKey.length !== crypto_sign_SECRETKEYBYTES) + throw new Error('bad secret key size'); + var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length); + crypto_sign(signedMsg, msg, msg.length, secretKey); + return signedMsg; + }, + + open: (signedMsg, publicKey) => { + checkArrayTypes(signedMsg, publicKey); + if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) + throw new Error('bad public key size'); + var tmp = new Uint8Array(signedMsg.length); + var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey); + if (mlen < 0) return null; + var m = new Uint8Array(mlen); + for (var i = 0; i < m.length; i++) m[i] = tmp[i]; + return m; + }, + + detach: (msg, secretKey) => { + var signedMsg = nacl.sign(msg, secretKey); + var sig = new Uint8Array(crypto_sign_BYTES); + for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i]; + return sig; + }, + + verify:(msg, sig, publicKey) => { + checkArrayTypes(msg, sig, publicKey); + if (sig.length !== crypto_sign_BYTES) + throw new Error('bad signature size'); + if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) + throw new Error('bad public key size'); + var sm = new Uint8Array(crypto_sign_BYTES + msg.length); + var m = new Uint8Array(crypto_sign_BYTES + msg.length); + var i; + for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i]; + for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i]; + return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0); + } + } - nacl.sign.keyPair.fromSeed = function(seed) { - checkArrayTypes(seed); - if (seed.length !== crypto_sign_SEEDBYTES) - throw new Error('bad seed size'); - var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); - var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES); - for (var i = 0; i < 32; i++) sk[i] = seed[i]; - crypto_sign_keypair(pk, sk, true); - return {publicKey: pk, secretKey: sk}; - }; + const keyPair = { + create: () => { + var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); + var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES); + crypto_sign_keypair(pk, sk); + return {publicKey: pk, secretKey: sk}; + }, + + fromSecretKey: (secretKey) => { + checkArrayTypes(secretKey); + if (secretKey.length !== crypto_sign_SECRETKEYBYTES) + throw new Error('bad secret key size'); + var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); + for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i]; + return {publicKey: pk, secretKey: new Uint8Array(secretKey)}; + }, + + fromSeed: (seed) => { + checkArrayTypes(seed); + if (seed.length !== crypto_sign_SEEDBYTES) + throw new Error('bad seed size'); + var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); + var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES); + for (var i = 0; i < 32; i++) sk[i] = seed[i]; + crypto_sign_keypair(pk, sk, true); + return {publicKey: pk, secretKey: sk}; + } + } - nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES; - nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES; - nacl.sign.seedLength = crypto_sign_SEEDBYTES; - nacl.sign.signatureLength = crypto_sign_BYTES; + export let publicKeyLength = crypto_sign_PUBLICKEYBYTES; + export let secretKeyLength = crypto_sign_SECRETKEYBYTES; + export let seedLength = crypto_sign_SEEDBYTES; + export let signatureLength = crypto_sign_BYTES; - nacl.hash = function(msg) { + const hash = function(msg) { checkArrayTypes(msg); var h = new Uint8Array(crypto_hash_BYTES); crypto_hash(h, msg, msg.length); return h; }; - nacl.hash.hashLength = crypto_hash_BYTES; + hash.hashLength = crypto_hash_BYTES; - nacl.verify = function(x, y) { + const verify = function(x, y) { checkArrayTypes(x, y); // Zero length arguments are considered not equal. if (x.length === 0 || y.length === 0) return false; @@ -2339,7 +2273,7 @@ import blake2b from 'blake2b' return (vn(x, 0, y, 0, x.length) === 0) ? true : false; }; - nacl.setPRNG = function(fn) { + const setPRNG = function(fn) { randombytes = fn; }; @@ -2371,5 +2305,4 @@ import blake2b from 'blake2b' } })(); - })(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {})); - \ No newline at end of file + export { keyPair, hash, message, randomBytes, scalarMult } \ No newline at end of file