identity base test

This commit is contained in:
ghy
2021-03-09 16:25:07 +08:00
parent 755ddc2129
commit 9827414ac5
4 changed files with 107 additions and 8 deletions
@@ -13,6 +13,8 @@ import minsecurity.crypto.sm2.SM2PrivateKey;
import minsecurity.crypto.sm2.SM2PublicKey;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.slf4j.LoggerFactory;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
@@ -22,6 +24,7 @@ import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Arrays;
import java.util.Base64;
/*
@@ -32,7 +35,7 @@ import java.util.Base64;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Identity {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Identity.class);
private String Name;
private KeyParam KeyParam;
private PrivateKeyInterface Prikey;
@@ -108,6 +111,7 @@ public class Identity {
ObjectMapper mapper = new ObjectMapper();
InnerIdentity innerIdentity = IdentityUtil.parseIdentityToInner(this);
byte[] bytesOfInnerIdentity = mapper.writeValueAsBytes(innerIdentity);
// logger.debug(ByteUtils.toHexString(bytesOfInnerIdentity));
if(passwd != null && passwd.length() != 0) {
byte[] sm4key = KeyUtils.get16bytePasswd(passwd.getBytes());
byte[] cipher = SM4.encrypt_CBC_Padding(sm4key, new byte[16], bytesOfInnerIdentity);
@@ -119,7 +123,7 @@ public class Identity {
public static Identity load(byte[] data, String passwd)
throws BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException,
NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException,
InvalidKeyException, IOException, CertException {
InvalidKeyException, CertException {
byte[] bytesOfIdentity = Base64.getDecoder().decode(data);
byte[] plain;
if(passwd != null && passwd.length() != 0){
@@ -129,7 +133,13 @@ public class Identity {
plain = bytesOfIdentity;
}
ObjectMapper mapper = new ObjectMapper();
InnerIdentity innerIdentity = mapper.readValue(plain, InnerIdentity.class);
InnerIdentity innerIdentity = null;
// logger.debug(ByteUtils.toHexString(plain));
try {
innerIdentity = mapper.readValue(plain, InnerIdentity.class);
} catch (IOException e) {
e.printStackTrace();
}
return IdentityUtil.parseInnerToIdentity(innerIdentity);
}
// TODO: passwd ???
@@ -230,6 +240,16 @@ public class Identity {
Cert = cert;
}
@Override
public String toString() {
return "Identity{" +
"Name='" + Name + '\'' +
", KeyParam=" + KeyParam +
", Prikey=" + Prikey +
", PrikeyRawByte=" + (PrikeyRawByte == null ? "null":ByteUtils.toHexString(PrikeyRawByte)) +
", Pubkey=" + Pubkey +
", Passwd='" + Passwd + '\'' +
", Cert=" + Cert +
'}';
}
}
@@ -35,7 +35,7 @@ public class IdentityUtil {
innerIdentity.setPrikey(identity.getPrikey().getBytes());
}
if(identity.getPubkey() != null){
innerIdentity.setPrikey(identity.getPubkey().getBytes());
innerIdentity.setPubkey(identity.getPubkey().getBytes());
}
innerIdentity.setPasswd(identity.getPasswd());
innerIdentity.setCert(CertUtils.toPem(identity.getCert(), identity.getPasswd().getBytes(), Common.SM4CBC));
@@ -85,8 +85,8 @@ public class InnerIdentity {
return "InnerIdentity{" +
"Name='" + Name + '\'' +
", KeyParam=" + KeyParam +
", Prikey=" + ByteUtils.toHexString(Prikey) +
", Pubkey=" + ByteUtils.toHexString(Pubkey) +
", Prikey=" + (Prikey == null ? "null" : ByteUtils.toHexString(Prikey)) +
", Pubkey=" + (Pubkey == null ? "null" : ByteUtils.toHexString(Pubkey)) +
", Passwd='" + Passwd + '\'' +
", Cert='" + Cert + '\'' +
'}';
@@ -0,0 +1,79 @@
package minsecurity.identity;
import minsecurity.Common;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.TestSM2;
import minsecurity.crypto.sm2.SM2Base;
import minsecurity.crypto.sm2.SM2PrivateKey;
import minsecurity.crypto.sm2.SM2PublicKey;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.junit.Test;
import static org.junit.Assert.*;
import org.slf4j.LoggerFactory;
/*
* @Author: hongyu guo
* @Description:
* @Version: 1.0.0
* @Date: 15:24 2021/03/09
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TestIdentity {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
@Test
public void testIdentity1() throws Exception {
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
byte[] d = priKey.getD().toByteArray();
// d = Arrays.copyOf(d,32);
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
// BigInteger bigInteger = priKey.getD();
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null);
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
Common.SM3withSM2, Common.SM2, "root", "root",
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
Common.CertSign, true, System.currentTimeMillis());
CertUtils.signCert(certificate, sm2PrivateKey);
identity.setCert(certificate);
String text = "2020-03-09";
// test sign and verify
byte[] signature = identity.sign(text.getBytes());
boolean flag = identity.verify(text.getBytes(), signature);
logger.debug("identity verify: {}", flag);
assertTrue(flag);
// test enc and dec
byte[] cipher = identity.encrypt(text.getBytes());
byte[] bytesOfDec = identity.decrypt(cipher);
assertArrayEquals(bytesOfDec, text.getBytes());
// test dump and load
byte[] bytesOfDump = identity.dump(identity.getPasswd());
Identity idFromBytes = Identity.load(bytesOfDump, identity.getPasswd());
assertEquals(ByteUtils.toHexString(identity.getPrikey().getBytes()), ByteUtils.toHexString(idFromBytes.getPrikey().getBytes()));
assertEquals(ByteUtils.toHexString(identity.getPubkey().getBytes()), ByteUtils.toHexString(idFromBytes.getPubkey().getBytes()));
// test dump and load without passwd
bytesOfDump = identity.dump(null);
idFromBytes = Identity.load(bytesOfDump, null);
assertEquals(ByteUtils.toHexString(identity.getPrikey().getBytes()), ByteUtils.toHexString(idFromBytes.getPrikey().getBytes()));
assertEquals(ByteUtils.toHexString(identity.getPubkey().getBytes()), ByteUtils.toHexString(idFromBytes.getPubkey().getBytes()));
}
}