]> git.codecow.com Git - nano25519.git/commitdiff
Declare keys and signatures outside of try block so they can be properly cleared...
authorChris Duncan <chris@codecow.com>
Sat, 10 Oct 2026 18:46:26 +0000 (11:46 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 10 Oct 2026 18:46:26 +0000 (11:46 -0700)
src/lib/derive.ts
src/lib/sign.ts

index 3a40eae2bdfb0fa808ce46afa387af593e24e2c0..dbe7d6897112d69c146b2b8102f334f6ccd80779 100644 (file)
@@ -5,15 +5,16 @@ import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from
 import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm'
 
 const { KEY_BYTELENGTH, } = constants
+let privateKey = allocate(KEY_BYTELENGTH)
+let publicKey = allocate(KEY_BYTELENGTH)
 
 export function derive (prv: unknown, out?: unknown): string | void {
        if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === KEY_BYTELENGTH)) {
                throw new TypeError('Derive output buffer must be 32-byte Uint8Array<ArrayBuffer>')
        }
-       const privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv)
-       const publicKey = allocate(KEY_BYTELENGTH)
        Mutex.lock()
        try {
+               privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv)
                setInput('prv', privateKey)
                wasm_derive()
                for (let i = 0; i < KEY_BYTELENGTH; i++) {
index ce11676e1b307535b3b00970a060cf003304e560..1bc7c16b87569f88ec2548f0c0e343d4282a829c 100644 (file)
@@ -5,17 +5,19 @@ import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from
 import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm'
 
 const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants
+let privateKey = allocate(KEY_BYTELENGTH)
+let publicKey = allocate(KEY_BYTELENGTH)
+let 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)) {
                throw new TypeError(`Sign output buffer must be ${SIGNATURE_BYTELENGTH}-byte Uint8Array<ArrayBuffer>`)
        }
-       const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg)
-       const privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv)
-       const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub)
-       const signature = allocate(SIGNATURE_BYTELENGTH)
        Mutex.lock()
        try {
+               const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg)
+               privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv)
+               publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub)
                setInput('msg', message)
                setInput('prv', privateKey)
                setInput('pub', publicKey)