]> git.codecow.com Git - nano25519.git/commitdiff
Restore captured byte allocation and object & array accessors. Validate length of...
authorChris Duncan <chris@codecow.com>
Fri, 16 Oct 2026 14:42:04 +0000 (07:42 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 16 Oct 2026 14:42:04 +0000 (07:42 -0700)
src/lib/bytes.ts
src/lib/derive.ts
src/lib/sign.ts
src/lib/verify.ts

index 2a431efd9635d1373b30a2f0dc6760420a15ef3c..835ca1b70daa04650ede25272463fdb5fd6bef41 100644 (file)
@@ -9,9 +9,10 @@ import { Nano25519RangeError, Nano25519TypeError, } from './errors'
  * that code patching a prototype afterwards cannot change what is validated,
  * copied, scrubbed or decoded. The `%TypedArray%.prototype` getters also read
  * internal slots, which a caller's subclass cannot override, so use these
- * helpers on every typed array instead of its own properties. Global
- * constructors and static functions are looked up at call time; replacing them,
- * or running before this module, is out of scope.
+ * helpers on every typed array instead of its own properties. Use the captured
+ * `allocate()` and `words()` instead of `Uint8Array` and `Uint32Array`. Other
+ * global constructors and static functions are looked up at time-of-call, and
+ * capturing them is out of scope.
  */
 
 /** Type aliases for legibility. */
@@ -54,6 +55,15 @@ function isArrayBuffer (a: unknown): a is ArrayBuffer {
        return false
 }
 
+/** Captured version of `new Uint8Array(length)` with argument type-checking. */
+export function allocate (byteLength: number): Bytes {
+       const length = byteLength
+       if (typeof length !== 'number') {
+               throw new Nano25519TypeError('Invalid byte length')
+       }
+       return new Bytes(length)
+}
+
 /** Captured version of `Uint8Array.prototype.byteLength`. */
 export function byteLength (bytes: Uint8Array): number {
        return getByteLength(bytes)
@@ -175,7 +185,7 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n
                if (value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) {
                        throw new Nano25519TypeError(`Invalid hexadecimal length ${value.length} for ${name}`)
                }
-               if (typeof out === 'undefined') out = new Bytes(value.length >> 1)
+               if (typeof out === 'undefined') out = allocate(value.length >> 1)
                hexToBytes(out, value)
                return out
        }
@@ -188,7 +198,7 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n
                if (byteLength > byteLengthMax) {
                        throw new Nano25519TypeError(`${name} must be no more than ${byteLengthMax} bytes`)
                }
-               if (typeof out === 'undefined') out = new Bytes(byteLength)
+               if (typeof out === 'undefined') out = allocate(byteLength)
                copy(out, value)
                return out
        }
index 57862a21228597ed4bd047f373aa962cc9d2519c..4dc517944ea826c994530e038474dac72886ee9c 100644 (file)
@@ -1,13 +1,13 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './bytes'
+import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './bytes'
 import { Nano25519TypeError, } from './errors'
 import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm'
 
 const { KEY_BYTELENGTH, } = constants
-const privateKey = new Uint8Array(KEY_BYTELENGTH)
-const publicKey = new Uint8Array(KEY_BYTELENGTH)
+const privateKey = allocate(KEY_BYTELENGTH)
+const publicKey = allocate(KEY_BYTELENGTH)
 
 export function derive (prv: unknown, out?: unknown): string | void {
        if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === KEY_BYTELENGTH)) {
index 0de6bb792ed03a9452095d8f00a001caf37a1aec..6ad1a830ac1f4fd1d1ae6cd433467bbf267d1239 100644 (file)
@@ -1,14 +1,14 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './bytes'
+import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './bytes'
 import { Nano25519TypeError, } from './errors'
 import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm'
 
 const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants
-const privateKey = new Uint8Array(KEY_BYTELENGTH)
-const publicKey = new Uint8Array(KEY_BYTELENGTH)
-const signature = new Uint8Array(SIGNATURE_BYTELENGTH)
+const privateKey = allocate(KEY_BYTELENGTH)
+const publicKey = allocate(KEY_BYTELENGTH)
+const signature = allocate(SIGNATURE_BYTELENGTH)
 
 export function sign (msg: unknown, prv: unknown, pub: unknown, out?: unknown): string | void {
        if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === SIGNATURE_BYTELENGTH)) {
index a97918e0b1af36914ba68dd0eacda7a36d63246f..6fec00733e7beda1a876266edd5f93d7083141e5 100644 (file)
@@ -1,14 +1,16 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { byteLength, copy, normalize, } from './bytes'
+import { allocate, byteLength, copy, normalize, } from './bytes'
 import { Nano25519RangeError, Nano25519TypeError, } from './errors'
 import { Mutex, constants, getOutput, setInput, verify as wasm_verify, verify_blocks as wasm_verify_blocks, } from './wasm'
 
 const { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, SIGNATURE_BYTELENGTH, } = constants
-const hash = new Uint8Array(BLOCKHASH_BYTELENGTH)
-const publicKey = new Uint8Array(KEY_BYTELENGTH)
-const signature = new Uint8Array(SIGNATURE_BYTELENGTH)
+const { create, defineProperty, freeze } = Object
+const { isArray } = Array
+const hash = allocate(BLOCKHASH_BYTELENGTH)
+const publicKey = allocate(KEY_BYTELENGTH)
+const signature = allocate(SIGNATURE_BYTELENGTH)
 
 export function verify (sig: unknown, msg: unknown, pub: unknown): boolean {
        Mutex.lock()
@@ -28,8 +30,11 @@ export function verify (sig: unknown, msg: unknown, pub: unknown): boolean {
 }
 
 export function verify_blocks (pub: unknown, data: unknown): readonly boolean[] {
-       const blocks: unknown[] = Array.isArray(data) ? data : [data]
-       const count = blocks.length
+       const blocks: unknown[] = isArray(data) ? data : [data]
+       const count: unknown = blocks.length
+       if (typeof count !== 'number') {
+               throw new Nano25519TypeError('Invalid blocks length', { cause: data })
+       }
        if (count < 1) {
                throw new Nano25519RangeError('Must pass at least one block', { cause: data })
        }
@@ -41,7 +46,7 @@ export function verify_blocks (pub: unknown, data: unknown): readonly boolean[]
        try {
                normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey)
                setInput('pub', publicKey)
-               const message = new Uint8Array(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH))
+               const message = allocate(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH))
                for (let b = 0; b < count; b++) {
                        const block = blocks[b]
                        if (block == null || typeof block !== 'object') {
@@ -62,14 +67,14 @@ export function verify_blocks (pub: unknown, data: unknown): readonly boolean[]
                setInput('msg', message)
                wasm_verify_blocks(count)
                for (let i = 0; i < count; i++) {
-                       const descriptor = Object.create(null)
+                       const descriptor = create(null)
                        descriptor.value = getOutput('verify', i) === 1
                        descriptor.writable = true
                        descriptor.enumerable = true
                        descriptor.configurable = true
-                       Object.defineProperty(verified, i, descriptor)
+                       defineProperty(verified, i, descriptor)
                }
-               return Object.freeze(verified)
+               return freeze(verified)
        } finally {
                Mutex.unlock()
        }