-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 03fea02
Showing
51 changed files
with
8,035 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
/target/ | ||
nb-configuration.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Steven K Fisher | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
# CRYSTALS KYBER Java | ||
|
||
<p align="center"> | ||
<img src="./kyber.png"/> | ||
</p> | ||
|
||
**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) | ||
|
||
This pure Java implementation is intended for Android applications (compiles with OpenJDK 11 and OpenJDK 13) but can be used for any Java based application. (Some changes are needed to compile this project using the latest Java versions.) | ||
|
||
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 (unsigned). The Diffie-Hellman OpenJDK 11 code was used as a base. | ||
|
||
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 | ||
|
||
## Loading the Kyber JCE | ||
Because the JAR isn't signed, it will not run with the Oracle JDK. You can run this with OpenJDK. You will need to add these two lines to your program: | ||
|
||
```bash | ||
Security.setProperty("crypto.policy", "unlimited"); | ||
Security.addProvider(new KyberJCE()); | ||
``` | ||
## Example Use | ||
The following code will show a basic Key Agreement between two parties. (Additional AES encryption is recommended for further securing remote communication.) | ||
```bash | ||
// Alice generates a KeyPair and sends her public key to Bob | ||
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("Kyber1024"); | ||
KeyPair aliceKeyPair = keyGen.generateKeyPair(); | ||
``` | ||
```bash | ||
// Bob Generates a KeyPair and an initial Key Agreement | ||
// "Kyber512" or "Kyber768" or "Kyber1024" are options for Key Generation | ||
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("Kyber1024"); | ||
KeyPair bobKeyPair = keyGen.generateKeyPair(); | ||
KeyAgreement keyAgreement = KeyAgreement.getInstance("Kyber"); | ||
keyAgreement.init(bobKeyPair.getPrivate()); | ||
``` | ||
```bash | ||
// Bob generates a Secret Key and Cipher Text from Alice's Public Key | ||
// KyberEncrypted holds the Secret Key and the Cipher Text | ||
KyberEncrypted kyberEncrypted = (KyberEncrypted) keyAgreement.doPhase((KyberPublicKey) alicePublicKey, true); | ||
``` | ||
```bash | ||
// Bob sends Alice the generated Cipher Text | ||
// Alice creates her own KeyAgreement and initializes it with her private key | ||
KeyAgreement keyAgreement = KeyAgreement.getInstance("Kyber"); | ||
keyAgreement.init((KyberPrivateKey) alicePrivateKey); | ||
``` | ||
```bash | ||
// Alice generates the same Secret Key from the Cipher Text | ||
// KyberDecrypted holds the Secret Key (will be the same one that Bob generated) and the variant | ||
KyberDecrypted kyberDecrypted = (KyberDecrypted) keyAgreement.doPhase(cipherText, true); | ||
``` | ||
## DISCLAIMER | ||
This code has *NOT* been reviewed by the Crystals team for any potential issues. With that said, the tests from the [Go](https://github.com/symbolicsoft/kyber-k2so) implementation have also been converted to Java. The original test files are used as the main test source. Additional tests include X.509 encoding and decoding, a key agreement, and a massively multi-threaded key agreement test for good measure. | ||
## Further Information | ||
More details about CRYSTALS and the most secure ways to use it can be found [here](https://pq-crystals.org/index.shtml) | ||
## Contact | ||
[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.swiftcryptollc</groupId> | ||
<artifactId>kyberJCE</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
<name>KyberJCE</name> | ||
<description>Pure Java implementation of Kyber</description> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<repositories> | ||
<repository> | ||
<id>jitpack.io</id> | ||
<url>https://jitpack.io</url> | ||
</repository> | ||
</repositories> | ||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/com.github.aelstad/keccakj --> | ||
<dependency> | ||
<groupId>com.github.aelstad</groupId> | ||
<artifactId>keccakj</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
<!-- TESTING --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-javadoc-plugin</artifactId> | ||
<version>3.3.1</version> | ||
<configuration> | ||
<additionalparam>-Xdoclint:none</additionalparam> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/swiftcryptollc/crypto/interfaces/KyberKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.swiftcryptollc.crypto.interfaces; | ||
|
||
import com.swiftcryptollc.crypto.spec.KyberParameterSpec; | ||
|
||
/** | ||
* | ||
* @author Steven K Fisher <[email protected]> | ||
*/ | ||
public interface KyberKey { | ||
|
||
/** | ||
* Returns the key parameters. | ||
* | ||
* @return the key parameters | ||
*/ | ||
public KyberParameterSpec getParams(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/swiftcryptollc/crypto/interfaces/KyberPrivateKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.swiftcryptollc.crypto.interfaces; | ||
|
||
import com.swiftcryptollc.crypto.provider.KyberKeySize; | ||
|
||
/** | ||
* | ||
* @author Steven K Fisher <[email protected]> | ||
*/ | ||
public interface KyberPrivateKey extends KyberKey, java.security.PrivateKey { | ||
|
||
static final long serialVersionUID = 47572612783495691L; | ||
|
||
/** | ||
* Returns the private value, <code>x</code>. | ||
* | ||
* @return the private value, <code>x</code> | ||
*/ | ||
public byte[] getX(); | ||
|
||
public KyberKeySize getKyberKeySize(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/swiftcryptollc/crypto/interfaces/KyberPublicKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.swiftcryptollc.crypto.interfaces; | ||
|
||
import com.swiftcryptollc.crypto.provider.KyberKeySize; | ||
|
||
/** | ||
* | ||
* @author Steven K Fisher <[email protected]> | ||
*/ | ||
public interface KyberPublicKey extends KyberKey, java.security.PublicKey { | ||
|
||
static final long serialVersionUID = -2187346178259912349L; | ||
|
||
/** | ||
* Returns the public value, <code>y</code>. | ||
* | ||
* @return the public value, <code>y</code> | ||
*/ | ||
public byte[] getY(); | ||
|
||
public KyberKeySize getKyberKeySize(); | ||
} |
103 changes: 103 additions & 0 deletions
103
src/main/java/com/swiftcryptollc/crypto/provider/Kyber1024KeyPairGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.swiftcryptollc.crypto.provider; | ||
|
||
import com.github.aelstad.keccakj.fips202.SHA3_256; | ||
import com.swiftcryptollc.crypto.provider.kyber.Indcpa; | ||
import com.swiftcryptollc.crypto.provider.kyber.KyberParams; | ||
import com.swiftcryptollc.crypto.spec.KyberParameterSpec; | ||
import java.security.*; | ||
import java.security.spec.AlgorithmParameterSpec; | ||
|
||
/** | ||
* | ||
* @author Steven K Fisher <[email protected]> | ||
*/ | ||
public final class Kyber1024KeyPairGenerator extends KeyPairGeneratorSpi { | ||
|
||
private KyberParameterSpec params; | ||
private final KyberKeySize kyberKeySize = KyberKeySize.KEY_1024; | ||
private SecureRandom random; | ||
|
||
public Kyber1024KeyPairGenerator() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public void initialize(int keySize, SecureRandom random) { | ||
if (keySize != 1024) { | ||
throw new InvalidParameterException( | ||
"Kyber key size must be 1024. " | ||
+ "The specific key size " + keySize + " is not supported"); | ||
} | ||
this.random = random; | ||
} | ||
|
||
@Override | ||
public void initialize(AlgorithmParameterSpec algParams, | ||
SecureRandom random) throws InvalidAlgorithmParameterException { | ||
if (!(algParams instanceof KyberParameterSpec)) { | ||
throw new InvalidAlgorithmParameterException("Inappropriate parameter type"); | ||
} | ||
params = (KyberParameterSpec) algParams; | ||
if (params.getKyberKeySize() != this.kyberKeySize) { | ||
throw new InvalidAlgorithmParameterException("Invalid key size!"); | ||
} | ||
|
||
this.random = random; | ||
} | ||
|
||
/** | ||
* Generates a key pair. | ||
* | ||
* @return the new key pair | ||
*/ | ||
@Override | ||
public KeyPair generateKeyPair() { | ||
try { | ||
if (random == null) { | ||
random = SecureRandom.getInstanceStrong(); | ||
} | ||
|
||
} catch (Exception ex) { | ||
|
||
} | ||
KyberPKI kyberPKI = generateKeys1024(random); | ||
return new KeyPair(kyberPKI.getPublicKey(), kyberPKI.getPrivateKey()); | ||
} | ||
|
||
/** | ||
* Generate a 1024 public/private key set | ||
*/ | ||
private KyberPKI generateKeys1024(SecureRandom rand) { | ||
KyberPKI kyberPKI = new KyberPKI(); | ||
int paramsK = 4; | ||
try { | ||
KyberPackedPKI indcpaPKI = Indcpa.generateKyberKeys(paramsK); | ||
byte[] packedPrivateKey = indcpaPKI.getPackedPrivateKey(); | ||
byte[] packedPublicKey = indcpaPKI.getPackedPublicKey(); | ||
byte[] privateKeyFixedLength = new byte[KyberParams.Kyber1024SKBytes]; | ||
MessageDigest md = new SHA3_256(); | ||
|
||
byte[] encodedHash = md.digest(packedPublicKey); | ||
byte[] pkh = new byte[encodedHash.length]; | ||
System.arraycopy(encodedHash, 0, pkh, 0, encodedHash.length); | ||
byte[] rnd = new byte[KyberParams.paramsSymBytes]; | ||
rand.nextBytes(rnd); | ||
|
||
int offsetEnd = packedPrivateKey.length; | ||
System.arraycopy(packedPrivateKey, 0, privateKeyFixedLength, 0, offsetEnd); | ||
System.arraycopy(packedPublicKey, 0, privateKeyFixedLength, offsetEnd, packedPublicKey.length); | ||
offsetEnd = offsetEnd + packedPublicKey.length; | ||
|
||
System.arraycopy(pkh, 0, privateKeyFixedLength, offsetEnd, pkh.length); | ||
offsetEnd += pkh.length; | ||
System.arraycopy(rnd, 0, privateKeyFixedLength, offsetEnd, rnd.length); | ||
kyberPKI.setPublicKey(new KyberPublicKey(packedPublicKey, null, null)); | ||
kyberPKI.setPrivateKey(new KyberPrivateKey(privateKeyFixedLength, null, null)); | ||
} catch (Exception ex) { | ||
System.out.println("generateKeys1024 Exception! [" + ex.getMessage() + "]"); | ||
ex.printStackTrace(); | ||
} | ||
return kyberPKI; | ||
} | ||
|
||
} |
Oops, something went wrong.