Skip to content

Unified: Port ECBEncryption query - #23234

Draft
geoffw0 wants to merge 12 commits into
github:mainfrom
geoffw0:unifiedecb
Draft

geoffw0 wants to merge 12 commits into
github:mainfrom
geoffw0:unifiedecb

Conversation

@geoffw0

@geoffw0 geoffw0 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Port the ECB encryption query from Swift to Unified Swift, as unified/swift/ecb-encryption. Used Copilot with GPT-6.1 Sol, high effort, some manual and agent authored fixes afterwards.

A few thoughts:

  • the query itself is pretty straightforward and works as well as the original Swift version (neither find the commoncrypto test cases).
  • the test includes cases for a weak cryptographic algorithm query, which it appears we never got around to implementing for original Swift. I've preserved those test cases here, as they're a natural fit in this test code and I would like to port that query to unified Swift in future (and exercise them).
  • the agent suggested significant changes to the .qhelp. I often revert this kind of thing, but in this case it appears the original .qhelp was outdated and in places not very accurate. I iterated on the changes a bit instead. Both the description and suggestions are better now in my opinion. Obviously a docs review will be required.
  • DCA run to follow.

geoffw0 and others added 12 commits November 24, 2026 10:35
Establish a byte-for-byte baseline for the unified Swift port, including query help, its example, and all Swift test sources. Adaptations follow separately.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Inline the data flow configuration and query-specific models unchanged from ECBEncryptionQuery.qll and ECBEncryptionExtensions.qll. Mark their provenance before adapting to unified APIs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Model CryptoSwift ECB construction and reuse existing AES and Blowfish sinks with value-preserving global flow. All nine original CryptoSwift cases are detected; record four pre-existing CommonCrypto misses as inline expectations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Describe ECB's plaintext-pattern leakage rather than replay attacks. Recommend authenticated encryption and clarify that the CBC example needs ciphertext authentication and does not by itself prevent replay.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…sue with original Swift. It seems fine with unified Swift.
Generate a separate random IV for each AES cipher construction rather than sharing a CBC block mode between the two examples.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note Unified labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp

Encryption using ECB

ECB should not be used as a mode for encryption. With a fixed key, identical plaintext blocks always produce identical ciphertext blocks. This can expose sensitive information by revealing patterns in the plaintext.

Recommendation

Use an authenticated encryption mode such as GCM where available. If CBC is used, generate a fresh, unpredictable initialization vector for each encryption and use a separate message authentication mechanism to protect ciphertext integrity.

Example

The following example shows six cases of instantiating a cipher with different block modes. In the 'BAD' cases, the encryption mode is ECB, which reveals patterns in the plaintext. In the 'GOOD' cases, the encryption mode is CBC with a randomly generated initialization vector, avoiding this issue. Authentication of the ciphertext is not shown.

func encrypt(key : Key, padding : Padding) {
	// ...

	// BAD: ECB is used for block mode
	_ = try AES(key: key, blockMode: ECB(), padding: padding)
	_ = try AES(key: key, blockMode: ECB())
	_ = try Blowfish(key: key, blockMode: ECB(), padding: padding)

	// GOOD: ECB is not used for block mode
	_ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize)), padding: padding)
	_ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize)))
	_ = try Blowfish(key: key, blockMode: CBC(iv: Blowfish.randomIV(Blowfish.blockSize), padding: padding)

	// ...
}

References

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

Labels

documentation no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant