-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from itstheceo/ec_p256
Add ECDSA key support (SECP256R1) again
- Loading branch information
Showing
30 changed files
with
679 additions
and
2,552 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
89 changes: 89 additions & 0 deletions
89
src/main/java/org/biscuitsec/biscuit/crypto/Ed25519KeyPair.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,89 @@ | ||
package org.biscuitsec.biscuit.crypto; | ||
|
||
import biscuit.format.schema.Schema; | ||
import net.i2p.crypto.eddsa.EdDSAEngine; | ||
import net.i2p.crypto.eddsa.EdDSAPrivateKey; | ||
import net.i2p.crypto.eddsa.EdDSAPublicKey; | ||
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveSpec; | ||
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable; | ||
import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec; | ||
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec; | ||
import org.biscuitsec.biscuit.token.builder.Utils; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.SecureRandom; | ||
import java.security.Signature; | ||
|
||
final class Ed25519KeyPair extends KeyPair { | ||
|
||
static final int SIGNATURE_LENGTH = 64; | ||
|
||
private final EdDSAPrivateKey privateKey; | ||
private final EdDSAPublicKey publicKey; | ||
|
||
private static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519); | ||
|
||
public Ed25519KeyPair(byte[] bytes) { | ||
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(bytes, ed25519); | ||
EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); | ||
|
||
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ed25519); | ||
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); | ||
|
||
this.privateKey = privKey; | ||
this.publicKey = pubKey; | ||
} | ||
|
||
public Ed25519KeyPair(SecureRandom rng) { | ||
byte[] b = new byte[32]; | ||
rng.nextBytes(b); | ||
|
||
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(b, ed25519); | ||
EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); | ||
|
||
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ed25519); | ||
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); | ||
|
||
this.privateKey = privKey; | ||
this.publicKey = pubKey; | ||
} | ||
|
||
public Ed25519KeyPair(String hex) { | ||
this(Utils.hexStringToByteArray(hex)); | ||
} | ||
|
||
public static java.security.PublicKey decode(byte[] data) { | ||
return new EdDSAPublicKey(new EdDSAPublicKeySpec(data, ed25519)); | ||
} | ||
|
||
public static Signature getSignature() throws NoSuchAlgorithmException { | ||
return new EdDSAEngine(MessageDigest.getInstance(ed25519.getHashAlgorithm())); | ||
} | ||
|
||
@Override | ||
public byte[] toBytes() { | ||
return privateKey.getSeed(); | ||
} | ||
|
||
@Override | ||
public String toHex() { | ||
return Utils.byteArrayToHexString(toBytes()); | ||
} | ||
|
||
@Override | ||
public java.security.PublicKey publicKey() { | ||
return publicKey; | ||
} | ||
|
||
@Override | ||
public PrivateKey private_key() { | ||
return privateKey; | ||
} | ||
|
||
@Override | ||
public PublicKey public_key() { | ||
return new PublicKey(Schema.PublicKey.Algorithm.Ed25519, this.publicKey); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,106 +1,63 @@ | ||
package org.biscuitsec.biscuit.crypto; | ||
|
||
|
||
import biscuit.format.schema.Schema; | ||
import biscuit.format.schema.Schema.PublicKey.Algorithm; | ||
import net.i2p.crypto.eddsa.EdDSAEngine; | ||
import org.biscuitsec.biscuit.token.builder.Utils; | ||
import net.i2p.crypto.eddsa.EdDSAPrivateKey; | ||
import net.i2p.crypto.eddsa.EdDSAPublicKey; | ||
import net.i2p.crypto.eddsa.spec.*; | ||
import net.i2p.crypto.eddsa.Utils; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
import java.security.Signature; | ||
|
||
/** | ||
* Private and public key | ||
* Private and public key. | ||
*/ | ||
public final class KeyPair { | ||
public final EdDSAPrivateKey private_key; | ||
public final EdDSAPublicKey public_key; | ||
public abstract class KeyPair { | ||
|
||
private static final int ED25519_PUBLIC_KEYSIZE = 32; | ||
private static final int ED25519_PRIVATE_KEYSIZE = 64; | ||
private static final int ED25519_SEED_SIZE = 32; | ||
public static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519); | ||
|
||
public KeyPair() { | ||
this(new SecureRandom()); | ||
public static KeyPair generate(Algorithm algorithm) { | ||
return generate(algorithm, new SecureRandom()); | ||
} | ||
|
||
public KeyPair(final SecureRandom rng) { | ||
byte[] b = new byte[32]; | ||
rng.nextBytes(b); | ||
|
||
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(b, ed25519); | ||
EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); | ||
|
||
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ed25519); | ||
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); | ||
|
||
this.private_key = privKey; | ||
this.public_key = pubKey; | ||
public static KeyPair generate(Algorithm algorithm, String hex) { | ||
return generate(algorithm, Utils.hexToBytes(hex)); | ||
} | ||
|
||
public static KeyPair generate(Algorithm algorithm) { | ||
return generate(algorithm, new SecureRandom()); | ||
public static KeyPair generate(Algorithm algorithm, byte[] bytes) { | ||
if (algorithm == Algorithm.Ed25519) { | ||
return new Ed25519KeyPair(bytes); | ||
} else if (algorithm == Algorithm.SECP256R1) { | ||
return new SECP256R1KeyPair(bytes); | ||
} else { | ||
throw new IllegalArgumentException("Unsupported algorithm"); | ||
} | ||
} | ||
|
||
public static KeyPair generate(Algorithm algorithm, SecureRandom rng) { | ||
if (algorithm == Algorithm.Ed25519) { | ||
return new KeyPair(rng); | ||
return new Ed25519KeyPair(rng); | ||
} else if (algorithm == Algorithm.SECP256R1) { | ||
return new SECP256R1KeyPair(rng); | ||
} else { | ||
throw new IllegalArgumentException("Unsupported algorithm"); | ||
} | ||
} | ||
|
||
public static Signature generateSignature(Algorithm algorithm) throws NoSuchAlgorithmException { | ||
if (algorithm == Algorithm.Ed25519) { | ||
return KeyPair.getSignature(); | ||
return Ed25519KeyPair.getSignature(); | ||
} else if (algorithm == Algorithm.SECP256R1) { | ||
return SECP256R1KeyPair.getSignature(); | ||
} else { | ||
throw new NoSuchAlgorithmException("Unsupported algorithm"); | ||
} | ||
} | ||
|
||
public byte[] toBytes() { | ||
return this.private_key.getSeed(); | ||
} | ||
|
||
public KeyPair(byte[] b) { | ||
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(b, ed25519); | ||
EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); | ||
|
||
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ed25519); | ||
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); | ||
|
||
this.private_key = privKey; | ||
this.public_key = pubKey; | ||
} | ||
|
||
public String toHex() { | ||
return Utils.byteArrayToHexString(this.toBytes()); | ||
} | ||
|
||
public KeyPair(String hex) { | ||
byte[] b = Utils.hexStringToByteArray(hex); | ||
|
||
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(b, ed25519); | ||
EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); | ||
public abstract byte[] toBytes(); | ||
|
||
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ed25519); | ||
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); | ||
public abstract String toHex(); | ||
|
||
this.private_key = privKey; | ||
this.public_key = pubKey; | ||
} | ||
public abstract java.security.PublicKey publicKey(); | ||
|
||
public static Signature getSignature() throws NoSuchAlgorithmException { | ||
return new EdDSAEngine(MessageDigest.getInstance(ed25519.getHashAlgorithm())); | ||
} | ||
public abstract java.security.PrivateKey private_key(); | ||
|
||
public PublicKey public_key() { | ||
return new PublicKey(Schema.PublicKey.Algorithm.Ed25519, this.public_key); | ||
} | ||
public abstract PublicKey public_key(); | ||
} |
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.