rules:
  # =============================================================================
  # PHP 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: php-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: md5(...)
          - pattern: md5_file(...)
          - pattern: hash('md5', ...)
          - pattern: hash("md5", ...)
          - pattern: hash_file('md5', ...)
          - pattern: hash_file("md5", ...)
          - pattern: hash_init('md5')
          - pattern: hash_init("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: [php]
    metadata:
      category: security
      technology:
        - php
        - 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: php-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: sha1(...)
          - pattern: sha1_file(...)
          - pattern: hash('sha1', ...)
          - pattern: hash("sha1", ...)
          - pattern: hash_file('sha1', ...)
          - pattern: hash_file("sha1", ...)
          - pattern: hash_init('sha1')
          - pattern: hash_init("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: [php]
    metadata:
      category: security
      technology:
        - php
        - 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

  - id: php-use-of-md4-quantum
    patterns:
      - pattern-either:
          - pattern: hash('md4', ...)
          - pattern: hash("md4", ...)
          - pattern: hash_file('md4', ...)
          - pattern: hash_file("md4", ...)
    message: >-
      MD4 is completely broken classically and quantum-weakened by Grover's algorithm. Use SHA-256,
      SHA-384, or SHA-3.
    severity: ERROR
    languages: [php]
    metadata:
      category: security
      technology:
        - php
        - 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_risk_score: 10

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

  - id: php-use-of-rsa-quantum
    patterns:
      - pattern-either:
          - pattern: openssl_pkey_new($CONFIG)
          - pattern: openssl_public_encrypt(...)
          - pattern: openssl_private_decrypt(...)
          - pattern: openssl_private_encrypt(...)
          - pattern: openssl_public_decrypt(...)
          - pattern: openssl_sign(...)
          - pattern: openssl_verify(...)
    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: [php]
    metadata:
      category: security
      technology:
        - php
        - openssl
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      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: php-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: openssl_encrypt($DATA, 'des', ...)
          - pattern: openssl_encrypt($DATA, "des", ...)
          - pattern: openssl_encrypt($DATA, 'des-ecb', ...)
          - pattern: openssl_encrypt($DATA, "des-ecb", ...)
          - pattern: openssl_encrypt($DATA, 'des-cbc', ...)
          - pattern: openssl_encrypt($DATA, "des-cbc", ...)
          - pattern: openssl_decrypt($DATA, 'des', ...)
          - pattern: openssl_decrypt($DATA, "des", ...)
          - pattern: mcrypt_encrypt(MCRYPT_DES, ...)
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [php]
    metadata:
      category: security
      technology:
        - php
        - 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: php-use-of-3des-quantum
    patterns:
      - pattern-either:
          - pattern: openssl_encrypt($DATA, 'des-ede3', ...)
          - pattern: openssl_encrypt($DATA, "des-ede3", ...)
          - pattern: openssl_encrypt($DATA, 'des-ede3-cbc', ...)
          - pattern: openssl_encrypt($DATA, "des-ede3-cbc", ...)
          - pattern: mcrypt_encrypt(MCRYPT_3DES, ...)
          - pattern: mcrypt_encrypt(MCRYPT_TRIPLEDES, ...)
    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: [php]
    metadata:
      category: security
      technology:
        - php
        - 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: php-use-of-blowfish-quantum
    patterns:
      - pattern-either:
          - pattern: openssl_encrypt($DATA, 'bf', ...)
          - pattern: openssl_encrypt($DATA, "bf", ...)
          - pattern: openssl_encrypt($DATA, 'bf-cbc', ...)
          - pattern: openssl_encrypt($DATA, "bf-cbc", ...)
          - pattern: mcrypt_encrypt(MCRYPT_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: [php]
    metadata:
      category: security
      technology:
        - php
        - 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: php-use-of-rc4-quantum
    patterns:
      - pattern-either:
          - pattern: openssl_encrypt($DATA, 'rc4', ...)
          - pattern: openssl_encrypt($DATA, "rc4", ...)
          - pattern: mcrypt_encrypt(MCRYPT_RC4, ...)
          - pattern: mcrypt_encrypt(MCRYPT_ARCFOUR, ...)
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [php]
    metadata:
      category: security
      technology:
        - php
        - 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"

  - id: php-mcrypt-deprecated-quantum
    patterns:
      - pattern-either:
          - pattern: mcrypt_encrypt(...)
          - pattern: mcrypt_decrypt(...)
          - pattern: mcrypt_create_iv(...)
    message: >-
      QUANTUM VULNERABILITY: The mcrypt extension is deprecated and removed in PHP 7.2+. Many algorithms
      it supports are quantum-vulnerable or broken.
    severity: ERROR
    languages: [php]
    metadata:
      category: security
      technology:
        - php
        - 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_risk_score: 8
      post_quantum_alternatives:
        - "OpenSSL AES-256-GCM"
        - "Sodium (libsodium)"
