-
Notifications
You must be signed in to change notification settings - Fork 123
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
Conversation
*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.
There was a problem hiding this 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.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No import stars please.
There was a problem hiding this comment.
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.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto on import star.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/main/java/com/amazonaws/encryptionsdk/internal/TrailingSignatureAlgorithm.java
Show resolved
Hide resolved
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; | ||
} |
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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
Issue #, if available: #41
Description of changes:
Removes explicit use of BouncyCastle from the
ECDSASignatureAlgorithm
implementation ofTrailingSignatureAlgorithm
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Check any applicable: