-
Notifications
You must be signed in to change notification settings - Fork 879
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 hard coded elliptic curve lengths #1967
Closed
daniel-iobuilders
wants to merge
6
commits into
hyperledger:master
from
daniel-iobuilders:remove_hard_ec_lengths
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
43deaf5
Removed hardcoded key lengths and added test with different key lengt…
daniel-iobuilders 00e8706
added missing javadoc parameter
daniel-iobuilders 458d49e
Added missing spaces to string. Remove fixed byte size in comment
daniel-iobuilders 11f551d
Removed fixed byte size from Packet class
daniel-iobuilders f6a00c1
Replaced Bytes32 in SignatureAlgorithm with Bytes
daniel-iobuilders 4a9ef40
WIP: Added SECP384R1 crypto algorithm
daniel-iobuilders File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Removed hardcoded key lengths and added test with different key lengt…
…hs and elliptic curves. Signed-off-by: Daniel Lehrner <daniel@io.builders>
- Loading branch information
commit 43deaf5986be00460939f342a49e88884777084b
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
crypto/src/main/java/org/hyperledger/besu/crypto/SECPKeyUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.crypto; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.MutableBytes; | ||
import org.bouncycastle.crypto.params.ECDomainParameters; | ||
import org.bouncycastle.math.ec.ECPoint; | ||
import org.bouncycastle.math.ec.FixedPointCombMultiplier; | ||
|
||
public class SECPKeyUtil { | ||
|
||
public static Bytes toBytes(final BigInteger key, final int byteLength) { | ||
final byte[] backing = key.toByteArray(); | ||
|
||
if (backing.length == byteLength) { | ||
return Bytes.wrap(backing); | ||
} | ||
|
||
// if the input is too long it is shifted to the left | ||
if (backing.length > byteLength) { | ||
return Bytes.wrap(backing, backing.length - byteLength, byteLength); | ||
} | ||
|
||
// if the input is too short it is shifted to the right | ||
final MutableBytes res = MutableBytes.create(byteLength); | ||
Bytes.wrap(backing).copyTo(res, byteLength - backing.length); | ||
return res; | ||
} | ||
|
||
public static Bytes toPublicKey(final SECPPrivateKey privateKey, final ECDomainParameters curve) { | ||
BigInteger privKey = privateKey.getEncodedBytes().toUnsignedBigInteger(); | ||
|
||
// ECDSA public keys are twice as long as the private keys | ||
final int publicKeyLength = privateKey.getEncodedBytes().size() * 2; | ||
|
||
/* | ||
* TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group | ||
* order, but that could change in future versions. | ||
*/ | ||
if (privKey.bitLength() > curve.getN().bitLength()) { | ||
privKey = privKey.mod(curve.getN()); | ||
} | ||
|
||
final ECPoint point = new FixedPointCombMultiplier().multiply(curve.getG(), privKey); | ||
|
||
return Bytes.wrap( | ||
// point.getEncoded returns as its first byte a constant prefix and the rest of the | ||
// bytes are the public key. | ||
// The to parameter of Arrays.copyOfRange must the publicKeyLength + 1, because it is | ||
// exclusive | ||
Arrays.copyOfRange(point.getEncoded(false), 1, publicKeyLength + 1)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The comment needs to be changed to reflect it might not be 65 anymore :)