@pqc-sdk/cli 0.1.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,12 +13,15 @@ npx @pqc-sdk/cli init
13
13
  # Generate keys serialized as base64url
14
14
  npx @pqc-sdk/cli keygen --algorithm ml-dsa-65 --out keys/
15
15
 
16
- # Detect pre-quantum crypto (RSA/ECDSA/ECDH) and what to migrate to PQC
16
+ # Heuristically detect pre-quantum crypto (RSA/ECDSA/ECDH) and what to migrate to PQC
17
17
  npx @pqc-sdk/cli audit
18
18
  ```
19
19
 
20
- `audit` exits with code 1 when it finds crypto to migrate usable as a CI
21
- gate. Output uses colors only when there is a TTY: readable in logs and pipes.
20
+ `audit` is a best-effort regex scan of your dependencies and source: expect the
21
+ occasional false positive or false negative, and treat it as a starting point,
22
+ not a proof. Files larger than 1 MiB are skipped and reported. It exits with
23
+ code 1 when it finds crypto to migrate — usable as a CI gate. Output uses colors
24
+ only when there is a TTY: readable in logs and pipes.
22
25
 
23
26
  ## License
24
27
 
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  import { defineCommand as defineCommand4, runMain } from "citty";
5
5
 
6
6
  // src/commands/audit.ts
7
- import { readFile, readdir } from "fs/promises";
7
+ import { readFile, readdir, stat } from "fs/promises";
8
8
  import { existsSync } from "fs";
9
9
  import { join, relative } from "path";
10
10
  import { defineCommand } from "citty";
@@ -28,8 +28,12 @@ var finding = (location, what, migrateTo) => {
28
28
  var heading = (message) => {
29
29
  console.log(pc.bold(message));
30
30
  };
31
+ var note = (message) => {
32
+ console.log(pc.dim(message));
33
+ };
31
34
 
32
35
  // src/commands/audit.ts
36
+ var MAX_FILE_BYTES = 1024 * 1024;
33
37
  var ML_DSA = "ML-DSA-65 (pqc.sign / pqc.verify)";
34
38
  var ML_KEM = "ML-KEM-768 + AES-256-GCM (pqc.encrypt / pqc.decrypt)";
35
39
  var RISKY_PACKAGES = {
@@ -106,7 +110,12 @@ async function auditPackageJson(cwd) {
106
110
  }
107
111
  async function auditSources(cwd) {
108
112
  const findings = [];
113
+ const skipped = [];
109
114
  for (const file of await collectSourceFiles(cwd)) {
115
+ if ((await stat(file)).size > MAX_FILE_BYTES) {
116
+ skipped.push(relative(cwd, file));
117
+ continue;
118
+ }
110
119
  const lines = (await readFile(file, "utf8")).split("\n");
111
120
  lines.forEach((line, index) => {
112
121
  for (const { re, what, migrateTo } of CODE_PATTERNS) {
@@ -116,16 +125,25 @@ async function auditSources(cwd) {
116
125
  }
117
126
  });
118
127
  }
119
- return findings;
128
+ return { findings, skipped };
120
129
  }
121
130
  var audit = defineCommand({
122
131
  meta: {
123
132
  name: "audit",
124
- description: "Detect pre-quantum crypto and suggest the PQC equivalent"
133
+ description: "Heuristically detect pre-quantum crypto (best-effort regex scan) and suggest the PQC equivalent"
125
134
  },
126
135
  async run() {
127
136
  const cwd = process.cwd();
128
- const findings = [...await auditPackageJson(cwd), ...await auditSources(cwd)];
137
+ const { findings: sourceFindings, skipped } = await auditSources(cwd);
138
+ const findings = [...await auditPackageJson(cwd), ...sourceFindings];
139
+ note(
140
+ "Heuristic, best-effort regex scan \u2014 expect occasional false positives and false negatives."
141
+ );
142
+ if (skipped.length > 0) {
143
+ note(
144
+ `Skipped ${skipped.length} file(s) larger than ${MAX_FILE_BYTES / 1024 / 1024} MiB: ${skipped.join(", ")}`
145
+ );
146
+ }
129
147
  if (findings.length === 0) {
130
148
  ok("No pre-quantum crypto detected.");
131
149
  return;
@@ -150,10 +168,26 @@ import { existsSync as existsSync3 } from "fs";
150
168
  import { defineCommand as defineCommand2 } from "citty";
151
169
 
152
170
  // src/keyfiles.ts
153
- import { chmod, mkdir, writeFile } from "fs/promises";
171
+ import { chmod, mkdir, readFile as readFile2, writeFile } from "fs/promises";
154
172
  import { existsSync as existsSync2 } from "fs";
155
173
  import { join as join2 } from "path";
156
174
  import { SUPPORTED_ALGORITHMS, pqc } from "@pqc-sdk/core";
175
+ var KEY_IGNORE_PATTERNS = ["keys/", "*.secret.pqc"];
176
+ async function ensureKeysIgnored(cwd) {
177
+ const gitignorePath = join2(cwd, ".gitignore");
178
+ const current = existsSync2(gitignorePath) ? await readFile2(gitignorePath, "utf8") : "";
179
+ const present = new Set(current.split(/\r?\n/).map((line) => line.trim()));
180
+ const missing = KEY_IGNORE_PATTERNS.filter((pattern) => !present.has(pattern));
181
+ if (missing.length === 0) {
182
+ return [];
183
+ }
184
+ const prefix = current === "" ? "" : current.endsWith("\n") ? "\n" : "\n\n";
185
+ const block = `${prefix}# PQC key material \u2014 never commit secret keys
186
+ ${missing.join("\n")}
187
+ `;
188
+ await writeFile(gitignorePath, current + block);
189
+ return [...missing];
190
+ }
157
191
  function assertSupportedAlgorithm(value) {
158
192
  if (!SUPPORTED_ALGORITHMS.includes(value)) {
159
193
  throw new Error(
@@ -162,6 +196,18 @@ function assertSupportedAlgorithm(value) {
162
196
  }
163
197
  return value;
164
198
  }
199
+ function assertSafeName(value) {
200
+ if (value === "") {
201
+ throw new Error("Invalid --name: must not be empty.");
202
+ }
203
+ if (value.includes("/") || value.includes("\\")) {
204
+ throw new Error('Invalid --name: must not contain path separators ("/" or "\\").');
205
+ }
206
+ if (value.includes("..")) {
207
+ throw new Error('Invalid --name: must not contain "..".');
208
+ }
209
+ return value;
210
+ }
165
211
  async function writeKeyPair(directory, baseName, algorithm, force) {
166
212
  const publicPath = join2(directory, `${baseName}.public.pqc`);
167
213
  const secretPath = join2(directory, `${baseName}.secret.pqc`);
@@ -223,11 +269,22 @@ var init = defineCommand2({
223
269
  await writeFile2(CONFIG_FILE, `${JSON.stringify(CONFIG, null, 2)}
224
270
  `);
225
271
  const keys = await writeKeyPair(CONFIG.keysDir, "dev", CONFIG.defaultAlgorithm, false);
226
- await writeFile2("example.ts", EXAMPLE);
272
+ const ignored = await ensureKeysIgnored(process.cwd());
273
+ const exampleWritten = !existsSync3("example.ts");
274
+ if (exampleWritten) {
275
+ await writeFile2("example.ts", EXAMPLE);
276
+ }
227
277
  heading("PQC project initialized:");
228
278
  item(`${CONFIG_FILE} \u2014 configuration with safe defaults`);
229
279
  item(`${keys.publicPath} / ${keys.secretPath} \u2014 development ${keys.algorithm} pair`);
230
- item("example.ts \u2014 full roundtrip ready to run");
280
+ if (ignored.length > 0) {
281
+ item(`.gitignore \u2014 added ${ignored.join(", ")} so secret keys are never committed`);
282
+ }
283
+ if (exampleWritten) {
284
+ item("example.ts \u2014 full roundtrip ready to run");
285
+ } else {
286
+ warn("example.ts already exists \u2014 left untouched.");
287
+ }
231
288
  console.log();
232
289
  warn("The keys/dev.* keys are for development ONLY \u2014 do NOT use them in production.");
233
290
  ok(`Next step: run example.ts (node --experimental-strip-types example.ts or tsx)`);
@@ -247,6 +304,10 @@ var keygen = defineCommand3({
247
304
  description: "Algorithm of the pair (ml-kem-768 or ml-dsa-65)",
248
305
  default: "ml-kem-768"
249
306
  },
307
+ name: {
308
+ type: "string",
309
+ description: "Base file name for the key pair (default: the algorithm, e.g. ml-kem-768)"
310
+ },
250
311
  out: {
251
312
  type: "string",
252
313
  description: "Output directory",
@@ -260,10 +321,15 @@ var keygen = defineCommand3({
260
321
  },
261
322
  async run({ args }) {
262
323
  const algorithm = assertSupportedAlgorithm(args.algorithm);
263
- const keys = await writeKeyPair(args.out, algorithm, algorithm, args.force);
324
+ const baseName = args.name === void 0 ? algorithm : assertSafeName(args.name);
325
+ const keys = await writeKeyPair(args.out, baseName, algorithm, args.force);
326
+ const ignored = await ensureKeysIgnored(process.cwd());
264
327
  ok(`${algorithm} pair generated:`);
265
328
  item(`public: ${keys.publicPath}`);
266
329
  item(`secret: ${keys.secretPath} (mode 0600)`);
330
+ if (ignored.length > 0) {
331
+ item(`.gitignore: added ${ignored.join(", ")} so secret keys are never committed`);
332
+ }
267
333
  warn("The secret key must not be committed or leave this environment.");
268
334
  }
269
335
  });
@@ -272,7 +338,7 @@ var keygen = defineCommand3({
272
338
  var main = defineCommand4({
273
339
  meta: {
274
340
  name: "pqc",
275
- version: "0.1.2",
341
+ version: "0.3.0",
276
342
  description: "CLI for the post-quantum cryptography SDK (@pqc-sdk/core)"
277
343
  },
278
344
  subCommands: {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/commands/audit.ts","../src/ui.ts","../src/commands/init.ts","../src/keyfiles.ts","../src/commands/keygen.ts"],"sourcesContent":["import { defineCommand, runMain } from 'citty';\n\nimport { audit } from './commands/audit.js';\nimport { init } from './commands/init.js';\nimport { keygen } from './commands/keygen.js';\n\n// Injected by tsup (`define` in tsup.config.ts) from package.json at build time.\ndeclare const __PQC_CLI_VERSION__: string;\n\nconst main = defineCommand({\n meta: {\n name: 'pqc',\n version: __PQC_CLI_VERSION__,\n description: 'CLI for the post-quantum cryptography SDK (@pqc-sdk/core)',\n },\n subCommands: {\n init,\n keygen,\n audit,\n },\n});\n\nawait runMain(main);\n","import { readFile, readdir } from 'node:fs/promises';\nimport { existsSync } from 'node:fs';\nimport { join, relative } from 'node:path';\n\nimport { defineCommand } from 'citty';\nimport pc from 'picocolors';\n\nimport { finding, heading, ok } from '../ui.js';\n\ninterface Finding {\n location: string;\n what: string;\n migrateTo: string;\n}\n\nconst ML_DSA = 'ML-DSA-65 (pqc.sign / pqc.verify)';\nconst ML_KEM = 'ML-KEM-768 + AES-256-GCM (pqc.encrypt / pqc.decrypt)';\n\nconst RISKY_PACKAGES: Record<string, { what: string; migrateTo: string }> = {\n jsonwebtoken: { what: 'JWTs signed with RSA/ECDSA (RS256/ES256)', migrateTo: ML_DSA },\n jose: { what: 'JOSE/JWT with RSA/ECDSA algorithms', migrateTo: ML_DSA },\n elliptic: { what: 'Classic elliptic curves (ECDSA/ECDH)', migrateTo: `${ML_DSA} and ${ML_KEM}` },\n secp256k1: { what: 'ECDSA signatures over secp256k1', migrateTo: ML_DSA },\n 'node-rsa': { what: 'RSA encryption and signatures', migrateTo: `${ML_KEM} and ${ML_DSA}` },\n 'node-forge': { what: 'Classic RSA/X.509', migrateTo: `${ML_KEM} and ${ML_DSA}` },\n tweetnacl: { what: 'Ed25519/X25519 (pre-quantum)', migrateTo: `${ML_DSA} and ${ML_KEM}` },\n};\n\nconst CODE_PATTERNS: ReadonlyArray<{ re: RegExp; what: string; migrateTo: string }> = [\n {\n re: /create(?:Sign|Verify)\\s*\\(/,\n what: 'RSA/ECDSA signing via node:crypto (createSign/createVerify)',\n migrateTo: ML_DSA,\n },\n {\n re: /createECDH\\s*\\(|\\.diffieHellman\\s*\\(/,\n what: 'ECDH/DH key exchange',\n migrateTo: ML_KEM,\n },\n {\n re: /publicEncrypt\\s*\\(|privateDecrypt\\s*\\(/,\n what: 'RSA encryption (publicEncrypt/privateDecrypt)',\n migrateTo: ML_KEM,\n },\n {\n re: /generateKeyPair(?:Sync)?\\s*\\(\\s*['\"](?:rsa|rsa-pss|dsa|ec|ed25519|ed448|x25519|x448)['\"]/,\n what: 'pre-quantum keypair generation',\n migrateTo: 'pqc.keys.generate (ML-KEM-768 for encryption, ML-DSA-65 for signatures)',\n },\n {\n re: /['\"`](?:RS|ES|PS)(?:256|384|512)['\"`]/,\n what: 'JWT with an RSA/ECDSA signing algorithm',\n migrateTo: ML_DSA,\n },\n {\n re: /['\"`](?:RSA-OAEP|ECDH|ECDSA)['\"`]/,\n what: 'WebCrypto with a pre-quantum algorithm',\n migrateTo: `${ML_KEM} or ${ML_DSA}`,\n },\n];\n\nconst SOURCE_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts', '.jsx', '.tsx']);\nconst IGNORED_DIRS = new Set(['node_modules', 'dist', 'build', 'coverage', '.git', '.next']);\n\nasync function collectSourceFiles(root: string): Promise<string[]> {\n const files: string[] = [];\n const pending = [root];\n while (pending.length > 0) {\n const dir = pending.pop()!;\n for (const entry of await readdir(dir, { withFileTypes: true })) {\n if (entry.isDirectory()) {\n if (!IGNORED_DIRS.has(entry.name)) pending.push(join(dir, entry.name));\n continue;\n }\n const dot = entry.name.lastIndexOf('.');\n if (dot !== -1 && SOURCE_EXTENSIONS.has(entry.name.slice(dot))) {\n files.push(join(dir, entry.name));\n }\n }\n }\n return files.sort();\n}\n\nasync function auditPackageJson(cwd: string): Promise<Finding[]> {\n const path = join(cwd, 'package.json');\n if (!existsSync(path)) return [];\n const manifest = JSON.parse(await readFile(path, 'utf8')) as Record<\n string,\n Record<string, string> | undefined\n >;\n const declared = {\n ...manifest.dependencies,\n ...manifest.devDependencies,\n ...manifest.peerDependencies,\n };\n return Object.keys(declared)\n .filter((name) => name in RISKY_PACKAGES)\n .map((name) => ({ location: `package.json (${name})`, ...RISKY_PACKAGES[name]! }));\n}\n\nasync function auditSources(cwd: string): Promise<Finding[]> {\n const findings: Finding[] = [];\n for (const file of await collectSourceFiles(cwd)) {\n const lines = (await readFile(file, 'utf8')).split('\\n');\n lines.forEach((line, index) => {\n for (const { re, what, migrateTo } of CODE_PATTERNS) {\n if (re.test(line)) {\n findings.push({ location: `${relative(cwd, file)}:${index + 1}`, what, migrateTo });\n }\n }\n });\n }\n return findings;\n}\n\nexport const audit = defineCommand({\n meta: {\n name: 'audit',\n description: 'Detect pre-quantum crypto and suggest the PQC equivalent',\n },\n async run() {\n const cwd = process.cwd();\n const findings = [...(await auditPackageJson(cwd)), ...(await auditSources(cwd))];\n\n if (findings.length === 0) {\n ok('No pre-quantum crypto detected.');\n return;\n }\n\n heading(`Pre-quantum crypto detected (${findings.length} findings):`);\n for (const f of findings) {\n finding(f.location, f.what, f.migrateTo);\n }\n console.log();\n console.log(\n pc.yellow(\n `${findings.length} usages to migrate. Algorithm guide: FIPS 203 (ML-KEM) encryption, FIPS 204 (ML-DSA) signatures.`,\n ),\n );\n process.exitCode = 1;\n },\n});\n","import pc from 'picocolors';\n\n/**\n * Output helpers. picocolors disables colors automatically when there is no\n * TTY (and NO_COLOR/FORCE_COLOR are honored), so output stays readable in\n * pipes and CI logs without ANSI codes.\n */\nexport const ok = (message: string): void => {\n console.log(`${pc.green('✓')} ${message}`);\n};\n\nexport const warn = (message: string): void => {\n console.log(`${pc.yellow('⚠')} ${pc.yellow(message)}`);\n};\n\nexport const item = (message: string): void => {\n console.log(` ${message}`);\n};\n\nexport const finding = (location: string, what: string, migrateTo: string): void => {\n console.log(` ${pc.red('●')} ${pc.bold(location)} — ${what}`);\n console.log(` ${pc.dim('migrate to:')} ${pc.cyan(migrateTo)}`);\n};\n\nexport const heading = (message: string): void => {\n console.log(pc.bold(message));\n};\n","import { writeFile } from 'node:fs/promises';\nimport { existsSync } from 'node:fs';\n\nimport { defineCommand } from 'citty';\n\nimport { writeKeyPair } from '../keyfiles.js';\nimport { heading, item, ok, warn } from '../ui.js';\n\nconst CONFIG_FILE = 'pqc.config.json';\n\nconst CONFIG = {\n defaultAlgorithm: 'ml-kem-768',\n keysDir: 'keys',\n} as const;\n\nconst EXAMPLE = `import { pqc } from '@pqc-sdk/core';\n\n// Full roundtrip: generate keys, encrypt and decrypt.\nconst pair = await pqc.keys.generate(); // ML-KEM-768 by default\nconst ciphertext = await pqc.encrypt('hello post-quantum', pair.publicKey);\nconst plaintext = await pqc.decrypt(ciphertext, pair.secretKey);\nconsole.log(new TextDecoder().decode(plaintext)); // \"hello post-quantum\"\n\n// Digital signatures (ML-DSA-65):\nconst signer = await pqc.keys.generate({ algorithm: 'ml-dsa-65' });\nconst signature = await pqc.sign('document', signer.secretKey);\nconsole.log(await pqc.verify('document', signature, signer.publicKey)); // true\n\n// Keys serialize to base64url with metadata, ready to persist:\nconst token = pqc.keys.serialize(pair.publicKey);\nconsole.log(token.slice(0, 48) + '…');\n\n// To load the development keys generated by \\`pqc init\\`:\n// import { readFile } from 'node:fs/promises';\n// const publicKey = pqc.keys.deserialize(\n// (await readFile('keys/dev.public.pqc', 'utf8')).trim(),\n// );\n`;\n\nexport const init = defineCommand({\n meta: {\n name: 'init',\n description: 'Initialize a project: config, development keys and example',\n },\n async run() {\n if (existsSync(CONFIG_FILE)) {\n throw new Error(`${CONFIG_FILE} already exists: the project is already initialized.`);\n }\n\n await writeFile(CONFIG_FILE, `${JSON.stringify(CONFIG, null, 2)}\\n`);\n const keys = await writeKeyPair(CONFIG.keysDir, 'dev', CONFIG.defaultAlgorithm, false);\n await writeFile('example.ts', EXAMPLE);\n\n heading('PQC project initialized:');\n item(`${CONFIG_FILE} — configuration with safe defaults`);\n item(`${keys.publicPath} / ${keys.secretPath} — development ${keys.algorithm} pair`);\n item('example.ts — full roundtrip ready to run');\n console.log();\n warn('The keys/dev.* keys are for development ONLY — do NOT use them in production.');\n ok(`Next step: run example.ts (node --experimental-strip-types example.ts or tsx)`);\n },\n});\n","import { chmod, mkdir, writeFile } from 'node:fs/promises';\nimport { existsSync } from 'node:fs';\nimport { join } from 'node:path';\n\nimport { SUPPORTED_ALGORITHMS, pqc, type SupportedAlgorithm } from '@pqc-sdk/core';\n\nexport interface WrittenKeyPair {\n algorithm: SupportedAlgorithm;\n publicPath: string;\n secretPath: string;\n}\n\nexport function assertSupportedAlgorithm(value: string): SupportedAlgorithm {\n if (!(SUPPORTED_ALGORITHMS as readonly string[]).includes(value)) {\n throw new Error(\n `Unsupported algorithm: ${value} (supported: ${SUPPORTED_ALGORITHMS.join(', ')})`,\n );\n }\n return value as SupportedAlgorithm;\n}\n\n/** Generates a pair and writes it serialized as base64url, one file per key. */\nexport async function writeKeyPair(\n directory: string,\n baseName: string,\n algorithm: SupportedAlgorithm,\n force: boolean,\n): Promise<WrittenKeyPair> {\n const publicPath = join(directory, `${baseName}.public.pqc`);\n const secretPath = join(directory, `${baseName}.secret.pqc`);\n\n if (!force) {\n for (const path of [publicPath, secretPath]) {\n if (existsSync(path)) {\n throw new Error(`${path} already exists. Use --force to overwrite it.`);\n }\n }\n }\n\n const pair = await pqc.keys.generate({ algorithm });\n await mkdir(directory, { recursive: true });\n await writeFile(publicPath, `${pqc.keys.serialize(pair.publicKey)}\\n`);\n await writeFile(secretPath, `${pqc.keys.serialize(pair.secretKey)}\\n`, { mode: 0o600 });\n await chmod(secretPath, 0o600);\n\n return { algorithm, publicPath, secretPath };\n}\n","import { defineCommand } from 'citty';\n\nimport { assertSupportedAlgorithm, writeKeyPair } from '../keyfiles.js';\nimport { item, ok, warn } from '../ui.js';\n\nexport const keygen = defineCommand({\n meta: {\n name: 'keygen',\n description: 'Generate a PQC key pair serialized as base64url',\n },\n args: {\n algorithm: {\n type: 'string',\n description: 'Algorithm of the pair (ml-kem-768 or ml-dsa-65)',\n default: 'ml-kem-768',\n },\n out: {\n type: 'string',\n description: 'Output directory',\n default: 'keys',\n },\n force: {\n type: 'boolean',\n description: 'Overwrite existing keys',\n default: false,\n },\n },\n async run({ args }) {\n const algorithm = assertSupportedAlgorithm(args.algorithm);\n const keys = await writeKeyPair(args.out, algorithm, algorithm, args.force);\n\n ok(`${algorithm} pair generated:`);\n item(`public: ${keys.publicPath}`);\n item(`secret: ${keys.secretPath} (mode 0600)`);\n warn('The secret key must not be committed or leave this environment.');\n },\n});\n"],"mappings":";;;AAAA,SAAS,iBAAAA,gBAAe,eAAe;;;ACAvC,SAAS,UAAU,eAAe;AAClC,SAAS,kBAAkB;AAC3B,SAAS,MAAM,gBAAgB;AAE/B,SAAS,qBAAqB;AAC9B,OAAOC,SAAQ;;;ACLf,OAAO,QAAQ;AAOR,IAAM,KAAK,CAAC,YAA0B;AAC3C,UAAQ,IAAI,GAAG,GAAG,MAAM,QAAG,CAAC,IAAI,OAAO,EAAE;AAC3C;AAEO,IAAM,OAAO,CAAC,YAA0B;AAC7C,UAAQ,IAAI,GAAG,GAAG,OAAO,QAAG,CAAC,IAAI,GAAG,OAAO,OAAO,CAAC,EAAE;AACvD;AAEO,IAAM,OAAO,CAAC,YAA0B;AAC7C,UAAQ,IAAI,KAAK,OAAO,EAAE;AAC5B;AAEO,IAAM,UAAU,CAAC,UAAkB,MAAc,cAA4B;AAClF,UAAQ,IAAI,KAAK,GAAG,IAAI,QAAG,CAAC,IAAI,GAAG,KAAK,QAAQ,CAAC,WAAM,IAAI,EAAE;AAC7D,UA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;AAAA,IACA;AAAA,EACF;AACF,CAAC;AAED,MAAM,QAAQ,IAAI;","names":["defineCommand","pc","pc","writeFile","existsSync","defineCommand","existsSync","join","defineCommand","existsSync","writeFile","defineCommand","defineCommand","defineCommand"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/commands/audit.ts","../src/ui.ts","../src/commands/init.ts","../src/keyfiles.ts","../src/commands/keygen.ts"],"sourcesContent":["import { defineCommand, runMain } from 'citty';\n\nimport { audit } from './commands/audit.js';\nimport { init } from './commands/init.js';\nimport { keygen } from './commands/keygen.js';\n\n// Injected by tsup (`define` in tsup.config.ts) from package.json at build time.\ndeclare const __PQC_CLI_VERSION__: string;\n\nconst main = defineCommand({\n meta: {\n name: 'pqc',\n version: __PQC_CLI_VERSION__,\n description: 'CLI for the post-quantum cryptography SDK (@pqc-sdk/core)',\n },\n subCommands: {\n init,\n keygen,\n audit,\n },\n});\n\nawait runMain(main);\n","import { readFile, readdir, stat } from 'node:fs/promises';\nimport { existsSync } from 'node:fs';\nimport { join, relative } from 'node:path';\n\nimport { defineCommand } from 'citty';\nimport pc from 'picocolors';\n\nimport { finding, heading, note, ok } from '../ui.js';\n\ninterface Finding {\n location: string;\n what: string;\n migrateTo: string;\n}\n\n/**\n * Upper bound on the size of a single source file the scanner will read. Files\n * larger than this are skipped: they are almost never hand-written source, and\n * reading them would slow the scan with no useful signal.\n */\nconst MAX_FILE_BYTES = 1024 * 1024; // 1 MiB\n\nconst ML_DSA = 'ML-DSA-65 (pqc.sign / pqc.verify)';\nconst ML_KEM = 'ML-KEM-768 + AES-256-GCM (pqc.encrypt / pqc.decrypt)';\n\nconst RISKY_PACKAGES: Record<string, { what: string; migrateTo: string }> = {\n jsonwebtoken: { what: 'JWTs signed with RSA/ECDSA (RS256/ES256)', migrateTo: ML_DSA },\n jose: { what: 'JOSE/JWT with RSA/ECDSA algorithms', migrateTo: ML_DSA },\n elliptic: { what: 'Classic elliptic curves (ECDSA/ECDH)', migrateTo: `${ML_DSA} and ${ML_KEM}` },\n secp256k1: { what: 'ECDSA signatures over secp256k1', migrateTo: ML_DSA },\n 'node-rsa': { what: 'RSA encryption and signatures', migrateTo: `${ML_KEM} and ${ML_DSA}` },\n 'node-forge': { what: 'Classic RSA/X.509', migrateTo: `${ML_KEM} and ${ML_DSA}` },\n tweetnacl: { what: 'Ed25519/X25519 (pre-quantum)', migrateTo: `${ML_DSA} and ${ML_KEM}` },\n};\n\nconst CODE_PATTERNS: ReadonlyArray<{ re: RegExp; what: string; migrateTo: string }> = [\n {\n re: /create(?:Sign|Verify)\\s*\\(/,\n what: 'RSA/ECDSA signing via node:crypto (createSign/createVerify)',\n migrateTo: ML_DSA,\n },\n {\n re: /createECDH\\s*\\(|\\.diffieHellman\\s*\\(/,\n what: 'ECDH/DH key exchange',\n migrateTo: ML_KEM,\n },\n {\n re: /publicEncrypt\\s*\\(|privateDecrypt\\s*\\(/,\n what: 'RSA encryption (publicEncrypt/privateDecrypt)',\n migrateTo: ML_KEM,\n },\n {\n re: /generateKeyPair(?:Sync)?\\s*\\(\\s*['\"](?:rsa|rsa-pss|dsa|ec|ed25519|ed448|x25519|x448)['\"]/,\n what: 'pre-quantum keypair generation',\n migrateTo: 'pqc.keys.generate (ML-KEM-768 for encryption, ML-DSA-65 for signatures)',\n },\n {\n re: /['\"`](?:RS|ES|PS)(?:256|384|512)['\"`]/,\n what: 'JWT with an RSA/ECDSA signing algorithm',\n migrateTo: ML_DSA,\n },\n {\n re: /['\"`](?:RSA-OAEP|ECDH|ECDSA)['\"`]/,\n what: 'WebCrypto with a pre-quantum algorithm',\n migrateTo: `${ML_KEM} or ${ML_DSA}`,\n },\n];\n\nconst SOURCE_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts', '.jsx', '.tsx']);\nconst IGNORED_DIRS = new Set(['node_modules', 'dist', 'build', 'coverage', '.git', '.next']);\n\nasync function collectSourceFiles(root: string): Promise<string[]> {\n const files: string[] = [];\n const pending = [root];\n while (pending.length > 0) {\n const dir = pending.pop()!;\n for (const entry of await readdir(dir, { withFileTypes: true })) {\n if (entry.isDirectory()) {\n if (!IGNORED_DIRS.has(entry.name)) pending.push(join(dir, entry.name));\n continue;\n }\n const dot = entry.name.lastIndexOf('.');\n if (dot !== -1 && SOURCE_EXTENSIONS.has(entry.name.slice(dot))) {\n files.push(join(dir, entry.name));\n }\n }\n }\n return files.sort();\n}\n\nasync function auditPackageJson(cwd: string): Promise<Finding[]> {\n const path = join(cwd, 'package.json');\n if (!existsSync(path)) return [];\n const manifest = JSON.parse(await readFile(path, 'utf8')) as Record<\n string,\n Record<string, string> | undefined\n >;\n const declared = {\n ...manifest.dependencies,\n ...manifest.devDependencies,\n ...manifest.peerDependencies,\n };\n return Object.keys(declared)\n .filter((name) => name in RISKY_PACKAGES)\n .map((name) => ({ location: `package.json (${name})`, ...RISKY_PACKAGES[name]! }));\n}\n\ninterface SourceScan {\n findings: Finding[];\n /** Files skipped because they exceed {@link MAX_FILE_BYTES}, relative to cwd. */\n skipped: string[];\n}\n\nasync function auditSources(cwd: string): Promise<SourceScan> {\n const findings: Finding[] = [];\n const skipped: string[] = [];\n for (const file of await collectSourceFiles(cwd)) {\n if ((await stat(file)).size > MAX_FILE_BYTES) {\n skipped.push(relative(cwd, file));\n continue;\n }\n const lines = (await readFile(file, 'utf8')).split('\\n');\n lines.forEach((line, index) => {\n for (const { re, what, migrateTo } of CODE_PATTERNS) {\n if (re.test(line)) {\n findings.push({ location: `${relative(cwd, file)}:${index + 1}`, what, migrateTo });\n }\n }\n });\n }\n return { findings, skipped };\n}\n\nexport const audit = defineCommand({\n meta: {\n name: 'audit',\n description:\n 'Heuristically detect pre-quantum crypto (best-effort regex scan) and suggest the PQC equivalent',\n },\n async run() {\n const cwd = process.cwd();\n const { findings: sourceFindings, skipped } = await auditSources(cwd);\n const findings = [...(await auditPackageJson(cwd)), ...sourceFindings];\n\n note(\n 'Heuristic, best-effort regex scan — expect occasional false positives and false negatives.',\n );\n if (skipped.length > 0) {\n note(\n `Skipped ${skipped.length} file(s) larger than ${MAX_FILE_BYTES / 1024 / 1024} MiB: ${skipped.join(', ')}`,\n );\n }\n\n if (findings.length === 0) {\n ok('No pre-quantum crypto detected.');\n return;\n }\n\n heading(`Pre-quantum crypto detected (${findings.length} findings):`);\n for (const f of findings) {\n finding(f.location, f.what, f.migrateTo);\n }\n console.log();\n console.log(\n pc.yellow(\n `${findings.length} usages to migrate. Algorithm guide: FIPS 203 (ML-KEM) encryption, FIPS 204 (ML-DSA) signatures.`,\n ),\n );\n process.exitCode = 1;\n },\n});\n","import pc from 'picocolors';\n\n/**\n * Output helpers. picocolors disables colors automatically when there is no\n * TTY (and NO_COLOR/FORCE_COLOR are honored), so output stays readable in\n * pipes and CI logs without ANSI codes.\n */\nexport const ok = (message: string): void => {\n console.log(`${pc.green('✓')} ${message}`);\n};\n\nexport const warn = (message: string): void => {\n console.log(`${pc.yellow('⚠')} ${pc.yellow(message)}`);\n};\n\nexport const item = (message: string): void => {\n console.log(` ${message}`);\n};\n\nexport const finding = (location: string, what: string, migrateTo: string): void => {\n console.log(` ${pc.red('●')} ${pc.bold(location)} — ${what}`);\n console.log(` ${pc.dim('migrate to:')} ${pc.cyan(migrateTo)}`);\n};\n\nexport const heading = (message: string): void => {\n console.log(pc.bold(message));\n};\n\nexport const note = (message: string): void => {\n console.log(pc.dim(message));\n};\n","import { writeFile } from 'node:fs/promises';\nimport { existsSync } from 'node:fs';\n\nimport { defineCommand } from 'citty';\n\nimport { ensureKeysIgnored, writeKeyPair } from '../keyfiles.js';\nimport { heading, item, ok, warn } from '../ui.js';\n\nconst CONFIG_FILE = 'pqc.config.json';\n\nconst CONFIG = {\n defaultAlgorithm: 'ml-kem-768',\n keysDir: 'keys',\n} as const;\n\nconst EXAMPLE = `import { pqc } from '@pqc-sdk/core';\n\n// Full roundtrip: generate keys, encrypt and decrypt.\nconst pair = await pqc.keys.generate(); // ML-KEM-768 by default\nconst ciphertext = await pqc.encrypt('hello post-quantum', pair.publicKey);\nconst plaintext = await pqc.decrypt(ciphertext, pair.secretKey);\nconsole.log(new TextDecoder().decode(plaintext)); // \"hello post-quantum\"\n\n// Digital signatures (ML-DSA-65):\nconst signer = await pqc.keys.generate({ algorithm: 'ml-dsa-65' });\nconst signature = await pqc.sign('document', signer.secretKey);\nconsole.log(await pqc.verify('document', signature, signer.publicKey)); // true\n\n// Keys serialize to base64url with metadata, ready to persist:\nconst token = pqc.keys.serialize(pair.publicKey);\nconsole.log(token.slice(0, 48) + '…');\n\n// To load the development keys generated by \\`pqc init\\`:\n// import { readFile } from 'node:fs/promises';\n// const publicKey = pqc.keys.deserialize(\n// (await readFile('keys/dev.public.pqc', 'utf8')).trim(),\n// );\n`;\n\nexport const init = defineCommand({\n meta: {\n name: 'init',\n description: 'Initialize a project: config, development keys and example',\n },\n async run() {\n if (existsSync(CONFIG_FILE)) {\n throw new Error(`${CONFIG_FILE} already exists: the project is already initialized.`);\n }\n\n await writeFile(CONFIG_FILE, `${JSON.stringify(CONFIG, null, 2)}\\n`);\n const keys = await writeKeyPair(CONFIG.keysDir, 'dev', CONFIG.defaultAlgorithm, false);\n const ignored = await ensureKeysIgnored(process.cwd());\n const exampleWritten = !existsSync('example.ts');\n if (exampleWritten) {\n await writeFile('example.ts', EXAMPLE);\n }\n\n heading('PQC project initialized:');\n item(`${CONFIG_FILE} — configuration with safe defaults`);\n item(`${keys.publicPath} / ${keys.secretPath} — development ${keys.algorithm} pair`);\n if (ignored.length > 0) {\n item(`.gitignore — added ${ignored.join(', ')} so secret keys are never committed`);\n }\n if (exampleWritten) {\n item('example.ts — full roundtrip ready to run');\n } else {\n warn('example.ts already exists — left untouched.');\n }\n console.log();\n warn('The keys/dev.* keys are for development ONLY — do NOT use them in production.');\n ok(`Next step: run example.ts (node --experimental-strip-types example.ts or tsx)`);\n },\n});\n","import { chmod, mkdir, readFile, writeFile } from 'node:fs/promises';\nimport { existsSync } from 'node:fs';\nimport { join } from 'node:path';\n\nimport { SUPPORTED_ALGORITHMS, pqc, type SupportedAlgorithm } from '@pqc-sdk/core';\n\n/** Patterns that keep generated secret keys out of version control. */\nexport const KEY_IGNORE_PATTERNS = ['keys/', '*.secret.pqc'] as const;\n\n/**\n * Ensures the project `.gitignore` ignores generated key material so a secret\n * key is never committed by an accidental `git add .`. Creates `.gitignore`\n * when it is missing, or appends only the patterns not already present.\n * Idempotent. Returns the patterns it added (empty when nothing changed).\n */\nexport async function ensureKeysIgnored(cwd: string): Promise<string[]> {\n const gitignorePath = join(cwd, '.gitignore');\n const current = existsSync(gitignorePath) ? await readFile(gitignorePath, 'utf8') : '';\n const present = new Set(current.split(/\\r?\\n/).map((line) => line.trim()));\n const missing = KEY_IGNORE_PATTERNS.filter((pattern) => !present.has(pattern));\n if (missing.length === 0) {\n return [];\n }\n const prefix = current === '' ? '' : current.endsWith('\\n') ? '\\n' : '\\n\\n';\n const block = `${prefix}# PQC key material — never commit secret keys\\n${missing.join('\\n')}\\n`;\n await writeFile(gitignorePath, current + block);\n return [...missing];\n}\n\nexport interface WrittenKeyPair {\n algorithm: SupportedAlgorithm;\n publicPath: string;\n secretPath: string;\n}\n\nexport function assertSupportedAlgorithm(value: string): SupportedAlgorithm {\n if (!(SUPPORTED_ALGORITHMS as readonly string[]).includes(value)) {\n throw new Error(\n `Unsupported algorithm: ${value} (supported: ${SUPPORTED_ALGORITHMS.join(', ')})`,\n );\n }\n return value as SupportedAlgorithm;\n}\n\n/**\n * Validates a base file name for a key pair. The name becomes part of the path\n * passed to {@link writeKeyPair}, so it must not be empty, contain path\n * separators (`/` or `\\`), or contain `..` — otherwise a caller could write\n * keys outside the intended output directory.\n */\nexport function assertSafeName(value: string): string {\n if (value === '') {\n throw new Error('Invalid --name: must not be empty.');\n }\n if (value.includes('/') || value.includes('\\\\')) {\n throw new Error('Invalid --name: must not contain path separators (\"/\" or \"\\\\\").');\n }\n if (value.includes('..')) {\n throw new Error('Invalid --name: must not contain \"..\".');\n }\n return value;\n}\n\n/** Generates a pair and writes it serialized as base64url, one file per key. */\nexport async function writeKeyPair(\n directory: string,\n baseName: string,\n algorithm: SupportedAlgorithm,\n force: boolean,\n): Promise<WrittenKeyPair> {\n const publicPath = join(directory, `${baseName}.public.pqc`);\n const secretPath = join(directory, `${baseName}.secret.pqc`);\n\n if (!force) {\n for (const path of [publicPath, secretPath]) {\n if (existsSync(path)) {\n throw new Error(`${path} already exists. Use --force to overwrite it.`);\n }\n }\n }\n\n const pair = await pqc.keys.generate({ algorithm });\n await mkdir(directory, { recursive: true });\n await writeFile(publicPath, `${pqc.keys.serialize(pair.publicKey)}\\n`);\n await writeFile(secretPath, `${pqc.keys.serialize(pair.secretKey)}\\n`, { mode: 0o600 });\n await chmod(secretPath, 0o600);\n\n return { algorithm, publicPath, secretPath };\n}\n","import { defineCommand } from 'citty';\n\nimport {\n assertSafeName,\n assertSupportedAlgorithm,\n ensureKeysIgnored,\n writeKeyPair,\n} from '../keyfiles.js';\nimport { item, ok, warn } from '../ui.js';\n\nexport const keygen = defineCommand({\n meta: {\n name: 'keygen',\n description: 'Generate a PQC key pair serialized as base64url',\n },\n args: {\n algorithm: {\n type: 'string',\n description: 'Algorithm of the pair (ml-kem-768 or ml-dsa-65)',\n default: 'ml-kem-768',\n },\n name: {\n type: 'string',\n description: 'Base file name for the key pair (default: the algorithm, e.g. ml-kem-768)',\n },\n out: {\n type: 'string',\n description: 'Output directory',\n default: 'keys',\n },\n force: {\n type: 'boolean',\n description: 'Overwrite existing keys',\n default: false,\n },\n },\n async run({ args }) {\n const algorithm = assertSupportedAlgorithm(args.algorithm);\n const baseName = args.name === undefined ? algorithm : assertSafeName(args.name);\n const keys = await writeKeyPair(args.out, baseName, algorithm, args.force);\n const ignored = await ensureKeysIgnored(process.cwd());\n\n ok(`${algorithm} pair generated:`);\n item(`public: ${keys.publicPath}`);\n item(`secret: ${keys.secretPath} (mode 0600)`);\n if (ignored.length > 0) {\n item(`.gitignore: added ${ignored.join(', ')} so secret keys are never committed`);\n }\n warn('The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C;AAAA,IACnF;AACA,SAAK,iEAAiE;AAAA,EACxE;AACF,CAAC;;;ALzCD,IAAM,OAAOC,eAAc;AAAA,EACzB,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,SAAS;AAAA,IACT,aAAa;AAAA,EACf;AAAA,EACA,aAAa;AAAA,IACX;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF,CAAC;AAED,MAAM,QAAQ,IAAI;","names":["defineCommand","pc","pc","writeFile","existsSync","defineCommand","readFile","existsSync","join","defineCommand","existsSync","writeFile","defineCommand","defineCommand","defineCommand"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pqc-sdk/cli",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "description": "CLI for the post-quantum cryptography SDK",
5
5
  "type": "module",
6
6
  "bin": {
@@ -15,7 +15,7 @@
15
15
  "dependencies": {
16
16
  "citty": "^0.2.2",
17
17
  "picocolors": "^1.1.1",
18
- "@pqc-sdk/core": "0.1.2"
18
+ "@pqc-sdk/core": "0.3.0"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/node": "^22.15.29",