]> git.codecow.com Git - libnemo.git/commitdiff
Condense array checks. Revert verify to calling crypto_sign_open directly.
authorChris Duncan <chris@zoso.dev>
Mon, 18 Aug 2025 04:32:42 +0000 (21:32 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 18 Aug 2025 04:32:42 +0000 (21:32 -0700)
src/lib/crypto/nano-nacl.ts

index 45950ac162c141cfd4ae108063f80556bc0584ed..52bf2abf03016a9d5a1346da8a46d88e0c3a5378 100644 (file)
@@ -509,18 +509,14 @@ export class NanoNaCl {
                        if (!(signedMessage instanceof Uint8Array)) {\r
                                throw new TypeError('Signed message must be Uint8Array')\r
                        }\r
-                       if (!(publicKey instanceof Uint8Array)) {\r
-                               throw new TypeError('Public key must be Uint8Array')\r
-                       }\r
-                       if (publicKey.byteLength !== this.crypto_sign_PUBLICKEYBYTES) {\r
-                               throw new Error(`Public key must be ${this.crypto_sign_PUBLICKEYBYTES} bytes`)\r
+                       if (!(publicKey instanceof Uint8Array) || publicKey.byteLength !== this.crypto_sign_PUBLICKEYBYTES) {\r
+                               throw new Error(`Public key must be ${this.crypto_sign_PUBLICKEYBYTES}-byte Uint8Array`)\r
                        }\r
                        const sm = new Uint8Array(signedMessage)\r
+                       const smLen = sm.byteLength\r
                        const pub = new Uint8Array(publicKey)\r
-                       signedMessage = undefined\r
-                       publicKey = undefined\r
-                       const tmp = new Uint8Array(sm.length)\r
-                       const mLen = this.crypto_sign_open(tmp, sm, sm.length, pub)\r
+                       const tmp = new Uint8Array(smLen)\r
+                       const mLen = this.crypto_sign_open(tmp, sm, smLen, pub)\r
                        if (mLen < 0) {\r
                                throw new Error('Signature verification failed')\r
                        }\r
@@ -545,19 +541,16 @@ export class NanoNaCl {
                        if (!(message instanceof Uint8Array)) {\r
                                throw new TypeError('Message must be Uint8Array')\r
                        }\r
-                       if (!(privateKey instanceof Uint8Array)) {\r
-                               throw new TypeError('Private key must be Uint8Array')\r
-                       }\r
-                       if (privateKey.byteLength !== this.crypto_sign_SEEDBYTES) {\r
-                               throw new Error(`Private key must be ${this.crypto_sign_PRIVATEKEYBYTES} bytes`)\r
+                       if (!(privateKey instanceof Uint8Array) || privateKey.byteLength !== this.crypto_sign_SEEDBYTES) {\r
+                               throw new Error(`Private key must be ${this.crypto_sign_PRIVATEKEYBYTES}-byte Uint8Array`)\r
                        }\r
-                       const msg = new Uint8Array(message)\r
                        const prv = new Uint8Array(privateKey)\r
-                       message = undefined\r
                        privateKey = undefined\r
-                       const signed = new Uint8Array(this.crypto_sign_BYTES + msg.length)\r
+                       const mLen = message.byteLength\r
+                       const msg = new Uint8Array(message)\r
+                       const signed = new Uint8Array(this.crypto_sign_BYTES + mLen)\r
                        const pub = this.convert(prv)\r
-                       this.crypto_sign(signed, msg, msg.length, prv, pub)\r
+                       this.crypto_sign(signed, msg, mLen, prv, pub)\r
                        return signed\r
                } catch (err) {\r
                        throw new Error('Failed to sign and return message', { cause: err })\r
@@ -578,16 +571,21 @@ export class NanoNaCl {
                        if (!(signedMessage instanceof Uint8Array)) {\r
                                throw new TypeError('Signed message must be Uint8Array')\r
                        }\r
-                       if (!(signature instanceof Uint8Array)) {\r
-                               throw new TypeError('Signature must be Uint8Array')\r
+                       if (!(signature instanceof Uint8Array) || signature.byteLength !== this.crypto_sign_BYTES) {\r
+                               throw new Error(`Signature must be ${this.crypto_sign_BYTES}-byte Uint8Array`)\r
                        }\r
-                       if (signature.byteLength !== this.crypto_sign_BYTES) {\r
-                               throw new Error(`Signature must be ${this.crypto_sign_BYTES} bytes`)\r
+                       if (!(publicKey instanceof Uint8Array) || publicKey.byteLength !== this.crypto_sign_PUBLICKEYBYTES) {\r
+                               throw new Error(`Public key must be ${this.crypto_sign_PUBLICKEYBYTES}-byte Uint8Array`)\r
                        }\r
-                       const sm = new Uint8Array(this.crypto_sign_BYTES + signedMessage.byteLength)\r
-                       sm.set(signature, 0)\r
-                       sm.set(signedMessage, this.crypto_sign_BYTES)\r
-                       return (this.open(sm, publicKey as Uint8Array<ArrayBuffer>).byteLength >= 0)\r
+                       const msg = new Uint8Array(signedMessage)\r
+                       const sig = new Uint8Array(signature)\r
+                       const pub = new Uint8Array(publicKey)\r
+                       const smLen = this.crypto_sign_BYTES + msg.byteLength\r
+                       const sm = new Uint8Array(smLen)\r
+                       const m = new Uint8Array(smLen)\r
+                       sm.set(sig, 0)\r
+                       sm.set(msg, this.crypto_sign_BYTES)\r
+                       return (this.crypto_sign_open(m, sm, smLen, pub) >= 0)\r
                } catch (err) {\r
                        throw new Error('Failed to verify signature on message with the given public key', { cause: err })\r
                }\r