rules:
  # =============================================================================
  # RUBY 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: ruby-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: Digest::MD5
          - pattern: Digest::MD5.hexdigest(...)
          - pattern: Digest::MD5.digest(...)
          - pattern: Digest::MD5.new
          - pattern: OpenSSL::Digest::MD5
          - pattern: OpenSSL::Digest.new('MD5')
          - pattern: OpenSSL::Digest.new("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: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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: ruby-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: Digest::SHA1
          - pattern: Digest::SHA1.hexdigest(...)
          - pattern: Digest::SHA1.digest(...)
          - pattern: Digest::SHA1.new
          - pattern: OpenSSL::Digest::SHA1
          - pattern: OpenSSL::Digest.new('SHA1')
          - pattern: OpenSSL::Digest.new("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: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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: ruby-use-of-rsa-quantum
    patterns:
      - pattern-either:
          - pattern: OpenSSL::PKey::RSA.new(...)
          - pattern: OpenSSL::PKey::RSA.generate(...)
          - pattern: OpenSSL::PKey.read(...)
    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: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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: ruby-use-of-dsa-quantum
    patterns:
      - pattern-either:
          - pattern: OpenSSL::PKey::DSA.new(...)
          - pattern: OpenSSL::PKey::DSA.generate(...)
    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: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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: ruby-use-of-ec-quantum
    patterns:
      - pattern-either:
          - pattern: OpenSSL::PKey::EC.new(...)
          - pattern: OpenSSL::PKey::EC.generate(...)
          - pattern: OpenSSL::PKey::EC::Group.new(...)
    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: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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+"

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

  - id: ruby-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: OpenSSL::Cipher.new('DES')
          - pattern: OpenSSL::Cipher.new("DES")
          - pattern: OpenSSL::Cipher.new('des')
          - pattern: OpenSSL::Cipher.new("des")
          - pattern: OpenSSL::Cipher.new('DES-CBC')
          - pattern: OpenSSL::Cipher.new("DES-CBC")
          - pattern: OpenSSL::Cipher.new('DES-ECB')
          - pattern: OpenSSL::Cipher.new("DES-ECB")
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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: ruby-use-of-3des-quantum
    patterns:
      - pattern-either:
          - pattern: OpenSSL::Cipher.new('3DES')
          - pattern: OpenSSL::Cipher.new("3DES")
          - pattern: OpenSSL::Cipher.new('DES-EDE3')
          - pattern: OpenSSL::Cipher.new("DES-EDE3")
          - pattern: OpenSSL::Cipher.new('DES-EDE3-CBC')
          - pattern: OpenSSL::Cipher.new("DES-EDE3-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: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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: ruby-use-of-blowfish-quantum
    patterns:
      - pattern-either:
          - pattern: OpenSSL::Cipher.new('BF')
          - pattern: OpenSSL::Cipher.new("BF")
          - pattern: OpenSSL::Cipher.new('BF-CBC')
          - pattern: OpenSSL::Cipher.new("BF-CBC")
          - pattern: OpenSSL::Cipher.new('Blowfish')
          - pattern: OpenSSL::Cipher.new("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: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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: ruby-use-of-rc4-quantum
    patterns:
      - pattern-either:
          - pattern: OpenSSL::Cipher.new('RC4')
          - pattern: OpenSSL::Cipher.new("RC4")
          - pattern: OpenSSL::Cipher.new('rc4')
          - pattern: OpenSSL::Cipher.new("rc4")
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [ruby]
    metadata:
      category: security
      technology:
        - ruby
        - openssl
        - 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"
