rules:
  # =============================================================================
  # SWIFT 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: swift-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: CC_MD5(...)
          - pattern: Insecure.MD5
          - pattern: Insecure.MD5.hash(...)
          - pattern: CryptoKit.Insecure.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: [swift]
    metadata:
      category: security
      technology:
        - swift
        - cryptokit
        - 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:
        - "SHA256"
        - "SHA384"
        - "SHA512"

  - id: swift-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: CC_SHA1(...)
          - pattern: Insecure.SHA1
          - pattern: Insecure.SHA1.hash(...)
          - pattern: CryptoKit.Insecure.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: [swift]
    metadata:
      category: security
      technology:
        - swift
        - cryptokit
        - 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: swift-use-of-rsa-quantum
    patterns:
      - pattern-either:
          - pattern: SecKeyAlgorithm.rsaEncryption...
          - pattern: SecKeyAlgorithm.rsaSignature...
          - pattern: kSecAttrKeyTypeRSA
          - pattern: .rsaEncryptionOAEP...
          - pattern: .rsaEncryptionPKCS1
          - pattern: .rsaSignatureMessage...
    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: [swift]
    metadata:
      category: security
      technology:
        - swift
        - security-framework
        - 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: swift-use-of-ec-quantum
    patterns:
      - pattern-either:
          - pattern: kSecAttrKeyTypeECSECPrimeRandom
          - pattern: SecKeyAlgorithm.ecies...
          - pattern: SecKeyAlgorithm.ecdhKeyExchange...
          - pattern: .ecdsaSignature...
          - pattern: P256.Signing
          - pattern: P384.Signing
          - pattern: P521.Signing
          - pattern: P256.KeyAgreement
          - pattern: P384.KeyAgreement
          - pattern: P521.KeyAgreement
    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: [swift]
    metadata:
      category: security
      technology:
        - swift
        - cryptokit
        - 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: swift-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: CCAlgorithm(kCCAlgorithmDES)
          - pattern: kCCAlgorithmDES
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [swift]
    metadata:
      category: security
      technology:
        - swift
        - commoncrypto
        - 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-GCM"
        - "ChaChaPoly"

  - id: swift-use-of-3des-quantum
    patterns:
      - pattern-either:
          - pattern: CCAlgorithm(kCCAlgorithm3DES)
          - pattern: kCCAlgorithm3DES
    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: [swift]
    metadata:
      category: security
      technology:
        - swift
        - commoncrypto
        - 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-GCM"

  - id: swift-use-of-rc4-quantum
    patterns:
      - pattern-either:
          - pattern: CCAlgorithm(kCCAlgorithmRC4)
          - pattern: kCCAlgorithmRC4
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [swift]
    metadata:
      category: security
      technology:
        - swift
        - commoncrypto
        - 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"
        - "ChaChaPoly"

  - id: swift-use-of-blowfish-quantum
    patterns:
      - pattern-either:
          - pattern: CCAlgorithm(kCCAlgorithmBlowfish)
          - pattern: kCCAlgorithmBlowfish
    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: [swift]
    metadata:
      category: security
      technology:
        - swift
        - commoncrypto
        - 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-GCM"

  - id: swift-use-of-cast-quantum
    patterns:
      - pattern-either:
          - pattern: CCAlgorithm(kCCAlgorithmCAST)
          - pattern: kCCAlgorithmCAST
    message: >-
      CAST cipher provides no post-quantum security and uses insufficient key sizes. Migrate to
      AES-256-GCM.
    severity: WARNING
    languages: [swift]
    metadata:
      category: security
      technology:
        - swift
        - commoncrypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: MEDIUM
      impact: MEDIUM
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 6
      post_quantum_alternatives:
        - "AES-256-GCM"

  # ---------------------------------------------------------------------------
  # IMPLEMENTATION VULNERABILITIES (added to match benchmark coverage)
  # ---------------------------------------------------------------------------

  - id: swift-aes-ecb-mode
    patterns:
      - pattern-either:
          - pattern: CCCrypt($OP, $ALG, kCCOptionECBMode, ...)
          - pattern: CCCryptorCreate($OP, $ALG, kCCOptionECBMode, ...)
    message: >-
      AES in ECB mode leaks data patterns -- identical plaintext blocks produce identical ciphertext. Use
      AES-256-GCM for authenticated encryption.
    severity: ERROR
    languages: [swift]
    metadata:
      category: security
      technology: [swift, commoncrypto]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 6

  - id: swift-static-iv
    patterns:
      - pattern-either:
          - pattern: CCCrypt($OP, $ALG, $OPT, $KEY, $KEYLEN, nil, ...)
          - pattern: |
              let $IV = [UInt8](repeating: 0, count: ...)
              ...
              CCCrypt($OP, $ALG, $OPT, $KEY, $KEYLEN, $IV, ...)
    message: >-
      Static or hardcoded IV breaks IND-CPA security -- identical plaintexts produce identical ciphertext.
      Generate a fresh random IV for every encryption operation.
    severity: ERROR
    languages: [swift]
    metadata:
      category: security
      technology: [swift, commoncrypto]
      cwe:
        - "CWE-329: Not Using a Random IV with CBC Mode"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: swift-hardcoded-crypto-key
    patterns:
      - pattern-either:
          - pattern: CCCrypt($OP, $ALG, $OPT, "...", ...)
          - pattern: |
              let $KEY = "..."
              ...
              CCCrypt($OP, $ALG, $OPT, $KEY, ...)
          - pattern: |
              let $KEY: [UInt8] = [...]
              ...
              CCCrypt($OP, $ALG, $OPT, $KEY, ...)
    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: [swift]
    metadata:
      category: security
      technology: [swift, commoncrypto]
      cwe:
        - "CWE-798: Use of Hard-coded Credentials"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 8

  - id: swift-weak-random
    patterns:
      - pattern-either:
          - pattern: rand()
          - pattern: random()
          - pattern: drand48()
          - pattern: |
              Int.random(in: ...)
    message: >-
      Non-cryptographic PRNG used for security-sensitive operations -- output is predictable. Use a
      cryptographically secure random number generator.
    severity: WARNING
    languages: [swift]
    metadata:
      category: security
      technology: [swift]
      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

  - id: swift-weak-pbkdf2-iterations
    patterns:
      - pattern: CCKeyDerivationPBKDF(kCCPBKDF2, $PWD, $PWDLEN, $SALT, $SALTLEN, $PRF, $ROUNDS, $KEY, $KEYLEN)
      - metavariable-comparison:
          metavariable: $ROUNDS
          comparison: $ROUNDS < 100000
    message: >-
      Insufficient PBKDF2 iterations make the derived key brute-forceable. Use at least 310,000 iterations
      (NIST SP 800-132) or switch to Argon2.
    severity: ERROR
    languages: [swift]
    metadata:
      category: security
      technology: [swift, commoncrypto]
      cwe:
        - "CWE-916: Use of Password Hash With Insufficient Computational Effort"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm (accelerates brute-force)"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "Argon2 (via swift-crypto)"
        - "scrypt"

  - id: swift-weak-rsa-key-size
    patterns:
      - pattern: |
          $ATTRS[kSecAttrKeyType as String] = kSecAttrKeyTypeRSA as AnyObject
          ...
          $ATTRS[kSecAttrKeySizeInBits as String] = $SIZE as AnyObject
      - metavariable-comparison:
          metavariable: $SIZE
          comparison: $SIZE < 2048
    message: >-
      RSA key size $SIZE bits is too small and quantum-vulnerable. 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: ERROR
    languages: [swift]
    metadata:
      category: security
      technology: [swift, security-framework, post-quantum]
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (ML-KEM)"
        - "CRYSTALS-Dilithium (ML-DSA)"
