Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c28080d
version upgrades, java version to 25
hhund May 20, 2026
c8a57e3
dependency and plugin upgrades, java to 25, version to 6.0.0-SNAPSHOT
hhund May 20, 2026
3c968e0
javadoc typo fix
hhund May 20, 2026
0f157ef
RFC 9180 Hybrid Public Key Encryption implementation for mode 0 and 1
hhund May 20, 2026
9492b7e
added missing export for hpke package
hhund May 21, 2026
1c01b21
Renamed provider interfaces, new / fixed convenience methods, new tests
hhund May 21, 2026
6718c47
fixed implementation, added javadoc and tests
hhund May 21, 2026
fff1702
refactored / simplified code, added test
hhund May 21, 2026
71c14b6
remove not needed code
hhund May 21, 2026
f52016c
fixed iv generation error, refactored and improved code, new tests
hhund May 22, 2026
2f5a047
more improvements, refactorings, missing throws, renamed test
hhund May 22, 2026
0ed6504
cleanup
hhund May 23, 2026
ef1acb1
added test based on RFC 9180 test vectors
hhund May 23, 2026
34d2182
code cleanup
hhund May 23, 2026
f4e5590
new RsaKemWrapper test, some code cleanup
hhund May 24, 2026
49953c1
error handling mods, new tests for DhKemWrapper and AbstractKemWrapper
hhund May 24, 2026
0ecab9b
refactorings, redesigns and additional tests
hhund May 24, 2026
4fa1bff
removed no longer needed parameters
hhund May 24, 2026
ca25886
added missing Override annotations
hhund May 24, 2026
7dd5a0d
added chunk length exponent to key-schedule info
hhund May 24, 2026
8c686b5
removed not needed test method parameters
hhund May 24, 2026
31138dc
some refactoring, improved exception handling, new tests
hhund May 25, 2026
2aadb55
renamed method, test to verify two kem calls result in different keys
hhund May 26, 2026
a515ed1
new workflow and dependabot config
hhund May 26, 2026
2b1adc4
renamed build step
hhund May 26, 2026
21bfd5c
read vs readNBytes fix
hhund May 26, 2026
0b9d63a
improved javadoc, small fixes and refactored code
hhund May 26, 2026
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
13 changes: 6 additions & 7 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v4
uses: actions/checkout@v6

- name: Set up JDK 21
uses: actions/setup-java@v4
- name: Set up JDK 25
uses: actions/setup-java@v5
with:
distribution: 'zulu'
java-version: 21
java-version: 25
cache: 'maven'

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
queries: security-extended, security-and-quality
Expand All @@ -42,6 +41,6 @@ jobs:
run: mvn -B --file pom.xml -Dmaven.javadoc.skip=true -Dgpg.skip -Denforcer.skip -DskipTests clean package

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{matrix.language}}"
8 changes: 4 additions & 4 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
- uses: actions/checkout@v6
- name: Set up JDK 25
uses: actions/setup-java@v5
with:
distribution: 'zulu'
java-version: 21
java-version: 25
cache: 'maven'
- name: Build with Maven
run: mvn -B verify -Dgpg.skip --file pom.xml
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# crypto-utils
Utility library for reading and writing X509 certificates as well as reading, writing and generating RSA and EC keys. Also provides a simple certificate authority implementation and RSA-KEM AES GCM based on BouncyCastle as well as a ECDH-KEM AES GCM implementation based on JEP 452.
Utility library for reading and writing X509 certificates as well as reading, writing and generating RSA and EC keys. Also provides a simple certificate authority and a RFC 9180 Hybrid Public Key Encryption implementation.


[![Java CI with Maven](https://github.com/hhund/crypto-utils/workflows/Java%20CI%20with%20Maven/badge.svg)](https://github.com/hhund/crypto-utils/actions?query=workflow%3A"Java+CI+with+Maven")
[![Maven Central](https://maven-badges.sml.io/sonatype-central/de.hs-heilbronn.mi/crypto-utils/badge.svg)](https://maven-badges.sml.io/sonatype-central/de.hs-heilbronn.mi/crypto-utils)
Expand All @@ -8,6 +9,6 @@ Utility library for reading and writing X509 certificates as well as reading, wr
<dependency>
<groupId>de.hs-heilbronn.mi</groupId>
<artifactId>crypto-utils</artifactId>
<version>5.2.1</version>
<version>6.0.0</version>
</dependency>
```
46 changes: 23 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

<groupId>de.hs-heilbronn.mi</groupId>
<artifactId>crypto-utils</artifactId>
<version>5.2.1</version>
<version>6.0.0-SNAPSHOT</version>

<name>crypto-utils</name>
<description>Utility library for reading and writing X509 certificates as well as reading, writing and generating RSA and EC keys. Also provides a simple certificate authority implementation and RSA-KEM AES GCM based on BouncyCastle as well as a ECDH-KEM AES GCM implementation based on JEP 452.</description>
<description>Utility library for reading and writing X509 certificates as well as reading, writing and generating RSA and EC keys. Also provides a simple certificate authority and a RFC 9180 Hybrid Public Key Encryption implementation.</description>
<url>https://github.com/hhund/crypto-utils</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.release>21</maven.compiler.release>
<maven.compiler.source>25</maven.compiler.source>
<maven.compiler.target>25</maven.compiler.target>
<maven.compiler.release>25</maven.compiler.release>

<slf4j.version>2.0.17</slf4j.version>
<bouncycastle.version>1.83</bouncycastle.version>
<junit.version>5.14.1</junit.version>
<slf4j.version>2.0.18</slf4j.version>
<bouncycastle.version>1.84</bouncycastle.version>
<junit.version>5.14.4</junit.version>
</properties>

<licenses>
Expand Down Expand Up @@ -78,21 +78,21 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.25.2</version>
<version>2.26.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand All @@ -101,7 +101,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<version>3.15.0</version>
<configuration>
<compilerArgs>
<arg>-proc:none</arg>
Expand All @@ -111,7 +111,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<version>3.4.0</version>
<executions>
<execution>
<id>attach-sources</id>
Expand Down Expand Up @@ -151,12 +151,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
<version>3.5.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.6.2</version>
<version>3.6.3</version>
<executions>
<execution>
<id>enforce-maven</id>
Expand All @@ -182,7 +182,7 @@
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.9.0</version>
<version>0.10.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package de.hsheilbronn.mi.utils.crypto.hpke;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Objects;

import javax.crypto.DecapsulateException;
import javax.crypto.KEM.Encapsulated;
import javax.crypto.SecretKey;

public abstract class AbstractKemWrapper implements KemWrapper
{
private final KemId kemId;

public AbstractKemWrapper(KemId kemId)
{
this.kemId = Objects.requireNonNull(kemId, "kemId");
}

@Override
public final Encapsulated getEncapsulated(PublicKey publicKey, SecureRandom secureRandom)
throws NoSuchAlgorithmException, InvalidKeyException
{
if (!kemId.isKeySupported(publicKey))
throw new IllegalArgumentException("publicKey not supported");

Encapsulated encapsulated = doGetEncapsulated(publicKey, secureRandom, kemId.getSharedSecretLength());

if (encapsulated.encapsulation().length != kemId.getEncapsulationLength())
throw new IllegalStateException("encapsulation.length not " + kemId.getEncapsulationLength());

if (encapsulated.key().getEncoded().length != kemId.getSharedSecretLength())
throw new IllegalStateException("sharedSecret.length not " + kemId.getSharedSecretLength());

return encapsulated;
}

protected abstract Encapsulated doGetEncapsulated(PublicKey publicKey, SecureRandom secureRandom,
int sharedSecretLength) throws NoSuchAlgorithmException, InvalidKeyException;

@Override
public final SecretKey getSharedSecret(PrivateKey privateKey, byte[] encapsulation)
throws NoSuchAlgorithmException, InvalidKeyException, DecapsulateException
{
if (!kemId.isKeySupported(privateKey))
throw new IllegalArgumentException("privateKey not supported");

if (encapsulation.length != kemId.getEncapsulationLength())
throw new IllegalArgumentException("encapsulation.length not " + kemId.getEncapsulationLength());

SecretKey sharedSecret = doGetSecretKey(privateKey, encapsulation, kemId.getSharedSecretLength());

if (sharedSecret.getEncoded().length != kemId.getSharedSecretLength())
throw new IllegalStateException("sharedSecret.length not " + kemId.getSharedSecretLength());

return sharedSecret;
}

protected abstract SecretKey doGetSecretKey(PrivateKey privateKey, byte[] encapsulation, int sharedSecretLength)
throws NoSuchAlgorithmException, InvalidKeyException, DecapsulateException;
}
138 changes: 138 additions & 0 deletions src/main/java/de/hsheilbronn/mi/utils/crypto/hpke/AeadId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package de.hsheilbronn.mi.utils.crypto.hpke;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Objects;
import java.util.function.BiFunction;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;

public enum AeadId
{
AES_128_GCM(0x0001, 16, 12, "AES", "AES/GCM/NoPadding", 128, GCMParameterSpec::new),

AES_256_GCM(0x0002, 32, 12, "AES", "AES/GCM/NoPadding", 128, GCMParameterSpec::new),

ChaCha20Poly1305(0x0003, 32, 12, "ChaCha20", "ChaCha20-Poly1305", 128, (_, iV) -> new IvParameterSpec(iV));

private final int id;
private final int keyLength;
private final int ivLength;
private final String keyAlgorithm;
private final String cipherAlgorithm;
private final int authenticationTagLengthBits;
private final BiFunction<Integer, byte[], AlgorithmParameterSpec> cipherAlgorithmParameterSpecFactory;

AeadId(int id, int keyLenght, int ivLength, String keyAlgorithm, String cipherAlgorithm,
int authenticationTagLengthBits,
BiFunction<Integer, byte[], AlgorithmParameterSpec> cipherAlgorithmParameterSpecFactory)
{
this.id = id;
this.keyLength = keyLenght;
this.ivLength = ivLength;
this.keyAlgorithm = keyAlgorithm;
this.cipherAlgorithm = cipherAlgorithm;
this.authenticationTagLengthBits = authenticationTagLengthBits;
this.cipherAlgorithmParameterSpecFactory = cipherAlgorithmParameterSpecFactory;
}

public int getId()
{
return id;
}

public byte[] getIdAsI2osp2Bytes()
{
return ByteEncoding.i2osp2(id);
}

public int getKeyLength()
{
return keyLength;
}

public byte[] getKeyLengthAsI2osp2Bytes()
{
return ByteEncoding.i2osp2(keyLength);
}

public int getIvLength()
{
return ivLength;
}

public byte[] getIvLengthAsI2osp2Bytes()
{
return ByteEncoding.i2osp2(ivLength);
}

public int getAuthenticationTagLengthBits()
{
return authenticationTagLengthBits;
}

public String getKeyAlgorithm()
{
return keyAlgorithm;
}

public void initEncryptionCipher(Cipher cipher, SecretKey key, byte[] iv)
throws NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
{
initCipher(cipher, key, iv, Cipher.ENCRYPT_MODE);
}

public void initDecryptionCipher(Cipher cipher, SecretKey key, byte[] iv)
throws NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
{
initCipher(cipher, key, iv, Cipher.DECRYPT_MODE);
}

private void initCipher(Cipher cipher, SecretKey key, byte[] iv, int mode)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
Objects.requireNonNull(key, "key");
Objects.requireNonNull(iv, "iv");
if (ivLength != iv.length)
throw new IllegalArgumentException("iv.length not " + ivLength);

AlgorithmParameterSpec spec = cipherAlgorithmParameterSpecFactory.apply(authenticationTagLengthBits, iv);
cipher.init(mode, key, spec);
}

public Cipher toCipher()
{
try
{
return Cipher.getInstance(cipherAlgorithm);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
}
catch (NoSuchAlgorithmException | NoSuchPaddingException e)
{
throw new RuntimeException(e);
}
}

public static AeadId from(byte[] value) throws IllegalArgumentException
{
Objects.requireNonNull(value, "value");
if (value.length != 2)
throw new IllegalArgumentException("value.length not 2");

long aeadId = ByteEncoding.os2ip(value);

if (AES_128_GCM.id == aeadId)
return AES_128_GCM;
else if (AES_256_GCM.id == aeadId)
return AES_256_GCM;
else if (ChaCha20Poly1305.id == aeadId)
return ChaCha20Poly1305;
else
throw new IllegalArgumentException("AeadId not supported");
}
}
Loading