rules:
  - id: rust-quantum-vulnerable-rsa-small-key
    patterns:
      - pattern-either:
          - pattern: |
              Rsa::generate($BITS, ...)
          - pattern: |
              RsaPrivateKey::new($RNG, $BITS)
          - pattern: |
              RsaKeyPair::generate($BITS, ...)
      - metavariable-comparison:
          metavariable: $BITS
          comparison: $BITS < 2048
    message: >-
      RSA key size below 2048 bits is vulnerable to quantum attacks using Shor's algorithm.
      Post-quantum cryptography recommends using quantum-resistant algorithms like CRYSTALS-Kyber,
      CRYSTALS-Dilithium, or SPHINCS+. If RSA must be used, minimum 3072-bit keys are recommended,
      though still vulnerable to future quantum computers.
    languages: [rust]
    severity: ERROR
    metadata:
      category: security
      subcategory: [quantum-vulnerability]
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Dilithium"
        - "CRYSTALS-Kyber"
        - "FALCON"
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      vulnerability_class:
        - "Quantum-Vulnerable Cryptography"
      references:
        - "https://csrc.nist.gov/projects/post-quantum-cryptography"
        - "https://www.enisa.europa.eu/publications/post-quantum-cryptography-current-state-and-quantum-mitigation"

  - id: rust-quantum-vulnerable-ecc-curve
    patterns:
      - pattern-either:
          - pattern: |
              EcGroup::from_curve_name(Nid::$CURVE)
          - pattern: |
              EllipticCurve::$CURVE
          - pattern: |
              p256::SecretKey::random(...)
          - pattern: |
              p384::SecretKey::random(...)
          - pattern: |
              k256::SecretKey::random(...)
    message: >-
      Elliptic Curve Cryptography (ECC) is vulnerable to quantum attacks using Shor's algorithm.
      All NIST P-curves (P-256, P-384, P-521), secp256k1, and other elliptic curves can be broken
      by quantum computers. Migrate to post-quantum algorithms like CRYSTALS-Kyber for key exchange
      or CRYSTALS-Dilithium for signatures.
    languages: [rust]
    severity: ERROR
    metadata:
      category: security
      subcategory: [quantum-vulnerability]
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm (modified for elliptic curves)"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (key exchange)"
        - "CRYSTALS-Dilithium (signatures)"
        - "FALCON (signatures)"
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      vulnerability_class:
        - "Quantum-Vulnerable Cryptography"
      references:
        - "https://csrc.nist.gov/projects/post-quantum-cryptography"

  - id: rust-quantum-vulnerable-dh-exchange
    patterns:
      - pattern-either:
          - pattern: |
              DhParameters::from_pem(...)
          - pattern: |
              DhParameters::generate(...)
          - pattern: |
              Dh::generate_params(...)
          - pattern: |
              EphemeralSecret::random(...)
    message: >-
      Diffie-Hellman (DH) and Elliptic Curve Diffie-Hellman (ECDH) key exchanges are vulnerable
      to quantum attacks using Shor's algorithm. Quantum computers can break the discrete logarithm
      problem. Use post-quantum key encapsulation mechanisms (KEMs) like CRYSTALS-Kyber instead.
    languages: [rust]
    severity: ERROR
    metadata:
      category: security
      subcategory: [quantum-vulnerability]
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (ML-KEM, FIPS 203)"
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      vulnerability_class:
        - "Quantum-Vulnerable Cryptography"

  - id: rust-des-3des-usage
    patterns:
      - pattern-either:
          - pattern: |
              Des::new(...)
          - pattern: |
              TripleDes::new(...)
          - pattern: |
              Des::new_from_slice(...)
          - pattern: |
              TripleDes::new_from_slice(...)
    message: >-
      DES and 3DES are deprecated encryption algorithms vulnerable to quantum attacks.
      Grover's algorithm halves effective key strength: DES (56-bit) ? 28-bit quantum security,
      3DES (112-bit) ? 56-bit quantum security. 3DES is also vulnerable to Sweet32 attacks.
      Use AES-256-GCM or ChaCha20-Poly1305 for symmetric encryption.
    languages: [rust]
    severity: ERROR
    metadata:
      category: security
      subcategory: [vuln]
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "AES-256-GCM"
        - "ChaCha20-Poly1305"
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      vulnerability_class:
        - "Broken Cryptography"

  - id: rust-weak-hash-md5-sha1
    patterns:
      - pattern-either:
          - pattern: |
              Md5::new()
          - pattern: |
              Sha1::new()
          - pattern: |
              md5::compute(...)
          - pattern: |
              sha1::Sha1::new()
    message: >-
      MD5 and SHA-1 are cryptographically broken hash functions vulnerable to collision attacks.
      Use SHA-256, SHA-384, SHA-512, SHA-3, or BLAKE2 for secure hashing.
      For quantum resistance considerations, SHA-256 provides approximately 128-bit quantum security
      (Grover's algorithm reduces classical 256-bit security to 128-bit quantum security).
    languages: [rust]
    severity: ERROR
    metadata:
      category: security
      subcategory: [vuln]
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "SHA-256"
        - "SHA-3 (Keccak)"
        - "BLAKE2b/BLAKE3"
      cwe:
        - "CWE-328: Use of Weak Hash"
      vulnerability_class:
        - "Broken Cryptography"

  - id: rust-rc4-cipher
    patterns:
      - pattern-either:
          - pattern: |
              Rc4::new(...)
          - pattern: |
              Rc4::new_from_slice(...)
    message: >-
      RC4 cipher is cryptographically broken and should not be used.
      It has known statistical biases exploited in attacks (BEAST, CRIME, Royal Holloway).
      Use AES-256-GCM or ChaCha20-Poly1305 instead.
    languages: [rust]
    severity: ERROR
    metadata:
      category: security
      subcategory: [vuln]
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "AES-256-GCM"
        - "ChaCha20-Poly1305"
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"

  - id: rust-ecb-mode-usage
    patterns:
      - pattern-either:
          - pattern: |
              ecb::Encryptor::new(...)
          - pattern: |
              ecb::Decryptor::new(...)
          - pattern: |
              Aes128Ecb::new(...)
          - pattern: |
              Aes256Ecb::new(...)
    message: >-
      Electronic Codebook (ECB) mode is insecure as it does not provide semantic security.
      Identical plaintext blocks produce identical ciphertext blocks, revealing patterns.
      Use AES-GCM, AES-CCM, or ChaCha20-Poly1305 (authenticated encryption modes) instead.
    languages: [rust]
    severity: ERROR
    metadata:
      category: security
      subcategory: [vuln]
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 6
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"

  - id: rust-blowfish-usage
    patterns:
      - pattern-either:
          - pattern: |
              Blowfish::new(...)
          - pattern: |
              Blowfish::new_from_slice(...)
    message: >-
      Blowfish cipher has known vulnerabilities and a 64-bit block size that makes it vulnerable
      to birthday attacks. Use AES-256-GCM or ChaCha20-Poly1305 instead.
    languages: [rust]
    severity: WARNING
    metadata:
      category: security
      subcategory: [vuln]
      confidence: HIGH
      likelihood: MEDIUM
      impact: MEDIUM
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm (64-bit block size worsens Grover's impact)"
      quantum_risk_score: 7
      post_quantum_alternatives:
        - "AES-256-GCM"
        - "ChaCha20-Poly1305"
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"

  - id: rust-hardcoded-crypto-key
    patterns:
      - pattern-either:
          - pattern: |
              $CIPHER::new(&[$...BYTES])
          - pattern: |
              $CIPHER::new_from_slice(&[$...BYTES])
          - pattern: |
              let $KEY = [$...BYTES];
              ...
              $CIPHER::new(&$KEY)
      - metavariable-pattern:
          metavariable: $CIPHER
          patterns:
            - pattern-either:
                - pattern: Aes128
                - pattern: Aes192
                - pattern: Aes256
                - pattern: ChaCha20
                - pattern: Des
                - pattern: TripleDes
    message: >-
      Hardcoded cryptographic keys are a security vulnerability. Keys should be stored securely
      and loaded from environment variables, key management services, or secure vaults.
    languages: [rust]
    severity: ERROR
    metadata:
      category: security
      subcategory: [vuln]
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 8
      cwe:
        - "CWE-798: Use of Hard-coded Credentials"

  - id: rust-small-aes-key
    patterns:
      - pattern-either:
          - pattern: |
              Aes128::new(...)
          - pattern: |
              Aes128Gcm::new(...)
          - pattern: |
              Aes192::new(...)
          - pattern: |
              Aes192Gcm::new(...)
    message: >-
      AES-128 provides 64-bit quantum security and AES-192 provides 96-bit quantum
      security due to Grover's algorithm (which effectively halves key strength).
      For full quantum resistance use AES-256 which provides 128-bit quantum security.
    languages: [rust]
    severity: WARNING
    metadata:
      category: security
      subcategory: [quantum-vulnerability]
      confidence: MEDIUM
      likelihood: LOW
      impact: MEDIUM
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm (halves effective key strength)"
      quantum_risk_score: 5
      post_quantum_alternatives:
        - "AES-256-GCM"
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      vulnerability_class:
        - "Quantum-Vulnerable Cryptography"
