add macFull3des method to Crypto

This commit is contained in:
Andrea Franz 2018-08-28 14:49:40 +02:00
parent d6ca276c3f
commit bcdc066fa4
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
4 changed files with 35 additions and 19 deletions

View File

@ -76,6 +76,31 @@ public class Crypto {
}
}
public static byte[] macFull3des(byte[] keyData, byte[] data, byte[] iv) {
try {
SecretKeySpec keyDes = new SecretKeySpec(resizeKey8(keyData), "DES");
Cipher cipherDes = Cipher.getInstance("DES/CBC/NoPadding");
cipherDes.init(Cipher.ENCRYPT_MODE, keyDes, new IvParameterSpec(iv));
SecretKeySpec keyDes3 = new SecretKeySpec(resizeKey24(keyData), "DESede");
Cipher cipherDes3 = Cipher.getInstance("DESede/CBC/NoPadding");
byte[] des3Iv = iv;
if (data.length > 8) {
byte[] tmp = cipherDes.doFinal(data, 0, data.length - 8);
System.arraycopy(tmp, tmp.length - 8, des3Iv, 0, 8);
}
cipherDes3.init(Cipher.ENCRYPT_MODE, keyDes3, new IvParameterSpec(des3Iv));
byte[] result = cipherDes3.doFinal(data, data.length - 8, 8);
byte[] tail = new byte[8];
System.arraycopy(result, result.length - 8, tail, 0, 8);
return tail;
} catch (GeneralSecurityException e) {
throw new RuntimeException("error generating full triple DES MAC.", e);
}
}
public static byte[] resizeKey24(byte[] keyData) {
byte[] key = new byte[24];
System.arraycopy(keyData, 0, key, 0, 16);

View File

@ -2,8 +2,6 @@ package im.status.applet_installer_test.appletinstaller;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.*;
public class APDUResponseTest {

View File

@ -40,4 +40,14 @@ public class CryptoTest {
byte[] cardCryptogram = HexUtils.hexStringToByteArray("05c4bb8a86014e22");
assertTrue(Crypto.verifyCryptogram(encKey, hostChallenge, cardChallenge, cardCryptogram));
}
@Test
public void macFull3des() {
byte[] key = HexUtils.hexStringToByteArray("5b02e75ad63190aece0622936f11abab");
byte[] data = HexUtils.hexStringToByteArray("8482010010810b098a8fbb88da800000");
String expected = "5271D7174A5A166A";
byte[] result = Crypto.macFull3des(key, data, Crypto.NullBytes8);
assertEquals(expected, HexUtils.byteArrayToHexString(result));
}
}

View File

@ -1,17 +0,0 @@
package im.status.applet_installer_test.appletinstaller;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}