-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add encryption caps, expand keystore to alias storage :(
moar cleanup
- Loading branch information
1 parent
20b97b9
commit c2d42a8
Showing
14 changed files
with
321 additions
and
137 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
116 changes: 116 additions & 0 deletions
116
cryptography/src/main/java/com/salesforce/apollo/cryptography/EncryptionAlgorithm.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,116 @@ | ||
package com.salesforce.apollo.cryptography; | ||
|
||
import java.math.BigInteger; | ||
import java.security.*; | ||
import java.security.interfaces.EdECPrivateKey; | ||
import java.security.interfaces.EdECPublicKey; | ||
import java.security.interfaces.XECPublicKey; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.NamedParameterSpec; | ||
import java.security.spec.XECPublicKeySpec; | ||
|
||
public enum EncryptionAlgorithm { | ||
X_25519 { | ||
@Override | ||
public String algorithmName() { | ||
return "X25519"; | ||
} | ||
|
||
@Override | ||
public String curveName() { | ||
return "Curve25519"; | ||
} | ||
|
||
@Override | ||
public int publicKeyLength() { | ||
return 32; | ||
} | ||
}, X_448 { | ||
@Override | ||
public String algorithmName() { | ||
return "X448"; | ||
} | ||
|
||
@Override | ||
public String curveName() { | ||
return "Curve448"; | ||
} | ||
|
||
@Override | ||
public int publicKeyLength() { | ||
return 57; | ||
} | ||
}; | ||
|
||
public static EncryptionAlgorithm lookup(PrivateKey privateKey) { | ||
return switch (privateKey.getAlgorithm()) { | ||
case "XDH" -> lookupX(((EdECPrivateKey) privateKey).getParams()); | ||
case "x25519" -> X_25519; | ||
case "x448" -> X_448; | ||
default -> throw new IllegalArgumentException("Unknown algorithm: " + privateKey.getAlgorithm()); | ||
}; | ||
} | ||
|
||
public static EncryptionAlgorithm lookup(PublicKey publicKey) { | ||
return switch (publicKey.getAlgorithm()) { | ||
case "XDH" -> lookupX(((EdECPublicKey) publicKey).getParams()); | ||
case "X25519" -> X_25519; | ||
case "X448" -> X_448; | ||
default -> throw new IllegalArgumentException("Unknown algorithm: " + publicKey.getAlgorithm()); | ||
}; | ||
} | ||
|
||
private static EncryptionAlgorithm lookupX(NamedParameterSpec params) { | ||
var curveName = params.getName(); | ||
return switch (curveName.toLowerCase()) { | ||
case "x25519" -> X_25519; | ||
case "x448" -> X_448; | ||
default -> throw new IllegalArgumentException("Unknown edwards curve: " + curveName); | ||
}; | ||
} | ||
|
||
abstract public String algorithmName(); | ||
|
||
abstract public String curveName(); | ||
|
||
final public byte[] encode(PublicKey publicKey) { | ||
return ((XECPublicKey) publicKey).getU().toByteArray(); | ||
} | ||
|
||
final public KeyPair generateKeyPair() { | ||
try { | ||
KeyPairGenerator kpg = KeyPairGenerator.getInstance("XDH"); | ||
kpg.initialize(getParamSpec()); | ||
return kpg.generateKeyPair(); | ||
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) { | ||
throw new IllegalArgumentException("Cannot generate key pair", e); | ||
} | ||
} | ||
|
||
final public KeyPair generateKeyPair(SecureRandom secureRandom) { | ||
try { | ||
KeyPairGenerator kpg = KeyPairGenerator.getInstance("XDH"); | ||
kpg.initialize(getParamSpec(), secureRandom); | ||
return kpg.generateKeyPair(); | ||
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) { | ||
throw new IllegalArgumentException("Cannot generate key pair", e); | ||
} | ||
} | ||
|
||
final public PublicKey publicKey(byte[] bytes) { | ||
try { | ||
KeyFactory kf = KeyFactory.getInstance("XDH"); | ||
BigInteger u = new BigInteger(bytes); | ||
XECPublicKeySpec pubSpec = new XECPublicKeySpec(getParamSpec(), u); | ||
return kf.generatePublic(pubSpec); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { | ||
throw new IllegalArgumentException("Cannot create public key", e); | ||
} | ||
} | ||
|
||
abstract public int publicKeyLength(); | ||
|
||
private NamedParameterSpec getParamSpec() { | ||
return new NamedParameterSpec(algorithmName()); | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
cryptography/src/test/java/com/salesforce/apollo/cryptography/XTest.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,56 @@ | ||
package com.salesforce.apollo.cryptography; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.crypto.KeyAgreement; | ||
import java.security.SecureRandom; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* @author hal.hildebrand | ||
**/ | ||
public class XTest { | ||
@Test | ||
public void testEncoding() throws Exception { | ||
var entropy = SecureRandom.getInstance("SHA1PRNG"); | ||
entropy.setSeed(new byte[] { 6, 6, 6 }); | ||
|
||
var algorithm = EncryptionAlgorithm.X_25519; | ||
var pair = algorithm.generateKeyPair(entropy); | ||
assertNotNull(pair); | ||
var encodedPublic = algorithm.encode(pair.getPublic()); | ||
assertNotNull(encodedPublic); | ||
var decodedPublic = algorithm.publicKey(encodedPublic); | ||
assertNotNull(decodedPublic); | ||
assertEquals(pair.getPublic(), decodedPublic); | ||
} | ||
|
||
@Test | ||
public void testRoundTrip() throws Exception { | ||
var entropy = SecureRandom.getInstance("SHA1PRNG"); | ||
entropy.setSeed(new byte[] { 6, 6, 6 }); | ||
|
||
var algorithm = EncryptionAlgorithm.X_25519; | ||
var pair1 = algorithm.generateKeyPair(entropy); | ||
assertNotNull(pair1); | ||
var pair2 = algorithm.generateKeyPair(entropy); | ||
assertNotNull(pair2); | ||
|
||
KeyAgreement ka = KeyAgreement.getInstance("XDH"); | ||
KeyAgreement ka2 = KeyAgreement.getInstance("XDH"); | ||
|
||
ka.init(pair1.getPrivate()); | ||
ka2.init(pair2.getPrivate()); | ||
|
||
ka.doPhase(pair2.getPublic(), true); | ||
ka2.doPhase(pair1.getPublic(), true); | ||
|
||
byte[] secret1 = ka.generateSecret(); | ||
assertNotNull(secret1); | ||
byte[] secret2 = ka2.generateSecret(); | ||
assertNotNull(secret2); | ||
|
||
assertArrayEquals(secret1, secret2); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.