rules:
  # =============================================================================
  # OCAML POST-QUANTUM CRYPTOGRAPHIC VULNERABILITY RULES
  # =============================================================================
  # Uses pattern-regex because opengrep's OCaml AST parser does not yet support
  # pattern/pattern-either for method call matching. Regex approach is reliable
  # and covers Cryptokit, Digestif, Mirage-crypto, Nocrypto API patterns.
  # =============================================================================

  # ---------------------------------------------------------------------------
  # HASH FUNCTION VULNERABILITIES (Grover's Algorithm)
  # ---------------------------------------------------------------------------

  - id: ocaml-use-of-md5-quantum
    pattern-regex: "(?:Hash\\.md5|Digest\\.(?:string|file|channel)|Digestif\\.(?:MD5|md5)|Md5\\.(?:string|file))\\b"
    message: >-
      MD5 is cryptographically broken and quantum-weakened by Grover's algorithm -- effective security
      drops from 128 to 64 bits. Use SHA-256, SHA-384, or SHA-3.
    severity: ERROR
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
        - "CWE-328: Use of Weak Hash"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: ocaml-use-of-sha1-quantum
    pattern-regex: "(?:Digestif\\.(?:SHA1|sha1)|Sha1\\.(?:string|file))\\b"
    message: >-
      SHA-1 is deprecated and quantum-weakened by Grover's algorithm -- effective security drops from 160
      to 80 bits. Use SHA-256, SHA-384, or SHA-3.
    severity: ERROR
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
        - "CWE-328: Use of Weak Hash"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  # ---------------------------------------------------------------------------
  # ASYMMETRIC CRYPTOGRAPHY VULNERABILITIES (Shor's Algorithm)
  # ---------------------------------------------------------------------------

  - id: ocaml-use-of-rsa-quantum
    pattern-regex: "(?:RSA\\.(?:new_key|generate|encrypt|decrypt|sign|verify)|Mirage_crypto_pk\\.Rsa|Nocrypto\\.Rsa|Cryptokit\\.RSA|Rsa\\.(?:generate|encrypt|decrypt|sign))\\b"
    message: >-
      RSA is broken by Shor's algorithm regardless of key size. Migrate to ML-KEM (FIPS 203) for key
      encapsulation or ML-DSA (FIPS 204) for signatures.
    severity: WARNING
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, mirage-crypto, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8

  - id: ocaml-use-of-dsa-quantum
    pattern-regex: "(?:Mirage_crypto_pk\\.Dsa|Nocrypto\\.Dsa|Dsa\\.(?:generate|sign))\\b"
    message: >-
      DH/DSA is broken by Shor's algorithm. Migrate to ML-KEM (FIPS 203) for key exchange or ML-DSA (FIPS
      204) for signatures.
    severity: WARNING
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, mirage-crypto, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8

  - id: ocaml-use-of-ec-quantum
    pattern-regex: "(?:Mirage_crypto_ec(?:\\.(?:P256|P384|P521))?|Nocrypto\\.Ec|Ecdsa\\.sign|Ecdh\\.shared_secret)\\b"
    message: >-
      ECC (ECDSA/ECDH) is broken by Shor's algorithm -- all curves (P-256, P-384, secp256k1, Curve25519)
      are equally vulnerable regardless of key size. Migrate: ECDSA  ML-DSA (FIPS 204)  ECDH  ML-KEM
      (FIPS 203).
    severity: WARNING
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, mirage-crypto, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8

  - id: ocaml-use-of-dh-quantum
    pattern-regex: "(?:Mirage_crypto_pk\\.Dh|Nocrypto\\.Dh|Dh\\.(?:shared_secret|gen_key))\\b"
    message: >-
      DH/DSA is broken by Shor's algorithm. Migrate to ML-KEM (FIPS 203) for key exchange or ML-DSA (FIPS
      204) for signatures.
    severity: WARNING
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, mirage-crypto, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8

  # ---------------------------------------------------------------------------
  # SYMMETRIC CRYPTOGRAPHY VULNERABILITIES (Grover's Algorithm)
  # ---------------------------------------------------------------------------

  - id: ocaml-use-of-des-quantum
    pattern-regex: "(?:Cipher\\.des\\b|Mirage_crypto\\.DES\\b|Nocrypto\\.Cipher\\.DES\\b)"
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 10

  - id: ocaml-use-of-3des-quantum
    pattern-regex: "(?:Cipher\\.triple_des|Mirage_crypto\\.DES\\.EDE|Nocrypto\\.Cipher\\.DES3)\\b"
    message: >-
      3DES has 112-bit effective key strength -- broken classically and quantum-vulnerable. NIST deprecated
      3DES in 2023. Migrate to AES-256-GCM.
    severity: ERROR
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 8

  - id: ocaml-use-of-blowfish-quantum
    pattern-regex: "(?:Cipher\\.blowfish|Nocrypto\\.Cipher\\.Blowfish)\\b"
    message: >-
      Blowfish uses 64-bit blocks, making it vulnerable to birthday attacks, and provides no post-quantum
      security. Migrate to AES-256-GCM.
    severity: WARNING
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 6

  - id: ocaml-use-of-arcfour-quantum
    pattern-regex: "(?:Cipher\\.arcfour|Nocrypto\\.Cipher\\.RC4)\\b"
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Classical + Grover's Algorithm"
      quantum_risk_score: 10

  - id: ocaml-use-of-aes128-quantum
    pattern-regex: "key128\\b|16 bytes = 128 bits"
    message: >-
      AES-128/192 provide only 64/96-bit quantum security under Grover's algorithm. Use AES-256 for
      128-bit post-quantum security.
    severity: WARNING
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, post-quantum]
      cwe: ["CWE-326: Inadequate Encryption Strength"]
      owasp: ["A02:2021 - Cryptographic Failures"]
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 7

  - id: ocaml-use-of-aes192-quantum
    pattern-regex: "key192\\b|24 bytes = 192 bits"
    message: >-
      AES-128/192 provide only 64/96-bit quantum security under Grover's algorithm. Use AES-256 for
      128-bit post-quantum security.
    severity: WARNING
    languages: [ocaml]
    metadata:
      category: security
      technology: [ocaml, post-quantum]
      cwe: ["CWE-326: Inadequate Encryption Strength"]
      owasp: ["A02:2021 - Cryptographic Failures"]
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 6
