Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,7 @@

import com.ibm.engine.language.csharp.tree.CSharpTree;
import com.ibm.engine.rule.IDetectionRule;
import com.ibm.plugin.rules.detection.dotnet.DotNetAES;
import com.ibm.plugin.rules.detection.dotnet.DotNetDES;
import com.ibm.plugin.rules.detection.dotnet.DotNetDSA;
import com.ibm.plugin.rules.detection.dotnet.DotNetECDiffieHellman;
import com.ibm.plugin.rules.detection.dotnet.DotNetECDsa;
import com.ibm.plugin.rules.detection.dotnet.DotNetHMAC;
import com.ibm.plugin.rules.detection.dotnet.DotNetRC2;
import com.ibm.plugin.rules.detection.dotnet.DotNetRSA;
import com.ibm.plugin.rules.detection.dotnet.DotNetRfc2898DeriveBytes;
import com.ibm.plugin.rules.detection.dotnet.DotNetSHA;
import com.ibm.plugin.rules.detection.dotnet.DotNetTripleDES;
import com.ibm.plugin.rules.detection.dotnet.*;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
Expand All @@ -47,6 +37,7 @@ private CSharpDetectionRules() {
public static List<IDetectionRule<CSharpTree>> rules() {
return Stream.of(
DotNetAES.rules().stream(),
DotNetChaCha20Poly1305.rules().stream(),
DotNetDES.rules().stream(),
DotNetTripleDES.rules().stream(),
DotNetRC2.rules().stream(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.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
}

private static final IDetectionRule<CSharpTree> CHACHA20POLY1305 =
new DetectionRuleBuilder<CSharpTree>()
.createDetectionRule()
.forObjectTypes("ChaCha20Poly1305")
.forMethods("<init>")
.shouldBeDetectedAs(new ValueActionFactory<>("CHACHA20POLY1305"))
.withMethodParameter("[]byte")
Comment thread
fynnth marked this conversation as resolved.
Outdated
.buildForContext(new CipherContext())
.inBundle(() -> "DotNet")
.withDependingDetectionRules(List.of());
Comment thread
fynnth marked this conversation as resolved.
Outdated

@Nonnull
public static List<IDetectionRule<CSharpTree>> rules() {
return List.of(CHACHA20POLY1305);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@
import com.ibm.mapper.mapper.jca.JcaPaddingMapper;
import com.ibm.mapper.model.INode;
import com.ibm.mapper.model.KeyLength;
import com.ibm.mapper.model.algorithms.AES;
import com.ibm.mapper.model.algorithms.DES;
import com.ibm.mapper.model.algorithms.DESede;
import com.ibm.mapper.model.algorithms.RC2;
import com.ibm.mapper.model.algorithms.RSA;
import com.ibm.mapper.model.algorithms.*;
import com.ibm.mapper.utils.DetectionLocation;
import java.util.Optional;
import javax.annotation.Nonnull;
Expand All @@ -63,6 +59,8 @@ public final class CSharpCipherContextTranslator
Optional<INode> result =
switch (valueStr) {
case "AES" -> Optional.of(new AES(detectionLocation));
case "CHACHA20POLY1305" ->
Comment thread
fynnth marked this conversation as resolved.
Optional.of(new ChaCha20Poly1305(detectionLocation));
case "DES" -> Optional.of(new DES(detectionLocation));
case "3DES", "DESEDE", "TRIPLEDES" ->
Optional.of(new DESede(detectionLocation));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Security.Cryptography;

public class DotNetChaCha20Poly1305Test
{
public void TestChaChaCreate()
{
var key = new byte [32];;
Comment thread
fynnth marked this conversation as resolved.
Outdated
var chaCha = new ChaCha20Poly1305(key); // Noncompliant
}

public void TestChaChaEncrypt()
{
var key = new byte [32];
var chaCha = new ChaCha20Poly1305(key); // Noncompliant

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 TestChaChaDecrypt()
{
var key = new byte [32];
var chaCha = new ChaCha20Poly1305(key); // Noncompliant

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.Decrypt(nonce, plainText, cipherText, tag, associatedData);
}

public void TestChaChaEncryptReadOnlySpan()
{
var key = new byte [32];
var chaCha = new ChaCha20Poly1305(key); // Noncompliant

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 TestChaChaDecryptReadOnlySpan()
{
var key = new byte [32];
var chaCha = new ChaCha20Poly1305(key); // Noncompliant

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.Decrypt(nonce, cipherText, tag, plainText, associatedData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 static org.assertj.core.api.Assertions.assertThat;

import com.ibm.engine.detection.DetectionStore;
import com.ibm.engine.language.csharp.CSharpCheck;
import com.ibm.engine.language.csharp.CSharpScanContext;
import com.ibm.engine.language.csharp.CSharpSymbol;
import com.ibm.engine.language.csharp.tree.CSharpTree;
import com.ibm.engine.model.IValue;
import com.ibm.engine.model.ValueAction;
import com.ibm.engine.model.context.CipherContext;
import com.ibm.mapper.model.AuthenticatedEncryption;
import com.ibm.mapper.model.INode;
import com.ibm.plugin.CSharpVerifier;
import com.ibm.plugin.TestBase;
import java.util.List;
import javax.annotation.Nonnull;
import org.junit.jupiter.api.Test;

class DotNetChaCha20Poly1305Test extends TestBase {
@Test
void test() throws Exception {
CSharpVerifier.verify("rules/detection/dotnet/DotNetChaCha20Poly1305TestFile.cs", this);
}

@Override
public void asserts(
Comment thread
fynnth marked this conversation as resolved.
int findingId,
@Nonnull
DetectionStore<CSharpCheck, CSharpTree, CSharpSymbol, CSharpScanContext>
detectionStore,
@Nonnull List<INode> nodes) {
assertThat(detectionStore.getDetectionValues()).hasSize(1);
assertThat(detectionStore.getDetectionValueContext()).isInstanceOf(CipherContext.class);
IValue<CSharpTree> value0 = detectionStore.getDetectionValues().get(0);
assertThat(value0).isInstanceOf(ValueAction.class);
assertThat(value0.asString()).isEqualTo("CHACHA20POLY1305");

assertThat(nodes).hasSize(1);
INode node = nodes.get(0);
assertThat(node.getKind()).isEqualTo(AuthenticatedEncryption.class);
assertThat(node.asString()).isEqualTo("ChaCha20-Poly1305");
}
}
Loading