init web
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package com.sunyard.chsm.sdf;
|
||||
|
||||
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPriKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bouncycastle.asn1.gm.GMNamedCurves;
|
||||
import org.bouncycastle.asn1.gm.GMObjectIdentifiers;
|
||||
import org.bouncycastle.asn1.x9.X9ECParameters;
|
||||
import org.bouncycastle.crypto.digests.SM3Digest;
|
||||
import org.bouncycastle.crypto.macs.HMac;
|
||||
import org.bouncycastle.crypto.params.KeyParameter;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.jce.spec.ECParameterSpec;
|
||||
import org.bouncycastle.util.BigIntegers;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/23
|
||||
*/
|
||||
@Service
|
||||
public class BCSdfApiService implements SdfApiService {
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] generateRandom(int len) {
|
||||
byte[] res = new byte[len];
|
||||
new SecureRandom().nextBytes(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public EccKey genKeyPairEcc() {
|
||||
|
||||
// 获取SM2参数
|
||||
X9ECParameters sm2Params = GMNamedCurves.getByOID(GMObjectIdentifiers.sm2p256v1);
|
||||
ECParameterSpec sm2Spec = new ECParameterSpec(sm2Params.getCurve(), sm2Params.getG(), sm2Params.getN(), sm2Params.getH());
|
||||
|
||||
// 创建密钥对生成器
|
||||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
|
||||
keyPairGenerator.initialize(sm2Spec);
|
||||
|
||||
// 生成密钥对
|
||||
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||
BCECPublicKey pubKey = (BCECPublicKey) keyPair.getPublic();
|
||||
BCECPrivateKey priKey = (BCECPrivateKey) keyPair.getPrivate();
|
||||
byte[] x = pubKey.getQ().getXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getYCoord().getEncoded();
|
||||
byte[] d = BigIntegers.asUnsignedByteArray(64, priKey.getD());
|
||||
|
||||
return new EccKey(new EccPubKey(256, x, y), new EccPriKey(256, d));
|
||||
}
|
||||
|
||||
@Override
|
||||
public EccKey genEccKeyPairAndEncByKek() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] calculateMAC(byte[] symKey, byte[] pucIv, byte[] pucData) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] hmac(byte[] key, byte[] srcData) {
|
||||
KeyParameter keyParameter = new KeyParameter(key);
|
||||
SM3Digest digest = new SM3Digest();
|
||||
HMac mac = new HMac(digest);
|
||||
mac.init(keyParameter);
|
||||
mac.update(srcData, 0, srcData.length);
|
||||
byte[] result = new byte[mac.getMacSize()];
|
||||
mac.doFinal(result, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] hash(byte[] pucData) {
|
||||
SM3Digest digest = new SM3Digest();
|
||||
digest.update(pucData, 0, pucData.length);
|
||||
byte[] hash = new byte[digest.getDigestSize()];
|
||||
digest.doFinal(hash, 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.sunyard.chsm.sdf;
|
||||
|
||||
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/23
|
||||
*/
|
||||
public interface SdfApiService {
|
||||
|
||||
|
||||
/**
|
||||
* 产生随机数
|
||||
*
|
||||
* @param len 随机数长度
|
||||
* @return 返回随机数
|
||||
*/
|
||||
byte[] generateRandom(int len);
|
||||
|
||||
/**
|
||||
* 产生ECC密钥对并输出
|
||||
*
|
||||
* @return pubKey 返回公钥 | priKey 返回私钥
|
||||
*/
|
||||
EccKey genKeyPairEcc();
|
||||
|
||||
/**
|
||||
* 产生ECC加密密钥对并使用kek加密私钥输出
|
||||
*
|
||||
* @return pubKey 返回公钥 | priKey 返回私钥
|
||||
*/
|
||||
EccKey genEccKeyPairAndEncByKek();
|
||||
|
||||
|
||||
/**
|
||||
* 计算MAC
|
||||
*
|
||||
* @param symKey 用户指定的密钥
|
||||
* @param pucIv 缓冲区指针,用于存放输入和返回的IV数据
|
||||
* @param pucData 缓冲区指针,用于存放输入的数据明文
|
||||
* @return pucEncData 返回MAC值 | puiLength 返回MAC值长度
|
||||
*/
|
||||
byte[] calculateMAC(byte[] symKey, byte[] pucIv, byte[] pucData);
|
||||
|
||||
byte[] hmac(byte[] key, byte[] srcData);
|
||||
|
||||
/**
|
||||
* 多包杂凑运算
|
||||
*
|
||||
* @param pucData 缓冲区指针,用于存放输入的数据明文
|
||||
* @return hash值
|
||||
*/
|
||||
byte[] hash(byte[] pucData);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/10/11
|
||||
*/
|
||||
@Data
|
||||
public class DeviceInfo {
|
||||
|
||||
private String issuerName;
|
||||
private String deviceName;
|
||||
private String deviceSerial;
|
||||
private int deviceVersion;
|
||||
private int standardVersion;
|
||||
private int[] asymAlgAbility;
|
||||
private int symAlgAbility;
|
||||
private int hashAlgAbility;
|
||||
private int bufferSize;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EccCipher {
|
||||
|
||||
// X 分量
|
||||
private byte[] x;
|
||||
// Y 分量
|
||||
private byte[] y;
|
||||
// 明文的 SM3 杂凑值
|
||||
private byte[] M;
|
||||
// 密文数据长度
|
||||
private int L;
|
||||
// 密文数据
|
||||
private byte[] C;
|
||||
|
||||
|
||||
public byte[] getC1C3C2Bytes() {
|
||||
int xLen = x.length;
|
||||
int yLen = y.length;
|
||||
int mLen = M.length;
|
||||
|
||||
byte[] rawData = new byte[xLen + yLen + mLen + L];
|
||||
|
||||
System.arraycopy(x, 0, rawData, 0, xLen);
|
||||
System.arraycopy(y, 0, rawData, xLen, yLen);
|
||||
System.arraycopy(M, 0, rawData, xLen + yLen, mLen);
|
||||
System.arraycopy(C, 0, rawData, xLen + yLen + mLen, L);
|
||||
|
||||
return rawData;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class EccKey {
|
||||
|
||||
private EccPubKey pubKey;
|
||||
private EccPriKey priKey;
|
||||
|
||||
public EccKey(EccPubKey pubKey, EccPriKey priKey) {
|
||||
this.pubKey = pubKey;
|
||||
this.priKey = priKey;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EccPriKey {
|
||||
// 密 钥 位 长
|
||||
private int bits;
|
||||
// 私 钥
|
||||
private byte[] D;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EccPubKey {
|
||||
// 密 钥 位 长
|
||||
private int bits;
|
||||
// 公 钥 x 坐 标
|
||||
private byte[] x;
|
||||
// 公 钥 y 坐 标
|
||||
private byte[] y;
|
||||
|
||||
|
||||
public byte[] getPubKeyBytes() {
|
||||
byte[] rawKey = new byte[x.length + y.length];
|
||||
System.arraycopy(x, 0, rawKey, 0, x.length);
|
||||
System.arraycopy(y, 0, rawKey, x.length, y.length);
|
||||
return rawKey;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EccSignature {
|
||||
|
||||
// 签名的 r 部分
|
||||
private byte[] r;
|
||||
|
||||
// 签名的 s 部分
|
||||
private byte[] s;
|
||||
|
||||
public byte[] getRawSignBytes() {
|
||||
byte[] signData = new byte[r.length + s.length];
|
||||
System.arraycopy(r, 0, signData, 0, r.length);
|
||||
System.arraycopy(s, 0, signData, r.length, s.length);
|
||||
return signData;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user