|
1 | 1 | package org.biscuitsec.biscuit.crypto;
|
2 | 2 |
|
3 | 3 | import biscuit.format.schema.Schema;
|
| 4 | +import java.security.InvalidKeyException; |
| 5 | +import java.security.MessageDigest; |
| 6 | +import java.security.NoSuchAlgorithmException; |
| 7 | +import java.security.SecureRandom; |
| 8 | +import java.security.Signature; |
| 9 | +import java.security.SignatureException; |
4 | 10 | import net.i2p.crypto.eddsa.EdDSAEngine;
|
5 | 11 | import net.i2p.crypto.eddsa.EdDSAPrivateKey;
|
6 | 12 | import net.i2p.crypto.eddsa.EdDSAPublicKey;
|
|
10 | 16 | import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
|
11 | 17 | import org.biscuitsec.biscuit.token.builder.Utils;
|
12 | 18 |
|
13 |
| -import java.security.InvalidKeyException; |
14 |
| -import java.security.MessageDigest; |
15 |
| -import java.security.NoSuchAlgorithmException; |
16 |
| -import java.security.SecureRandom; |
17 |
| -import java.security.Signature; |
18 |
| -import java.security.SignatureException; |
19 |
| - |
20 | 19 | final class Ed25519KeyPair extends KeyPair {
|
| 20 | + private static final int BUFFER_SIZE = 32; |
21 | 21 |
|
22 |
| - static final int SIGNATURE_LENGTH = 64; |
23 |
| - |
24 |
| - private final EdDSAPrivateKey privateKey; |
25 |
| - private final EdDSAPublicKey publicKey; |
| 22 | + public static final int SIGNATURE_LENGTH = 64; |
26 | 23 |
|
27 |
| - private static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519); |
| 24 | + private final EdDSAPrivateKey privateKey; |
| 25 | + private final EdDSAPublicKey publicKey; |
28 | 26 |
|
29 |
| - public Ed25519KeyPair(byte[] bytes) { |
30 |
| - EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(bytes, ed25519); |
31 |
| - EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); |
| 27 | + private static final EdDSANamedCurveSpec ED_25519 = |
| 28 | + EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519); |
32 | 29 |
|
33 |
| - EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ed25519); |
34 |
| - EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); |
| 30 | + Ed25519KeyPair(byte[] bytes) { |
| 31 | + EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(bytes, ED_25519); |
| 32 | + EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); |
35 | 33 |
|
36 |
| - this.privateKey = privKey; |
37 |
| - this.publicKey = pubKey; |
38 |
| - } |
| 34 | + EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ED_25519); |
| 35 | + EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); |
39 | 36 |
|
40 |
| - public Ed25519KeyPair(SecureRandom rng) { |
41 |
| - byte[] b = new byte[32]; |
42 |
| - rng.nextBytes(b); |
| 37 | + this.privateKey = privKey; |
| 38 | + this.publicKey = pubKey; |
| 39 | + } |
43 | 40 |
|
44 |
| - EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(b, ed25519); |
45 |
| - EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); |
| 41 | + Ed25519KeyPair(SecureRandom rng) { |
| 42 | + byte[] b = new byte[BUFFER_SIZE]; |
| 43 | + rng.nextBytes(b); |
46 | 44 |
|
47 |
| - EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ed25519); |
48 |
| - EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); |
| 45 | + EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(b, ED_25519); |
| 46 | + EdDSAPrivateKey privKey = new EdDSAPrivateKey(privKeySpec); |
49 | 47 |
|
50 |
| - this.privateKey = privKey; |
51 |
| - this.publicKey = pubKey; |
52 |
| - } |
| 48 | + EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKey.getA(), ED_25519); |
| 49 | + EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec); |
53 | 50 |
|
54 |
| - public Ed25519KeyPair(String hex) { |
55 |
| - this(Utils.hexStringToByteArray(hex)); |
56 |
| - } |
| 51 | + this.privateKey = privKey; |
| 52 | + this.publicKey = pubKey; |
| 53 | + } |
57 | 54 |
|
58 |
| - public static java.security.PublicKey decode(byte[] data) { |
59 |
| - return new EdDSAPublicKey(new EdDSAPublicKeySpec(data, ed25519)); |
60 |
| - } |
| 55 | + Ed25519KeyPair(String hex) { |
| 56 | + this(Utils.hexStringToByteArray(hex)); |
| 57 | + } |
61 | 58 |
|
62 |
| - public static Signature getSignature() throws NoSuchAlgorithmException { |
63 |
| - return new EdDSAEngine(MessageDigest.getInstance(ed25519.getHashAlgorithm())); |
64 |
| - } |
| 59 | + public static java.security.PublicKey decode(byte[] data) { |
| 60 | + return new EdDSAPublicKey(new EdDSAPublicKeySpec(data, ED_25519)); |
| 61 | + } |
65 | 62 |
|
66 |
| - @Override |
67 |
| - public byte[] sign(byte[] data) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { |
68 |
| - Signature sgr = KeyPair.generateSignature(Schema.PublicKey.Algorithm.Ed25519); |
69 |
| - sgr.initSign(privateKey); |
70 |
| - sgr.update(data); |
71 |
| - return sgr.sign(); |
72 |
| - } |
| 63 | + public static Signature getSignature() throws NoSuchAlgorithmException { |
| 64 | + return new EdDSAEngine(MessageDigest.getInstance(ED_25519.getHashAlgorithm())); |
| 65 | + } |
73 | 66 |
|
74 |
| - @Override |
75 |
| - public byte[] toBytes() { |
76 |
| - return privateKey.getSeed(); |
77 |
| - } |
| 67 | + @Override |
| 68 | + public byte[] sign(byte[] data) |
| 69 | + throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { |
| 70 | + Signature sgr = KeyPair.generateSignature(Schema.PublicKey.Algorithm.Ed25519); |
| 71 | + sgr.initSign(privateKey); |
| 72 | + sgr.update(data); |
| 73 | + return sgr.sign(); |
| 74 | + } |
78 | 75 |
|
79 |
| - @Override |
80 |
| - public String toHex() { |
81 |
| - return Utils.byteArrayToHexString(toBytes()); |
82 |
| - } |
| 76 | + @Override |
| 77 | + public byte[] toBytes() { |
| 78 | + return privateKey.getSeed(); |
| 79 | + } |
83 | 80 |
|
84 |
| - @Override |
85 |
| - public PublicKey public_key() { |
86 |
| - return new PublicKey(Schema.PublicKey.Algorithm.Ed25519, this.publicKey); |
87 |
| - } |
| 81 | + @Override |
| 82 | + public String toHex() { |
| 83 | + return Utils.byteArrayToHexString(toBytes()); |
| 84 | + } |
88 | 85 |
|
| 86 | + @Override |
| 87 | + public PublicKey getPublicKey() { |
| 88 | + return new PublicKey(Schema.PublicKey.Algorithm.Ed25519, this.publicKey); |
| 89 | + } |
89 | 90 | }
|
0 commit comments