# ============================================================
# Quantum Cryptography Vulnerability Rules -- R Language
# ============================================================
# Language: R
# Rule format: pattern-regex (NOT pattern: or patterns:)
#
# Why pattern-regex:
#   opengrep's AST parser does not have reliable support for R's
#   function-call syntax and named argument conventions (e.g.,
#   digest(x, algo="md5")). Using pattern-regex avoids false
#   negatives caused by AST parse failures and provides precise,
#   language-agnostic pattern matching across all R source files.
#
# Approach:
#   Each rule targets a specific cryptographic API call or function
#   name that is known to be quantum-vulnerable. Regex patterns are
#   anchored to the function signature to minimise false positives
#   while remaining tolerant of whitespace variation.
# ============================================================

rules:
  - id: r-use-of-md5-quantum
    pattern-regex: "digest\\s*\\(.*?algo\\s*=\\s*[\"'](?:md5|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: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
        - "CWE-328: Use of Weak Hash"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: r-use-of-sha1-quantum
    pattern-regex: "digest\\s*\\(.*?algo\\s*=\\s*[\"'](?:sha-?1|SHA-?1)[\"']"
    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: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
        - "CWE-328: Use of Weak Hash"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: r-use-of-rsa-quantum
    pattern-regex: "openssl::rsa_keygen\\b"
    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: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8

  - id: r-use-of-ecc-quantum
    pattern-regex: "openssl::ec_keygen\\b"
    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: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8

  - id: r-use-of-dsa-quantum
    pattern-regex: "openssl::dsa_keygen\\b"
    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: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 8

  - id: r-use-of-des-quantum
    pattern-regex: "openssl::des_\\w+"
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe: ["CWE-327: Use of a Broken or Risky Cryptographic Algorithm"]
      owasp: ["A02:2021 - Cryptographic Failures"]
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: r-use-of-rc4-quantum
    pattern-regex: "openssl::rc4\\s*\\("
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe: ["CWE-327: Use of a Broken or Risky Cryptographic Algorithm"]
      owasp: ["A02:2021 - Cryptographic Failures"]
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: r-use-of-aes128-quantum
    pattern-regex: "openssl::aes_keygen\\s*\\(\\s*128\\s*\\)"
    message: >-
      AES-128/192 provide only 64/96-bit quantum security under Grover's algorithm. Use AES-256 for
      128-bit post-quantum security.
    severity: WARNING
    languages: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe: ["CWE-326: Inadequate Encryption Strength"]
      owasp: ["A02:2021 - Cryptographic Failures"]
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 7

  - id: r-use-of-aes192-quantum
    pattern-regex: "openssl::aes_keygen\\s*\\(\\s*192\\s*\\)"
    message: >-
      AES-128/192 provide only 64/96-bit quantum security under Grover's algorithm. Use AES-256 for
      128-bit post-quantum security.
    severity: WARNING
    languages: [r]
    metadata:
      category: security
      technology: [r, post-quantum]
      cwe: ["CWE-326: Inadequate Encryption Strength"]
      owasp: ["A02:2021 - Cryptographic Failures"]
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 6
