rules:
  # =============================================================================
  # ELIXIR 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: elixir-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :crypto.hash(:md5, ...)
          - pattern: |
              :crypto.hash("md5", ...)
          - pattern: |
              :erlang.md5(...)
          - pattern: |
              :md5 |> :crypto.hash(...)
    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: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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:
        - ":sha256"
        - ":sha512"
        - "SPHINCS+"

  - id: elixir-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :crypto.hash(:sha, ...)
          - pattern: |
              :crypto.hash(:sha1, ...)
          - pattern: |
              :crypto.hash("sha", ...)
          - pattern: |
              :crypto.hash("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: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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: elixir-use-of-rsa-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :public_key.generate_key({:rsa, ...})
          - pattern: |
              :public_key.encrypt_public(..., :rsa)
          - pattern: |
              :public_key.decrypt_private(..., :rsa)
          - pattern: |
              :public_key.sign(..., :rsa)
          - pattern: |
              :crypto.public_encrypt(:rsa, ...)
          - pattern: |
              :crypto.private_decrypt(: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: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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: elixir-use-of-dsa-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :public_key.generate_key({:dsa, ...})
          - pattern: |
              :public_key.sign(..., :dsa)
          - pattern: |
              :public_key.verify(..., :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: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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: elixir-use-of-ec-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :public_key.generate_key({:ec, ...})
          - pattern: |
              :crypto.generate_key(:ecdh, ...)
          - pattern: |
              :crypto.compute_key(:ecdh, ...)
          - pattern: |
              :public_key.sign(..., :ecdsa)
          - pattern: |
              :public_key.verify(..., :ecdsa)
    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: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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: elixir-use-of-dh-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :crypto.generate_key(:dh, ...)
          - pattern: |
              :crypto.compute_key(:dh, ...)
          - pattern: |
              :public_key.generate_key({:dh, ...})
    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: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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: elixir-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :crypto.crypto_one_time(:des_cbc, ...)
          - pattern: |
              :crypto.crypto_one_time(:des_ecb, ...)
          - pattern: |
              :crypto.block_encrypt(:des_cbc, ...)
          - pattern: |
              :crypto.block_encrypt(:des_ecb, ...)
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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_gcm"

  - id: elixir-use-of-3des-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :crypto.crypto_one_time(:des_ede3_cbc, ...)
          - pattern: |
              :crypto.block_encrypt(:des_ede3, ...)
          - pattern: |
              :crypto.block_encrypt(:des3_cbc, ...)
    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: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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_gcm"

  - id: elixir-use-of-rc4-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :crypto.stream_encrypt(:rc4, ...)
          - pattern: |
              :crypto.stream_decrypt(:rc4, ...)
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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"

  - id: elixir-use-of-blowfish-quantum
    patterns:
      - pattern-either:
          - pattern: |
              :crypto.crypto_one_time(:blowfish_cbc, ...)
          - pattern: |
              :crypto.block_encrypt(:blowfish_cbc, ...)
    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: [elixir]
    metadata:
      category: security
      technology:
        - elixir
        - erlang
        - 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_gcm"
