Skip to content

Java: Update Encryption.qll for NIST SP 800-131A Rev. 2 - #23224

Open
owen-mc wants to merge 2 commits into
github:mainfrom
owen-mc:java/encryption-nist-800-131ar2
Open

owen-mc wants to merge 2 commits into
github:mainfrom
owen-mc:java/encryption-nist-800-131ar2

Conversation

@owen-mc

@owen-mc owen-mc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Supersedes #13830 (originally contributed by @unprovable), updated for the current Encryption.qll design now that NIST SP 800-131A Rev. 2's end-of-2023 deprecation of 3TDEA (Triple DES) is in effect.

  • Add 3TDEA / Triple DES (DESede, DESedeWrap, TripleDES, 3DES, DES3) and SKIPJACK / Skipjack to insecureAlgorithm with NIST SP 800-131A Rev. 2 explanations in Encryption.qll.
  • Guard single DES with negative lookbehinds/lookahead so 3DES strings (such as "DESede", "DESedeWrap", "TripleDES", and "3DES") are no longer matched by single DES with its 56-bit key-length message or reported twice, and all-uppercase/lowercase variants such as "DESEDE" are also detected.
  • Add ChaCha20 and ChaCha20Poly1305 (covering JCA standard "ChaCha20" and "ChaCha20-Poly1305" from JEP 329 as well as unhyphenated "ChaCha20Poly1305") to getASecureAlgorithmName() so java/potentially-weak-cryptographic-algorithm no longer flags them.
  • Update BrokenCryptoAlgorithm.qhelp to link to NIST SP 800-131A Rev. 2 over HTTPS.
  • Add library tests, query tests, and a change note.

Evidence

  • codeql test run -j 0 java/ql/test/query-tests/security/CWE-327/semmle/tests java/ql/test/library-tests/Encryption passes all 5 tests (BrokenCryptoAlgorithm.qlref, MaybeBrokenCryptoAlgorithm.qlref, secure.ql, cryptoalgospec.ql, insecure.ql).

Report SKIPJACK and 3DES variants (DESede, DESedeWrap, TripleDES, 3DES)
with dedicated insecurity explanations, prevent single-DES from
misclassifying 3DES strings, and add ChaCha20 / ChaCha20-Poly1305 to
the secure algorithm allowlist.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

java/ql/src/Security/CWE/CWE-327/BrokenCryptoAlgorithm.qhelp

Use of a broken or risky cryptographic algorithm

Using broken or weak cryptographic algorithms may compromise security guarantees such as confidentiality, integrity, and authenticity.

Many cryptographic algorithms are known to be weak or flawed. The security guarantees of a system often rely on the underlying cryptography, so using a weak algorithm can have severe consequences. For example:

  • If a weak encryption algorithm is used, an attacker may be able to decrypt sensitive data.
  • If a weak hashing algorithm is used to protect data integrity, an attacker may be able to craft a malicious input that has the same hash as a benign one.
  • If a weak algorithm is used for digital signatures, an attacker may be able to forge signatures and impersonate legitimate users.

Recommendation

Ensure that you use a strong, modern cryptographic algorithm. Use at least AES-128 or RSA-2048. Do not use the ECB encryption mode since it is vulnerable to replay and other attacks.

Example

The following code shows an example of using a java Cipher to encrypt some data. When creating a Cipher instance, you must specify the encryption 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.

// BAD: DES is a weak algorithm 
Cipher des = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));

// ...

// GOOD: AES is a strong algorithm
Cipher aes = Cipher.getInstance("AES");

// ...

References

@owen-mc
owen-mc marked this pull request as ready for review November 24, 2026 12:35
@owen-mc
owen-mc requested a review from a team as a code owner November 24, 2026 12:35
@owen-mc
owen-mc requested review from a team and an adaptive review from Copilot November 24, 2026 12:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new DES exclusions drop existing weak-algorithm alerts for PBEWithSHA1AndDESedE and DESedeRFC3211Wrap because neither matches the replacement aliases.

1 open issue
What changed in this PR

Updates Java cryptography analysis for NIST SP 800-131A Rev. 2 and recognizes ChaCha20 algorithms as secure.

Changes:

  • Adds Triple DES and Skipjack detection, with dedicated explanations and guards against single-DES misclassification.
  • Recognizes ChaCha20 and Poly1305 variants to reduce false positives.
  • Adds regression tests, updates expected results and the NIST reference, and documents the analysis changes.
File Description
java/​ql/​test/​query-tests/​security/​CWE-327/​semmle/​tests/​Test.java Adds weak-algorithm alerts and secure ChaCha20 cases.
java/​ql/​test/​query-tests/​security/​CWE-327/​semmle/​tests/​BrokenCryptoAlgorithm.expected Records new Triple DES and Skipjack results.
java/​ql/​test/​library-tests/​Encryption/​Test.java Adds insecure aliases and secure algorithm strings.
java/​ql/​test/​library-tests/​Encryption/​secure.expected Records secure ChaCha20 recognition.
java/​ql/​test/​library-tests/​Encryption/​insecure.expected Records dedicated Triple DES and Skipjack explanations.
java/​ql/​test/​library-tests/​Encryption/​cryptoalgospec.expected Updates source locations after test additions.
java/​ql/​src/​Security/​CWE/​CWE-327/​BrokenCryptoAlgorithm.qhelp Links to NIST Rev. 2 over HTTPS.
java/​ql/​lib/​semmle/​code/​java/​security/​Encryption.qll Updates insecure matching and secure algorithm recognition.
java/​ql/​lib/​change-notes/​2026-11-24-encryption-nist-800-131ar2.md Documents changes to both cryptography queries.

Review effort: Max (adaptive)

Recognize case-insensitive PBEWithSHA1AndDESede aliases and the RFC3211 wrapping suffix. Add regression tests ensuring each name has exactly one Triple DES explanation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Inconsistent regex scoping lets weak algorithms escape both detection queries, while a registered Triple DES cipher is unrecognized and the NIST guidance misrepresents permitted legacy operations.

3 open issues
1 resolved since last review

Review effort: Max (adaptive)

// 3TDEA (Triple DES) has different names across JCA, JSSE, and Bouncy Castle.
name =
[
"DES[-_]?ede((RFC3211)?Wrap)?", "(?i)PBEWithSHA1AndDESede", "Triple[-_]?DES", "3[-_]?DES",
name =
[
"DES[-_]?ede((RFC3211)?Wrap)?", "(?i)PBEWithSHA1AndDESede", "Triple[-_]?DES", "3[-_]?DES",
"DES[-_]?3"
Comment on lines +223 to +227
"It uses a 64-bit block size, making it vulnerable to birthday attacks, and has been disallowed by NIST. Consider using AES instead."
or
name = ["SKIPJACK", "Skipjack"] and
reason =
"It has a short key length of 80 bits, making it vulnerable to brute-force attacks, and has been disallowed by NIST. Consider using AES instead."

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants