Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EC Curve selection by size #68

Merged
merged 3 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

### Patches
* Detects stuck AMD Ryzen RDRAND and correctly treats as an error
* `KeyPairGenerator` for "EC" keys when initialized with a `int` always uses "secp*r1" curves now.
This matches the behavior of SunEC.
**This changes the curves selected for 192 and 256 from secp192k1 to secp192r1/P-192 and secp256k1 to secp256r1/P-256 respectively.**

### Maintenance
* Add prefix to test output lines indicating if suite will fail.
Expand Down
7 changes: 1 addition & 6 deletions src/com/amazon/corretto/crypto/provider/EcGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,7 @@ private static byte[] encodeSpec(final AlgorithmParameterSpec spec) {
public void initialize(final int keysize, final SecureRandom random)
throws InvalidParameterException {
try {
final String curveName;
if (keysize == 192 || keysize == 256) {
curveName = "secp" + keysize + "k1";
} else {
curveName = "secp" + keysize + "r1";
}
final String curveName = "secp" + keysize + "r1";
initialize(new ECGenParameterSpec(curveName), random);
} catch (final InvalidAlgorithmParameterException ex) {
throw new InvalidParameterException(ex.getMessage());
Expand Down
26 changes: 24 additions & 2 deletions tst/com/amazon/corretto/crypto/provider/test/EcGenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
package com.amazon.corretto.crypto.provider.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidParameterException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
Expand Down Expand Up @@ -42,6 +41,7 @@
import org.junit.Test;

public class EcGenTest {
public static final int[] KNOWN_SIZES = {192, 224, 256, 384, 521};
public static final String[][] KNOWN_CURVES = new String[][] {
// Prime Curves
new String[]{"secp112r1", "1.3.132.0.6"},
Expand Down Expand Up @@ -170,6 +170,28 @@ public void knownCurves() throws GeneralSecurityException {
}
}

@Test
public void knownSizes() throws GeneralSecurityException {
TestUtil.assumeMinimumVersion("1.2.0", nativeGen.getProvider());
for (int keysize : KNOWN_SIZES) {
nativeGen.initialize(keysize);
jceGen.initialize(keysize);

final KeyPair nativePair = nativeGen.generateKeyPair();
final KeyPair jcePair = jceGen.generateKeyPair();

final ECParameterSpec jceParams = ((ECPublicKey) jcePair.getPublic()).getParams();
final ECParameterSpec nativeParams = ((ECPublicKey) nativePair.getPublic()).getParams();
assertECEquals(Integer.toString(keysize), jceParams, nativeParams);
}
}

@Test
public void unknownSize() throws GeneralSecurityException {
TestUtil.assertThrows(InvalidParameterException.class,
() -> nativeGen.initialize(13));
}

@Test
public void explicitCurve() throws GeneralSecurityException {
jceGen.initialize(EXPLICIT_CURVE);
Expand Down
Binary file not shown.
Binary file not shown.