]> git.codecow.com Git - libnemo.git/commitdiff
Execute functions regardless of conditional result.
authorChris Duncan <chris@zoso.dev>
Mon, 18 Aug 2025 04:39:42 +0000 (21:39 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 18 Aug 2025 04:39:42 +0000 (21:39 -0700)
To maintain constant time verification, conditional calls or returns now continue through the rest of the flow and discard results if unneeded.

src/lib/crypto/nano-nacl.ts

index 52bf2abf03016a9d5a1346da8a46d88e0c3a5378..33962ce55797cca98a228cfa58f575adb89b06e0 100644 (file)
@@ -370,20 +370,16 @@ export class NanoNaCl {
 \r
                this.Square(chk, r[0])\r
                this.Multiply(chk, chk, den)\r
-               if (this.neq25519(chk, num)) {\r
-                       this.Multiply(r[0], r[0], this.I)\r
-               }\r
+               this.Multiply(this.neq25519(chk, num) ? r[0] : new Float64Array(16), r[0], this.I)\r
 \r
                this.Square(chk, r[0])\r
                this.Multiply(chk, chk, den)\r
 \r
-               if (this.neq25519(chk, num)) return -1\r
+               const result = this.neq25519(chk, num) ? -1 : 0\r
 \r
-               if (this.par25519(r[0]) === (p[31] >> 7)) {\r
-                       this.Subtract(r[0], new Float64Array(16), r[0])\r
-               }\r
+               this.Subtract(this.par25519(r[0]) === (p[31] >> 7) ? r[0] : new Float64Array(16), new Float64Array(16), r[0])\r
                this.Multiply(r[3], r[0], r[1])\r
-               return 0\r
+               return result\r
        }\r
 \r
        static crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array, pk: Uint8Array): void {\r
@@ -506,7 +502,7 @@ export class NanoNaCl {
        static open (signedMessage: Uint8Array<ArrayBuffer>, publicKey: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>\r
        static open (signedMessage: unknown, publicKey: unknown): Uint8Array<ArrayBuffer> {\r
                try {\r
-                       if (!(signedMessage instanceof Uint8Array)) {\r
+                       if (!(signedMessage instanceof Uint8Array) || signedMessage.byteLength < this.crypto_sign_BYTES) {\r
                                throw new TypeError('Signed message must be Uint8Array')\r
                        }\r
                        if (!(publicKey instanceof Uint8Array) || publicKey.byteLength !== this.crypto_sign_PUBLICKEYBYTES) {\r