Permalink
Browse files

Small tweaks to the OathHOTP code and new unit test added

  • Loading branch information...
1 parent 47450aa commit 6fef34b9e8d166d10ac178c45f45bc0895fce2cf @Ratler committed Aug 9, 2011
@@ -46,7 +46,7 @@ private OathHOTPCmd() {}
* @throws YubiHSMErrorException error exception
*/
public static int validateOTP(YubiHSM hsm, int keyHandle, String nonce, String aead, int counter, String otp, int lookAhead) throws YubiHSMInputException, YubiHSMCommandFailedException, YubiHSMErrorException {
- int otpLength = String.valueOf(otp).trim().length();
+ int otpLength = otp.trim().length();
if (otpLength < 6 || otpLength > 8) {
throw new YubiHSMInputException("OTP not of required length, should be between 6-8 digits long but was " + otpLength);
}
@@ -75,9 +75,7 @@ public static int validateOTP(YubiHSM hsm, int keyHandle, String nonce, String a
*/
public static String truncate(String hmac, int otpLength) throws YubiHSMInputException {
byte[] hmacBA = Utils.hexToByteArray(hmac);
- if (hmacBA.length != 20) {
- throw new YubiHSMInputException("HMAC not of required length. Expected 20 bytes but got " + hmacBA.length);
- }
+ Utils.validateByteArray("hmacBA", hmacBA, 0, 20, 0);
int offset = hmacBA[19] & 0xf;
int binCode = (hmacBA[offset] & 0x7f) << 24 | (hmacBA[offset+1] & 0xff) << 16 | (hmacBA[offset+2] & 0xff) << 8 | (hmacBA[offset+3] & 0xff);
@@ -20,7 +20,9 @@
import org.junit.After;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.unitedid.yhsm.SetupCommon;
import org.unitedid.yhsm.utility.Utils;
@@ -37,6 +39,9 @@
private String aead;
private int keyHandle = 8192;
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Before
public void setUp() throws Exception {
super.setUp();
@@ -84,6 +89,11 @@ public void testOathHotpValidation() throws YubiHSMCommandFailedException, YubiH
}
}
+ @Test
+ public void testOathHotpTruncateHMACLength() throws YubiHSMInputException {
+ thrown.expect(YubiHSMInputException.class);
+ OathHOTPCmd.truncate("cccccccccccccccccccccccccccccccccccccccccc", 6);
+ }
}
class OathHotpValueMap {

0 comments on commit 6fef34b

Please sign in to comment.