Permalink
Browse files

Code refactoring, using static imports to make the code a bit more re…

…adable.
  • Loading branch information...
1 parent 55469de commit 884982bbc458cc135463298e1dcd8442cd9e0765 @Ratler committed Aug 18, 2011
@@ -21,7 +21,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unitedid.yhsm.internal.*;
-import org.unitedid.yhsm.utility.Utils;
+
+import static org.unitedid.yhsm.utility.Utils.*;
import java.util.Map;
@@ -147,9 +148,9 @@ public String generateOathHotpAEAD(String nonce, int keyHandle, String tokenSeed
if (tokenSeed.length() != 40)
throw new YubiHSMInputException("Seed is not of required length, got " + tokenSeed.length() + " but expected 40");
- byte[] seed = Utils.hexToByteArray(tokenSeed);
- byte[] flag = Utils.leIntToBA(0x10000); // Generate HMAC SHA1 Flag
- loadBufferData(Utils.concatAllArrays(seed, flag), 0);
+ byte[] seed = hexToByteArray(tokenSeed);
+ byte[] flag = leIntToBA(0x10000); // Generate HMAC SHA1 Flag
+ loadBufferData(concatAllArrays(seed, flag), 0);
return generateBufferAEAD(nonce, keyHandle).get("aead");
}
@@ -19,11 +19,14 @@
package org.unitedid.yhsm.internal;
import org.unitedid.yhsm.YubiHSM;
-import org.unitedid.yhsm.utility.Utils;
import java.util.HashMap;
import java.util.Map;
+import static org.unitedid.yhsm.internal.Defines.*;
+import static org.unitedid.yhsm.utility.Utils.*;
+
+
/** <code>AEADCmd</code> implements AEAD commands for the YubiHSM */
public class AEADCmd {
@@ -43,12 +46,12 @@ private AEADCmd() {}
* @throws YubiHSMErrorException error exception
*/
public static Map<String, String> generateAEAD(DeviceHandler device, String nonce, int keyHandle, String data) throws YubiHSMInputException, YubiHSMCommandFailedException, YubiHSMErrorException {
- byte[] nonceBA = Utils.validateNonce(Utils.hexToByteArray(nonce), true);
- byte[] newdata = Utils.validateByteArray("data", data.getBytes(), 0, 0, YubiHSM.minHashLength);
- byte[] cmdBuffer = Utils.concatAllArrays(nonceBA, Utils.leIntToBA(keyHandle), Utils.addLengthToData(newdata));
- byte[] result = CommandHandler.execute(device, Defines.YSM_AEAD_GENERATE, cmdBuffer, true);
+ byte[] nonceBA = validateNonce(hexToByteArray(nonce), true);
+ byte[] newdata = validateByteArray("data", data.getBytes(), 0, 0, YubiHSM.minHashLength);
+ byte[] cmdBuffer = concatAllArrays(nonceBA, leIntToBA(keyHandle), addLengthToData(newdata));
+ byte[] result = CommandHandler.execute(device, YSM_AEAD_GENERATE, cmdBuffer, true);
- return parseResult(result, nonce, keyHandle, Defines.YSM_AEAD_GENERATE);
+ return parseResult(result, nonce, keyHandle, YSM_AEAD_GENERATE);
}
/**
@@ -65,12 +68,12 @@ private AEADCmd() {}
* @throws YubiHSMErrorException error exception
*/
public static Map<String, String> generateRandomAEAD(DeviceHandler device, String nonce, int keyHandle, int size) throws YubiHSMInputException, YubiHSMCommandFailedException, YubiHSMErrorException {
- byte[] nonceBA = Utils.validateNonce(Utils.hexToByteArray(nonce), true);
+ byte[] nonceBA = validateNonce(hexToByteArray(nonce), true);
byte[] len = {(byte) ((size << 24) >> 24)};
- byte[] cmdBuffer = Utils.concatAllArrays(nonceBA, Utils.leIntToBA(keyHandle), len);
- byte[] result = CommandHandler.execute(device, Defines.YSM_RANDOM_AEAD_GENERATE, cmdBuffer, true);
+ byte[] cmdBuffer = concatAllArrays(nonceBA, leIntToBA(keyHandle), len);
+ byte[] result = CommandHandler.execute(device, YSM_RANDOM_AEAD_GENERATE, cmdBuffer, true);
- return parseResult(result, nonce, keyHandle, Defines.YSM_RANDOM_AEAD_GENERATE);
+ return parseResult(result, nonce, keyHandle, YSM_RANDOM_AEAD_GENERATE);
}
/**
@@ -89,11 +92,11 @@ private AEADCmd() {}
* @throws YubiHSMErrorException error exception
*/
public static Map<String, String> generateBufferAEAD(DeviceHandler device, String nonce, int keyHandle) throws YubiHSMInputException, YubiHSMCommandFailedException, YubiHSMErrorException {
- byte[] nonceBA = Utils.validateNonce(Utils.hexToByteArray(nonce), true);
- byte[] cmdBuffer = Utils.concatAllArrays(nonceBA, Utils.leIntToBA(keyHandle));
- byte[] result = CommandHandler.execute(device, Defines.YSM_BUFFER_AEAD_GENERATE, cmdBuffer, true);
+ byte[] nonceBA = validateNonce(hexToByteArray(nonce), true);
+ byte[] cmdBuffer = concatAllArrays(nonceBA, leIntToBA(keyHandle));
+ byte[] result = CommandHandler.execute(device, YSM_BUFFER_AEAD_GENERATE, cmdBuffer, true);
- return parseResult(result, nonce, keyHandle, Defines.YSM_BUFFER_AEAD_GENERATE);
+ return parseResult(result, nonce, keyHandle, YSM_BUFFER_AEAD_GENERATE);
}
/**
@@ -111,14 +114,14 @@ private AEADCmd() {}
* @throws YubiHSMErrorException error exception
*/
public static boolean validateAEAD(DeviceHandler device, String nonce, int keyHandle, String aead, String plaintext) throws YubiHSMInputException, YubiHSMCommandFailedException, YubiHSMErrorException {
- byte[] aeadBA = Utils.hexToByteArray(aead);
- byte[] plainBA = Utils.validateByteArray("plaintext", plaintext.getBytes(), 0, aeadBA.length - Defines.YSM_AEAD_MAC_SIZE, YubiHSM.minHashLength);
- byte[] plainAndAead = Utils.concatAllArrays(plainBA, aeadBA);
- if (plainAndAead.length > (Defines.YSM_MAX_PKT_SIZE - 0x10))
+ byte[] aeadBA = hexToByteArray(aead);
+ byte[] plainBA = validateByteArray("plaintext", plaintext.getBytes(), 0, aeadBA.length - YSM_AEAD_MAC_SIZE, YubiHSM.minHashLength);
+ byte[] plainAndAead = concatAllArrays(plainBA, aeadBA);
+ if (plainAndAead.length > (YSM_MAX_PKT_SIZE - 0x10))
throw new YubiHSMInputException("Plaintext+aead too long");
- byte[] nonceBA = Utils.validateNonce(Utils.hexToByteArray(nonce), true);
- byte[] cmdBuffer = Utils.concatAllArrays(nonceBA, Utils.leIntToBA(keyHandle), Utils.addLengthToData(plainAndAead));
- byte[] result = CommandHandler.execute(device, Defines.YSM_AEAD_DECRYPT_CMP, cmdBuffer, true);
+ byte[] nonceBA = validateNonce(hexToByteArray(nonce), true);
+ byte[] cmdBuffer = concatAllArrays(nonceBA, leIntToBA(keyHandle), addLengthToData(plainAndAead));
+ byte[] result = CommandHandler.execute(device, YSM_AEAD_DECRYPT_CMP, cmdBuffer, true);
return parseValidationResult(result, nonce, keyHandle);
}
@@ -137,14 +140,14 @@ public static boolean validateAEAD(DeviceHandler device, String nonce, int keyHa
private static Map<String, String> parseResult(byte[] data, String nonce, int keyHandle, byte command) throws YubiHSMCommandFailedException, YubiHSMErrorException {
Map<String, String> result = new HashMap<String, String>();
- if (data[10] == Defines.YSM_STATUS_OK) {
- byte[] aead = Utils.rangeOfByteArray(data, Defines.YSM_AEAD_NONCE_SIZE + 6, data[11]);
- Utils.validateCmdResponseBA("keyHandle", Utils.rangeOfByteArray(data, 6, 4), Utils.leIntToBA(keyHandle));
- result.put("nonce", Utils.validateCmdResponseString("nonce",
- Utils.byteArrayToHex(Utils.rangeOfByteArray(data, 0, Defines.YSM_AEAD_NONCE_SIZE)), nonce));
- result.put("aead", Utils.byteArrayToHex(aead));
+ if (data[10] == YSM_STATUS_OK) {
+ byte[] aead = rangeOfByteArray(data, YSM_AEAD_NONCE_SIZE + 6, data[11]);
+ validateCmdResponseBA("keyHandle", rangeOfByteArray(data, 6, 4), leIntToBA(keyHandle));
+ result.put("nonce", validateCmdResponseString("nonce",
+ byteArrayToHex(rangeOfByteArray(data, 0, YSM_AEAD_NONCE_SIZE)), nonce));
+ result.put("aead", byteArrayToHex(aead));
} else {
- throw new YubiHSMCommandFailedException("Command " + Defines.getCommandString(command) + " failed: " + Defines.getCommandStatus(data[10]));
+ throw new YubiHSMCommandFailedException("Command " + getCommandString(command) + " failed: " + getCommandStatus(data[10]));
}
return result;
@@ -161,14 +164,14 @@ public static boolean validateAEAD(DeviceHandler device, String nonce, int keyHa
* @throws YubiHSMErrorException error exception
*/
private static boolean parseValidationResult(byte[] data, String nonce, int keyHandle) throws YubiHSMCommandFailedException, YubiHSMErrorException {
- Utils.validateCmdResponseBA("keyHandle", Utils.rangeOfByteArray(data, Defines.YSM_AEAD_NONCE_SIZE, 4), Utils.leIntToBA(keyHandle));
- Utils.validateCmdResponseString("nonce", Utils.byteArrayToHex(new String(data, 0, Defines.YSM_AEAD_NONCE_SIZE).getBytes()), nonce);
- if (data[10] == Defines.YSM_STATUS_OK) {
+ validateCmdResponseBA("keyHandle", rangeOfByteArray(data, YSM_AEAD_NONCE_SIZE, 4), leIntToBA(keyHandle));
+ validateCmdResponseString("nonce", byteArrayToHex(new String(data, 0, YSM_AEAD_NONCE_SIZE).getBytes()), nonce);
+ if (data[10] == YSM_STATUS_OK) {
return true;
- } else if (data[10] == Defines.YSM_MISMATCH) {
+ } else if (data[10] == YSM_MISMATCH) {
return false;
} else {
- throw new YubiHSMCommandFailedException("Command " + Defines.getCommandString(Defines.YSM_AEAD_DECRYPT_CMP) + " failed: " + Defines.getCommandStatus(data[10]));
+ throw new YubiHSMCommandFailedException("Command " + getCommandString(YSM_AEAD_DECRYPT_CMP) + " failed: " + getCommandStatus(data[10]));
}
}
}
@@ -18,7 +18,8 @@
package org.unitedid.yhsm.internal;
-import org.unitedid.yhsm.utility.Utils;
+import static org.unitedid.yhsm.internal.Defines.*;
+import static org.unitedid.yhsm.utility.Utils.*;
/** <code>AESECBCmd</code> implements AES ECB block cipher commands for the YubiHSM. */
public class AESECBCmd {
@@ -38,10 +39,10 @@ private AESECBCmd() {}
* @throws YubiHSMCommandFailedException if the YubiHSM fail to execute the command
*/
public static String encrypt(DeviceHandler deviceHandler, int keyHandle, String plaintext) throws YubiHSMInputException, YubiHSMErrorException, YubiHSMCommandFailedException {
- byte[] cmdBuffer = Utils.concatAllArrays(Utils.leIntToBA(keyHandle), Utils.validateByteArray("plaintext", plaintext.getBytes(), Defines.YSM_BLOCK_SIZE, 0, Defines.YSM_BLOCK_SIZE));
- byte[] result = CommandHandler.execute(deviceHandler, Defines.YSM_AES_ECB_BLOCK_ENCRYPT, cmdBuffer, true);
+ byte[] cmdBuffer = concatAllArrays(leIntToBA(keyHandle), validateByteArray("plaintext", plaintext.getBytes(), YSM_BLOCK_SIZE, 0, YSM_BLOCK_SIZE));
+ byte[] result = CommandHandler.execute(deviceHandler, YSM_AES_ECB_BLOCK_ENCRYPT, cmdBuffer, true);
- return parseResult(result, keyHandle, Defines.YSM_AES_ECB_BLOCK_ENCRYPT, false);
+ return parseResult(result, keyHandle, YSM_AES_ECB_BLOCK_ENCRYPT, false);
}
/**
@@ -56,10 +57,10 @@ public static String encrypt(DeviceHandler deviceHandler, int keyHandle, String
* @throws YubiHSMCommandFailedException if the YubiHSM fail to execute the command
*/
public static String decrypt(DeviceHandler deviceHandler, int keyHandle, String cipherText) throws YubiHSMErrorException, YubiHSMInputException, YubiHSMCommandFailedException {
- byte[] cmdBuffer = Utils.concatAllArrays(Utils.leIntToBA(keyHandle), Utils.validateByteArray("cipherText", Utils.hexToByteArray(cipherText), 0, Defines.YSM_BLOCK_SIZE, 0));
- byte[] result = CommandHandler.execute(deviceHandler, Defines.YSM_AES_ECB_BLOCK_DECRYPT, cmdBuffer, true);
+ byte[] cmdBuffer = concatAllArrays(leIntToBA(keyHandle), validateByteArray("cipherText", hexToByteArray(cipherText), 0, YSM_BLOCK_SIZE, 0));
+ byte[] result = CommandHandler.execute(deviceHandler, YSM_AES_ECB_BLOCK_DECRYPT, cmdBuffer, true);
- return parseResult(result, keyHandle, Defines.YSM_AES_ECB_BLOCK_DECRYPT, true);
+ return parseResult(result, keyHandle, YSM_AES_ECB_BLOCK_DECRYPT, true);
}
/**
@@ -75,20 +76,20 @@ public static String decrypt(DeviceHandler deviceHandler, int keyHandle, String
* @throws YubiHSMCommandFailedException if the YubiHSM fail to execute the command
*/
public static boolean compare(DeviceHandler deviceHandler, int keyHandle, String cipherText, String plaintext) throws YubiHSMInputException, YubiHSMErrorException, YubiHSMCommandFailedException {
- byte[] cipherTextBA = Utils.validateByteArray("cipherText", Utils.hexToByteArray(cipherText), 0, Defines.YSM_BLOCK_SIZE, 0);
- byte[] plaintextBA = Utils.validateByteArray("plaintext", plaintext.getBytes(), Defines.YSM_BLOCK_SIZE, 0, Defines.YSM_BLOCK_SIZE);
- byte[] keyHandleBA = Utils.leIntToBA(keyHandle);
- byte[] cmdBuffer = Utils.concatAllArrays(keyHandleBA, cipherTextBA, plaintextBA);
- byte[] result = CommandHandler.execute(deviceHandler, Defines.YSM_AES_ECB_BLOCK_DECRYPT_CMP, cmdBuffer, true);
+ byte[] cipherTextBA = validateByteArray("cipherText", hexToByteArray(cipherText), 0, YSM_BLOCK_SIZE, 0);
+ byte[] plaintextBA = validateByteArray("plaintext", plaintext.getBytes(), YSM_BLOCK_SIZE, 0, YSM_BLOCK_SIZE);
+ byte[] keyHandleBA = leIntToBA(keyHandle);
+ byte[] cmdBuffer = concatAllArrays(keyHandleBA, cipherTextBA, plaintextBA);
+ byte[] result = CommandHandler.execute(deviceHandler, YSM_AES_ECB_BLOCK_DECRYPT_CMP, cmdBuffer, true);
- Utils.validateCmdResponseBA("keyHandle", Utils.rangeOfByteArray(result, 0, 4), keyHandleBA);
+ validateCmdResponseBA("keyHandle", rangeOfByteArray(result, 0, 4), keyHandleBA);
- if (result[4] == Defines.YSM_STATUS_OK) {
+ if (result[4] == YSM_STATUS_OK) {
return true;
- } else if (result[4] == Defines.YSM_MISMATCH) {
+ } else if (result[4] == YSM_MISMATCH) {
return false;
} else {
- throw new YubiHSMCommandFailedException("Command " + Defines.getCommandString(Defines.YSM_AES_ECB_BLOCK_DECRYPT_CMP) + " failed: " + Defines.getCommandStatus(result[4]));
+ throw new YubiHSMCommandFailedException("Command " + getCommandString(YSM_AES_ECB_BLOCK_DECRYPT_CMP) + " failed: " + getCommandStatus(result[4]));
}
}
@@ -104,13 +105,13 @@ public static boolean compare(DeviceHandler deviceHandler, int keyHandle, String
* @throws YubiHSMCommandFailedException if the YubiHSM fail to execute the command
*/
private static String parseResult(byte[] data, int keyHandle, byte command, boolean decrypt) throws YubiHSMErrorException, YubiHSMCommandFailedException {
- Utils.validateCmdResponseBA("keyHandle", Utils.rangeOfByteArray(data, 0, 4), Utils.leIntToBA(keyHandle));
- byte[] result = Utils.rangeOfByteArray(data, 4, Defines.YSM_BLOCK_SIZE);
+ validateCmdResponseBA("keyHandle", rangeOfByteArray(data, 0, 4), leIntToBA(keyHandle));
+ byte[] result = rangeOfByteArray(data, 4, YSM_BLOCK_SIZE);
- if (data[20] == Defines.YSM_STATUS_OK) {
- return decrypt ? new String(result).trim() : Utils.byteArrayToHex(result);
+ if (data[20] == YSM_STATUS_OK) {
+ return decrypt ? new String(result).trim() : byteArrayToHex(result);
} else {
- throw new YubiHSMCommandFailedException("Command " + Defines.getCommandString(command) + " failed: " + Defines.getCommandStatus(result[4]));
+ throw new YubiHSMCommandFailedException("Command " + getCommandString(command) + " failed: " + getCommandStatus(result[4]));
}
}
}
@@ -18,7 +18,9 @@
package org.unitedid.yhsm.internal;
-import org.unitedid.yhsm.utility.Utils;
+import static org.unitedid.yhsm.internal.Defines.*;
+import static org.unitedid.yhsm.utility.Utils.*;
+
/** <code>BufferCmd</code> implements the internal buffer functions of the YubiHSM. */
public class BufferCmd {
@@ -39,8 +41,8 @@ public static int loadData(DeviceHandler device, String data, int offset) throws
int dataLength = data.getBytes().length;
byte[] off = {(byte) ((offset << 24) >> 24)};
- byte[] cmdBuffer = Utils.concatAllArrays(off, Utils.addLengthToData(data.getBytes()));
- byte[] result = CommandHandler.execute(device, Defines.YSM_BUFFER_LOAD, cmdBuffer, true);
+ byte[] cmdBuffer = concatAllArrays(off, addLengthToData(data.getBytes()));
+ byte[] result = CommandHandler.execute(device, YSM_BUFFER_LOAD, cmdBuffer, true);
return parseResult(offset, dataLength, result);
}
@@ -57,8 +59,8 @@ public static int loadData(DeviceHandler device, String data, int offset) throws
public static int loadData(DeviceHandler device, byte[] data, int offset) throws YubiHSMErrorException {
int dataLength = data.length;
byte[] off = {(byte) ((offset << 24) >> 24)};
- byte[] cmdBuffer = Utils.concatAllArrays(off, Utils.addLengthToData(data));
- byte[] result = CommandHandler.execute(device, Defines.YSM_BUFFER_LOAD, cmdBuffer, true);
+ byte[] cmdBuffer = concatAllArrays(off, addLengthToData(data));
+ byte[] result = CommandHandler.execute(device, YSM_BUFFER_LOAD, cmdBuffer, true);
return parseResult(offset, dataLength, result);
}
@@ -74,7 +76,7 @@ public static int loadData(DeviceHandler device, byte[] data, int offset) throws
*/
public static int loadRandomData(DeviceHandler device, int length, int offset) throws YubiHSMErrorException {
byte[] cmdBuffer = {(byte) ((offset << 24) >> 24), (byte) ((length << 24) >> 24)};
- byte[] result = CommandHandler.execute(device, Defines.YSM_BUFFER_RANDOM_LOAD, cmdBuffer, true);
+ byte[] result = CommandHandler.execute(device, YSM_BUFFER_RANDOM_LOAD, cmdBuffer, true);
return parseResult(offset, length, result);
}
Oops, something went wrong.

0 comments on commit 884982b

Please sign in to comment.