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

Remove use of BouncyCastle for EC key generation and point (de)compression #129

Merged
merged 3 commits into from
Oct 16, 2019

Conversation

WesleyRosenblum
Copy link
Contributor

Issue #, if available: #41

Description of changes:

Removes explicit use of BouncyCastle from the ECDSASignatureAlgorithm implementation of TrailingSignatureAlgorithm.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Check any applicable:

  • Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.

*Issue #, if available:* #41

*Description of changes:*

Removes explicit use of BouncyCastle from the `ECDSASignatureAlgorithm` implementation of `TrailingSignatureAlgorithm`.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

# Check any applicable:
- [ ] Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.
@WesleyRosenblum WesleyRosenblum changed the title Remove use BouncyCastle for EC key generation and point (de)compression Remove use of BouncyCastle for EC key generation and point (de)compression Oct 15, 2019
Copy link
Contributor

@SalusaSecondus SalusaSecondus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent first pass. Just needs a few tweaks.

import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.ECPublicKey;
import java.security.spec.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No import stars please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, and updated my IDE

import static com.amazonaws.encryptionsdk.internal.BouncyCastleConfiguration.INTERNAL_BOUNCY_CASTLE_PROVIDER;
import static com.amazonaws.encryptionsdk.internal.Utils.bigIntegerToByteArray;
import static com.amazonaws.encryptionsdk.internal.Utils.encodeBase64String;
import static org.apache.commons.lang3.Validate.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto on import star.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

throw new IllegalArgumentException("Compressed y value was invalid");
}

final BigInteger p = ((ECFieldFp)ecParameterSpec.getCurve().getField()).getP();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and throughout: Please have a space between the closing parenthesis of the cast and the following token.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 292 to 308
final byte[] result = new byte[length];
byte[] rawBytes = bigInteger.toByteArray();

//Remove sign byte if one is present
if(rawBytes[0] == 0) {
rawBytes = Arrays.copyOfRange(rawBytes, 1, rawBytes.length);
}

if(length < rawBytes.length) {
throw new IllegalArgumentException("Length must be as least as long as the BigInteger byte array " +
"without the sign byte");
}

System.arraycopy(rawBytes, 0, result, length - rawBytes.length, rawBytes.length);

return result;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify this (and reduce unneeded allocations).

final byte[] rawBytes = bigInteger.toByteArray();
// If we're already the correct length, just return it.
if (rawBytes.length ==length) {
    return rawBytes;
}
// If we're exactly one byte too large, but we have a leading zero byte, remove it and return.
if (rawBytes.length == length + 1 && rawBytes[0] == 0) {
    return Arrays.copyOfRange(rawBytes, 1, rawBytes.length);
}

if (rawBytes.length > length) {
    throw new IllegalArgumentException("Length must be as least as long as the BigInteger byte array " +
      "without the sign byte");
}
final byte[] paddedResult = new byte[length];
System.arraycopy(rawBytes, 0, paddedResult , length - rawBytes.length, rawBytes.length);
return paddedResult;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

public static byte[] unsignedBytesToSignedBytes(final int[] unsignedBytes) {
byte[] signedBytes = new byte[unsignedBytes.length];

for(int i= 0 ; i < unsignedBytes.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and throughout: space after if, for, and other keywords to distinguish them from function calls.

Here: Space on either side of the equals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -96,5 +97,19 @@ public void base64something() {
assertEquals(encoded, Utils.encodeBase64String(data));
assertArrayEquals(data, Utils.decodeBase64String(encoded));
}

@Test
public void testBigIntegerToByteArray() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also test exceptional cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests

*Issue #, if available:* #41

*Description of changes:*

Removes explicit use of BouncyCastle from the `ECDSASignatureAlgorithm` implementation of `TrailingSignatureAlgorithm`.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

# Check any applicable:
- [ ] Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.
 into pull/41

� Conflicts:
�	src/main/java/com/amazonaws/encryptionsdk/internal/TrailingSignatureAlgorithm.java
�	src/main/java/com/amazonaws/encryptionsdk/internal/Utils.java
�	src/test/java/com/amazonaws/encryptionsdk/TestUtils.java
�	src/test/java/com/amazonaws/encryptionsdk/UtilsTest.java
�	src/test/java/com/amazonaws/encryptionsdk/internal/TrailingSignatureAlgorithmTest.java
Copy link
Contributor

@SalusaSecondus SalusaSecondus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@WesleyRosenblum WesleyRosenblum merged commit 4ebbbcc into master Oct 16, 2019
@WesleyRosenblum WesleyRosenblum deleted the pull/41 branch October 16, 2019 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants