Skip to content

Commit 1d383b3

Browse files
add HMAC enricher, update test cases (#234)
Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>
1 parent bf44408 commit 1d383b3

File tree

13 files changed

+533
-281
lines changed

13 files changed

+533
-281
lines changed

enricher/src/main/java/com/ibm/enricher/Enricher.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.ibm.enricher.algorithm.DESEnricher;
2424
import com.ibm.enricher.algorithm.DHEnricher;
2525
import com.ibm.enricher.algorithm.DSAEnricher;
26+
import com.ibm.enricher.algorithm.HMACEnricher;
2627
import com.ibm.enricher.algorithm.KEMEnricher;
2728
import com.ibm.enricher.algorithm.PBKDF2Enricher;
2829
import com.ibm.enricher.algorithm.RSAEnricher;
@@ -85,6 +86,7 @@ private static INode enrichTree(@Nonnull INode node) {
8586
new DSAEnricher(),
8687
new SHA2Enricher(),
8788
new SHA3Enricher(),
89+
new HMACEnricher(),
8890
new PBKDF2Enricher(),
8991
new RSAssaPSSEnricher(),
9092
new RSAoaepEnricher(),
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* SonarQube Cryptography Plugin
3+
* Copyright (C) 2025 IBM
4+
*
5+
* Licensed to the Apache Software Foundation (ASF) under one or more
6+
* contributor license agreements. See the NOTICE file distributed with
7+
* this work for additional information regarding copyright ownership.
8+
* The ASF licenses this file to you under the Apache License, Version 2.0
9+
* (the "License"); you may not use this file except in compliance with
10+
* the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package com.ibm.enricher.algorithm;
21+
22+
import com.ibm.enricher.IEnricher;
23+
import com.ibm.mapper.model.INode;
24+
import com.ibm.mapper.model.MessageDigest;
25+
import com.ibm.mapper.model.Oid;
26+
import com.ibm.mapper.model.algorithms.HMAC;
27+
import com.ibm.mapper.model.algorithms.SHA;
28+
import com.ibm.mapper.model.algorithms.SHA2;
29+
import com.ibm.mapper.model.algorithms.SHA3;
30+
import javax.annotation.Nonnull;
31+
32+
public class HMACEnricher implements IEnricher {
33+
34+
@Nonnull
35+
@Override
36+
public INode enrich(@Nonnull INode node) {
37+
if (node instanceof HMAC hmac) {
38+
hmac.hasChildOfType(MessageDigest.class)
39+
.ifPresent(
40+
digest -> {
41+
if (digest instanceof SHA) {
42+
hmac.put(
43+
new Oid(
44+
"1.2.840.113549.2.7",
45+
hmac.getDetectionContext()));
46+
} else if (digest instanceof SHA2 sha2) {
47+
sha2.getDigestSize()
48+
.ifPresent(
49+
digestSize -> {
50+
switch (digestSize.getValue()) {
51+
case 224 ->
52+
hmac.put(
53+
new Oid(
54+
"1.2.840.113549.2.8",
55+
hmac
56+
.getDetectionContext()));
57+
case 256 ->
58+
hmac.put(
59+
new Oid(
60+
"1.2.840.113549.2.9",
61+
hmac
62+
.getDetectionContext()));
63+
case 384 ->
64+
hmac.put(
65+
new Oid(
66+
"1.2.840.113549.2.10",
67+
hmac
68+
.getDetectionContext()));
69+
case 512 ->
70+
hmac.put(
71+
new Oid(
72+
"1.2.840.113549.2.11",
73+
hmac
74+
.getDetectionContext()));
75+
}
76+
});
77+
} else if (digest instanceof SHA3 sha3) {
78+
sha3.getDigestSize()
79+
.ifPresent(
80+
digestSize -> {
81+
switch (digestSize.getValue()) {
82+
case 224 ->
83+
hmac.put(
84+
new Oid(
85+
"2.16.840.1.101.3.4.2.13",
86+
hmac
87+
.getDetectionContext()));
88+
case 256 ->
89+
hmac.put(
90+
new Oid(
91+
"2.16.840.1.101.3.4.2.14",
92+
hmac
93+
.getDetectionContext()));
94+
case 384 ->
95+
hmac.put(
96+
new Oid(
97+
"2.16.840.1.101.3.4.2.15",
98+
hmac
99+
.getDetectionContext()));
100+
case 512 ->
101+
hmac.put(
102+
new Oid(
103+
"2.16.840.1.101.3.4.2.16",
104+
hmac
105+
.getDetectionContext()));
106+
}
107+
});
108+
}
109+
});
110+
}
111+
return node;
112+
}
113+
}

0 commit comments

Comments
 (0)