Skip to content

Commit

Permalink
Merge pull request #622 from fgonzal/master
Browse files Browse the repository at this point in the history
Move `padISO9797Method2` from `JCESecurityModule` to `ISOUtil`.
  • Loading branch information
ar authored Nov 7, 2024
2 parents ceb5190 + 822b72a commit 125a677
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 38 deletions.
15 changes: 15 additions & 0 deletions jpos/src/main/java/org/jpos/iso/ISOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ public static String zeropadRight (String s, int len) {
d.append('0');
return d.toString();
}

/**
* Pads {@code data} as per ISO/IEC 9797-1, method 2.
* @param data Data to be padded.
* @return Returns {@code data} padded as per ISO/IEC 9797-1, method 2.
*/
public static byte[] padISO9797Method2(byte[] data) {
byte[] t = new byte[data.length - data.length % 8 + 8];
System.arraycopy(data, 0, t, 0, data.length);
for (int i = data.length; i < t.length; i++)
t[i] = (byte) (i == data.length ? 0x80 : 0x00);
data = t;
return data;
}

/**
* converts to BCD
* @param s - the number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,26 +551,11 @@ protected boolean verifyCVC3Impl(SecureDESKey imkcvc3, String accountNo, String

private byte[] calculateIVCVC3(Key mkcvc3, byte[] data)
throws JCEHandlerException {
byte[] paddedData = paddingISO9797Method2(data);
byte[] paddedData = ISOUtil.padISO9797Method2(data);
byte[] mac = calculateMACISO9797Alg3(mkcvc3, paddedData);
return Arrays.copyOfRange(mac,6,8);
}

/**
* ISO/IEC 9797-1 padding method 2
* @param d da to be padded
* @return padded data
*/
private byte[] paddingISO9797Method2(byte[] d) {
//Padding - first byte 0x80 rest 0x00
byte[] t = new byte[d.length - d.length%8 + 8];
System.arraycopy(d, 0, t, 0, d.length);
for (int i=d.length;i<t.length;i++)
t[i] = (byte)(i==d.length?0x80:0x00);
d = t;
return d;
}

/**
* Calculate MAC according to ISO/IEC 9797-1 Alg 3
* @param key DES double length key
Expand Down Expand Up @@ -825,11 +810,11 @@ private EncryptedPIN translatePINExt (EncryptedPIN oldPinUnderKd1, EncryptedPIN
case VSDC:
//Add VSDC pin block padding
clearPINBlock = ISOUtil.concat(new byte[]{0x08}, clearPINBlock);
clearPINBlock = paddingISO9797Method2(clearPINBlock);
clearPINBlock = ISOUtil.padISO9797Method2(clearPINBlock);
break;
case CCD:
//Add CCD pin block padding
clearPINBlock = paddingISO9797Method2(clearPINBlock);
clearPINBlock = ISOUtil.padISO9797Method2(clearPINBlock);
break;
default:
}
Expand Down Expand Up @@ -1073,7 +1058,7 @@ protected byte[] calculateARPC(Key skarpc, byte[] arqc, ARPCMethod arpcMethod
b = ISOUtil.concat(arqc, arc);
if (propAuthData != null)
b = ISOUtil.concat(b, propAuthData);
b = paddingISO9797Method2(b);
b = ISOUtil.padISO9797Method2(b);
b = calculateMACISO9797Alg3(skarpc, b);
return Arrays.copyOf(b, 4);
default:
Expand All @@ -1093,12 +1078,12 @@ protected byte[] generateSM_MACImpl(MKDMethod mkdm, SKDMethod skdm
switch(skdm){
case VSDC:
smi = deriveSK_VISA(mksmi, atc);
data = paddingISO9797Method2(data);
data = ISOUtil.padISO9797Method2(data);
break;
case MCHIP:
case EMV_CSKD:
smi = deriveCommonSK_SM(mksmi,arqc);
data = paddingISO9797Method2(data);
data = ISOUtil.padISO9797Method2(data);
break;
default:
throw new SMException("Session Key Derivation "+skdm+" not supported");
Expand Down
10 changes: 10 additions & 0 deletions jpos/src/test/java/org/jpos/iso/ISOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,16 @@ public void testBlankUnPadThrowsNullPointerException() throws Throwable {
});
}

@Test
public void testpadISO9797Method2() {
byte[] data = ISOUtil.hex2byte("010203");
byte[] result = ISOUtil.padISO9797Method2(data);
assertEquals("0102038000000000", ISOUtil.hexString(result));
data = ISOUtil.hex2byte("01020304010203");
result = ISOUtil.padISO9797Method2(data);
assertEquals("0102030401020380", ISOUtil.hexString(result));
}

@Test
public void testByte2BitSet() throws Throwable {
byte[] b = new byte[9];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ byte[] getDataNoPad() {
}

byte[] getDataPad80() {
return paddingISO9797Method2(getDataNoPad());
return ISOUtil.padISO9797Method2(getDataNoPad());
}

byte[] getATC() {
Expand All @@ -250,22 +250,6 @@ byte[] getATC() {
byte[] getUPN() {
return dm.get("UPN");
}

}

/**
* ISO/IEC 9797-1 padding method 2
* @param d da to be padded
* @return padded data
*/
static byte[] paddingISO9797Method2(byte[] d) {
//Padding - first byte 0x80 rest 0x00
byte[] t = new byte[d.length - d.length%8 + 8];
System.arraycopy(d, 0, t, 0, d.length);
for (int i=d.length;i<t.length;i++)
t[i] = (byte)(i==d.length?0x80:0x00);
d = t;
return d;
}

@Test
Expand Down

0 comments on commit 125a677

Please sign in to comment.