@quantakrypto/core 0.3.0 → 0.4.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.
Files changed (166) hide show
  1. package/dist/agent-types.d.ts +48 -0
  2. package/dist/agent-types.d.ts.map +1 -0
  3. package/dist/agent-types.js +2 -0
  4. package/dist/agent-types.js.map +1 -0
  5. package/dist/cache.d.ts +27 -0
  6. package/dist/cache.d.ts.map +1 -0
  7. package/dist/cache.js +93 -0
  8. package/dist/cache.js.map +1 -0
  9. package/dist/cbom.d.ts.map +1 -1
  10. package/dist/cbom.js +26 -1
  11. package/dist/cbom.js.map +1 -1
  12. package/dist/changed.d.ts.map +1 -1
  13. package/dist/changed.js +14 -2
  14. package/dist/changed.js.map +1 -1
  15. package/dist/codemods/config-toggle.d.ts +3 -0
  16. package/dist/codemods/config-toggle.d.ts.map +1 -0
  17. package/dist/codemods/config-toggle.js +26 -0
  18. package/dist/codemods/config-toggle.js.map +1 -0
  19. package/dist/codemods/registry.d.ts +26 -0
  20. package/dist/codemods/registry.d.ts.map +1 -0
  21. package/dist/codemods/registry.js +8 -0
  22. package/dist/codemods/registry.js.map +1 -0
  23. package/dist/comments.d.ts +45 -0
  24. package/dist/comments.d.ts.map +1 -0
  25. package/dist/comments.js +164 -0
  26. package/dist/comments.js.map +1 -0
  27. package/dist/config.d.ts +1 -1
  28. package/dist/config.d.ts.map +1 -1
  29. package/dist/config.js +7 -0
  30. package/dist/config.js.map +1 -1
  31. package/dist/dependencies.d.ts +31 -13
  32. package/dist/dependencies.d.ts.map +1 -1
  33. package/dist/dependencies.js +460 -40
  34. package/dist/dependencies.js.map +1 -1
  35. package/dist/detect-utils.d.ts +44 -1
  36. package/dist/detect-utils.d.ts.map +1 -1
  37. package/dist/detect-utils.js +113 -17
  38. package/dist/detect-utils.js.map +1 -1
  39. package/dist/detectors/c.d.ts +20 -0
  40. package/dist/detectors/c.d.ts.map +1 -0
  41. package/dist/detectors/c.js +104 -0
  42. package/dist/detectors/c.js.map +1 -0
  43. package/dist/detectors/csharp.d.ts +15 -0
  44. package/dist/detectors/csharp.d.ts.map +1 -0
  45. package/dist/detectors/csharp.js +76 -0
  46. package/dist/detectors/csharp.js.map +1 -0
  47. package/dist/detectors/go.d.ts +25 -0
  48. package/dist/detectors/go.d.ts.map +1 -0
  49. package/dist/detectors/go.js +132 -0
  50. package/dist/detectors/go.js.map +1 -0
  51. package/dist/detectors/java.d.ts +25 -0
  52. package/dist/detectors/java.d.ts.map +1 -0
  53. package/dist/detectors/java.js +200 -0
  54. package/dist/detectors/java.js.map +1 -0
  55. package/dist/detectors/python.d.ts +26 -0
  56. package/dist/detectors/python.d.ts.map +1 -0
  57. package/dist/detectors/python.js +171 -0
  58. package/dist/detectors/python.js.map +1 -0
  59. package/dist/detectors/ruby.d.ts +15 -0
  60. package/dist/detectors/ruby.d.ts.map +1 -0
  61. package/dist/detectors/ruby.js +75 -0
  62. package/dist/detectors/ruby.js.map +1 -0
  63. package/dist/detectors/rust.d.ts +13 -0
  64. package/dist/detectors/rust.d.ts.map +1 -0
  65. package/dist/detectors/rust.js +94 -0
  66. package/dist/detectors/rust.js.map +1 -0
  67. package/dist/detectors/source.d.ts.map +1 -1
  68. package/dist/detectors/source.js +70 -22
  69. package/dist/detectors/source.js.map +1 -1
  70. package/dist/index.d.ts +18 -1
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +15 -1
  73. package/dist/index.js.map +1 -1
  74. package/dist/parallel.d.ts +4 -0
  75. package/dist/parallel.d.ts.map +1 -1
  76. package/dist/parallel.js +59 -8
  77. package/dist/parallel.js.map +1 -1
  78. package/dist/patch-policy.d.ts +22 -0
  79. package/dist/patch-policy.d.ts.map +1 -0
  80. package/dist/patch-policy.js +21 -0
  81. package/dist/patch-policy.js.map +1 -0
  82. package/dist/redact.d.ts +19 -0
  83. package/dist/redact.d.ts.map +1 -0
  84. package/dist/redact.js +77 -0
  85. package/dist/redact.js.map +1 -0
  86. package/dist/registry.d.ts +13 -3
  87. package/dist/registry.d.ts.map +1 -1
  88. package/dist/registry.js +31 -4
  89. package/dist/registry.js.map +1 -1
  90. package/dist/remediate-pipeline.d.ts +41 -0
  91. package/dist/remediate-pipeline.d.ts.map +1 -0
  92. package/dist/remediate-pipeline.js +38 -0
  93. package/dist/remediate-pipeline.js.map +1 -0
  94. package/dist/remediate-request.d.ts +21 -0
  95. package/dist/remediate-request.d.ts.map +1 -0
  96. package/dist/remediate-request.js +28 -0
  97. package/dist/remediate-request.js.map +1 -0
  98. package/dist/remediation.d.ts +10 -0
  99. package/dist/remediation.d.ts.map +1 -1
  100. package/dist/remediation.js +21 -8
  101. package/dist/remediation.js.map +1 -1
  102. package/dist/report.d.ts.map +1 -1
  103. package/dist/report.js +16 -1
  104. package/dist/report.js.map +1 -1
  105. package/dist/scan-worker.js +6 -2
  106. package/dist/scan-worker.js.map +1 -1
  107. package/dist/scan.d.ts +5 -4
  108. package/dist/scan.d.ts.map +1 -1
  109. package/dist/scan.js +52 -29
  110. package/dist/scan.js.map +1 -1
  111. package/dist/triage.d.ts +27 -0
  112. package/dist/triage.d.ts.map +1 -0
  113. package/dist/triage.js +33 -0
  114. package/dist/triage.js.map +1 -0
  115. package/dist/types.d.ts +53 -3
  116. package/dist/types.d.ts.map +1 -1
  117. package/dist/verify.d.ts +30 -0
  118. package/dist/verify.d.ts.map +1 -0
  119. package/dist/verify.js +65 -0
  120. package/dist/verify.js.map +1 -0
  121. package/dist/version.d.ts +1 -1
  122. package/dist/version.js +1 -1
  123. package/dist/walk.d.ts +7 -0
  124. package/dist/walk.d.ts.map +1 -1
  125. package/dist/walk.js +85 -11
  126. package/dist/walk.js.map +1 -1
  127. package/dist/worktree.d.ts +6 -0
  128. package/dist/worktree.d.ts.map +1 -0
  129. package/dist/worktree.js +43 -0
  130. package/dist/worktree.js.map +1 -0
  131. package/package.json +1 -1
  132. package/src/agent-types.ts +52 -0
  133. package/src/cache.ts +120 -0
  134. package/src/cbom.ts +27 -1
  135. package/src/changed.ts +13 -4
  136. package/src/codemods/config-toggle.ts +35 -0
  137. package/src/codemods/registry.ts +31 -0
  138. package/src/comments.ts +180 -0
  139. package/src/config.ts +8 -0
  140. package/src/dependencies.ts +483 -51
  141. package/src/detect-utils.ts +123 -15
  142. package/src/detectors/c.ts +132 -0
  143. package/src/detectors/csharp.ts +98 -0
  144. package/src/detectors/go.ts +167 -0
  145. package/src/detectors/java.ts +235 -0
  146. package/src/detectors/python.ts +212 -0
  147. package/src/detectors/ruby.ts +97 -0
  148. package/src/detectors/rust.ts +112 -0
  149. package/src/detectors/source.ts +69 -21
  150. package/src/index.ts +45 -1
  151. package/src/parallel.ts +63 -6
  152. package/src/patch-policy.ts +42 -0
  153. package/src/redact.ts +92 -0
  154. package/src/registry.ts +32 -4
  155. package/src/remediate-pipeline.ts +81 -0
  156. package/src/remediate-request.ts +52 -0
  157. package/src/remediation.ts +27 -11
  158. package/src/report.ts +27 -1
  159. package/src/scan-worker.ts +6 -2
  160. package/src/scan.ts +62 -31
  161. package/src/triage.ts +58 -0
  162. package/src/types.ts +66 -3
  163. package/src/verify.ts +82 -0
  164. package/src/version.ts +1 -1
  165. package/src/walk.ts +81 -12
  166. package/src/worktree.ts +45 -0
@@ -0,0 +1,235 @@
1
+ /**
2
+ * Source-code detector for classical, non-quantum-safe asymmetric cryptography
3
+ * in Java / Kotlin (the JCA — Java Cryptography Architecture). Same lexical
4
+ * strategy as the other language packs.
5
+ *
6
+ * The JCA funnels almost everything through a handful of factory methods keyed
7
+ * by an ALGORITHM STRING: `KeyPairGenerator.getInstance("RSA")`,
8
+ * `Signature.getInstance("SHA256withECDSA")`, `Cipher.getInstance("RSA/ECB/…")`,
9
+ * `KeyAgreement.getInstance("ECDH")`. So detection here is two steps: match the
10
+ * `<Factory>.getInstance("<alg>")` call, then classify the (factory, alg) pair
11
+ * into a rule. Symmetric / hashing algorithms ("AES", "HmacSHA256", …) classify
12
+ * to nothing and are ignored. A small set of BouncyCastle lightweight-API class
13
+ * names (`new ECDSASigner()`, `new X25519Agreement()`, …) is matched directly.
14
+ *
15
+ * HNDL policy mirrors the other detectors: KEM / key agreement (RSA encryption,
16
+ * (EC)DH, X25519/X448) is harvest-now-decrypt-later exposed (hndl:true);
17
+ * signatures (RSA/ECDSA/DSA/EdDSA) are hndl:false but forgeable by a quantum
18
+ * attacker. `KeyPairGenerator.getInstance("EC")` is ambiguous (an EC key feeds
19
+ * both ECDSA and ECDH), so it is classified conservatively as
20
+ * key-exchange-capable (hndl:true), exactly as the Node/Python EC keygen rules.
21
+ */
22
+ import type { Detector, Finding, RuleMeta } from "../types.js";
23
+ import { JAVA_EXTENSIONS, eachMatch, findingFromRule, hasExtension } from "../detect-utils.js";
24
+ import { CWE_BROKEN_CRYPTO } from "../cwe.js";
25
+
26
+ /* -------------------------------------------------------------------------- */
27
+ /* Regexes */
28
+ /* -------------------------------------------------------------------------- */
29
+
30
+ // <Factory>.getInstance("<alg>") — the JCA entry points that carry asymmetric
31
+ // algorithms. KeyGenerator/MessageDigest/Mac are intentionally excluded (they
32
+ // are symmetric / hashing only).
33
+ const RE_JAVA_GETINSTANCE =
34
+ /\b(KeyPairGenerator|Signature|Cipher|KeyAgreement|KeyFactory)\s*\.\s*getInstance\s*\(\s*"([^"]+)"/g;
35
+
36
+ // BouncyCastle lightweight-API class instantiations.
37
+ const RE_JAVA_BC =
38
+ /\bnew\s+(RSAKeyPairGenerator|DSAKeyPairGenerator|ECKeyPairGenerator|ECDSASigner|Ed25519Signer|Ed448Signer|X25519Agreement|X448Agreement)\s*\(/g;
39
+
40
+ /* -------------------------------------------------------------------------- */
41
+ /* Rule catalog */
42
+ /* -------------------------------------------------------------------------- */
43
+
44
+ const RULE_JAVA_RSA: RuleMeta = {
45
+ id: "java-rsa",
46
+ title: "Java RSA key/encryption",
47
+ description: "JCA RSA KeyPairGenerator / Cipher / KeyFactory",
48
+ category: "kem",
49
+ severity: "high",
50
+ confidence: "high",
51
+ algorithm: "RSA",
52
+ hndl: true,
53
+ cwe: CWE_BROKEN_CRYPTO,
54
+ message: "Classical RSA (Java/JCA) is not quantum-safe and RSA encryption is HNDL-exposed.",
55
+ };
56
+ const RULE_JAVA_RSA_SIGN: RuleMeta = {
57
+ id: "java-rsa-sign",
58
+ title: "Java RSA signature",
59
+ description: 'JCA Signature.getInstance("…withRSA")',
60
+ category: "signature",
61
+ severity: "high",
62
+ confidence: "high",
63
+ algorithm: "RSA",
64
+ hndl: false,
65
+ cwe: CWE_BROKEN_CRYPTO,
66
+ message: "Classical RSA signing (Java/JCA) is forgeable by a quantum attacker.",
67
+ remediation: "ML-DSA-65 (FIPS 204) or SLH-DSA (FIPS 205)",
68
+ };
69
+ const RULE_JAVA_EC_KEYGEN: RuleMeta = {
70
+ id: "java-ec-keygen",
71
+ title: "Java EC key generation",
72
+ description: 'JCA KeyPairGenerator.getInstance("EC")',
73
+ category: "key-exchange",
74
+ severity: "high",
75
+ confidence: "high",
76
+ algorithm: "ECDH",
77
+ hndl: true,
78
+ cwe: CWE_BROKEN_CRYPTO,
79
+ message:
80
+ "Generates a classical EC key pair (Java/JCA). EC keys feed BOTH ECDSA signatures and ECDH key agreement; the ECDH path is harvest-now-decrypt-later exposed.",
81
+ remediation:
82
+ "For key agreement: hybrid X25519MLKEM768 (ML-KEM-768). For signatures: ML-DSA-65 (FIPS 204).",
83
+ };
84
+ const RULE_JAVA_ECDSA_SIGN: RuleMeta = {
85
+ id: "java-ecdsa-sign",
86
+ title: "Java ECDSA signature",
87
+ description: 'JCA Signature.getInstance("…withECDSA")',
88
+ category: "signature",
89
+ severity: "high",
90
+ confidence: "high",
91
+ algorithm: "ECDSA",
92
+ hndl: false,
93
+ cwe: CWE_BROKEN_CRYPTO,
94
+ message: "Classical ECDSA signing (Java/JCA) is forgeable by a quantum attacker.",
95
+ remediation: "ML-DSA-65 (FIPS 204) or SLH-DSA (FIPS 205)",
96
+ };
97
+ const RULE_JAVA_ECDH: RuleMeta = {
98
+ id: "java-ecdh",
99
+ title: "Java ECDH key agreement",
100
+ description: 'JCA KeyAgreement.getInstance("ECDH")',
101
+ category: "key-exchange",
102
+ severity: "high",
103
+ confidence: "high",
104
+ algorithm: "ECDH",
105
+ hndl: true,
106
+ cwe: CWE_BROKEN_CRYPTO,
107
+ message:
108
+ "Elliptic-curve Diffie-Hellman (Java/JCA) is broken by Shor's algorithm (harvest-now-decrypt-later).",
109
+ };
110
+ const RULE_JAVA_DSA: RuleMeta = {
111
+ id: "java-dsa",
112
+ title: "Java DSA key/signature",
113
+ description: "JCA DSA KeyPairGenerator / Signature",
114
+ category: "signature",
115
+ severity: "high",
116
+ confidence: "high",
117
+ algorithm: "DSA",
118
+ hndl: false,
119
+ cwe: CWE_BROKEN_CRYPTO,
120
+ message: "Classical DSA (Java/JCA) is deprecated and forgeable by a quantum attacker.",
121
+ remediation: "Rotate off DSA and migrate to ML-DSA-65 (FIPS 204).",
122
+ };
123
+ const RULE_JAVA_DH: RuleMeta = {
124
+ id: "java-dh",
125
+ title: "Java Diffie-Hellman key exchange",
126
+ description: "JCA DiffieHellman KeyPairGenerator / KeyAgreement",
127
+ category: "key-exchange",
128
+ severity: "high",
129
+ confidence: "high",
130
+ algorithm: "DH",
131
+ hndl: true,
132
+ cwe: CWE_BROKEN_CRYPTO,
133
+ message:
134
+ "Finite-field Diffie-Hellman (Java/JCA) is broken by Shor's algorithm (harvest-now-decrypt-later).",
135
+ };
136
+ const RULE_JAVA_XDH: RuleMeta = {
137
+ id: "java-xdh",
138
+ title: "Java X25519/X448 key agreement",
139
+ description: "JCA XDH / X25519 / X448 (KeyPairGenerator / KeyAgreement)",
140
+ category: "key-exchange",
141
+ severity: "low",
142
+ confidence: "high",
143
+ algorithm: "X25519",
144
+ hndl: true,
145
+ cwe: CWE_BROKEN_CRYPTO,
146
+ message:
147
+ "X25519/X448 (Java/JCA) is modern but still classical key agreement — harvest-now-decrypt-later.",
148
+ };
149
+ const RULE_JAVA_EDDSA: RuleMeta = {
150
+ id: "java-eddsa",
151
+ title: "Java Ed25519/Ed448 signature",
152
+ description: "JCA EdDSA / Ed25519 / Ed448",
153
+ category: "signature",
154
+ severity: "low",
155
+ confidence: "high",
156
+ algorithm: "EdDSA",
157
+ hndl: false,
158
+ cwe: CWE_BROKEN_CRYPTO,
159
+ message: "Ed25519/Ed448 (Java/JCA) is a modern but still classical signature scheme.",
160
+ };
161
+
162
+ /**
163
+ * Classify a `<factory>.getInstance("<alg>")` pair into a rule, or null when the
164
+ * algorithm is not classical asymmetric crypto (AES, HMAC, PBKDF2, hashes, …).
165
+ * `alg` is normalised to upper-case with any Cipher transformation suffix
166
+ * (`RSA/ECB/OAEPPadding` → `RSA`) stripped.
167
+ */
168
+ function classifyGetInstance(factory: string, rawAlg: string): RuleMeta | null {
169
+ const alg = rawAlg.split("/")[0].trim().toUpperCase();
170
+ const isSignature = factory === "Signature";
171
+
172
+ if (alg.includes("ECDSA")) return RULE_JAVA_ECDSA_SIGN;
173
+ if (alg.includes("ECDH")) return RULE_JAVA_ECDH;
174
+ if (alg === "EC") return isSignature ? RULE_JAVA_ECDSA_SIGN : RULE_JAVA_EC_KEYGEN;
175
+ if (alg.includes("ED25519") || alg.includes("ED448") || alg.includes("EDDSA"))
176
+ return RULE_JAVA_EDDSA;
177
+ if (alg.includes("X25519") || alg.includes("X448") || alg === "XDH") return RULE_JAVA_XDH;
178
+ if (alg.includes("RSA")) return isSignature ? RULE_JAVA_RSA_SIGN : RULE_JAVA_RSA;
179
+ if (alg.includes("DSA")) return RULE_JAVA_DSA; // ECDSA already handled above
180
+ if (alg.includes("DH") || alg.includes("DIFFIEHELLMAN")) return RULE_JAVA_DH;
181
+ return null;
182
+ }
183
+
184
+ /** BouncyCastle lightweight-API class name → rule. */
185
+ const BC_CLASS_RULES: Record<string, RuleMeta> = {
186
+ RSAKeyPairGenerator: RULE_JAVA_RSA,
187
+ DSAKeyPairGenerator: RULE_JAVA_DSA,
188
+ ECKeyPairGenerator: RULE_JAVA_EC_KEYGEN,
189
+ ECDSASigner: RULE_JAVA_ECDSA_SIGN,
190
+ Ed25519Signer: RULE_JAVA_EDDSA,
191
+ Ed448Signer: RULE_JAVA_EDDSA,
192
+ X25519Agreement: RULE_JAVA_XDH,
193
+ X448Agreement: RULE_JAVA_XDH,
194
+ };
195
+
196
+ /** Detects classical asymmetric crypto in Java / Kotlin (JCA + BouncyCastle). */
197
+ export const javaDetector: Detector = {
198
+ id: "java-crypto",
199
+ description: "Classical asymmetric crypto in Java/Kotlin (JCA getInstance + BouncyCastle)",
200
+ scope: "source",
201
+ language: "java",
202
+ rules: [
203
+ RULE_JAVA_RSA,
204
+ RULE_JAVA_RSA_SIGN,
205
+ RULE_JAVA_EC_KEYGEN,
206
+ RULE_JAVA_ECDSA_SIGN,
207
+ RULE_JAVA_ECDH,
208
+ RULE_JAVA_DSA,
209
+ RULE_JAVA_DH,
210
+ RULE_JAVA_XDH,
211
+ RULE_JAVA_EDDSA,
212
+ ],
213
+ appliesTo: (f) => hasExtension(f, JAVA_EXTENSIONS),
214
+ detect({ file, content }): Finding[] {
215
+ const findings: Finding[] = [];
216
+
217
+ eachMatch(RE_JAVA_GETINSTANCE, content, (m) => {
218
+ const rule = classifyGetInstance(m[1], m[2]);
219
+ if (!rule) return; // symmetric / hashing algorithm — not our concern.
220
+ findings.push(
221
+ findingFromRule(rule, { file, content, index: m.index, matchLength: m[0].length }),
222
+ );
223
+ });
224
+
225
+ eachMatch(RE_JAVA_BC, content, (m) => {
226
+ const rule = BC_CLASS_RULES[m[1]];
227
+ if (!rule) return;
228
+ findings.push(
229
+ findingFromRule(rule, { file, content, index: m.index, matchLength: m[0].length }),
230
+ );
231
+ });
232
+
233
+ return findings;
234
+ },
235
+ };
@@ -0,0 +1,212 @@
1
+ /**
2
+ * Source-code detector for classical, non-quantum-safe asymmetric cryptography
3
+ * in Python. Same lexical (regex-over-source) strategy as the JS/TS detectors:
4
+ * robust to formatting, dependency-free, and confidence-tagged per pattern.
5
+ *
6
+ * Covers the three libraries that carry ~all real-world Python asymmetric
7
+ * crypto:
8
+ * - `cryptography` (hazmat): rsa/ec/dh/x25519/x448/ed25519/ed448 + OAEP padding
9
+ * - PyCryptodome (`Crypto.PublicKey`): RSA/ECC/DSA generate + PKCS1_OAEP
10
+ * - paramiko (SSH): RSAKey / ECDSAKey / Ed25519Key / DSSKey
11
+ *
12
+ * JWT/JOSE algorithm strings (`algorithm="RS256"`) are NOT handled here — the
13
+ * language-agnostic `jwt-jose` detector now applies to Python too (a quoted
14
+ * `"RS256"` is the same evidence in any language).
15
+ *
16
+ * HNDL policy mirrors the JS detectors: key agreement / KEM (RSA-OAEP, ECDH, DH,
17
+ * X25519/X448) is harvest-now-decrypt-later exposed (hndl:true); signatures
18
+ * (ECDSA, DSA, Ed25519/Ed448) are hndl:false but still high/low severity because
19
+ * a quantum attacker can forge them. EC key generation is ambiguous (an EC key
20
+ * feeds both ECDSA and ECDH), so it is classified conservatively as
21
+ * key-exchange-capable (hndl:true), exactly as the Node `ec` keygen rule is.
22
+ */
23
+ import type { Detector, Finding, RuleMeta } from "../types.js";
24
+ import { PYTHON_EXTENSIONS, eachMatch, findingFromRule, hasExtension } from "../detect-utils.js";
25
+ import { CWE_BROKEN_CRYPTO } from "../cwe.js";
26
+
27
+ /* -------------------------------------------------------------------------- */
28
+ /* Precompiled regexes (module scope) */
29
+ /* -------------------------------------------------------------------------- */
30
+
31
+ // RSA key generation: cryptography `rsa.generate_private_key(`, PyCryptodome
32
+ // `RSA.generate(`, paramiko `paramiko.RSAKey` / `RSAKey.generate(`.
33
+ const RE_PY_RSA_KEYGEN =
34
+ /\brsa\.generate_private_key\s*\(|\bRSA\.generate\s*\(|\bparamiko\.RSAKey\b|\bRSAKey\.generate\s*\(/g;
35
+ // RSA public-key encryption padding (RSA-OAEP): confidentiality → HNDL.
36
+ const RE_PY_RSA_ENCRYPT = /\bpadding\.OAEP\s*\(|\bPKCS1_OAEP\.new\s*\(/g;
37
+ // EC key generation (ambiguous ECDSA/ECDH): cryptography + PyCryptodome.
38
+ const RE_PY_EC_KEYGEN = /\bec\.generate_private_key\s*\(|\bECC\.generate\s*\(/g;
39
+ // ECDSA signatures: cryptography `ec.ECDSA(`, paramiko ECDSAKey.
40
+ const RE_PY_ECDSA = /\bec\.ECDSA\s*\(|\bparamiko\.ECDSAKey\b|\bECDSAKey\.generate\s*\(/g;
41
+ // DSA: PyCryptodome `DSA.generate(`, paramiko DSSKey.
42
+ const RE_PY_DSA = /\bDSA\.generate\s*\(|\bparamiko\.DSSKey\b|\bDSSKey\.generate\s*\(/g;
43
+ // Finite-field Diffie-Hellman (cryptography `dh`).
44
+ const RE_PY_DH = /\bdh\.generate_parameters\s*\(|\bdh\.DHParameterNumbers\s*\(/g;
45
+ // Modern-but-classical curve primitives (cryptography). `.generate(` anchors so
46
+ // a bare import of the class name does not fire.
47
+ const RE_PY_X25519 = /\bX25519PrivateKey\.generate\s*\(/g;
48
+ const RE_PY_X448 = /\bX448PrivateKey\.generate\s*\(/g;
49
+ const RE_PY_EDDSA = /\b(?:Ed25519|Ed448)PrivateKey\.generate\s*\(|\bparamiko\.Ed25519Key\b/g;
50
+
51
+ /* -------------------------------------------------------------------------- */
52
+ /* Rule catalog */
53
+ /* -------------------------------------------------------------------------- */
54
+
55
+ const RULE_PY_RSA_KEYGEN: RuleMeta = {
56
+ id: "python-rsa-keygen",
57
+ title: "Python RSA key generation",
58
+ description:
59
+ "cryptography rsa.generate_private_key / PyCryptodome RSA.generate / paramiko RSAKey",
60
+ category: "kem",
61
+ severity: "high",
62
+ confidence: "high",
63
+ algorithm: "RSA",
64
+ hndl: true,
65
+ cwe: CWE_BROKEN_CRYPTO,
66
+ message: "Generates a classical RSA key pair (Python), which is not quantum-safe.",
67
+ };
68
+ const RULE_PY_RSA_ENCRYPT: RuleMeta = {
69
+ id: "python-rsa-encrypt",
70
+ title: "Python RSA public-key encryption",
71
+ description: "RSA-OAEP / PKCS1_OAEP encryption padding",
72
+ category: "kem",
73
+ severity: "high",
74
+ confidence: "high",
75
+ algorithm: "RSA",
76
+ hndl: true,
77
+ cwe: CWE_BROKEN_CRYPTO,
78
+ message:
79
+ "RSA public-key encryption (OAEP) is broken by Shor's algorithm and exposed to harvest-now-decrypt-later.",
80
+ };
81
+ const RULE_PY_EC_KEYGEN: RuleMeta = {
82
+ id: "python-ec-keygen",
83
+ title: "Python EC key generation",
84
+ description: "cryptography ec.generate_private_key / PyCryptodome ECC.generate",
85
+ category: "key-exchange",
86
+ severity: "high",
87
+ confidence: "high",
88
+ algorithm: "ECDH",
89
+ hndl: true,
90
+ cwe: CWE_BROKEN_CRYPTO,
91
+ message:
92
+ "Generates a classical EC key pair (Python). EC keys feed BOTH ECDSA signatures and ECDH key agreement; the ECDH path is harvest-now-decrypt-later exposed.",
93
+ remediation:
94
+ "For key agreement: hybrid X25519MLKEM768 (ML-KEM-768). For signatures: ML-DSA-65 (FIPS 204).",
95
+ };
96
+ const RULE_PY_ECDSA: RuleMeta = {
97
+ id: "python-ecdsa",
98
+ title: "Python ECDSA signature",
99
+ description: "cryptography ec.ECDSA / paramiko ECDSAKey",
100
+ category: "signature",
101
+ severity: "high",
102
+ confidence: "medium",
103
+ algorithm: "ECDSA",
104
+ hndl: false,
105
+ cwe: CWE_BROKEN_CRYPTO,
106
+ message: "Classical ECDSA signing (Python) is forgeable by a quantum attacker.",
107
+ remediation: "ML-DSA-65 (FIPS 204) or SLH-DSA (FIPS 205)",
108
+ };
109
+ const RULE_PY_DSA: RuleMeta = {
110
+ id: "python-dsa",
111
+ title: "Python DSA key/usage",
112
+ description: "PyCryptodome DSA.generate / paramiko DSSKey",
113
+ category: "signature",
114
+ severity: "high",
115
+ confidence: "high",
116
+ algorithm: "DSA",
117
+ hndl: false,
118
+ cwe: CWE_BROKEN_CRYPTO,
119
+ message: "Classical DSA (Python) is deprecated and forgeable by a quantum attacker.",
120
+ remediation: "Rotate off DSA and migrate to ML-DSA-65 (FIPS 204).",
121
+ };
122
+ const RULE_PY_DH: RuleMeta = {
123
+ id: "python-dh",
124
+ title: "Python Diffie-Hellman key exchange",
125
+ description: "cryptography dh.generate_parameters / DHParameterNumbers",
126
+ category: "key-exchange",
127
+ severity: "high",
128
+ confidence: "high",
129
+ algorithm: "DH",
130
+ hndl: true,
131
+ cwe: CWE_BROKEN_CRYPTO,
132
+ message:
133
+ "Finite-field Diffie-Hellman (Python) is broken by Shor's algorithm (harvest-now-decrypt-later).",
134
+ };
135
+ const RULE_PY_X25519: RuleMeta = {
136
+ id: "python-x25519",
137
+ title: "Python X25519 key exchange",
138
+ description: "cryptography X25519PrivateKey.generate",
139
+ category: "key-exchange",
140
+ severity: "low",
141
+ confidence: "high",
142
+ algorithm: "X25519",
143
+ hndl: true,
144
+ cwe: CWE_BROKEN_CRYPTO,
145
+ message:
146
+ "X25519 (Python) is modern but still classical key agreement — harvest-now-decrypt-later.",
147
+ };
148
+ const RULE_PY_X448: RuleMeta = {
149
+ id: "python-x448",
150
+ title: "Python X448 key exchange",
151
+ description: "cryptography X448PrivateKey.generate",
152
+ category: "key-exchange",
153
+ severity: "low",
154
+ confidence: "high",
155
+ algorithm: "X448",
156
+ hndl: true,
157
+ cwe: CWE_BROKEN_CRYPTO,
158
+ message: "X448 (Python) is modern but still classical key agreement — harvest-now-decrypt-later.",
159
+ };
160
+ const RULE_PY_EDDSA: RuleMeta = {
161
+ id: "python-eddsa",
162
+ title: "Python Ed25519/Ed448 signature",
163
+ description: "cryptography Ed25519/Ed448 PrivateKey.generate / paramiko Ed25519Key",
164
+ category: "signature",
165
+ severity: "low",
166
+ confidence: "high",
167
+ algorithm: "EdDSA",
168
+ hndl: false,
169
+ cwe: CWE_BROKEN_CRYPTO,
170
+ message: "Ed25519/Ed448 (Python) is a modern but still classical signature scheme.",
171
+ };
172
+
173
+ /** Detects classical asymmetric crypto in Python source. */
174
+ export const pythonDetector: Detector = {
175
+ id: "python-crypto",
176
+ description: "Classical asymmetric crypto in Python (cryptography, PyCryptodome, paramiko)",
177
+ scope: "source",
178
+ language: "python",
179
+ rules: [
180
+ RULE_PY_RSA_KEYGEN,
181
+ RULE_PY_RSA_ENCRYPT,
182
+ RULE_PY_EC_KEYGEN,
183
+ RULE_PY_ECDSA,
184
+ RULE_PY_DSA,
185
+ RULE_PY_DH,
186
+ RULE_PY_X25519,
187
+ RULE_PY_X448,
188
+ RULE_PY_EDDSA,
189
+ ],
190
+ appliesTo: (f) => hasExtension(f, PYTHON_EXTENSIONS),
191
+ detect({ file, content }): Finding[] {
192
+ const findings: Finding[] = [];
193
+ const add = (re: RegExp, rule: RuleMeta) =>
194
+ eachMatch(re, content, (m) =>
195
+ findings.push(
196
+ findingFromRule(rule, { file, content, index: m.index, matchLength: m[0].length }),
197
+ ),
198
+ );
199
+
200
+ add(RE_PY_RSA_KEYGEN, RULE_PY_RSA_KEYGEN);
201
+ add(RE_PY_RSA_ENCRYPT, RULE_PY_RSA_ENCRYPT);
202
+ add(RE_PY_EC_KEYGEN, RULE_PY_EC_KEYGEN);
203
+ add(RE_PY_ECDSA, RULE_PY_ECDSA);
204
+ add(RE_PY_DSA, RULE_PY_DSA);
205
+ add(RE_PY_DH, RULE_PY_DH);
206
+ add(RE_PY_X25519, RULE_PY_X25519);
207
+ add(RE_PY_X448, RULE_PY_X448);
208
+ add(RE_PY_EDDSA, RULE_PY_EDDSA);
209
+
210
+ return findings;
211
+ },
212
+ };
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Source-code detector for classical asymmetric cryptography in Ruby. Ruby's
3
+ * asymmetric crypto is almost entirely `OpenSSL::PKey::{RSA,EC,DSA,DH}`, which
4
+ * makes the signals precise. Lexical, same strategy as the other packs.
5
+ *
6
+ * HNDL: RSA (keygen/encryption) and (EC)DH key agreement are harvest-now-
7
+ * decrypt-later exposed (hndl:true); DSA signatures are hndl:false but forgeable.
8
+ * `OpenSSL::PKey::EC` is ambiguous (feeds ECDSA + ECDH), so it is classified
9
+ * conservatively as key-exchange-capable (hndl:true), like the other EC keygen
10
+ * rules.
11
+ */
12
+ import type { Detector, Finding, RuleMeta } from "../types.js";
13
+ import { RUBY_EXTENSIONS, eachMatch, findingFromRule, hasExtension } from "../detect-utils.js";
14
+ import { CWE_BROKEN_CRYPTO } from "../cwe.js";
15
+
16
+ const RE_RB_RSA = /\bOpenSSL::PKey::RSA\.(?:new|generate)\s*\(/g;
17
+ const RE_RB_EC = /\bOpenSSL::PKey::EC\.(?:new|generate)\s*\(/g;
18
+ const RE_RB_DSA = /\bOpenSSL::PKey::DSA\.(?:new|generate)\s*\(/g;
19
+ const RE_RB_DH = /\bOpenSSL::PKey::DH\.new\s*\(/g;
20
+
21
+ const RULE_RB_RSA: RuleMeta = {
22
+ id: "ruby-rsa",
23
+ title: "Ruby RSA key/usage",
24
+ description: "OpenSSL::PKey::RSA.new / .generate",
25
+ category: "kem",
26
+ severity: "high",
27
+ confidence: "high",
28
+ algorithm: "RSA",
29
+ hndl: true,
30
+ cwe: CWE_BROKEN_CRYPTO,
31
+ message: "Classical RSA (Ruby/OpenSSL) is not quantum-safe and RSA encryption is HNDL-exposed.",
32
+ };
33
+ const RULE_RB_EC: RuleMeta = {
34
+ id: "ruby-ec",
35
+ title: "Ruby EC key generation",
36
+ description: "OpenSSL::PKey::EC.new / .generate",
37
+ category: "key-exchange",
38
+ severity: "high",
39
+ confidence: "high",
40
+ algorithm: "ECDH",
41
+ hndl: true,
42
+ cwe: CWE_BROKEN_CRYPTO,
43
+ message:
44
+ "Generates a classical EC key pair (Ruby/OpenSSL). EC keys feed BOTH ECDSA signatures and ECDH key agreement; the ECDH path is harvest-now-decrypt-later exposed.",
45
+ remediation:
46
+ "For key agreement: hybrid X25519MLKEM768 (ML-KEM-768). For signatures: ML-DSA-65 (FIPS 204).",
47
+ };
48
+ const RULE_RB_DSA: RuleMeta = {
49
+ id: "ruby-dsa",
50
+ title: "Ruby DSA key/signature",
51
+ description: "OpenSSL::PKey::DSA.new / .generate",
52
+ category: "signature",
53
+ severity: "high",
54
+ confidence: "high",
55
+ algorithm: "DSA",
56
+ hndl: false,
57
+ cwe: CWE_BROKEN_CRYPTO,
58
+ message: "Classical DSA (Ruby/OpenSSL) is deprecated and forgeable by a quantum attacker.",
59
+ remediation: "Rotate off DSA and migrate to ML-DSA-65 (FIPS 204).",
60
+ };
61
+ const RULE_RB_DH: RuleMeta = {
62
+ id: "ruby-dh",
63
+ title: "Ruby Diffie-Hellman key exchange",
64
+ description: "OpenSSL::PKey::DH.new",
65
+ category: "key-exchange",
66
+ severity: "high",
67
+ confidence: "high",
68
+ algorithm: "DH",
69
+ hndl: true,
70
+ cwe: CWE_BROKEN_CRYPTO,
71
+ message:
72
+ "Finite-field Diffie-Hellman (Ruby/OpenSSL) is broken by Shor's algorithm (harvest-now-decrypt-later).",
73
+ };
74
+
75
+ /** Detects classical asymmetric crypto in Ruby (OpenSSL::PKey). */
76
+ export const rubyDetector: Detector = {
77
+ id: "ruby-crypto",
78
+ description: "Classical asymmetric crypto in Ruby (OpenSSL::PKey::{RSA,EC,DSA,DH})",
79
+ scope: "source",
80
+ language: "ruby",
81
+ rules: [RULE_RB_RSA, RULE_RB_EC, RULE_RB_DSA, RULE_RB_DH],
82
+ appliesTo: (f) => hasExtension(f, RUBY_EXTENSIONS),
83
+ detect({ file, content }): Finding[] {
84
+ const findings: Finding[] = [];
85
+ const add = (re: RegExp, rule: RuleMeta) =>
86
+ eachMatch(re, content, (m) =>
87
+ findings.push(
88
+ findingFromRule(rule, { file, content, index: m.index, matchLength: m[0].length }),
89
+ ),
90
+ );
91
+ add(RE_RB_RSA, RULE_RB_RSA);
92
+ add(RE_RB_EC, RULE_RB_EC);
93
+ add(RE_RB_DSA, RULE_RB_DSA);
94
+ add(RE_RB_DH, RULE_RB_DH);
95
+ return findings;
96
+ },
97
+ };
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Source-code detector for classical asymmetric cryptography in Rust. Covers the
3
+ * common crypto crates: `rsa`, `ring`, the `*-dalek` family (ed25519/x25519),
4
+ * and the RustCrypto elliptic-curve crates (`p256`/`p384`/`k256`). Lexical, same
5
+ * strategy as the other packs; the `::` path syntax makes the signals precise.
6
+ *
7
+ * HNDL: RSA encryption and (EC/X)DH key agreement are harvest-now-decrypt-later
8
+ * exposed (hndl:true); ECDSA / Ed25519 signatures are hndl:false but forgeable.
9
+ */
10
+ import type { Detector, Finding, RuleMeta } from "../types.js";
11
+ import { RUST_EXTENSIONS, eachMatch, findingFromRule, hasExtension } from "../detect-utils.js";
12
+ import { CWE_BROKEN_CRYPTO } from "../cwe.js";
13
+
14
+ // `rsa` crate + ring RsaKeyPair. `::` after the type marks an associated-fn call.
15
+ const RE_RUST_RSA = /\b(?:RsaPrivateKey|RsaPublicKey|RsaKeyPair)::/g;
16
+ // ECDSA: RustCrypto `p256::ecdsa::SigningKey` (contains `ecdsa::SigningKey`),
17
+ // ring `EcdsaKeyPair::`, or a bare `ecdsa::SigningKey`.
18
+ const RE_RUST_ECDSA = /\becdsa::SigningKey\b|\bEcdsaKeyPair::/g;
19
+ // ECDH: RustCrypto `p256::ecdh::EphemeralSecret`, ring agreement / ECDH_P*.
20
+ const RE_RUST_ECDH = /\becdh::EphemeralSecret\b|\bagreement::ECDH_P(?:256|384)\b/g;
21
+ // Ed25519 signatures (dalek + ring).
22
+ const RE_RUST_ED25519 = /\bed25519_dalek::(?:SigningKey|Keypair|SecretKey)\b|\bEd25519KeyPair::/g;
23
+ // X25519 key agreement (dalek).
24
+ const RE_RUST_X25519 = /\bx25519_dalek::(?:EphemeralSecret|StaticSecret)\b/g;
25
+
26
+ const RULE_RUST_RSA: RuleMeta = {
27
+ id: "rust-rsa",
28
+ title: "Rust RSA key/usage",
29
+ description: "the `rsa` crate / ring RsaKeyPair",
30
+ category: "kem",
31
+ severity: "high",
32
+ confidence: "high",
33
+ algorithm: "RSA",
34
+ hndl: true,
35
+ cwe: CWE_BROKEN_CRYPTO,
36
+ message: "Classical RSA (Rust) is not quantum-safe and RSA encryption is HNDL-exposed.",
37
+ };
38
+ const RULE_RUST_ECDSA: RuleMeta = {
39
+ id: "rust-ecdsa",
40
+ title: "Rust ECDSA signature",
41
+ description: "p256/p384/k256 ecdsa::SigningKey / ring EcdsaKeyPair",
42
+ category: "signature",
43
+ severity: "high",
44
+ confidence: "high",
45
+ algorithm: "ECDSA",
46
+ hndl: false,
47
+ cwe: CWE_BROKEN_CRYPTO,
48
+ message: "Classical ECDSA signing (Rust) is forgeable by a quantum attacker.",
49
+ remediation: "ML-DSA-65 (FIPS 204) or SLH-DSA (FIPS 205)",
50
+ };
51
+ const RULE_RUST_ECDH: RuleMeta = {
52
+ id: "rust-ecdh",
53
+ title: "Rust ECDH key agreement",
54
+ description: "p256/p384 ecdh::EphemeralSecret / ring agreement",
55
+ category: "key-exchange",
56
+ severity: "high",
57
+ confidence: "high",
58
+ algorithm: "ECDH",
59
+ hndl: true,
60
+ cwe: CWE_BROKEN_CRYPTO,
61
+ message:
62
+ "Elliptic-curve Diffie-Hellman (Rust) is broken by Shor's algorithm (harvest-now-decrypt-later).",
63
+ };
64
+ const RULE_RUST_ED25519: RuleMeta = {
65
+ id: "rust-ed25519",
66
+ title: "Rust Ed25519 signature",
67
+ description: "ed25519-dalek SigningKey/Keypair / ring Ed25519KeyPair",
68
+ category: "signature",
69
+ severity: "low",
70
+ confidence: "high",
71
+ algorithm: "EdDSA",
72
+ hndl: false,
73
+ cwe: CWE_BROKEN_CRYPTO,
74
+ message: "Ed25519 (Rust) is a modern but still classical signature scheme.",
75
+ };
76
+ const RULE_RUST_X25519: RuleMeta = {
77
+ id: "rust-x25519",
78
+ title: "Rust X25519 key agreement",
79
+ description: "x25519-dalek EphemeralSecret/StaticSecret",
80
+ category: "key-exchange",
81
+ severity: "low",
82
+ confidence: "high",
83
+ algorithm: "X25519",
84
+ hndl: true,
85
+ cwe: CWE_BROKEN_CRYPTO,
86
+ message: "X25519 (Rust) is modern but still classical key agreement — harvest-now-decrypt-later.",
87
+ };
88
+
89
+ /** Detects classical asymmetric crypto in Rust (rsa, ring, dalek, p256/k256). */
90
+ export const rustDetector: Detector = {
91
+ id: "rust-crypto",
92
+ description: "Classical asymmetric crypto in Rust (rsa, ring, *-dalek, p256/k256)",
93
+ scope: "source",
94
+ language: "rust",
95
+ rules: [RULE_RUST_RSA, RULE_RUST_ECDSA, RULE_RUST_ECDH, RULE_RUST_ED25519, RULE_RUST_X25519],
96
+ appliesTo: (f) => hasExtension(f, RUST_EXTENSIONS),
97
+ detect({ file, content }): Finding[] {
98
+ const findings: Finding[] = [];
99
+ const add = (re: RegExp, rule: RuleMeta) =>
100
+ eachMatch(re, content, (m) =>
101
+ findings.push(
102
+ findingFromRule(rule, { file, content, index: m.index, matchLength: m[0].length }),
103
+ ),
104
+ );
105
+ add(RE_RUST_RSA, RULE_RUST_RSA);
106
+ add(RE_RUST_ECDSA, RULE_RUST_ECDSA);
107
+ add(RE_RUST_ECDH, RULE_RUST_ECDH);
108
+ add(RE_RUST_ED25519, RULE_RUST_ED25519);
109
+ add(RE_RUST_X25519, RULE_RUST_X25519);
110
+ return findings;
111
+ },
112
+ };