Skip to content

Commit 23fae34

Browse files
Merge pull request #22 from fisherstevenk/remove-sun
remove sun.security requirement
2 parents 42c18b1 + 8e3d67f commit 23fae34

File tree

15 files changed

+2793
-70
lines changed

15 files changed

+2793
-70
lines changed

README.md

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,13 @@
77

88
**KYBER** is an IND-CCA2-secure key encapsulation mechanism (KEM), whose security is based on the hardness of solving the learning-with-errors (LWE) problem over module lattices. The homepage for CRYSTALS Kyber can be found [here](https://pq-crystals.org/kyber/index.shtml) (some information from this README is pulled directly from their site).
99

10-
The initial Java implementation was intended for Android applications. In order to use it on Android however, you need to include the sun.security.util classes in your final jar. The Android version of java does not have them available.
11-
12-
Some minor changes were needed for this library to work with JDK 18 (version 2.0+). In order to use the library in your Java 18 app, you do need modifications to your maven pom (sorry.. no gradle example).
13-
14-
*Please note, do not add the "..." to your pom file. That's just a placeholder instead of adding a full pom file.*
15-
16-
```bash
17-
<project ...>
18-
....
19-
<properties>
20-
<!-- This property must be added -->
21-
<argLine>--add-modules java.base --add-opens java.base/sun.security.util=ALL-UNNAMED</argLine>
22-
...
23-
</properties>
24-
<build>
25-
<plugins>
26-
<plugin>
27-
<groupId>org.apache.maven.plugins</groupId>
28-
<artifactId>maven-compiler-plugin</artifactId>
29-
<version>3.10.1</version>
30-
<configuration>
31-
<source>18</source>
32-
<target>18</target>
33-
<!-- This section must be added -->
34-
<compilerArgs>
35-
<arg>--add-exports</arg>
36-
<arg>java.base/sun.security.util=ALL-UNNAMED</arg>
37-
</compilerArgs>
38-
</configuration>
39-
</plugin>
40-
...
41-
</project>
42-
```
43-
4410
The initial creation of this code was translated from this Go implementation of [Kyber (version 3)](https://github.com/symbolicsoft/kyber-k2so). After getting that to work, the code was modified into a JCE. The Diffie-Hellman OpenJDK 11 code was used as a base.
4511

4612
Kyber has three different parameter sets: 512, 768, and 1024. Kyber-512 aims at security roughly equivalent to AES-128, Kyber-768 aims at security roughly equivalent to AES-192, and Kyber-1024 aims at security roughly equivalent to AES-256.
4713

14+
## Sun Libraries
15+
The "sun.security.\*" library requirements have been removed from version 1.1 of this library. The required "sun.security.\*" classes were copied from Java 13 and refactored into "com.swiftcryptollc.crypto.util" under the GNU General Public License version 2. Part of the refactoring was to remove unused methods and variables, and to change to new base classes where possible.
16+
4817
## Loading the Kyber JCE
4918
There are a couple ways to load the Kyber JCE. One way is to add these two lines to your program:
5019

pom.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.swiftcryptollc</groupId>
55
<artifactId>kyberJCE</artifactId>
6-
<version>2.1.5</version>
6+
<version>3.0.0</version>
77
<packaging>jar</packaging>
88
<name>KyberJCE</name>
99
<description>Pure Java implementation of Kyber</description>
1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12-
<argLine>--add-modules java.base --add-opens java.base/sun.security.util=ALL-UNNAMED</argLine>
1312
</properties>
1413
<repositories>
1514
<repository>
@@ -28,7 +27,7 @@
2827
<dependency>
2928
<groupId>org.junit.jupiter</groupId>
3029
<artifactId>junit-jupiter</artifactId>
31-
<version>5.8.2</version>
30+
<version>5.10.0</version>
3231
<scope>test</scope>
3332
</dependency>
3433
</dependencies>
@@ -37,15 +36,15 @@
3736
<plugin>
3837
<groupId>org.apache.maven.plugins</groupId>
3938
<artifactId>maven-javadoc-plugin</artifactId>
40-
<version>3.3.1</version>
39+
<version>3.5.0</version>
4140
<configuration>
4241
<additionalparam>-Xdoclint:none</additionalparam>
4342
</configuration>
4443
</plugin>
4544
<plugin>
4645
<groupId>org.apache.maven.plugins</groupId>
4746
<artifactId>maven-compiler-plugin</artifactId>
48-
<version>3.10.1</version>
47+
<version>3.11.0</version>
4948
<configuration>
5049
<source>17</source>
5150
<target>17</target>

src/main/java/com/swiftcryptollc/crypto/provider/KyberCipherText.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import com.swiftcryptollc.crypto.provider.kyber.KyberParams;
44
import com.swiftcryptollc.crypto.spec.KyberParameterSpec;
5+
import com.swiftcryptollc.crypto.util.DerInputStream;
6+
import com.swiftcryptollc.crypto.util.DerOutputStream;
7+
import com.swiftcryptollc.crypto.util.DerValue;
58
import com.swiftcryptollc.crypto.util.KyberKeyUtil;
9+
import com.swiftcryptollc.crypto.util.ObjectIdentifier;
610
import java.io.ByteArrayInputStream;
711
import java.io.IOException;
812
import java.io.InputStream;
@@ -13,10 +17,7 @@
1317
import java.security.ProviderException;
1418
import java.security.PublicKey;
1519
import java.util.Objects;
16-
import sun.security.util.DerInputStream;
17-
import sun.security.util.DerOutputStream;
18-
import sun.security.util.DerValue;
19-
import sun.security.util.ObjectIdentifier;
20+
2021

2122
/**
2223
* A cipher text in X.509 format for the Kyber key agreement algorithm.

src/main/java/com/swiftcryptollc/crypto/provider/KyberJCE.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.security.AccessController;
44
import java.security.Provider;
55
import java.security.SecureRandom;
6-
import static sun.security.util.SecurityConstants.PROVIDER_VER;
76

87
/**
98
* Java implementation of the CRYSTALS Kyber Algorithm.
@@ -45,7 +44,7 @@ static SecureRandom getRandom() {
4544
}
4645

4746
public KyberJCE() {
48-
super("KyberJCE", PROVIDER_VER, info);
47+
super("KyberJCE", System.getProperty("java.specification.version"), info);
4948

5049
AccessController.doPrivileged(new java.security.PrivilegedAction<Object>() {
5150
@Override

src/main/java/com/swiftcryptollc/crypto/provider/KyberPrivateKey.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.swiftcryptollc.crypto.provider.kyber.KyberParams;
44
import com.swiftcryptollc.crypto.spec.KyberParameterSpec;
5-
import com.swiftcryptollc.crypto.util.KyberKeyUtil;
65
import java.io.ByteArrayInputStream;
76
import java.io.IOException;
87
import java.io.InputStream;
@@ -13,10 +12,11 @@
1312
import java.security.PrivateKey;
1413
import java.security.ProviderException;
1514
import java.util.Objects;
16-
import sun.security.util.DerInputStream;
17-
import sun.security.util.DerOutputStream;
18-
import sun.security.util.DerValue;
19-
import sun.security.util.ObjectIdentifier;
15+
import com.swiftcryptollc.crypto.util.DerInputStream;
16+
import com.swiftcryptollc.crypto.util.DerOutputStream;
17+
import com.swiftcryptollc.crypto.util.DerValue;
18+
import com.swiftcryptollc.crypto.util.KyberKeyUtil;
19+
import com.swiftcryptollc.crypto.util.ObjectIdentifier;
2020

2121
/**
2222
* A private key in PKCS#8 format for the Diffie-Hellman key agreement

src/main/java/com/swiftcryptollc/crypto/provider/KyberPublicKey.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.swiftcryptollc.crypto.provider.kyber.KyberParams;
44
import com.swiftcryptollc.crypto.spec.KyberParameterSpec;
5-
import com.swiftcryptollc.crypto.util.KyberKeyUtil;
65
import java.io.ByteArrayInputStream;
76
import java.io.IOException;
87
import java.io.InputStream;
@@ -13,10 +12,11 @@
1312
import java.security.ProviderException;
1413
import java.security.PublicKey;
1514
import java.util.Objects;
16-
import sun.security.util.DerInputStream;
17-
import sun.security.util.DerOutputStream;
18-
import sun.security.util.DerValue;
19-
import sun.security.util.ObjectIdentifier;
15+
import com.swiftcryptollc.crypto.util.DerInputStream;
16+
import com.swiftcryptollc.crypto.util.DerOutputStream;
17+
import com.swiftcryptollc.crypto.util.DerValue;
18+
import com.swiftcryptollc.crypto.util.KyberKeyUtil;
19+
import com.swiftcryptollc.crypto.util.ObjectIdentifier;
2020

2121
/**
2222
* A public key in X.509 format for the Kyber key agreement algorithm.

src/main/java/com/swiftcryptollc/crypto/provider/KyberSecretKey.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.swiftcryptollc.crypto.provider.kyber.KyberParams;
44
import com.swiftcryptollc.crypto.spec.KyberParameterSpec;
5-
import com.swiftcryptollc.crypto.util.KyberKeyUtil;
65
import java.io.ByteArrayInputStream;
76
import java.io.IOException;
87
import java.io.InputStream;
@@ -13,10 +12,11 @@
1312
import java.security.ProviderException;
1413
import java.security.PublicKey;
1514
import java.util.Objects;
16-
import sun.security.util.DerInputStream;
17-
import sun.security.util.DerOutputStream;
18-
import sun.security.util.DerValue;
19-
import sun.security.util.ObjectIdentifier;
15+
import com.swiftcryptollc.crypto.util.DerInputStream;
16+
import com.swiftcryptollc.crypto.util.DerOutputStream;
17+
import com.swiftcryptollc.crypto.util.DerValue;
18+
import com.swiftcryptollc.crypto.util.KyberKeyUtil;
19+
import com.swiftcryptollc.crypto.util.ObjectIdentifier;
2020

2121
/**
2222
* A public key in X.509 format for the Kyber key agreement algorithm.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.swiftcryptollc.crypto.util;
27+
28+
import java.io.IOException;
29+
import java.io.OutputStream;
30+
31+
/**
32+
* Interface to an object that knows how to write its own DER
33+
* encoding to an output stream.
34+
*
35+
* @author D. N. Hoover
36+
*/
37+
public interface DerEncoder {
38+
39+
/**
40+
* DER encode this object and write the results to a stream.
41+
*
42+
* @param out the stream on which the DER encoding is written.
43+
*/
44+
public void derEncode(OutputStream out)
45+
throws IOException;
46+
47+
}

0 commit comments

Comments
 (0)