rules:
  # =============================================================================
  # LUA POST-QUANTUM CRYPTOGRAPHIC VULNERABILITY RULES
  # =============================================================================
  # Covers luacrypto, lua-openssl, luasec, and OpenResty (resty.openssl).
  # TLS rules are in tls-config.yaml.
  # =============================================================================

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

  - id: lua-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.digest("md5", ...)
          - pattern: crypto.digest('md5', ...)
          - pattern: openssl.digest.new("MD5")
          - pattern: openssl.digest.new('MD5')
          - pattern: md5(...)
          - pattern: md5.sum(...)
          - pattern: md5.digest(...)
          - pattern: resty.md5:new()
    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: [lua]
    metadata:
      category: security
      technology: [lua, openresty, nginx, 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: lua-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.digest("sha1", ...)
          - pattern: crypto.digest('sha1', ...)
          - pattern: openssl.digest.new("SHA1")
          - pattern: openssl.digest.new('SHA1')
          - pattern: openssl.digest.new("SHA-1")
          - pattern: sha1(...)
          - pattern: resty.sha1:new()
    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: [lua]
    metadata:
      category: security
      technology: [lua, openresty, nginx, 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: lua-use-of-rsa-quantum
    patterns:
      - pattern-either:
          - pattern-regex: 'openssl\.pkey\.new\s*\(\s*\{[^}]*type\s*=\s*[''"]rsa[''"]'
          - pattern-regex: 'crypto\.pkey\.new\s*\(\s*\{[^}]*type\s*=\s*[''"]rsa[''"]'
          - pattern: rsa.generate(...)
          - pattern: pkey.generate("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: [lua]
    metadata:
      category: security
      technology: [lua, openresty, 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

  - id: lua-use-of-ec-quantum
    patterns:
      - pattern-either:
          - pattern-regex: 'openssl\.pkey\.new\s*\(\s*\{[^}]*type\s*=\s*[''"]ec[''"]'
          - pattern: pkey.generate("ec", ...)
    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: [lua]
    metadata:
      category: security
      technology: [lua, openresty, 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

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

  - id: lua-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.encrypt("des", ...)
          - pattern: crypto.encrypt('des', ...)
          - pattern: crypto.encrypt("des-cbc", ...)
          - pattern: crypto.encrypt("des-ecb", ...)
          - pattern: openssl.cipher.new("DES-CBC")
          - pattern: openssl.cipher.new("DES-ECB")
          - pattern: openssl.cipher.new('DES-CBC')
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [lua]
    metadata:
      category: security
      technology: [lua, openresty, 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

  - id: lua-use-of-3des-quantum
    patterns:
      - pattern-either:
          - pattern: openssl.cipher.new("DES-EDE3-CBC")
          - pattern: openssl.cipher.new("DES-EDE3")
          - pattern: openssl.cipher.new('DES-EDE3-CBC')
          - pattern: crypto.encrypt("des-ede3", ...)
          - pattern: crypto.encrypt("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: [lua]
    metadata:
      category: security
      technology: [lua, openresty, 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

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

  # ---------------------------------------------------------------------------
  # KEY MATERIAL VULNERABILITIES
  # ---------------------------------------------------------------------------

  - id: lua-hardcoded-crypto-key
    patterns:
      - pattern-either:
          - pattern: openssl.cipher.new($ALG):encrypt('...', ...)
          - pattern: openssl.cipher.new($ALG):encrypt("...", ...)
          - pattern: crypto.encrypt($ALG, '...', ...)
          - pattern: crypto.encrypt($ALG, "...", ...)
          - pattern: crypto.hmac.digest($ALG, $DATA, '...')
          - pattern: crypto.hmac.digest($ALG, $DATA, "...")
    message: >-
      Hardcoded cryptographic key in source code exposes key material to anyone with code access. Use
      environment variables or a dedicated secrets manager.
    severity: ERROR
    languages: [lua]
    metadata:
      category: security
      technology: [lua, openresty]
      cwe:
        - "CWE-798: Use of Hard-coded Credentials"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 8

  # ---------------------------------------------------------------------------
  # WEAK RANDOM NUMBER GENERATOR
  # ---------------------------------------------------------------------------

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