Skip to content

Commit

Permalink
Fix EC Curve selection by size (#68)
Browse files Browse the repository at this point in the history
* Fix EC Curve selection by size

Now, when a new EC key pair is generated by size (rather than by curve), always select a 'secp*r1' curve.
For the standard sizes of 192, 224, 256, 384, and 521 this always matches the behavior of the SunEC provider.

This changes the behavior for sizes 192 and 256 from the secp*k1 to the secp*r1 curves (NIST P-192 and P-256) respectively.
  • Loading branch information
SalusaSecondus authored Nov 8, 2019
1 parent 348f01c commit 6d183bd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
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.

0 comments on commit 6d183bd

Please sign in to comment.