-
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
base: main
Are you sure you want to change the base?
Changes from all commits
221625b
0d2d19b
9255404
c32190c
c42353b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * 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.context.CipherContext; | ||
| 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 ValueActionFactory<>("ENCRYPT")) | ||
| .withAnyParameters() // Byte[] or ReadOnlySpan<Byte> overloads | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This depends on how specific you want to be. withAnyParameters() means what is says - anything is ok. If I read the C# docs correctly this Encrypt requires 5 parameters. Your test now uses 4 which is OK if you have ANY here, but is this correct C#?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct c# code because AAD in this function has a default value that will be taken if only 4 parameters are there. That is also the reason for that specific test case and the test before that covers the 5 parameter option. HOWEVER i have tried to compile the file and it seems that For me this works better: |
||
| .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 ValueActionFactory<>("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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Test file for System.Security.Cryptography.ChaCha20Poly1305 detection rules. | ||
| * | ||
| * Covers the full API surface of the ChaCha20Poly1305 class: | ||
| * - constructor overloads (byte[] key, ReadOnlySpan<byte> key) | ||
| * - Encrypt overloads (byte[] and ReadOnlySpan<byte>), with and without associatedData | ||
| * - Decrypt overloads (byte[] and ReadOnlySpan<byte>), with and without associatedData | ||
| * | ||
| * Architecture note: Encrypt/Decrypt are depending rules attached to the constructor | ||
| * (see DotNetChaCha20Poly1305.java), so each method below that constructs-then-uses | ||
| * the object produces a single finding with nested Encrypt/Decrypt children, not separate | ||
| * disconnected findings. | ||
| */ | ||
|
|
||
| using System.Security.Cryptography; | ||
|
|
||
| public class DotNetChaCha20Poly1305ComprehensiveTest | ||
| { | ||
| // ------------------------------------------------------------------------- | ||
| // Section 1: Constructor overloads | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| public void TestConstructByteArrayKey() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
| } | ||
|
|
||
| public void TestConstructSpanKey() | ||
| { | ||
| var key = new ReadOnlySpan<byte>[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Section 2: Encrypt — byte[] overload | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| public void TestEncryptByteArrayWithAad() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
|
|
||
| var nonce = new byte[12]; | ||
| var plainText = new byte[32]; | ||
| var cipherText = new byte[32]; | ||
| var tag = new byte[16]; | ||
| var associatedData = new byte[32]; | ||
|
|
||
| chaCha.Encrypt(nonce, plainText, cipherText, tag, associatedData); | ||
| } | ||
|
|
||
| public void TestEncryptByteArrayNoAad() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
|
|
||
| var nonce = new byte[12]; | ||
| var plainText = new byte[32]; | ||
| var cipherText = new byte[32]; | ||
| var tag = new byte[16]; | ||
|
|
||
| chaCha.Encrypt(nonce, plainText, cipherText, tag); | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Section 3: Encrypt — ReadOnlySpan<byte> overload | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| public void TestEncryptSpanWithAad() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
|
|
||
| var nonce = new ReadOnlySpan<byte>[12]; | ||
| var plainText = new ReadOnlySpan<byte>[32]; | ||
| var cipherText = new ReadOnlySpan<byte>[32]; | ||
| var tag = new ReadOnlySpan<byte>[16]; | ||
| var associatedData = new ReadOnlySpan<byte>[32]; | ||
|
|
||
| chaCha.Encrypt(nonce, plainText, cipherText, tag, associatedData); | ||
| } | ||
|
|
||
| public void TestEncryptSpanNoAad() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
|
|
||
| var nonce = new ReadOnlySpan<byte>[12]; | ||
| var plainText = new ReadOnlySpan<byte>[32]; | ||
| var cipherText = new ReadOnlySpan<byte>[32]; | ||
| var tag = new ReadOnlySpan<byte>[16]; | ||
|
|
||
| chaCha.Encrypt(nonce, plainText, cipherText, tag); | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Section 4: Decrypt — byte[] overload | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| public void TestDecryptByteArrayWithAad() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
|
|
||
| var nonce = new byte[12]; | ||
| var cipherText = new byte[32]; | ||
| var tag = new byte[16]; | ||
| var plainText = new byte[32]; | ||
| var associatedData = new byte[32]; | ||
|
|
||
| chaCha.Decrypt(nonce, cipherText, tag, plainText, associatedData); | ||
| } | ||
|
|
||
| public void TestDecryptByteArrayNoAad() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
|
|
||
| var nonce = new byte[12]; | ||
| var cipherText = new byte[32]; | ||
| var tag = new byte[16]; | ||
| var plainText = new byte[32]; | ||
|
|
||
| chaCha.Decrypt(nonce, cipherText, tag, plainText); | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Section 5: Decrypt — ReadOnlySpan<byte> overload | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| public void TestDecryptSpanWithAad() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
|
|
||
| var nonce = new ReadOnlySpan<byte>[12]; | ||
| var cipherText = new ReadOnlySpan<byte>[32]; | ||
| var tag = new ReadOnlySpan<byte>[16]; | ||
| var plainText = new ReadOnlySpan<byte>[32]; | ||
| var associatedData = new ReadOnlySpan<byte>[32]; | ||
|
|
||
| chaCha.Decrypt(nonce, cipherText, tag, plainText, associatedData); | ||
| } | ||
|
|
||
| public void TestDecryptSpanNoAad() | ||
| { | ||
| var key = new byte[32]; | ||
| var chaCha = new ChaCha20Poly1305(key); | ||
|
|
||
| var nonce = new ReadOnlySpan<byte>[12]; | ||
| var cipherText = new ReadOnlySpan<byte>[32]; | ||
| var tag = new ReadOnlySpan<byte>[16]; | ||
| var plainText = new ReadOnlySpan<byte>[32]; | ||
|
|
||
| chaCha.Decrypt(nonce, cipherText, tag, plainText); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you detect this as
you would not have to mix this into the handling of the value actions in the translator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats a good change thanks, will also implement that in my local rules :)