Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.fisco.bcos.sdk.crypto;

import java.security.KeyPair;
import org.fisco.bcos.sdk.crypto.exceptions.UnsupportedCryptoTypeException;
import org.fisco.bcos.sdk.crypto.hash.Hash;
import org.fisco.bcos.sdk.crypto.hash.Keccak256;
import org.fisco.bcos.sdk.crypto.hash.SM3Hash;
Expand All @@ -24,7 +25,6 @@
import org.fisco.bcos.sdk.crypto.signature.SM2Signature;
import org.fisco.bcos.sdk.crypto.signature.Signature;
import org.fisco.bcos.sdk.crypto.signature.SignatureResult;
import org.fisco.bcos.sdk.exceptions.UnsupportedCryptoTypeException;

public class CryptoInterface {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.exceptions;
package org.fisco.bcos.sdk.crypto.exceptions;

/** Exceptioned when calling hash. */
public class HashException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>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
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>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.
*/
package org.fisco.bcos.sdk.crypto.exceptions;

/** Exceptioned when calling hash. */
public class KeyPairException extends RuntimeException {
public KeyPairException(String message) {
super(message);
}

public KeyPairException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>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
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>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.
*/
package org.fisco.bcos.sdk.crypto.exceptions;

/** Exceptioned when calling KeyManager. */
public class LoadKeyStoreException extends RuntimeException {
public LoadKeyStoreException(String message) {
super(message);
}

public LoadKeyStoreException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.exceptions;
package org.fisco.bcos.sdk.crypto.exceptions;

/** Exceptioned when calling signature related functions. */
public class SignatureException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.exceptions;
package org.fisco.bcos.sdk.crypto.exceptions;

/** Exceptioned when calling CryptoInterface. */
public class UnsupportedCryptoTypeException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.webank.wedpr.crypto.CryptoResult;
import com.webank.wedpr.crypto.NativeInterface;
import org.fisco.bcos.sdk.exceptions.HashException;
import org.fisco.bcos.sdk.crypto.exceptions.HashException;
import org.fisco.bcos.sdk.utils.Hex;

public class Keccak256 implements Hash {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/fisco/bcos/sdk/crypto/hash/SM3Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.webank.wedpr.crypto.CryptoResult;
import com.webank.wedpr.crypto.NativeInterface;
import org.fisco.bcos.sdk.exceptions.HashException;
import org.fisco.bcos.sdk.crypto.exceptions.HashException;
import org.fisco.bcos.sdk.utils.Hex;

public class SM3Hash implements Hash {
Expand Down
113 changes: 90 additions & 23 deletions src/main/java/org/fisco/bcos/sdk/crypto/keypair/CryptoKeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@
import java.util.Objects;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.fisco.bcos.sdk.crypto.exceptions.KeyPairException;
import org.fisco.bcos.sdk.crypto.hash.Hash;
import org.fisco.bcos.sdk.utils.Hex;
import org.fisco.bcos.sdk.utils.Numeric;
import org.fisco.bcos.sdk.utils.Strings;
import org.fisco.bcos.sdk.utils.exceptions.DecoderException;

public abstract class CryptoKeyPair {
public static final int ADDRESS_SIZE = 160;
public static final int ADDRESS_LENGTH_IN_HEX = ADDRESS_SIZE >> 2;

public static final int PUBLIC_KEY_SIZE = 64;
public static final int PUBLIC_KEY_LENGTH_IN_HEX = PUBLIC_KEY_SIZE << 1;

private BigInteger privateKey;
protected BigInteger publicKey;

protected String hexPrivateKey;
protected String hexPublicKey;
public KeyPair keyPair;

protected Hash hashImpl;

public CryptoKeyPair() {}

public CryptoKeyPair(final BigInteger privateKey) {
Expand Down Expand Up @@ -61,13 +75,10 @@ public CryptoKeyPair(KeyPair keyPair) {
this.publicKey =
new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));
calculateHexedKeyPair();
// Note: In the current version of sm2 verification, the public key prefix must contain 04,
// otherwise an error will be reported
this.hexPublicKey = "04" + this.hexPublicKey;
}

private void calculateHexedKeyPair() {
this.hexPrivateKey = this.privateKey.toString(16);
this.hexPublicKey = this.publicKey.toString(16);
}

/**
* get CryptoKeyPair information from CryptoResult
*
Expand All @@ -77,7 +88,14 @@ private void calculateHexedKeyPair() {
this.hexPrivateKey = nativeResult.privteKey;
this.hexPublicKey = nativeResult.publicKey;
this.privateKey = new BigInteger(this.hexPrivateKey, 16);
this.publicKey = new BigInteger(this.hexPublicKey, 16);
// Note: The generated publicKey is prefixed with 04, When converting it to BigInteger, need
// to remove 04
this.publicKey = new BigInteger(this.hexPublicKey.substring(2), 16);
}

private void calculateHexedKeyPair() {
this.hexPrivateKey = this.privateKey.toString(16);
this.hexPublicKey = this.publicKey.toString(16);
}

public BigInteger getPrivateKey() {
Expand All @@ -96,22 +114,9 @@ public String getHexPublicKey() {
return hexPublicKey;
}

/**
* todo: get the public key from the given private key
*
* @param privateKey
* @return: the public key calculated from the private key public abstract BigInteger
* privateKeyToPublic(BigInteger privateKey);
*/

/**
* generate keyPair randomly
*
* @return: the generated keyPair
*/
public abstract CryptoKeyPair generateKeyPair();

public abstract CryptoKeyPair createKeyPair(KeyPair keyPair);
public KeyPair getKeyPair() {
return this.keyPair;
}

@Override
public boolean equals(Object o) {
Expand All @@ -126,4 +131,66 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(privateKey, publicKey);
}

/**
* generate keyPair randomly
*
* @return: the generated keyPair
*/
public abstract CryptoKeyPair generateKeyPair();

public abstract CryptoKeyPair createKeyPair(KeyPair keyPair);

protected String getPublicKeyNoPrefix(String publicKeyStr) {
String publicKeyNoPrefix = Numeric.cleanHexPrefix(publicKeyStr);
// Hexadecimal public key length is less than 128, add 0 in front
if (publicKeyNoPrefix.length() < PUBLIC_KEY_LENGTH_IN_HEX) {
publicKeyNoPrefix =
Strings.zeros(PUBLIC_KEY_LENGTH_IN_HEX - publicKeyNoPrefix.length())
+ publicKeyNoPrefix;
}
return publicKeyNoPrefix;
}
/**
* get the address according to the public key
*
* @return: the hexed address calculated from the publicKey
*/
public String getAddress() {
// Note: The generated publicKey is prefixed with 04, When calculate the address, need to
// remove 04
return getAddress(this.getHexPublicKey().substring(2));
}
/**
* calculate the address according to the given public key
*
* @param publicKey: the Hexed publicKey that need to calculate address
* @return
*/
public String getAddress(String publicKey) {
try {
String publicKeyNoPrefix = getPublicKeyNoPrefix(publicKey);
// calculate hash for the public key
String publicKeyHash = Hex.toHexString(hashImpl.hash(Hex.decode(publicKeyNoPrefix)));
// right most 160 bits
return publicKeyHash.substring(publicKeyHash.length() - ADDRESS_LENGTH_IN_HEX);
} catch (DecoderException e) {
throw new KeyPairException(
"getAddress for "
+ publicKey
+ "failed, the publicKey param must be hex string, error message: "
+ e.getMessage(),
e);
}
}

public byte[] getAddress(byte[] publicKey) {
byte[] hash = hashImpl.hash(publicKey);
return Arrays.copyOfRange(hash, hash.length - 20, hash.length); // right most 160 bits
}

public byte[] getAddress(BigInteger publicKey) {
byte[] publicKeyBytes = Numeric.toBytesPadded(publicKey, PUBLIC_KEY_SIZE);
return getAddress(publicKeyBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@
import com.webank.wedpr.crypto.NativeInterface;
import java.math.BigInteger;
import java.security.KeyPair;
import org.fisco.bcos.sdk.crypto.hash.Keccak256;

public class ECDSAKeyPair extends CryptoKeyPair {

public ECDSAKeyPair() {}
public ECDSAKeyPair() {
hashImpl = new Keccak256();
}

public ECDSAKeyPair(BigInteger privateKey) {
super(privateKey);
hashImpl = new Keccak256();
}

public ECDSAKeyPair(final BigInteger privateKey, final BigInteger publicKey) {
super(privateKey, publicKey);
hashImpl = new Keccak256();
}

public ECDSAKeyPair(KeyPair javaKeyPair) {
super(javaKeyPair);
hashImpl = new Keccak256();
}

protected ECDSAKeyPair(final CryptoResult ecKeyPairInfo) {
super(ecKeyPairInfo);
hashImpl = new Keccak256();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
import com.webank.wedpr.crypto.CryptoResult;
import com.webank.wedpr.crypto.NativeInterface;
import java.security.KeyPair;
import org.fisco.bcos.sdk.crypto.hash.SM3Hash;

public class SM2KeyPair extends CryptoKeyPair {
public SM2KeyPair() {}
public SM2KeyPair() {
hashImpl = new SM3Hash();
}

public SM2KeyPair(KeyPair javaKeyPair) {
super(javaKeyPair);
hashImpl = new SM3Hash();
}

protected SM2KeyPair(CryptoResult sm2keyPairInfo) {
super(sm2keyPairInfo);
hashImpl = new SM3Hash();
}

/**
Expand Down
Loading