SYD_SM4_ShortData 入参检查
This commit is contained in:
parent
d950ae74db
commit
0e5fb0f56f
@ -1,9 +1,13 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sun.jna.ptr.IntByReference;
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.sge.bytes.BytesUtil;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
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.util.SYMUtil;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -181,7 +185,7 @@ public class SydApiBaseFunction implements SydApi {
|
||||
public LongDataReturn<String, String> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM3_Hash_LongData方法接收参数:pcData:{},iPkgNum:{},pcProcData:{}", Arrays.toString(pcData), iPkgNum, pcProcData);
|
||||
log.debug("SYD_SM3_Hash_LongData 方法接收参数:pcData:{},iPkgNum:{},pcProcData:{}", Arrays.toString(pcData), iPkgNum, pcProcData);
|
||||
}
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
@ -247,22 +251,25 @@ public class SydApiBaseFunction implements SydApi {
|
||||
@Override
|
||||
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if ( iKeyIndex < 0 ) {
|
||||
throw new SydApiException(0x01000001);
|
||||
}
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iFlag);
|
||||
}
|
||||
if (iFlag == Consts.ECB_DEC && null == pcData) {
|
||||
throw new SydApiException(19);
|
||||
throw new SydApiException(0x01000001);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
if ( pcData.length > 1024 ) {
|
||||
throw new SydApiException("输入内容超长",19);
|
||||
if ( 0 == pcData.length || pcData.length > 1024 ) {
|
||||
throw new SydApiException("输入内容超长",0x01000001);
|
||||
}
|
||||
} else {
|
||||
if ( pcData.length > 1040 ) {
|
||||
throw new SydApiException("输入内容超长",19);
|
||||
if ( 0 == pcData.length || pcData.length % 16 != 0 || pcData.length > 1040 ) {
|
||||
throw new SydApiException("输入密文数据长度错误", 0x01000014);
|
||||
}
|
||||
}
|
||||
updateLastUseTime();
|
||||
@ -831,23 +838,77 @@ public class SydApiBaseFunction implements SydApi {
|
||||
throw new SydApiException(19);
|
||||
}
|
||||
|
||||
byte[] enData = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_ENC);
|
||||
String mac = SYD_SM4Mac_ShortData(iMacKeyIndex, enData);
|
||||
return new DataAndMac(enData, mac);
|
||||
if ( ! ConnectHandlerManager.isInited() ) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
int enLen = BytesUtil.calculatePaddedLength(pcData.length);
|
||||
byte[] encData = new byte[enLen];
|
||||
byte[] mac = new byte[16];
|
||||
IntByReference encDataLen = new IntByReference(encData.length);
|
||||
|
||||
int i = SydLibrary.INSTANCE.SYD_SM4_Encrypt_Mac_ShortData(
|
||||
ConnectHandlerManager.getHandler(),
|
||||
iEncKeyIndex,
|
||||
iMacKeyIndex,
|
||||
pcData,
|
||||
pcData.length,
|
||||
encData,
|
||||
encDataLen,
|
||||
mac
|
||||
);
|
||||
if ( 0 == i ){
|
||||
return new DataAndMac(encData, Util.bytes2HexString(mac));
|
||||
} else {
|
||||
throw new SydApiException(i);
|
||||
}
|
||||
|
||||
// byte[] enData = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_ENC);
|
||||
// String mac = SYD_SM4Mac_ShortData(iMacKeyIndex, enData);
|
||||
// return new DataAndMac(enData, mac);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, String mac) {
|
||||
boolean ret = SYD_SM4Mac_ShortData(iMacKeyIndex, pcData, mac);
|
||||
byte[] data = null;
|
||||
try {
|
||||
data = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_DEC);
|
||||
}catch (Exception e){
|
||||
return new DataAndMacCheck(null, ret);
|
||||
// boolean ret = SYD_SM4Mac_ShortData(iMacKeyIndex, pcData, mac);
|
||||
// byte[] data = null;
|
||||
// try {
|
||||
// data = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_DEC);
|
||||
// }catch (Exception e){
|
||||
// return new DataAndMacCheck(null, ret);
|
||||
// }
|
||||
// return new DataAndMacCheck(data, ret);
|
||||
|
||||
if ( ! ConnectHandlerManager.isInited() ) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
byte[] decryptedData = new byte[pcData.length];
|
||||
IntByReference decryptedDataLen = new IntByReference(decryptedData.length);
|
||||
|
||||
int i = SydLibrary.INSTANCE.SYD_SM4_Decrypt_Mac_ShortData(
|
||||
ConnectHandlerManager.getHandler(),
|
||||
iEncKeyIndex,
|
||||
iMacKeyIndex,
|
||||
pcData,
|
||||
pcData.length,
|
||||
Util.hexString2Bytes(mac),
|
||||
decryptedData,
|
||||
decryptedDataLen
|
||||
);
|
||||
|
||||
byte[] data = new byte[decryptedDataLen.getValue()];
|
||||
System.arraycopy(decryptedData, 0, data, 0, data.length);
|
||||
|
||||
if ( 0 == i ){
|
||||
return new DataAndMacCheck(data, true);
|
||||
} if (0x01000010 == i) {
|
||||
return new DataAndMacCheck(data, false);
|
||||
} else {
|
||||
throw new SydApiException(i);
|
||||
}
|
||||
return new DataAndMacCheck(data, ret);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Loading…
Reference in New Issue
Block a user