Skip to content

Commit 2671070

Browse files
authored
Merge pull request #35 from str4d/33-jca-naming
Correct algorithm and curve names
2 parents 228fb3d + a0d50c1 commit 2671070

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
*
6565
*/
6666
public final class EdDSAEngine extends Signature {
67+
public static final String SIGNATURE_ALGORITHM = "NONEwithEdDSA";
68+
6769
private MessageDigest digest;
6870
private ByteArrayOutputStream baos;
6971
private EdDSAKey key;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import java.security.Provider;
1717
import java.security.Security;
1818

19-
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
20-
2119
/**
2220
* A security {@link Provider} that can be registered via {@link Security#addProvider(Provider)}
2321
*
@@ -43,6 +41,6 @@ protected void setup() {
4341
// see https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/HowToImplAProvider.html
4442
put("KeyPairGenerator." + EdDSAKey.KEY_ALGORITHM, "net.i2p.crypto.eddsa.KeyPairGenerator");
4543
put("KeyFactory." + EdDSAKey.KEY_ALGORITHM, "net.i2p.crypto.eddsa.KeyFactory");
46-
put("Signature." + EdDSANamedCurveTable.CURVE_ED25519_SHA512, "net.i2p.crypto.eddsa.EdDSAEngine");
44+
put("Signature." + EdDSAEngine.SIGNATURE_ALGORITHM, "net.i2p.crypto.eddsa.EdDSAEngine");
4745
}
4846
}

src/net/i2p/crypto/eddsa/spec/EdDSANamedCurveTable.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package net.i2p.crypto.eddsa.spec;
1313

1414
import java.util.Hashtable;
15+
import java.util.Locale;
1516

1617
import net.i2p.crypto.eddsa.Utils;
1718
import net.i2p.crypto.eddsa.math.Curve;
@@ -25,7 +26,7 @@
2526
*
2627
*/
2728
public class EdDSANamedCurveTable {
28-
public static final String CURVE_ED25519_SHA512 = "SHA512withEd25519";
29+
public static final String CURVE_ED25519_SHA512 = "ed25519-sha-512";
2930

3031
private static final Field ed25519field = new Field(
3132
256, // b
@@ -48,14 +49,25 @@ public class EdDSANamedCurveTable {
4849
private static final Hashtable<String, EdDSANamedCurveSpec> curves = new Hashtable<String, EdDSANamedCurveSpec>();
4950

5051
public static void defineCurve(String name, EdDSANamedCurveSpec curve) {
51-
curves.put(name, curve);
52+
curves.put(name.toLowerCase(Locale.ENGLISH), curve);
53+
}
54+
55+
static void defineCurveAlias(String name, String alias) {
56+
EdDSANamedCurveSpec curve = curves.get(name.toLowerCase(Locale.ENGLISH));
57+
if (curve == null) {
58+
throw new IllegalStateException();
59+
}
60+
curves.put(alias.toLowerCase(Locale.ENGLISH), curve);
5261
}
5362

5463
static {
5564
defineCurve(CURVE_ED25519_SHA512, ed25519sha512);
65+
66+
// RFC 8032
67+
defineCurveAlias(CURVE_ED25519_SHA512, "Ed25519");
5668
}
5769

5870
public static EdDSANamedCurveSpec getByName(String name) {
59-
return curves.get(name);
71+
return curves.get(name.toLowerCase(Locale.ENGLISH));
6072
}
6173
}

test/net/i2p/crypto/eddsa/EdDSASecurityProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void canGetInstancesWhenProviderIsPresent() throws Exception {
3838

3939
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EdDSA", "EdDSA");
4040
KeyFactory keyFac = KeyFactory.getInstance("EdDSA", "EdDSA");
41-
Signature sgr = Signature.getInstance("SHA512withEd25519", "EdDSA");
41+
Signature sgr = Signature.getInstance("NONEwithEdDSA", "EdDSA");
4242

4343
Security.removeProvider("EdDSA");
4444
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* EdDSA-Java by str4d
3+
*
4+
* To the extent possible under law, the person who associated CC0 with
5+
* EdDSA-Java has waived all copyright and related or neighboring rights
6+
* to EdDSA-Java.
7+
*
8+
* You should have received a copy of the CC0 legalcode along with this
9+
* work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
10+
*
11+
*/
12+
package net.i2p.crypto.eddsa.spec;
13+
14+
import static org.hamcrest.Matchers.*;
15+
import static org.junit.Assert.*;
16+
17+
import org.junit.Test;
18+
19+
/**
20+
* @author str4d
21+
*
22+
*/
23+
public class EdDSANamedCurveTableTest {
24+
/**
25+
* Ensure curve names are case-inspecific
26+
*/
27+
@Test
28+
public void curveNamesAreCaseInspecific() {
29+
EdDSANamedCurveSpec mixed = EdDSANamedCurveTable.getByName("Ed25519");
30+
EdDSANamedCurveSpec lower = EdDSANamedCurveTable.getByName("ed25519");
31+
EdDSANamedCurveSpec upper = EdDSANamedCurveTable.getByName("ED25519");
32+
33+
assertThat(lower, is(equalTo(mixed)));
34+
assertThat(upper, is(equalTo(mixed)));
35+
}
36+
}

0 commit comments

Comments
 (0)