sdf api
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package com.sunyard.chsm.sdf;
|
||||
|
||||
import com.sunyard.chsm.enums.AlgMode;
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import com.sunyard.chsm.enums.Padding;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -27,10 +25,10 @@ public abstract class AbstractSdfApiService implements SdfApiService {
|
||||
* 密钥长度根据 alg 指定的算法自动选择。
|
||||
* @return 密钥
|
||||
*/
|
||||
@Override
|
||||
public byte[] genSymKey(KeyAlg alg) {
|
||||
return genSymKey( alg, algKeyLen.get( alg ) );
|
||||
}
|
||||
// @Override
|
||||
// public byte[] genSymKey(KeyAlg alg) {
|
||||
// return genSymKey( alg, algKeyLen.get( alg ) );
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
@@ -40,10 +38,10 @@ public abstract class AbstractSdfApiService implements SdfApiService {
|
||||
* @param key 密钥值,明文
|
||||
* @param data 原始数据
|
||||
*/
|
||||
@Override
|
||||
public byte[] symEncrypt(KeyAlg alg, byte[] key, byte[] data){
|
||||
return symEncrypt( alg, AlgMode.ECB, Padding.PCKS5Padding, key, data );
|
||||
}
|
||||
// @Override
|
||||
// public byte[] symEncrypt(KeyAlg alg, byte[] key, byte[] data){
|
||||
// return symEncrypt( alg, AlgMode.ECB, Padding.PCKS5Padding, key, data );
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
@@ -53,9 +51,9 @@ public abstract class AbstractSdfApiService implements SdfApiService {
|
||||
* @param key 密钥值,明文
|
||||
* @param data 密文数据
|
||||
*/
|
||||
@Override
|
||||
public byte[] symDecrypt(KeyAlg alg, byte[] key, byte[] data) {
|
||||
return symDecrypt( alg, AlgMode.ECB, Padding.PCKS5Padding, key, data );
|
||||
}
|
||||
// @Override
|
||||
// public byte[] symDecrypt(KeyAlg alg, byte[] key, byte[] data) {
|
||||
// return symDecrypt( alg, AlgMode.ECB, Padding.PCKS5Padding, key, data );
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.sunyard.chsm.sdf;
|
||||
|
||||
|
||||
import com.sunyard.chsm.enums.AlgMode;
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import com.sunyard.chsm.enums.Padding;
|
||||
import com.sunyard.chsm.sdf.context.AlgId;
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
|
||||
@@ -32,23 +29,21 @@ public interface SdfApiService {
|
||||
* 禁止传 null
|
||||
* @return 对称密钥
|
||||
*/
|
||||
byte[] genSymKey(KeyAlg alg, Integer keyLen);
|
||||
|
||||
byte[] genSymKey(KeyAlg alg);
|
||||
// byte[] genSymKey(KeyAlg alg, Integer keyLen);
|
||||
//
|
||||
// byte[] genSymKey(KeyAlg alg);
|
||||
|
||||
|
||||
/**
|
||||
* 对称加密
|
||||
*
|
||||
* @param alg 算法,只支持对称算法
|
||||
* @param mode 轮模式
|
||||
* @param padding 填充模式
|
||||
* @param key 密钥值,明文
|
||||
* @param data 原始数据
|
||||
*/
|
||||
byte[] symEncrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data);
|
||||
|
||||
byte[] symEncrypt(KeyAlg alg, byte[] key, byte[] data);
|
||||
// byte[] symEncrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data);
|
||||
//
|
||||
// byte[] symEncrypt(KeyAlg alg, byte[] key, byte[] data);
|
||||
|
||||
byte[] symEncrypt(AlgId alg, byte[] key, byte[] iv, byte[] data);
|
||||
|
||||
@@ -61,9 +56,9 @@ public interface SdfApiService {
|
||||
* @param padding 填充模式
|
||||
* @param data 密文数据
|
||||
*/
|
||||
byte[] symDecrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data);
|
||||
|
||||
byte[] symDecrypt(KeyAlg alg, byte[] key, byte[] data);
|
||||
// byte[] symDecrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data);
|
||||
//
|
||||
// byte[] symDecrypt(KeyAlg alg, byte[] key, byte[] data);
|
||||
|
||||
byte[] symDecrypt(AlgId alg, byte[] key, byte[] iv, byte[] data);
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.sunyard.chsm.utils.gm.BCSM2Utils;
|
||||
import com.sunyard.chsm.utils.gm.BCSM4Utils;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
import org.bouncycastle.crypto.digests.SM3Digest;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
|
||||
@@ -23,6 +24,7 @@ import java.math.BigInteger;
|
||||
import java.security.KeyPair;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -62,7 +64,7 @@ public class BcSdfApiAdaptor implements SdfApiAdapter {
|
||||
|
||||
@Override
|
||||
public String openSession(String deviceHandle) {
|
||||
return "6975feaffaa35b31b6d4e4555ac403a1";
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -171,14 +173,9 @@ public class BcSdfApiAdaptor implements SdfApiAdapter {
|
||||
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public byte[] symEncrypt(String sessionHandle, String keyHandle, AlgId alg, byte[] pucIv, byte[] pucData) {
|
||||
byte[] symKey = getSymKey(keyHandle);
|
||||
return externalSymEncrypt(sessionHandle, symKey, alg, pucIv, pucData);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public byte[] externalSymEncrypt(String sessionHandle, byte[] symKey, AlgId alg, byte[] pucIv, byte[] pucData) {
|
||||
switch (alg) {
|
||||
case SGD_SM4_ECB:
|
||||
return BCSM4Utils.encrypt_ECB_NoPadding(symKey, pucData);
|
||||
@@ -190,14 +187,9 @@ public class BcSdfApiAdaptor implements SdfApiAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public byte[] symDecrypt(String sessionHandle, String keyHandle, AlgId alg, byte[] pucIv, byte[] pucEncData) {
|
||||
byte[] symKey = getSymKey(keyHandle);
|
||||
return externalSymDecrypt(sessionHandle, symKey, alg, pucIv, pucEncData);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public byte[] externalSymDecrypt(String sessionHandle, byte[] symKey, AlgId alg, byte[] pucIv, byte[] pucEncData) {
|
||||
switch (alg) {
|
||||
case SGD_SM4_ECB:
|
||||
return BCSM4Utils.decrypt_ECB_NoPadding(symKey, pucEncData);
|
||||
@@ -215,32 +207,34 @@ public class BcSdfApiAdaptor implements SdfApiAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public byte[] calculateMAC(String sessionHandle, String keyHandle, AlgId uiAlg, byte[] pucIv, byte[] pucData) {
|
||||
return new byte[0];
|
||||
byte[] symKey = getSymKey(keyHandle);
|
||||
return BCSM4Utils.doCBCMac(symKey, pucIv, null, pucData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] calculateMAC(String sessionHandle, byte[] symKey, AlgId uiAlg, byte[] pucIv, byte[] pucData) {
|
||||
return new byte[0];
|
||||
}
|
||||
protected static final Map<String, SM3Digest> HASH_CONTEXT = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public int hashInit(String sessionHandle, AlgId alg, EccPubKey pucPublicKey, byte[] pucID) {
|
||||
HASH_CONTEXT.put(sessionHandle, new SM3Digest());
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashUpdate(String sessionHandle, byte[] pucData) {
|
||||
Optional.ofNullable(HASH_CONTEXT.get(sessionHandle))
|
||||
.ifPresent(it -> it.update(pucData, 0, pucData.length));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] hashFinish(String sessionHandle) {
|
||||
return new byte[0];
|
||||
SM3Digest digest = HASH_CONTEXT.remove(sessionHandle);
|
||||
Assert.notNull(digest, "session异常");
|
||||
byte[] hash = new byte[digest.getDigestSize()];
|
||||
digest.doFinal(hash, 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] hashOperation(String sessionHandle, AlgId alg, EccPubKey pucPublicKey, byte[] pucID, byte[] pucData) {
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,25 +206,6 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
return pucEncData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] externalSymEncrypt(String sessionHandle, byte[] symKey, AlgId alg, byte[] pucIv, byte[] pucData) {
|
||||
int uiDataLength = pucData.length;
|
||||
byte[] pucEncData = new byte[uiDataLength];
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
int length = symKey.length;
|
||||
PointerByReference phKeyHandle = new PointerByReference();
|
||||
// 先导入密钥
|
||||
sdfLibrary.SDF_ImportKey(hSessionHandle, symKey, length, phKeyHandle);
|
||||
//加密
|
||||
Pointer hKeyHandle = phKeyHandle.getValue();
|
||||
IntByReference puiLength = new IntByReference();
|
||||
sdfLibrary.SDF_Encrypt(hSessionHandle, hKeyHandle, getAlgId(alg),
|
||||
pucIv, pucData, uiDataLength, pucEncData, puiLength);
|
||||
// 使用后销毁密钥
|
||||
sdfLibrary.SDF_DestroyKey(hSessionHandle, hKeyHandle);
|
||||
return pucEncData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] symDecrypt(String sessionHandle, String keyHandle, AlgId alg, byte[] pucIv, byte[] pucEncData) {
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
@@ -238,26 +219,6 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
return pucData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] externalSymDecrypt(String sessionHandle, byte[] symKey, AlgId alg, byte[] pucIv, byte[] pucEncData) {
|
||||
int uiEncDataLength = pucEncData.length;
|
||||
byte[] pucData = new byte[uiEncDataLength];
|
||||
|
||||
PointerByReference phKeyHandle = new PointerByReference();
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
int length = symKey.length;
|
||||
// 导入会话密钥
|
||||
sdfLibrary.SDF_ImportKey(hSessionHandle, symKey, length, phKeyHandle);
|
||||
// 解密
|
||||
Pointer hKeyHandle = phKeyHandle.getValue();
|
||||
IntByReference puiLength = new IntByReference();
|
||||
sdfLibrary.SDF_Decrypt(hSessionHandle, hKeyHandle, getAlgId(alg),
|
||||
pucIv, pucEncData, uiEncDataLength, pucData, puiLength);
|
||||
// 销毁会话密钥
|
||||
sdfLibrary.SDF_DestroyKey(hSessionHandle, hKeyHandle);
|
||||
return pucData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] calculateMAC(String sessionHandle, String keyHandle, AlgId uiAlg, byte[] pucIv, byte[] pucData) {
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
@@ -272,24 +233,6 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
return pucMac;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] calculateMAC(String sessionHandle, byte[] symKey, AlgId uiAlg, byte[] pucIv, byte[] pucData) {
|
||||
PointerByReference phKeyHandle = new PointerByReference();
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
int length = symKey.length;
|
||||
// 导入会话密钥
|
||||
sdfLibrary.SDF_ImportKey(hSessionHandle, symKey, length, phKeyHandle);
|
||||
|
||||
int uiMacLength = pucData.length;
|
||||
byte[] pucMac = new byte[uiMacLength];
|
||||
Pointer hKeyHandle = phKeyHandle.getValue();
|
||||
IntByReference puiLength = new IntByReference();
|
||||
sdfLibrary.SDF_CalculateMAC(hSessionHandle, hKeyHandle, getAlgId(uiAlg), pucIv, pucData, uiMacLength, pucMac, puiLength);
|
||||
// 销毁会话密钥
|
||||
sdfLibrary.SDF_DestroyKey(hSessionHandle, hKeyHandle);
|
||||
return pucMac;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashInit(String sessionHandle, AlgId alg, EccPubKey pucPublicKey, byte[] pucID) {
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
@@ -312,15 +255,4 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
return pucHash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] hashOperation(String sessionHandle, AlgId alg, EccPubKey pucPublicKey, byte[] pucID, byte[] pucData) {
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
byte[] pubPub = pucPublicKey == null ? null : pucPublicKey.toSdfData();
|
||||
sdfLibrary.SDF_HashInit(hSessionHandle, getAlgId(AlgId.SGD_SM3), pubPub, pucID, pucID.length);
|
||||
sdfLibrary.SDF_HashUpdate(hSessionHandle, pucData, pucData.length);
|
||||
byte[] pucHash = new byte[32];
|
||||
IntByReference puiLength = new IntByReference();
|
||||
sdfLibrary.SDF_HashFinal(hSessionHandle, pucHash, puiLength);
|
||||
return pucHash;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +120,6 @@ public interface SdfApiAdapter {
|
||||
*/
|
||||
byte[] symEncrypt(String sessionHandle, String keyHandle, AlgId alg, byte[] pucIv, byte[] pucData);
|
||||
|
||||
byte[] externalSymEncrypt(String sessionHandle, byte[] symKey, AlgId alg, byte[] pucIv, byte[] pucData);
|
||||
|
||||
/**
|
||||
* 对称解密
|
||||
*
|
||||
@@ -134,8 +132,6 @@ public interface SdfApiAdapter {
|
||||
*/
|
||||
byte[] symDecrypt(String sessionHandle, String keyHandle, AlgId alg, byte[] pucIv, byte[] pucEncData);
|
||||
|
||||
byte[] externalSymDecrypt(String sessionHandle, byte[] symKey, AlgId alg, byte[] pucIv, byte[] pucEncData);
|
||||
|
||||
/**
|
||||
* @param sessionHandle 与设备建立的会话句柄
|
||||
* @param keyHandle 指定的密钥句柄
|
||||
@@ -145,7 +141,6 @@ public interface SdfApiAdapter {
|
||||
* @return MAC值
|
||||
*/
|
||||
byte[] calculateMAC(String sessionHandle, String keyHandle, AlgId uiAlg, byte[] pucIv, byte[] pucData);
|
||||
byte[] calculateMAC(String sessionHandle, byte[] symKey, AlgId uiAlg, byte[] pucIv, byte[] pucData);
|
||||
|
||||
/**
|
||||
* 杂凑运算初始化
|
||||
@@ -175,7 +170,4 @@ public interface SdfApiAdapter {
|
||||
*/
|
||||
byte[] hashFinish(String sessionHandle);
|
||||
|
||||
|
||||
byte[] hashOperation(String sessionHandle, AlgId alg, EccPubKey pucPublicKey, byte[] pucID, byte[] pucData);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user