rules:
  # =============================================================================
  # SCALA POST-QUANTUM CRYPTOGRAPHIC VULNERABILITY RULES
  # =============================================================================
  # These rules detect cryptographic algorithms vulnerable to quantum attacks
  # using Shor's algorithm (asymmetric) or Grover's algorithm (symmetric/hash)
  # =============================================================================

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

  - id: scala-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: MessageDigest.getInstance("MD5")
          - pattern: MessageDigest.getInstance("md5")
          - pattern: DigestUtils.md5(...)
          - pattern: DigestUtils.md5Hex(...)
          - pattern: Hashing.md5()
    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: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "SHA-256"
        - "SHA-3"
        - "SPHINCS+"

  - id: scala-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: MessageDigest.getInstance("SHA-1")
          - pattern: MessageDigest.getInstance("SHA1")
          - pattern: MessageDigest.getInstance("sha-1")
          - pattern: MessageDigest.getInstance("sha1")
          - pattern: DigestUtils.sha1(...)
          - pattern: DigestUtils.sha1Hex(...)
          - pattern: Hashing.sha1()
    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: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

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

  - id: scala-use-of-rsa-quantum
    patterns:
      - pattern-either:
          - pattern: KeyPairGenerator.getInstance("RSA")
          - pattern: KeyPairGenerator.getInstance("rsa")
          - pattern: KeyFactory.getInstance("RSA")
          - pattern: Cipher.getInstance("RSA")
          - pattern: Cipher.getInstance("RSA/...")
          - pattern: Signature.getInstance("...RSA...")
    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: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (ML-KEM)"
        - "CRYSTALS-Dilithium (ML-DSA)"
        - "SPHINCS+"

  - id: scala-use-of-dsa-quantum
    patterns:
      - pattern-either:
          - pattern: KeyPairGenerator.getInstance("DSA")
          - pattern: KeyPairGenerator.getInstance("dsa")
          - pattern: Signature.getInstance("DSA")
          - pattern: Signature.getInstance("...DSA...")
    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: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "CRYSTALS-Dilithium (ML-DSA)"
        - "FALCON"
        - "SPHINCS+"

  - id: scala-use-of-ec-quantum
    patterns:
      - pattern-either:
          - pattern: KeyPairGenerator.getInstance("EC")
          - pattern: KeyPairGenerator.getInstance("ECDSA")
          - pattern: KeyPairGenerator.getInstance("ECDH")
          - pattern: KeyAgreement.getInstance("ECDH")
          - pattern: Signature.getInstance("...ECDSA...")
          - pattern: ECGenParameterSpec(...)
    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: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (ML-KEM)"
        - "CRYSTALS-Dilithium (ML-DSA)"
        - "SPHINCS+"

  - id: scala-use-of-dh-quantum
    patterns:
      - pattern-either:
          - pattern: KeyPairGenerator.getInstance("DH")
          - pattern: KeyPairGenerator.getInstance("DiffieHellman")
          - pattern: KeyAgreement.getInstance("DH")
          - pattern: KeyAgreement.getInstance("DiffieHellman")
    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: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (ML-KEM)"

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

  - id: scala-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: Cipher.getInstance("DES")
          - pattern: Cipher.getInstance("DES/...")
          - pattern: SecretKeyFactory.getInstance("DES")
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "AES-256"

  - id: scala-use-of-3des-quantum
    patterns:
      - pattern-either:
          - pattern: Cipher.getInstance("DESede")
          - pattern: Cipher.getInstance("DESede/...")
          - pattern: Cipher.getInstance("TripleDES")
          - pattern: SecretKeyFactory.getInstance("DESede")
    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: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "AES-256"

  - id: scala-use-of-blowfish-quantum
    patterns:
      - pattern-either:
          - pattern: Cipher.getInstance("Blowfish")
          - pattern: Cipher.getInstance("Blowfish/...")
    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: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 6
      post_quantum_alternatives:
        - "AES-256"

  - id: scala-use-of-rc4-quantum
    patterns:
      - pattern-either:
          - pattern: Cipher.getInstance("RC4")
          - pattern: Cipher.getInstance("ARCFOUR")
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [scala]
    metadata:
      category: security
      technology:
        - scala
        - java
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Classical + Grover's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "AES-256-GCM"
        - "ChaCha20-Poly1305"

  # ---------------------------------------------------------------------------
  # IMPLEMENTATION VULNERABILITIES (added to match Java benchmark coverage)
  # ---------------------------------------------------------------------------

  - id: scala-aes-ecb-mode
    patterns:
      - pattern-either:
          - pattern: Cipher.getInstance("AES/ECB/...")
          - pattern: Cipher.getInstance("AES")
    message: >-
      AES in ECB mode leaks data patterns -- identical plaintext blocks produce identical ciphertext. Use
      AES-256-GCM for authenticated encryption.
    severity: ERROR
    languages: [scala]
    metadata:
      category: security
      technology: [scala, java, crypto]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 6

  - id: scala-static-iv
    patterns:
      - pattern-either:
          - pattern: new IvParameterSpec(new byte[$SIZE])
          - pattern: |
              val $IV = new byte[$SIZE]
              ...
              new IvParameterSpec($IV)
          - pattern: |
              val $IV: Array[Byte] = Array(...)
              ...
              new IvParameterSpec($IV)
    message: >-
      Static or hardcoded IV breaks IND-CPA security -- identical plaintexts produce identical ciphertext.
      Generate a fresh random IV for every encryption operation.
    severity: ERROR
    languages: [scala]
    metadata:
      category: security
      technology: [scala, java, crypto]
      cwe:
        - "CWE-329: Not Using a Random IV with CBC Mode"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: scala-hardcoded-crypto-key
    patterns:
      - pattern-either:
          - pattern: |
              val $KEY = "..."
              ...
              new SecretKeySpec($KEY.getBytes(), ...)
          - pattern: |
              val $KEY: Array[Byte] = Array(...)
              ...
              new SecretKeySpec($KEY, ...)
          - pattern: new SecretKeySpec("...".getBytes(), ...)
    message: >-
      CRYPTOGRAPHIC VULNERABILITY: Hard-coded cryptographic keys are visible in source code and compiled
      bytecode, making them trivial to extract.
    severity: ERROR
    languages: [scala]
    metadata:
      category: security
      technology: [scala, java, crypto]
      cwe:
        - "CWE-798: Use of Hard-coded Credentials"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 8

  - id: scala-weak-random
    patterns:
      - pattern-either:
          - pattern: new java.util.Random(...)
          - pattern: new java.util.Random()
          - pattern: scala.util.Random.nextInt(...)
          - pattern: scala.util.Random.nextBytes(...)
    message: >-
      Non-cryptographic PRNG used for security-sensitive operations -- output is predictable. Use a
      cryptographically secure random number generator.
    severity: WARNING
    languages: [scala]
    metadata:
      category: security
      technology: [scala, crypto]
      cwe:
        - "CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: scala-weak-pbkdf2-iterations
    patterns:
      - pattern: new PBEKeySpec($PASSWORD, $SALT, $ITERATIONS, $KEYLEN)
      - metavariable-comparison:
          metavariable: $ITERATIONS
          comparison: $ITERATIONS < 100000
    message: >-
      Insufficient PBKDF2 iterations make the derived key brute-forceable. Use at least 310,000 iterations
      (NIST SP 800-132) or switch to Argon2.
    severity: ERROR
    languages: [scala]
    metadata:
      category: security
      technology: [scala, java, crypto]
      cwe:
        - "CWE-916: Use of Password Hash With Insufficient Computational Effort"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm (accelerates brute-force)"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "Argon2"
        - "scrypt"
        - "bcrypt"

  - id: scala-weak-rsa-key-size
    patterns:
      - pattern-either:
          - pattern: |
              val $KEY = KeyPairGenerator.getInstance("RSA")
              ...
              $KEY.initialize(512)
          - pattern: |
              val $KEY = KeyPairGenerator.getInstance("RSA")
              ...
              $KEY.initialize(1024)
          - pattern: |
              val $KEY = KeyPairGenerator.getInstance("RSA")
              ...
              $KEY.initialize(2048)
    message: >-
      RSA key size $SIZE bits is too small and quantum-vulnerable. 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: ERROR
    languages: [scala]
    metadata:
      category: security
      technology: [scala, java, post-quantum]
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (ML-KEM)"
        - "CRYSTALS-Dilithium (ML-DSA)"
        - "FALCON"
