Skip to content

Commit b918755

Browse files
Lyor Goldsteinstr4d
authored andcommitted
Added EdDSASecurityProvider that can be registered in the JCE
1 parent 9c27708 commit b918755

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<packaging>bundle</packaging>
99
<description>Implementation of EdDSA in Java</description>
1010
<url>https://github.com/str4d/ed25519-java</url>
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
</properties>
1114
<licenses>
1215
<license>
1316
<name>CC0 1.0 Universal</name>
@@ -100,6 +103,9 @@
100103
<goals>
101104
<goal>jar</goal>
102105
</goals>
106+
<configuration> <!-- There are some malformed javadoc comments -->
107+
<failOnError>false</failOnError>
108+
</configuration>
103109
</execution>
104110
</executions>
105111
</plugin>

src/net/i2p/crypto/eddsa/EdDSAKey.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
/**
1717
* Common interface for all EdDSA keys.
1818
* @author str4d
19-
*
2019
*/
2120
public interface EdDSAKey {
21+
/**
22+
* The reported key algorithm for all EdDSA keys
23+
*/
24+
String KEY_ALGORITHM = "EdDSA";
25+
2226
/**
2327
* return a parameter specification representing the EdDSA domain
2428
* parameters for the key.
2529
*/
26-
public EdDSAParameterSpec getParams();
30+
EdDSAParameterSpec getParams();
2731
}

src/net/i2p/crypto/eddsa/EdDSAPrivateKey.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ public EdDSAPrivateKey(PKCS8EncodedKeySpec spec) throws InvalidKeySpecException
5656
EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512)));
5757
}
5858

59+
@Override
5960
public String getAlgorithm() {
60-
return "EdDSA";
61+
return KEY_ALGORITHM;
6162
}
6263

64+
@Override
6365
public String getFormat() {
6466
return "PKCS#8";
6567
}
@@ -101,6 +103,7 @@ public String getFormat() {
101103
*
102104
* @return 49 bytes for Ed25519, null for other curves
103105
*/
106+
@Override
104107
public byte[] getEncoded() {
105108
if (!edDsaSpec.equals(EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512)))
106109
return null;
@@ -177,6 +180,7 @@ private static byte[] decode(byte[] d) throws InvalidKeySpecException {
177180
}
178181
}
179182

183+
@Override
180184
public EdDSAParameterSpec getParams() {
181185
return edDsaSpec;
182186
}

src/net/i2p/crypto/eddsa/EdDSAPublicKey.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ public EdDSAPublicKey(X509EncodedKeySpec spec) throws InvalidKeySpecException {
5151
EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512)));
5252
}
5353

54+
@Override
5455
public String getAlgorithm() {
55-
return "EdDSA";
56+
return KEY_ALGORITHM;
5657
}
5758

59+
@Override
5860
public String getFormat() {
5961
return "X.509";
6062
}
@@ -82,6 +84,7 @@ public String getFormat() {
8284
*
8385
* @return 47 bytes for Ed25519, null for other curves
8486
*/
87+
@Override
8588
public byte[] getEncoded() {
8689
if (!edDsaSpec.equals(EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512)))
8790
return null;
@@ -148,6 +151,7 @@ private static byte[] decode(byte[] d) throws InvalidKeySpecException {
148151
}
149152
}
150153

154+
@Override
151155
public EdDSAParameterSpec getParams() {
152156
return edDsaSpec;
153157
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.i2p.crypto.eddsa;
2+
3+
import java.security.AccessController;
4+
import java.security.PrivilegedAction;
5+
import java.security.Provider;
6+
import java.security.Security;
7+
8+
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
9+
10+
/**
11+
* A security {@link Provider} that can be registered via {@link Security#addProvider(Provider)}
12+
*
13+
* @author str4d
14+
*/
15+
public class EdDSASecurityProvider extends Provider {
16+
private static final long serialVersionUID = 1210027906682292307L;
17+
public static final String PROVIDER_NAME = "EdDSA";
18+
19+
public EdDSASecurityProvider() {
20+
super(PROVIDER_NAME, 0.1 /* should match POM major.minor version */, "str4d " + PROVIDER_NAME + " security provider wrapper");
21+
22+
AccessController.doPrivileged(new PrivilegedAction<Object>() {
23+
@Override
24+
public Object run() {
25+
setup();
26+
return null;
27+
}
28+
});
29+
}
30+
31+
protected void setup() {
32+
// see https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/HowToImplAProvider.html
33+
put("KeyPairGenerator." + EdDSAKey.KEY_ALGORITHM, "net.i2p.crypto.eddsa.KeyPairGenerator");
34+
put("KeyFactory." + EdDSAKey.KEY_ALGORITHM, "net.i2p.crypto.eddsa.KeyFactory");
35+
put("Signature." + EdDSANamedCurveTable.CURVE_ED25519_SHA512, "net.i2p.crypto.eddsa.EdDSAEngine");
36+
}
37+
}

0 commit comments

Comments
 (0)