From 33f1537d9e6e8b12de4bfc22254f5e94d64ac2d0 Mon Sep 17 00:00:00 2001 From: zhanglin33 Date: Mon, 8 Apr 2019 11:13:56 +0800 Subject: [PATCH] modify the "resolveXXX" method --- .../classic/AESEncryptionFunction.java | 14 +- .../classic/ED25519SignatureFunction.java | 21 +- .../crypto/utils/classic/ED25519Utils.java | 10 + .../classic/AESEncryptionFunctionTest.java | 4 +- .../classic/ED25519SignatureFunctionTest.java | 259 ++++++++++++++++++ .../crypto/service/sm/SM2CryptoFunction.java | 28 +- .../service/sm/SM4EncryptionFunction.java | 14 +- 7 files changed, 326 insertions(+), 24 deletions(-) create mode 100644 source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/utils/classic/ED25519Utils.java diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java index c4cb74ad..a8b4078d 100644 --- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java +++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java @@ -189,8 +189,11 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction { @Override public SymmetricKey resolveSymmetricKey(byte[] symmetricKeyBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new SymmetricKey(symmetricKeyBytes); + if (supportSymmetricKey(symmetricKeyBytes)) { + return new SymmetricKey(symmetricKeyBytes); + } else { + throw new CryptoException("symmetricKeyBytes is invalid!"); + } } @Override @@ -202,8 +205,11 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction { @Override public SymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new SymmetricCiphertext(ciphertextBytes); + if (supportCiphertext(ciphertextBytes)) { + return new SymmetricCiphertext(ciphertextBytes); + } else { + throw new CryptoException("ciphertextBytes is invalid!"); + } } @Override diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java index e19779d4..ed9200f5 100644 --- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java +++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java @@ -101,8 +101,11 @@ public class ED25519SignatureFunction implements SignatureFunction { @Override public PrivKey resolvePrivKey(byte[] privKeyBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new PrivKey(privKeyBytes); + if (supportPrivKey(privKeyBytes)) { + return new PrivKey(privKeyBytes); + } else { + throw new CryptoException("privKeyBytes is invalid!"); + } } @Override @@ -115,8 +118,11 @@ public class ED25519SignatureFunction implements SignatureFunction { @Override public PubKey resolvePubKey(byte[] pubKeyBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new PubKey(pubKeyBytes); + if (supportPubKey(pubKeyBytes)) { + return new PubKey(pubKeyBytes); + } else { + throw new CryptoException("pubKeyBytes is invalid!"); + } } @Override @@ -127,8 +133,11 @@ public class ED25519SignatureFunction implements SignatureFunction { @Override public SignatureDigest resolveDigest(byte[] digestBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new SignatureDigest(digestBytes); + if (supportDigest(digestBytes)) { + return new SignatureDigest(digestBytes); + } else { + throw new CryptoException("digestBytes is invalid!"); + } } @Override diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/utils/classic/ED25519Utils.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/utils/classic/ED25519Utils.java new file mode 100644 index 00000000..563ccd28 --- /dev/null +++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/utils/classic/ED25519Utils.java @@ -0,0 +1,10 @@ +package com.jd.blockchain.crypto.utils.classic; + +/** + * @author zhanglin33 + * @title: ED25519Utils + * @description: ED25519 signature algorithm + * @date 2019-04-04, 20:01 + */ +public class ED25519Utils { +} diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java index a031a882..b841a50a 100644 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java @@ -89,8 +89,8 @@ public class AESEncryptionFunctionTest { byte[] ciphertextBytes = ciphertext.toBytes(); assertEquals(2 + 16 + 1024 , ciphertextBytes.length); CryptoAlgorithm ciphertextAlgo = ciphertext.getAlgorithm(); - assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name()); - assertEquals(algorithm.toString(),symmetricKey.getAlgorithm().toString()); + assertEquals(algorithm.name(),ciphertext.getAlgorithm().name()); + assertEquals(algorithm.toString(),ciphertext.getAlgorithm().toString()); byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo); byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java index f16bcd52..723f2818 100644 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java @@ -1,5 +1,21 @@ package test.com.jd.blockchain.crypto.service.classic; +import com.jd.blockchain.crypto.CryptoAlgorithm; +import com.jd.blockchain.crypto.CryptoServiceProviders; +import com.jd.blockchain.crypto.PrivKey; +import com.jd.blockchain.crypto.PubKey; +import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; +import com.jd.blockchain.crypto.asymmetric.SignatureDigest; +import com.jd.blockchain.crypto.asymmetric.SignatureFunction; +import com.jd.blockchain.utils.io.BytesUtils; +import org.junit.Test; + +import java.util.Random; + +import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE; +import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC; +import static org.junit.Assert.*; + /** * @author zhanglin33 * @title: ED25519SignatureFunctionTest @@ -7,4 +23,247 @@ package test.com.jd.blockchain.crypto.service.classic; * @date 2019-04-01, 14:01 */ public class ED25519SignatureFunctionTest { + @Test + public void getAlgorithmTest(){ + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); + assertNotNull(algorithm); + + SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); + + + assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name()); + assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code()); + + algorithm = CryptoServiceProviders.getAlgorithm("Ed25519"); + assertNotNull(algorithm); + + assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name()); + assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code()); + + algorithm = CryptoServiceProviders.getAlgorithm("eddsa"); + assertNull(algorithm); + } + + @Test + public void generateKeyPairTest(){ + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); + assertNotNull(algorithm); + + SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); + + + CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); + + PubKey pubKey = keyPair.getPubKey(); + PrivKey privKey = keyPair.getPrivKey(); + + assertEquals(PUBLIC.CODE,pubKey.getKeyType().CODE); + assertEquals(32, pubKey.getRawKeyBytes().length); + assertEquals(PRIVATE.CODE,privKey.getKeyType().CODE); + assertEquals(32, privKey.getRawKeyBytes().length); + + assertEquals(algorithm.name(),pubKey.getAlgorithm().name()); + assertEquals(algorithm.toString(),pubKey.getAlgorithm().toString()); + assertEquals(algorithm.name(),privKey.getAlgorithm().name()); + assertEquals(algorithm.toString(),privKey.getAlgorithm().toString()); + + assertEquals(2 + 1 + 32, pubKey.toBytes().length); + assertEquals(2 + 1 + 32, privKey.toBytes().length); + + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE}; + byte[] privKeyTypeBytes = new byte[] {PRIVATE.CODE}; + byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); + byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); + assertArrayEquals(BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawPubKeyBytes),pubKey.toBytes()); + assertArrayEquals(BytesUtils.concat(algoBytes,privKeyTypeBytes,rawPrivKeyBytes),privKey.toBytes()); + + + + } + + + + @Test + public void signTest(){ + + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); + assertNotNull(algorithm); + + SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); + + CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); + + PrivKey privKey = keyPair.getPrivKey(); + SignatureDigest signatureDigest = signatureFunction.sign(privKey,data); + + byte[] signatureBytes = signatureDigest.toBytes(); + + assertEquals(2 + 64, signatureBytes.length); + CryptoAlgorithm signatureAlgo = signatureDigest.getAlgorithm(); + assertEquals(algorithm.name(),signatureDigest.getAlgorithm().name()); + assertEquals(algorithm.toString(),signatureDigest.getAlgorithm().toString()); + + byte[] algoBytes = CryptoAlgorithm.toBytes(signatureAlgo); + byte[] rawSinatureBytes = signatureDigest.getRawDigest(); + assertArrayEquals(BytesUtils.concat(algoBytes,rawSinatureBytes),signatureBytes); + } + + @Test + public void verifyTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); + assertNotNull(algorithm); + + SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); + + CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); + + PubKey pubKey = keyPair.getPubKey(); + PrivKey privKey = keyPair.getPrivKey(); + SignatureDigest signatureDigest = signatureFunction.sign(privKey,data); + + assertTrue(signatureFunction.verify(signatureDigest,pubKey,data)); + } + + + @Test + public void supportPrivKeyTest(){ + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); + assertNotNull(algorithm); + + SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); + + + CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); + + PrivKey privKey = keyPair.getPrivKey(); + byte[] privKeyBytes = privKey.toBytes(); + + assertTrue(signatureFunction.supportPrivKey(privKeyBytes)); + + algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); + assertNotNull(algorithm); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE}; + byte[] rawKeyBytes = privKey.getRawKeyBytes(); + byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes); + + assertFalse(signatureFunction.supportPrivKey(ripemd160PubKeyBytes)); + } + +// @Test +// public void resolveSymmetricKeyTest(){ +// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes"); +// assertNotNull(algorithm); +// +// SymmetricEncryptionFunction symmetricEncryptionFunction = +// CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm); +// +// SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey(); +// byte[] symmetricKeyBytes = symmetricKey.toBytes(); +// +// SymmetricKey resolvedKey = symmetricEncryptionFunction.resolveSymmetricKey(symmetricKeyBytes); +// +// assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE); +// assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length); +// +// assertEquals(algorithm.name(),resolvedKey.getAlgorithm().name()); +// assertEquals(algorithm.toString(),resolvedKey.getAlgorithm().toString()); +// +// +// algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); +// assertNotNull(algorithm); +// byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); +// byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE}; +// byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); +// byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes); +// +// +// Class expectedException = CryptoException.class; +// Exception actualEx = null; +// try { +// symmetricEncryptionFunction.resolveSymmetricKey(ripemd160KeyBytes); +// } catch (Exception e) { +// actualEx = e; +// } +// assertNotNull(actualEx); +// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); +// } +// +// @Test +// public void supportCiphertextTest(){ +// +// byte[] data = new byte[1024]; +// Random random = new Random(); +// random.nextBytes(data); +// +// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes"); +// assertNotNull(algorithm); +// +// SymmetricEncryptionFunction symmetricEncryptionFunction = +// CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm); +// +// SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey(); +// +// Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data); +// +// byte[] ciphertextBytes = ciphertext.toBytes(); +// assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes)); +// +// algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); +// assertNotNull(algorithm); +// byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); +// byte[] rawCiphertextBytes = ciphertext.toBytes(); +// byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); +// +// assertFalse(symmetricEncryptionFunction.supportCiphertext(ripemd160CiphertextBytes)); +// } +// +// @Test +// public void resolveCiphertextTest(){ +// +// byte[] data = new byte[1024]; +// Random random = new Random(); +// random.nextBytes(data); +// +// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes"); +// assertNotNull(algorithm); +// +// SymmetricEncryptionFunction symmetricEncryptionFunction = +// CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm); +// +// SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey(); +// +// Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data); +// +// byte[] ciphertextBytes = ciphertext.toBytes(); +// assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes)); +// +// algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); +// assertNotNull(algorithm); +// byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); +// byte[] rawCiphertextBytes = ciphertext.toBytes(); +// byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); +// +// Class expectedException = CryptoException.class; +// Exception actualEx = null; +// try { +// symmetricEncryptionFunction.resolveSymmetricKey(ripemd160CiphertextBytes); +// } catch (Exception e) { +// actualEx = e; +// } +// assertNotNull(actualEx); +// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); +// } } diff --git a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java index 8a5c66a7..1c57ec37 100644 --- a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java +++ b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java @@ -95,8 +95,11 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur @Override public PrivKey resolvePrivKey(byte[] privKeyBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new PrivKey(privKeyBytes); + if (supportPrivKey(privKeyBytes)) { + return new PrivKey(privKeyBytes); + } else { + throw new CryptoException("privKeyBytes is invalid!"); + } } @Override @@ -108,8 +111,11 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur @Override public PubKey resolvePubKey(byte[] pubKeyBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new PubKey(pubKeyBytes); + if (supportPubKey(pubKeyBytes)) { + return new PubKey(pubKeyBytes); + } else { + throw new CryptoException("pubKeyBytes is invalid!"); + } } @Override @@ -121,8 +127,11 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur @Override public AsymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new AsymmetricCiphertext(ciphertextBytes); + if (supportCiphertext(ciphertextBytes)) { + return new AsymmetricCiphertext(ciphertextBytes); + } else { + throw new CryptoException("ciphertextBytes is invalid!"); + } } @Override @@ -177,8 +186,11 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur @Override public SignatureDigest resolveDigest(byte[] digestBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new SignatureDigest(digestBytes); + if (supportDigest(digestBytes)) { + return new SignatureDigest(digestBytes); + } else { + throw new CryptoException("digestBytes is invalid!"); + } } @Override diff --git a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java index 626a4512..8f7cae68 100644 --- a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java +++ b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java @@ -177,8 +177,11 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction { @Override public SymmetricKey resolveSymmetricKey(byte[] symmetricKeyBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new SymmetricKey(symmetricKeyBytes); + if (supportSymmetricKey(symmetricKeyBytes)) { + return new SymmetricKey(symmetricKeyBytes); + } else { + throw new CryptoException("symmetricKeyBytes is invalid!"); + } } @Override @@ -190,8 +193,11 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction { @Override public SymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new SymmetricCiphertext(ciphertextBytes); + if (supportCiphertext(ciphertextBytes)) { + return new SymmetricCiphertext(ciphertextBytes); + } else { + throw new CryptoException("ciphertextBytes is invalid!"); + } } @Override