-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: eliminate OTP bias and timing attack vulnerability (#93)
* add unbiased random digit generator and timing-safe comparison * fix(password): use unbiased OTP generation and timing-safe comparison * fix(code): use unbiased OTP generation and timing-safe comparison * Create otp-bias-fix.md
- Loading branch information
1 parent
70cb2cb
commit 8d6a243
Showing
4 changed files
with
36 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@openauthjs/openauth": patch | ||
--- | ||
|
||
fix: eliminate OTP bias and timing attack vulnerability |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { timingSafeEqual } from "crypto" | ||
|
||
export function generateUnbiasedDigits(length: number): string { | ||
const result: number[] = [] | ||
while (result.length < length) { | ||
const buffer = crypto.getRandomValues(new Uint8Array(length * 2)) | ||
for (const byte of buffer) { | ||
if (byte < 250 && result.length < length) { | ||
result.push(byte % 10) | ||
} | ||
} | ||
} | ||
return result.join("") | ||
} | ||
|
||
export function timingSafeCompare(a: string, b: string): boolean { | ||
if (typeof a !== "string" || typeof b !== "string") { | ||
return false | ||
} | ||
if (a.length !== b.length) { | ||
return false | ||
} | ||
return timingSafeEqual(Buffer.from(a), Buffer.from(b)) | ||
} |