CodeQL queries 1.23

Skip to end of metadata
Go to start of metadata

Name: Use of a broken or risky cryptographic algorithm

Description: Using broken or weak cryptographic algorithms can allow an attacker to compromise security.

ID: cpp/weak-cryptographic-algorithm

Kind: problem

Severity: error

Precision: medium

Query: BrokenCryptoAlgorithm.ql
 * @name Use of a broken or risky cryptographic algorithm
 * @description Using broken or weak cryptographic algorithms can allow
 *              an attacker to compromise security.
 * @kind problem
 * @problem.severity error
 * @precision medium
 * @id cpp/weak-cryptographic-algorithm
 * @tags security
 *       external/cwe/cwe-327

import cpp

abstract class InsecureCryptoSpec extends Locatable {
  abstract string description();

Function getAnInsecureFunction() {
  result.getName().regexpMatch(algorithmBlacklistRegex()) and

class InsecureFunctionCall extends InsecureCryptoSpec, FunctionCall {
  InsecureFunctionCall() { this.getTarget() = getAnInsecureFunction() }

  override string description() { result = "function call" }

  override string toString() { result = FunctionCall.super.toString() }

  override Location getLocation() { result = FunctionCall.super.getLocation() }

Macro getAnInsecureMacro() {
  result.getName().regexpMatch(algorithmBlacklistRegex()) and

class InsecureMacroSpec extends InsecureCryptoSpec, MacroInvocation {
  InsecureMacroSpec() { this.getMacro() = getAnInsecureMacro() }

  override string description() { result = "macro invocation" }

  override string toString() { result = MacroInvocation.super.toString() }

  override Location getLocation() { result = MacroInvocation.super.getLocation() }

from InsecureCryptoSpec c
select c, "This " + c.description() + " specifies a broken or weak cryptographic algorithm."

Using broken or weak cryptographic algorithms can leave data vulnerable to being decrypted.

Many cryptographic algorithms provided by cryptography libraries are known to be weak, or flawed. Using such an algorithm means that an attacker may be able to easily decrypt the encrypted data.


Ensure that you use a strong, modern cryptographic algorithm. Use at least AES-128 or RSA-2048.


The following code shows an example of using the advapi windows API to decrypt some data. When creating a key, you must specify which algorithm to use. The first example uses DES which is an older algorithm that is now considered weak. The second example uses AES, which is a strong modern algorithm.

void advapi() {
  HCRYPTPROV hCryptProv;
  // other preparation goes here

  // BAD: use 3DES for key
  CryptDeriveKey(hCryptProv, CALG_3DES, hHash, 0, &hKey);

  // GOOD: use AES
  CryptDeriveKey(hCryptProv, CALG_AES_256, hHash, 0, &hKey);