修改测试代码

This commit is contained in:
cheney 2025-07-11 15:28:25 +08:00
parent 6f5279c744
commit b075246612
3 changed files with 51 additions and 56 deletions

View File

@ -1,6 +1,7 @@
package com.sunyard.sge.database; package com.sunyard.sge.database;
public interface ISM3 { public interface ISM3 {
public void update(byte[] pcData); void update(byte[] pcData);
public byte[] digest();
byte[] digest();
} }

View File

@ -1,7 +1,7 @@
package com.sunyard.sge.database; package com.sunyard.sge.database;
public interface ISM4Mac { public interface ISM4Mac {
public void update(byte[] pcData); void update(byte[] pcData);
public byte[] digest(); byte[] digest();
public boolean checkDigest(byte[] mac); boolean checkDigest(byte[] mac);
} }

View File

@ -1,13 +1,9 @@
package com.sunyard.sge.database; package com.sunyard.sge.database;
import com.sun.jna.ptr.IntByReference;
import com.sunyard.SydApiException; import com.sunyard.SydApiException;
import com.sunyard.proto.Util; import com.sunyard.proto.Util;
import com.sunyard.sge.bytes.BytesUtil;
import com.sunyard.sge.database.pool.HsmLinkInfo; import com.sunyard.sge.database.pool.HsmLinkInfo;
import com.sunyard.sge.log.LogFactory; import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.plus.ConnectHandlerManager;
import com.sunyard.sge.plus.SydLibrary;
import com.sunyard.sge.pool.ObjectInPool; import com.sunyard.sge.pool.ObjectInPool;
import com.sunyard.util.SYMUtil; import com.sunyard.util.SYMUtil;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -48,8 +44,8 @@ public class SydApiBaseFunction implements SydApi {
this.oip = oip; this.oip = oip;
} }
private void updateLastUseTime(){ private void updateLastUseTime() {
if ( null != this.oip ) { if (null != this.oip) {
this.oip.updateLastUseTime(); this.oip.updateLastUseTime();
} }
} }
@ -130,10 +126,10 @@ public class SydApiBaseFunction implements SydApi {
} }
if (null == pcData) { if (null == pcData) {
throw new SydApiException("输入内容为 null",19); throw new SydApiException("输入内容为 null", 19);
} }
if ( pcData.length > 1024 ) { if (pcData.length > 1024) {
throw new SydApiException("输入内容为空",19); throw new SydApiException("输入内容为空", 19);
} }
SydApiException le = new SydApiException(-1); SydApiException le = new SydApiException(-1);
@ -175,7 +171,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -242,7 +238,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -251,7 +247,7 @@ public class SydApiBaseFunction implements SydApi {
@Override @Override
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) { public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
Logger log = LogFactory.getLogger(); Logger log = LogFactory.getLogger();
if ( iKeyIndex < 0 ) { if (iKeyIndex < 0) {
throw new SydApiException(0x01000001); throw new SydApiException(0x01000001);
} }
if (LogFactory.iLogLevel == 3) { if (LogFactory.iLogLevel == 3) {
@ -264,11 +260,11 @@ public class SydApiBaseFunction implements SydApi {
throw new SydApiException(-1); throw new SydApiException(-1);
} }
if (Consts.ECB_ENC == iFlag) { if (Consts.ECB_ENC == iFlag) {
if ( 0 == pcData.length || pcData.length > 1024 ) { if (0 == pcData.length || pcData.length > 1024) {
throw new SydApiException("输入内容长度错误",0x01000001); throw new SydApiException("输入内容长度错误", 0x01000001);
} }
} else { } else {
if ( 0 == pcData.length || pcData.length % 16 != 0 || pcData.length > 1040 ) { if (0 == pcData.length || pcData.length % 16 != 0 || pcData.length > 1040) {
throw new SydApiException("输入密文数据长度错误", 0x01000014); throw new SydApiException("输入密文数据长度错误", 0x01000014);
} }
} }
@ -329,7 +325,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -342,7 +338,7 @@ public class SydApiBaseFunction implements SydApi {
log.debug("SYD_SM4_BatchData方法接收参数iKeyIndex{},pcData元素个数为{},iFlag{}", iKeyIndex, pcData.size(), iFlag); log.debug("SYD_SM4_BatchData方法接收参数iKeyIndex{},pcData元素个数为{},iFlag{}", iKeyIndex, pcData.size(), iFlag);
} }
if ( null == pcData ) { if (null == pcData) {
throw new SydApiException("输入参数错误", 0x01000001); throw new SydApiException("输入参数错误", 0x01000001);
} }
@ -352,13 +348,13 @@ public class SydApiBaseFunction implements SydApi {
if (iFlag == Consts.ECB_DEC) { if (iFlag == Consts.ECB_DEC) {
for (byte[] pcdata : pcData) { for (byte[] pcdata : pcData) {
if ( pcdata.length == 0 || pcdata.length % 16 != 0 || pcdata.length > 1040) { if (pcdata.length == 0 || pcdata.length % 16 != 0 || pcdata.length > 1040) {
throw new SydApiException(0x01000015); throw new SydApiException(0x01000015);
} }
} }
} else { } else {
for (byte[] pcdata : pcData) { for (byte[] pcdata : pcData) {
if ( pcdata.length > 1024) { if (pcdata.length > 1024) {
throw new SydApiException(19); throw new SydApiException(19);
} }
} }
@ -371,7 +367,7 @@ public class SydApiBaseFunction implements SydApi {
String[] keys = new String[iKeyIndex.length]; String[] keys = new String[iKeyIndex.length];
updateLastUseTime(); updateLastUseTime();
for (int i = 0; i < iKeyIndex.length; i++) { for (int i = 0; i < iKeyIndex.length; i++) {
if ( iKeyIndex[i] < 0 ) { if (iKeyIndex[i] < 0) {
throw new SydApiException(0x01000001); throw new SydApiException(0x01000001);
} }
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
@ -418,7 +414,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -431,7 +427,7 @@ public class SydApiBaseFunction implements SydApi {
log.debug("SYD_SM4_LongData方法接收参数iKeyIndex{},pcData{},iPkgNum{},iFlag{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag); log.debug("SYD_SM4_LongData方法接收参数iKeyIndex{},pcData{},iPkgNum{},iFlag{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag);
} }
if ( iKeyIndex < 0 ) { if (iKeyIndex < 0) {
throw new SydApiException(0x01000001); throw new SydApiException(0x01000001);
} }
@ -495,7 +491,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} catch (SydApiException e) { } catch (SydApiException e) {
if (null==e){ if (null == e) {
} }
le = e; le = e;
@ -508,7 +504,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -532,7 +528,7 @@ public class SydApiBaseFunction implements SydApi {
String[] keys = new String[iKeyIndex.length]; String[] keys = new String[iKeyIndex.length];
updateLastUseTime(); updateLastUseTime();
for (int i = 0; i < iKeyIndex.length; i++) { for (int i = 0; i < iKeyIndex.length; i++) {
if ( iKeyIndex[i] < 0 ) { if (iKeyIndex[i] < 0) {
throw new SydApiException(0x01000001); throw new SydApiException(0x01000001);
} }
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
@ -550,7 +546,7 @@ public class SydApiBaseFunction implements SydApi {
byte[] mac = new byte[16]; byte[] mac = new byte[16];
byte[] buff = list.get(i); byte[] buff = list.get(i);
System.arraycopy(buff, buff.length - 16, mac, 0, 16); System.arraycopy(buff, buff.length - 16, mac, 0, 16);
ret.add( mac ); ret.add(mac);
} }
return ret; return ret;
@ -571,7 +567,7 @@ public class SydApiBaseFunction implements SydApi {
byte[] mac = new byte[16]; byte[] mac = new byte[16];
byte[] buff = list.get(i); byte[] buff = list.get(i);
System.arraycopy(buff, buff.length - 16, mac, 0, 16); System.arraycopy(buff, buff.length - 16, mac, 0, 16);
ret.add( mac ); ret.add(mac);
} }
return ret; return ret;
@ -586,7 +582,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -662,7 +658,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -752,7 +748,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -787,7 +783,7 @@ public class SydApiBaseFunction implements SydApi {
log.debug("SYD_SM4Mac_ShortData方法接收参数iKeyIndex{},pcData{},iFlag{}", iKeyIndex, Arrays.toString(pcData)); log.debug("SYD_SM4Mac_ShortData方法接收参数iKeyIndex{},pcData{},iFlag{}", iKeyIndex, Arrays.toString(pcData));
} }
if ( null == pcData || pcData.length > limitDataLen ) { if (null == pcData || pcData.length > limitDataLen) {
throw new SydApiException("输入内容长度错误", 0x01000001); throw new SydApiException("输入内容长度错误", 0x01000001);
} }
@ -818,9 +814,8 @@ public class SydApiBaseFunction implements SydApi {
} }
throw e; throw e;
} }
} } else {
else { synchronized (api) {
synchronized (api){
try { try {
return api.SYD_SM4Mac_ShortData(keyIndex, pcData); return api.SYD_SM4Mac_ShortData(keyIndex, pcData);
@ -836,7 +831,7 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) { if (!this.isShortLinkMode) {
this.oip.setError(); this.oip.setError();
} }
throw le; throw le;
@ -854,8 +849,8 @@ public class SydApiBaseFunction implements SydApi {
log.debug("SYD_SM4Mac_ShortData 方法接收参数:{}", iKeyIndex, Util.bytes2HexString(pcData), Util.bytes2HexString(pcMac)); log.debug("SYD_SM4Mac_ShortData 方法接收参数:{}", iKeyIndex, Util.bytes2HexString(pcData), Util.bytes2HexString(pcMac));
} }
if ( null == pcData || pcData.length > limitDataLen ) { if (null == pcData || pcData.length > limitDataLen) {
throw new SydApiException("输入内容超长",19); throw new SydApiException("输入内容超长", 19);
} }
if (null == this.hsms || this.hsms.length < 1) { if (null == this.hsms || this.hsms.length < 1) {
@ -874,6 +869,7 @@ public class SydApiBaseFunction implements SydApi {
/** /**
* 非依赖 C 实现仅长连接依赖 C * 非依赖 C 实现仅长连接依赖 C
*
* @param iEncKeyIndex输入加密所需要的密钥索引值 * @param iEncKeyIndex输入加密所需要的密钥索引值
* @param iMacKeyIndex输入计算mac所需要的密钥索引值 * @param iMacKeyIndex输入计算mac所需要的密钥索引值
* @param pcData 输入计算mac所需要的数据 * @param pcData 输入计算mac所需要的数据
@ -893,7 +889,6 @@ public class SydApiBaseFunction implements SydApi {
} }
@Override @Override
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] mac) { public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] mac) {
Logger log = LogFactory.getLogger(); Logger log = LogFactory.getLogger();
@ -905,7 +900,7 @@ public class SydApiBaseFunction implements SydApi {
byte[] data = null; byte[] data = null;
try { try {
data = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_DEC); data = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_DEC);
}catch (Exception e){ } catch (Exception e) {
return new DataAndMacCheck(null, ret); return new DataAndMacCheck(null, ret);
} }
return new DataAndMacCheck(data, ret); return new DataAndMacCheck(data, ret);
@ -919,11 +914,11 @@ public class SydApiBaseFunction implements SydApi {
} }
if (null == this.hsms || null == iMacKeyIndex || null == pcData) { if (null == this.hsms || null == iMacKeyIndex || null == pcData) {
throw new SydApiException("输入内容为空",0x01000001); throw new SydApiException("输入内容为空", 0x01000001);
} }
if ( iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size() ) { if (iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size()) {
throw new SydApiException("密钥长度和数据长度不对应", 0x01000001); throw new SydApiException("密钥长度和数据长度不对应", 0x01000001);
} }
@ -935,7 +930,7 @@ public class SydApiBaseFunction implements SydApi {
List<byte[]> enDatas = SYD_SM4_BatchData(iEncKeyIndex, pcData, Consts.ECB_ENC); List<byte[]> enDatas = SYD_SM4_BatchData(iEncKeyIndex, pcData, Consts.ECB_ENC);
List<byte[]> macs = SYD_SM4Mac_BatchData(iMacKeyIndex, enDatas); List<byte[]> macs = SYD_SM4Mac_BatchData(iMacKeyIndex, enDatas);
List<DataAndMac> ret = new ArrayList<DataAndMac>(); List<DataAndMac> ret = new ArrayList<DataAndMac>();
for ( int i=0 ; i< enDatas.size() ; i++ ) { for (int i = 0; i < enDatas.size(); i++) {
DataAndMac dam = new DataAndMac(enDatas.get(i), macs.get(i)); DataAndMac dam = new DataAndMac(enDatas.get(i), macs.get(i));
ret.add(dam); ret.add(dam);
} }
@ -954,16 +949,16 @@ public class SydApiBaseFunction implements SydApi {
return new ArrayList<>(); return new ArrayList<>();
} }
if (null == this.hsms || null == iMacKeyIndex ) { if (null == this.hsms || null == iMacKeyIndex) {
throw new SydApiException("输入内容为空", 0x01000001); throw new SydApiException("输入内容为空", 0x01000001);
} }
if ( iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size() ) { if (iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size()) {
throw new SydApiException("密钥长度和数据长度不对应", 0x01000001); throw new SydApiException("密钥长度和数据长度不对应", 0x01000001);
} }
List<byte[]> macs = new ArrayList<>(pcData.size()); List<byte[]> macs = new ArrayList<>(pcData.size());
List<byte[]>datas = new ArrayList<>(pcData.size()); List<byte[]> datas = new ArrayList<>(pcData.size());
for ( int i=0 ; i<macs.size(); i++ ) { for (int i = 0; i < macs.size(); i++) {
macs.add(pcData.get(i).getMac()); macs.add(pcData.get(i).getMac());
datas.add(pcData.get(i).getData()); datas.add(pcData.get(i).getData());
} }
@ -971,8 +966,8 @@ public class SydApiBaseFunction implements SydApi {
boolean[] macCheck = SYD_SM4Mac_BatchData(iMacKeyIndex, datas, macs); boolean[] macCheck = SYD_SM4Mac_BatchData(iMacKeyIndex, datas, macs);
List<byte[]> enData = SYD_SM4_BatchData(iEncKeyIndex, datas, Consts.ECB_DEC); List<byte[]> enData = SYD_SM4_BatchData(iEncKeyIndex, datas, Consts.ECB_DEC);
List<DataAndMacCheck>ret = new ArrayList<>(enData.size()); List<DataAndMacCheck> ret = new ArrayList<>(enData.size());
for ( int i=0 ; i<enData.size() ; i++ ) { for (int i = 0; i < enData.size(); i++) {
DataAndMacCheck damc = new DataAndMacCheck(enData.get(i), macCheck[i]); DataAndMacCheck damc = new DataAndMacCheck(enData.get(i), macCheck[i]);
ret.add(damc); ret.add(damc);
} }
@ -983,7 +978,7 @@ public class SydApiBaseFunction implements SydApi {
@Override @Override
public ISM4 initSM4(int iKeyIndex, int ende) { public ISM4 initSM4(int iKeyIndex, int ende) {
return new SydSM4(this,iKeyIndex,ende); return new SydSM4(this, iKeyIndex, ende);
} }
@Override @Override
@ -997,7 +992,6 @@ public class SydApiBaseFunction implements SydApi {
} }
protected static int[] toIntArray(List<Integer> list) { protected static int[] toIntArray(List<Integer> list) {
if (null == list || list.isEmpty()) { if (null == list || list.isEmpty()) {
return new int[0]; return new int[0];