Skip to content

Commit

Permalink
fix: SecretKey.fromBytes perform constant time comparison to zero byt…
Browse files Browse the repository at this point in the history
…es (#108)

* fix: SecretKey.fromBytes perform constant time comparison to zero bytes

* Adds ZERO_BYTES constant

* Update lib.ts

---------

Co-authored-by: Lion - dapplion <35266934+dapplion@users.noreply.github.com>
  • Loading branch information
has5aan and dapplion authored Sep 11, 2023
1 parent b1ba633 commit 56d9a80
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const PUBLIC_KEY_LENGTH_COMPRESSED = 48;
const PUBLIC_KEY_LENGTH_UNCOMPRESSED = 48 * 2;
const SIGNATURE_LENGTH_COMPRESSED = 96;
const SIGNATURE_LENGTH_UNCOMPRESSED = 96 * 2;
const SECRET_KEY_ZERO_BYTES = new Uint8Array(SECRET_KEY_LENGTH);

export {BLST_ERROR};
export class ErrorBLST extends Error {
Expand Down Expand Up @@ -64,7 +65,7 @@ export class SecretKey {
if (skBytes.length !== SECRET_KEY_LENGTH) {
throw new ErrorBLST(BLST_ERROR.BLST_INVALID_SIZE);
}
if (isZeroBytes(skBytes)) {
if (crypto.timingSafeEqual(skBytes, SECRET_KEY_ZERO_BYTES)) {
throw new ErrorBLST(BLST_ERROR.ZERO_SECRET_KEY);
}
const sk = new SkConstructor();
Expand Down Expand Up @@ -308,12 +309,3 @@ function randomBytesNonZero(BYTES_COUNT: number): Buffer {
rand[0] = 1;
return rand;
}

function isZeroBytes(bytes: Uint8Array): boolean {
for (let i = 0; i < bytes.length; i++) {
if (bytes[i] !== 0) {
return false;
}
}
return true;
}

0 comments on commit 56d9a80

Please sign in to comment.