]> zoso.dev Git - libnemo.git/commitdiff
We're only going to work in browser environments from here on out, so lose the node...
authorChris Duncan <chris@zoso.dev>
Fri, 29 Nov 2024 10:35:49 +0000 (02:35 -0800)
committerChris Duncan <chris@zoso.dev>
Fri, 29 Nov 2024 10:35:49 +0000 (02:35 -0800)
src/lib/workers/nano-nacl.ts

index 296b3ecef87bb6b5dfc86cb22e0ca02105856350..4ef0df1012520a92506c8e97d2c8952d8e14abb1 100644 (file)
@@ -34,9 +34,6 @@ async function NanoNaCl (Blake2b: Blake2b) {
                return r\r
        }\r
 \r
-       //  Pluggable, initialized in high-level API below.\r
-       var randombytes = function (x, n) { throw new Error('no PRNG') }\r
-\r
        var gf0 = gf(),\r
                gf1 = gf([1]),\r
                D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]),\r
@@ -606,12 +603,21 @@ async function NanoNaCl (Blake2b: Blake2b) {
                scalarmult(p, q, s)\r
        }\r
 \r
-       function crypto_sign_keypair (pk, sk, seeded?: boolean) {\r
+       function crypto_sign_keypair (pk: Uint8Array, sk: Uint8Array, seeded?: boolean) {\r
                var d = new Uint8Array(64)\r
                var p = [gf(), gf(), gf(), gf()]\r
                var i\r
 \r
-               if (!seeded) randombytes(sk, 32)\r
+               if (!seeded) {\r
+                       var QUOTA = 65536       // https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues\r
+                       var i, n = 32, v = new Uint8Array(n)\r
+                       for (i = 0; i < n; i += QUOTA) {\r
+                               crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)))\r
+                       }\r
+                       for (i = 0; i < n; i++) sk[i] = v[i]\r
+                       cleanup(v)\r
+               }\r
+\r
                crypto_hash(d, sk, 32)\r
                d[0] &= 248\r
                d[31] &= 127\r
@@ -856,41 +862,6 @@ async function NanoNaCl (Blake2b: Blake2b) {
        const seedLength = crypto_sign_SEEDBYTES\r
        const signatureLength = crypto_sign_BYTES\r
 \r
-       const setPRNG = function (fn) {\r
-               randombytes = fn\r
-       };\r
-\r
-       (function () {\r
-               // Initialize PRNG if environment provides CSPRNG.\r
-               // If not, methods calling randombytes will throw.\r
-               var crypto = self?.crypto ?? null\r
-               if (crypto && crypto.getRandomValues) {\r
-                       // Browsers.\r
-                       var QUOTA = 65536\r
-                       setPRNG(function (x, n) {\r
-                               var i, v = new Uint8Array(n)\r
-                               for (i = 0; i < n; i += QUOTA) {\r
-                                       crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)))\r
-                               }\r
-                               for (i = 0; i < n; i++) x[i] = v[i]\r
-                               cleanup(v)\r
-                       })\r
-               } else if (typeof require !== 'undefined') {\r
-                       // Node.js.\r
-                       try {\r
-                               crypto = require('node:crypto')\r
-                               if (crypto && crypto.randomBytes) {\r
-                                       setPRNG(function (x, n) {\r
-                                               var i, v = crypto.randomBytes(n)\r
-                                               for (i = 0; i < n; i++) x[i] = v[i]\r
-                                               cleanup(v)\r
-                                       })\r
-                               }\r
-                       } catch (err) {\r
-                               throw new Error('failed to load node:crypto')\r
-                       }\r
-               }\r
-       })()\r
 }\r
 \r
 export const nacl = {\r