-
Notifications
You must be signed in to change notification settings - Fork 58
feat(csharp): add C# ChaCha20Poly1305 detection rules #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
221625b
added chacha20poly1305 detection rules
Kaldesyvon 0d2d19b
added chacha20poly1305 detection rules
Kaldesyvon 9255404
resolving comments in detection rules
Kaldesyvon c32190c
fixing detection rules and adding tests
Kaldesyvon c42353b
minor changes in .cs test file
Kaldesyvon 304f6c7
Merge branch 'cbomkit:main' into chacha20poly1305
Kaldesyvon 8172212
replacing encrypt/decrypt values with CipherAction
Kaldesyvon fd07132
removing unused imports
Kaldesyvon b3fa5fe
adding more test cases
Kaldesyvon 81b705b
Merge remote-tracking branch 'origin/chacha20poly1305' into chacha20p…
Kaldesyvon 70ab74c
added cipheraction and improved tests to that
Kaldesyvon d04c671
Merge branch 'main' into chacha20poly1305
fynnth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
csharp/src/main/java/com/ibm/plugin/rules/detection/dotnet/DotNetChaCha20Poly1305.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Sonar Cryptography Plugin | ||
| * Copyright (C) 2026 PQCA | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.ibm.plugin.rules.detection.dotnet; | ||
|
|
||
| import com.ibm.engine.detection.MethodMatcher; | ||
| import com.ibm.engine.language.csharp.tree.CSharpTree; | ||
| import com.ibm.engine.model.CipherAction; | ||
| import com.ibm.engine.model.context.CipherContext; | ||
| import com.ibm.engine.model.factory.CipherActionFactory; | ||
| import com.ibm.engine.model.factory.ValueActionFactory; | ||
| import com.ibm.engine.rule.IDetectionRule; | ||
| import com.ibm.engine.rule.builder.DetectionRuleBuilder; | ||
| import java.util.List; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| public final class DotNetChaCha20Poly1305 { | ||
| private DotNetChaCha20Poly1305() { | ||
| // nothing | ||
| } | ||
|
|
||
| // chaCha20Poly1305.Encrypt(nonce, plaintext, ciphertext, tag [, associatedData]) | ||
| private static final IDetectionRule<CSharpTree> CHACHA20POLY1305_ENCRYPT_OP = | ||
| new DetectionRuleBuilder<CSharpTree>() | ||
| .createDetectionRule() | ||
| .forObjectTypes(MethodMatcher.ANY) | ||
| .forMethods("Encrypt") | ||
| .shouldBeDetectedAs(new CipherActionFactory<>(CipherAction.Action.ENCRYPT)) | ||
| .withAnyParameters() // Byte[] or ReadOnlySpan<Byte> overloads | ||
| .buildForContext(new CipherContext()) | ||
| .inBundle(() -> "DotNet") | ||
| .withoutDependingDetectionRules(); | ||
|
|
||
| // chaCha20Poly1305.Decrypt(nonce, ciphertext, tag, plaintext [, associatedData]) | ||
| private static final IDetectionRule<CSharpTree> CHACHA20POLY1305_DECRYPT_OP = | ||
| new DetectionRuleBuilder<CSharpTree>() | ||
| .createDetectionRule() | ||
| .forObjectTypes(MethodMatcher.ANY) | ||
| .forMethods("Decrypt") | ||
| .shouldBeDetectedAs(new CipherActionFactory<>(CipherAction.Action.DECRYPT)) | ||
| .withAnyParameters() // Byte[] or ReadOnlySpan<Byte> overloads | ||
| .buildForContext(new CipherContext()) | ||
| .inBundle(() -> "DotNet") | ||
| .withoutDependingDetectionRules(); | ||
|
|
||
| private static final List<IDetectionRule<CSharpTree>> CHACHA20POLY1305_OP_RULES = | ||
| List.of(CHACHA20POLY1305_ENCRYPT_OP, CHACHA20POLY1305_DECRYPT_OP); | ||
|
|
||
| // new ChaCha20Poly1305(key) | ||
| private static final IDetectionRule<CSharpTree> CHACHA20POLY1305 = | ||
| new DetectionRuleBuilder<CSharpTree>() | ||
| .createDetectionRule() | ||
| .forObjectTypes("ChaCha20Poly1305") | ||
| .forMethods("<init>") | ||
| .shouldBeDetectedAs(new ValueActionFactory<>("CHACHA20POLY1305")) | ||
| .withAnyParameters() // Byte[] or ReadOnlySpan<Byte>, 1 parameter | ||
| .buildForContext(new CipherContext()) | ||
| .inBundle(() -> "DotNet") | ||
| .withDependingDetectionRules(CHACHA20POLY1305_OP_RULES); | ||
|
|
||
| @Nonnull | ||
| public static List<IDetectionRule<CSharpTree>> rules() { | ||
| return List.of(CHACHA20POLY1305); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.