rules:
  - id: cpp-use-of-md5
    patterns:
      - pattern-either:
          - pattern: MD5_Init(...)
          - pattern: MD5_Update(...)
          - pattern: MD5_Final(...)
          - pattern: MD5(...)
    message: "Use of MD5 hash function detected. MD5 is cryptographically broken and vulnerable to collision attacks. It is also vulnerable to quantum attacks. Use SHA-256, SHA-3, or post-quantum alternatives like SPHINCS+."
    severity: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      references:
        - https://www.kb.cert.org/vuls/id/836068
        - https://csrc.nist.gov/projects/post-quantum-cryptography
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_risk_score: 9

  - id: cpp-use-of-sha1
    patterns:
      - pattern-either:
          - pattern: SHA1_Init(...)
          - pattern: SHA1_Update(...)
          - pattern: SHA1_Final(...)
          - pattern: SHA1(...)
          - pattern: SHA_Init(...)
          - pattern: SHA_Update(...)
          - pattern: SHA_Final(...)
    message: "Use of SHA-1 hash function detected. SHA-1 is cryptographically broken and vulnerable to collision attacks. It is also vulnerable to quantum attacks. Use SHA-256, SHA-3, or post-quantum alternatives."
    severity: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
      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: 9

  - id: cpp-use-of-des
    patterns:
      - pattern-either:
          - pattern: DES_set_key(...)
          - pattern: DES_ecb_encrypt(...)
          - pattern: DES_cbc_encrypt(...)
          - pattern: DES_cfb_encrypt(...)
          - pattern: DES_ofb_encrypt(...)
          - pattern: DES_key_schedule(...)
    message: "Use of DES encryption detected. DES uses a 56-bit key which is completely broken and can be brute-forced. It is extremely vulnerable to quantum attacks. Use AES-256 or post-quantum encryption algorithms."
    severity: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
      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: 9

  - id: cpp-weak-rsa-key
    patterns:
      - pattern-either:
          - pattern: RSA_generate_key_ex($RSA, 512, ...)
          - pattern: RSA_generate_key_ex($RSA, 1024, ...)
          - pattern: RSA_generate_key_ex($RSA, 2048, ...)
          - pattern: RSA_generate_key($RSA, 512, ...)
          - pattern: RSA_generate_key($RSA, 1024, ...)
          - pattern: RSA_generate_key($RSA, 2048, ...)
    message: "Weak RSA key size detected. RSA with key sizes <= 2048 bits is vulnerable to quantum attacks using Shor's algorithm. Use RSA-4096 or migrate to post-quantum alternatives like Kyber or Dilithium."
    severity: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_risk_score: 9

  - id: cpp-aes-ecb-mode
    patterns:
      - pattern-either:
          - pattern: AES_ecb_encrypt(...)
          - pattern: AES_set_encrypt_key(..., $KEY, $MODE)
          - pattern: EVP_aes_$SIZE_ecb(...)
    message: "Use of AES in ECB mode detected. ECB mode does not provide semantic security and identical plaintext blocks produce identical ciphertext blocks, revealing patterns. Use AES-GCM, AES-CTR, or ChaCha20-Poly1305 instead."
    severity: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
      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: 9

  - id: cpp-hardcoded-crypto-key
    patterns:
      - pattern-either:
          - pattern: const char* $KEY = "...";
          - pattern: constexpr const char* $KEY = "...";
          - pattern: const unsigned char $KEY[] = "...";
          - pattern: char $KEY[] = "...";
    message: "Hard-coded cryptographic key detected. Hard-coded keys are visible in source code and binaries, making them easy to extract. Store keys securely using key management systems or hardware security modules."
    severity: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - crypto
      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: 9

  - id: cpp-weak-random-seed
    patterns:
      - pattern-either:
          - pattern: srand(time(...))
          - pattern: srand($SEED)
    message: "Predictable random number generator seed detected. Using time-based seeds makes random numbers predictable. For cryptographic purposes, use std::random_device or platform-specific secure random number generators."
    severity: WARNING
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - crypto
      cwe:
        - "CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 9

  - id: cpp-quantum-vulnerable-ecc
    patterns:
      - pattern-either:
          - pattern: EC_KEY_new(...)
          - pattern: EC_KEY_new_by_curve_name(...)
          - pattern: EC_KEY_generate_key(...)
          - pattern: ECDSA_sign(...)
          - pattern: ECDSA_do_sign(...)
          - pattern: ECDH_compute_key(...)
          - pattern: EVP_PKEY_EC
          - pattern: EVP_PKEY_CTX_new_id(EVP_PKEY_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: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
        - 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: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (encryption/KEM)"
        - "CRYSTALS-Dilithium (signatures)"
        - "FALCON (signatures)"
        - "SPHINCS+ (hash-based signatures)"
      references:
        - https://csrc.nist.gov/projects/post-quantum-cryptography
        - https://nvlpubs.nist.gov/nistpubs/ir/2024/NIST.IR.8547.ipd.pdf

  - id: cpp-quantum-vulnerable-dsa
    patterns:
      - pattern-either:
          - pattern: DSA_new(...)
          - pattern: DSA_generate_parameters(...)
          - pattern: DSA_generate_parameters_ex(...)
          - pattern: DSA_generate_key(...)
          - pattern: DSA_sign(...)
          - pattern: DSA_do_sign(...)
          - pattern: EVP_PKEY_DSA
          - pattern: EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, ...)
    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: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
        - 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: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Dilithium"
        - "FALCON"
        - "SPHINCS+"
      references:
        - https://csrc.nist.gov/projects/post-quantum-cryptography

  - id: cpp-quantum-vulnerable-diffie-hellman
    patterns:
      - pattern-either:
          - pattern: DH_new(...)
          - pattern: DH_generate_parameters(...)
          - pattern: DH_generate_parameters_ex(...)
          - pattern: DH_generate_key(...)
          - pattern: DH_compute_key(...)
          - pattern: EVP_PKEY_DH
          - pattern: EVP_PKEY_CTX_new_id(EVP_PKEY_DH, ...)
    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: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
        - 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: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Kyber"
      references:
        - https://csrc.nist.gov/projects/post-quantum-cryptography
        - https://www.rfc-editor.org/rfc/rfc9180.html

  - id: cpp-use-of-3des
    patterns:
      - pattern-either:
          - pattern: DES_ede3_cbc_encrypt(...)
          - pattern: DES_ede3_ecb_encrypt(...)
          - pattern: DES_ede3_cfb64_encrypt(...)
          - pattern: DES_ede3_ofb64_encrypt(...)
          - pattern: EVP_des_ede3(...)
          - pattern: EVP_des_ede3_cbc(...)
          - pattern: EVP_des_ede3_ecb(...)
    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: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
        - 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: 8
      post_quantum_alternatives:
        - "AES-256"

  - id: cpp-use-of-rc4
    patterns:
      - pattern-either:
          - pattern: RC4_set_key(...)
          - pattern: RC4(...)
          - pattern: EVP_rc4(...)
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
      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: 9
      post_quantum_alternatives:
        - "AES-256-GCM"
        - "ChaCha20-Poly1305"

  - id: cpp-insecure-strcpy
    patterns:
      - pattern-either:
          - pattern: strcpy($DEST, $SRC)
          - pattern: strcat($DEST, $SRC)
    message: "Use of insecure string copy function detected. strcpy/strcat do not perform bounds checking and can lead to buffer overflows. Use strncpy, strncat, or better yet, use std::string in C++."
    severity: WARNING
    languages: [c, cpp]
    metadata:
      category: security
      technology:
        - c-stdlib
      cwe:
        - "CWE-120: Buffer Copy without Checking Size of Input"
      owasp:
        - A03:2021 - Injection
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 9

  - id: cpp-static-hardcoded-iv
    patterns:
      - pattern-either:
          - pattern: unsigned char $IV[] = {0};
          - pattern: unsigned char $IV[$SIZE] = {0};
          - pattern: const unsigned char $IV[] = {0};
          - pattern: unsigned char $IV[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    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: [c, cpp]
    metadata:
      category: security
      technology:
        - openssl
        - crypto
      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
      references:
        - https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption
