Skip to content

Commit

Permalink
Padding of the R and S components in case they are smaller than 31 by…
Browse files Browse the repository at this point in the history
…tes for SECP256r1 (#52)

* code modified to allow 30 bytes signature and test added

Signed-off-by: Miguel Rojo <[email protected]>
  • Loading branch information
freemanzMrojo authored Jan 26, 2022
1 parent 6a73c7d commit 0353a6f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ private byte[] convertToNativeRepresentation(final byte[] signature) {
return nativeSignature;
}

// the first byte in the native representation was 0, but in the Java representation this is ignored.
// the first bytes in the native representation were 0, but in the Java representation this is ignored.
// the signature needs to be shifted to the right for the native representation
if (signature.length == CURVE_BYTE_LENGTH - 1) {
if (signature.length < CURVE_BYTE_LENGTH) {
byte[] nativeSignature = new byte[CURVE_BYTE_LENGTH];

// copy signature to nativeSignature, shifting it one to the right
System.arraycopy(signature, 0, nativeSignature, 1, signature.length);
// copy signature to nativeSignature, shifting it to the right
System.arraycopy(signature, 0, nativeSignature, CURVE_BYTE_LENGTH - signature.length, signature.length);

return nativeSignature;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package org.hyperledger.besu.nativelib.secp256r1;

import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.util.Hexadecimals;
import org.junit.Before;
import org.junit.Test;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

Expand Down Expand Up @@ -93,6 +95,24 @@ public void keyRecovery_should_return_expected_public_key() {

}

@Test
public void keyRecovery_should_return_expected_public_key_if_r_is_less_than_32_bytes_long() {
final BigInteger r = new BigInteger("607232317131644998607993399928086035368869502933999419429470745918733484");
final BigInteger s = new BigInteger("909326537358980219114547956988636184748037502936154044628658501523731230682");
final byte v = (byte) 1;
final Bytes dataHash = Bytes.fromHexString("0x5d2a686cbe81873192db62f069cc1f0c10a1580c89d19c21407dcd1cde48ad06");


byte[] actualPublicKey = libSecp256r1.keyRecovery(
dataHash.toArrayUnsafe(),
r.toByteArray(),
s.toByteArray(),
v
);

assertThat(Hexadecimals.toHexString(actualPublicKey)).isEqualTo("33F004F357282E13036385D3F52A90F5E62FA0D51C39DFF99CB72FD06CB5FAB72F2DC5E05786154DD7A349DC3FDD9BE2F0B9665C4E08FA6CDC1FD447112ACF3F");
}

@Test(expected = IllegalArgumentException.class)
public void keyRecovery_should_throw_exception_if_parameter_is_invalid() {
libSecp256r1.keyRecovery(
Expand Down

0 comments on commit 0353a6f

Please sign in to comment.