8000 refactor: drop modular copy by aeitwoen · Pull Request #420 · agntn/keys · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 11 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ catalog:
catalogs:
deps:
"@agntn/ciphers": 0.5.2
"@agntn/curves": 0.2.2
"@agntn/curves": 0.2.3
"@agntn/encodings": 0.2.0
"@agntn/hashes": 0.7.3
"@agntn/tools": 0.3.0
Expand Down
50 changes: 0 additions & 50 deletions src/utils/modular.ts

This file was deleted.

13 changes: 8 additions & 5 deletions src/utils/openpgp/algorithms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPrime } from "@agntn/curves";
import { invert, isPrime, power } from "@agntn/curves";
import { verify as verifyBrainpoolP256 } from "@agntn/curves/brainpoolp256r1";
import { verify as verifyBrainpoolP384 } from "@agntn/curves/brainpoolp384r1";
import { verify as verifyBrainpoolP512 } from "@agntn/curves/brainpoolp512r1";
Expand All @@ -20,7 +20,6 @@ import {
import { ed25519 } from "@noble/curves/ed25519.js";
import { secp256k1 } from "@noble/curves/secp256k1.js";
import { bytesToNumberBE, concatBytes, equalBytes, numberToBytesBE } from "../bytes.ts";
import { invert, pow } from "../modular.ts";
import { type Reader, createReader } from "./packets.ts";

/** One hash from RFC 9580 Table 23 that this module can compute. */
Expand Down Expand Up @@ -329,7 +328,8 @@ function checkDsaGroup(
rest: { readonly q: bigint; readonly g: bigint; readonly y: bigint },
): void {
const { q, g, y } = rest;
const inGroup = (value: bigint): boolean => value > 1n && value < p && pow(value, q, p) === 1n;
const inGroup = (value: bigint): boolean =>
value > 1n && value < p && power(value, q, { modulus: p }) === 1n;
const sized = p.toString(2).length >= MIN_DSA_BITS && DSA_SUBGROUP_BITS.has(q.toString(2).length);
if (!sized || (p - 1n) % q !== 0n || !isPrime(q) || !inGroup(g) || !inGroup(y)) {
throw new TypeError("OpenPGP DSA key has broken group parameters");
Expand Down Expand Up @@ -452,7 +452,7 @@ function verifyRsa(
const expected = pkcs1(digest, hash.prefix, length);
return (
expected !== undefined &&
equalBytes(numberToBytesBE(pow(signature, key.e, key.n), length), expected)
equalBytes(numberToBytesBE(power(signature, key.e, { modulus: key.n }), length), expected)
);
}

Expand All @@ -474,7 +474,10 @@ function verifyDsa(
if (r <= 0n || s <= 0n || r >= key.q || s >= key.q || digest.length * 8 < qBits) return false;
const z = bytesToNumberBE(digest) >> BigInt(digest.length * 8 - qBits);
const w = invert(s, key.q);
const v = (pow(key.g, (z * w) % key.q, key.p) * pow(key.y, (r * w) % key.q, key.p)) % key.p;
const v =
(power(key.g, (z * w) % key.q, { modulus: key.p }) *
power(key.y, (r * w) % key.q, { modulus: key.p })) %
key.p;
return v % key.q === r;
}

Expand Down
55 changes: 0 additions & 55 deletions test/utils/modular.test.ts

This file was deleted.

12 changes: 12 additions & 0 deletions test/utils/openpgp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ describe("OpenPGP cleartext verification", () => {
).toThrow("OpenPGP DSA key has broken group parameters");
});

/* curves inverts mod 1 to 0n instead of throwing. */
it("refuses a DSA key with q = 1 before invert sees it", () => {
const [packet] = packetsOf(dsa.publicKey);
if (packet === undefined) throw new Error("No key packet");
const { material } = readPublicKey(packet.body);
if (material.kind !== "dsa") throw new Error("Not a DSA key");
const numbers = [material.p, 1n, material.g, material.y];
expect(() =>
readKeyMaterial(17, Uint8Array.from(numbers.flatMap((value) => mpiOf(value)))),
).toThrow("OpenPGP DSA key has broken group parameters");
});

it("refuses an Ed25519 key at the identity, which ZIP 215 rules would let sign anything", () => {
const identity = Uint8Array.from([1, ...Array.from({ length: 31 }, () => 0)]);
const value = Uint8Array.from([...identity, ...Array.from({ length: 32 }, () => 0)]);
Expand Down
Loading
0