From 58ebbe700d922af33b79886f715ee302fb62c523 Mon Sep 17 00:00:00 2001 From: clemenso Date: Fri, 28 Jan 2011 17:22:04 +0000 Subject: Read PuK_QS (secure messaging case1b/2) git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@896 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../at/gv/egiz/smcc/activation/RetailCBCMac.java | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'smccTest/src/main/java/at/gv/egiz/smcc/activation/RetailCBCMac.java') diff --git a/smccTest/src/main/java/at/gv/egiz/smcc/activation/RetailCBCMac.java b/smccTest/src/main/java/at/gv/egiz/smcc/activation/RetailCBCMac.java index f8cedc0e..c614e57f 100644 --- a/smccTest/src/main/java/at/gv/egiz/smcc/activation/RetailCBCMac.java +++ b/smccTest/src/main/java/at/gv/egiz/smcc/activation/RetailCBCMac.java @@ -13,6 +13,10 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class RetailCBCMac { + + public enum PADDING { + NoPadding, ISO9797_2 + }; /** * Calculates a Retail CBC Mac from the given message. @@ -53,7 +57,8 @@ public class RetailCBCMac { * @throws InvalidKeyException if the key cannot be used with the Ciphers * @throws GeneralSecurityException if the Cipher operation(s) fails */ - static byte[] retailMac(byte[] msg, + static byte[] retailMac(byte[] msg, + PADDING msgPadding, String cbcCipherAlg, String cipherAlg, SecretKey csk, @@ -84,17 +89,20 @@ public class RetailCBCMac { System.arraycopy(rawCsk, 0, rawCbcCipherKey, 0, blockSize); cbcCipherKey = new SecretKeySpec(rawCbcCipherKey, cbcCipherAlg); } - // if necessary pad message with zeros - byte[] paddedMsg = pad(msg, blockSize); - + + if (msgPadding == PADDING.ISO9797_2) { + // if necessary pad message with zeros + msg = pad(msg, blockSize); + } + // calculate CBC Mac for the first n-1 blocks - int n = paddedMsg.length; + int n = msg.length; int n_1 = n - blockSize; - byte[] cbcMac = cbcMac(paddedMsg, 0, n_1, cbcCipherKey, cbcCipherAlg, blockSize); + byte[] cbcMac = cbcMac(msg, 0, n_1, cbcCipherKey, cbcCipherAlg, blockSize); // calculate retail mac byte[] xor = new byte[blockSize]; - CryptoUtils.xorBlock(paddedMsg, n_1, cbcMac, 0, xor, 0, blockSize); + CryptoUtils.xorBlock(msg, n_1, cbcMac, 0, xor, 0, blockSize); Cipher cipher = Cipher.getInstance(cipherAlg+"/ECB/NoPadding", "IAIK"); cipher.init(Cipher.ENCRYPT_MODE, csk); byte[] retailMac = cipher.doFinal(xor); -- cgit v1.2.3