Skip to content

Commit

Permalink
Implemented YSM_NONCE_GET
Browse files Browse the repository at this point in the history
- New utility methods to handle datatype short
- Unit tests for YSM_NONCE_GET
  • Loading branch information
Ratler committed Aug 9, 2011
1 parent 2161d31 commit 47450aa
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/org/unitedid/yhsm/YubiHSM.java
Expand Up @@ -383,6 +383,20 @@ public int oathHOTPValidateOTP(YubiHSM hsm, int keyHandle, String nonce, String
return OathHOTPCmd.validateOTP(hsm, keyHandle, nonce, aead, counter, otp, lookAhead);
}


/**
* Get a nonce from the YubiHSM. Increment the nonce by the number supplied as increment.
* To get the current nonce send 0 as increment.
*
* @param increment the increment (short)
* @return returns a Nonce class
* @throws YubiHSMErrorException error exception
* @throws YubiHSMCommandFailedException command failed exception
*/
public Nonce getNonce(short increment) throws YubiHSMErrorException, YubiHSMCommandFailedException {
return NonceGetCmd.execute(deviceHandler, increment);
}

/**
* Drain all remaining output from the YubiHSM, used for debugging.
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/unitedid/yhsm/internal/Defines.java
Expand Up @@ -63,6 +63,7 @@ private Defines() {}
final static public byte YSM_TEMP_KEY_LOAD = 0x11;
final static public byte YSM_BUFFER_LOAD = 0x20;
final static public byte YSM_BUFFER_RANDOM_LOAD = 0x21;
final static public byte YSM_NONCE_GET = 0x22;
final static public byte YSM_ECHO = 0x23;
final static public byte YSM_SYSTEM_INFO_QUERY = 0x26;
final static public byte YSM_KEY_STORAGE_UNLOCK = 0x27;
Expand All @@ -88,6 +89,7 @@ private Defines() {}
put(0x11, "YSM_TEMP_KEY_LOAD");
put(0x20, "YSM_BUFFER_LOAD");
put(0x21, "YSM_BUFFER_RANDOM_LOAD");
put(0x22, "YSM_NONCE_GET");
put(0x23, "YSM_ECHO");
put(0x26, "YSM_SYSTEM_INFO_QUERY");
put(0x27, "YSM_KEY_STORAGE_UNLOCK");
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/org/unitedid/yhsm/internal/Nonce.java
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2011 United ID. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Stefan Wold <stefan.wold@unitedid.org>
*/

package org.unitedid.yhsm.internal;

/** <code>Nonce</code> a class that represent an YSM_NONCE_GET */
public class Nonce {

private int vtile;
private int powerUpCount;
private int nonceInt;
private String nonce;

public Nonce(int vtile, int powerUpCount, int nonceInt, String nonce) {
this.vtile = vtile;
this.powerUpCount = powerUpCount;
this.nonceInt = nonceInt;
this.nonce = nonce;
}

public int getVolative() {
return vtile;
}

public int getPowerUpCount() {
return powerUpCount;
}

public int getNonceInt() {
return nonceInt;
}

public String getNonce() {
return nonce;
}

public String toString() {
return "Nonce: " + nonce + " Power up count: " + powerUpCount + " Volatile: " + vtile;
}
}
67 changes: 67 additions & 0 deletions src/main/java/org/unitedid/yhsm/internal/NonceGetCmd.java
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2011 United ID. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Stefan Wold <stefan.wold@unitedid.org>
*/

package org.unitedid.yhsm.internal;

import org.unitedid.yhsm.utility.Utils;

/** <code>NonceGetCmd</code> implements get nonce from the YubiHSM */
public class NonceGetCmd {

/** Private constructor */
private NonceGetCmd() {}

/**
* Get a nonce from the YubiHSM. Increment nonce by supplied number.
* To get the current nonce send 0 as increment.
*
* @param device the YubiHSM device
* @param increment the increment (short)
* @return returns a Nonce class
* @throws YubiHSMErrorException error exception
* @throws YubiHSMCommandFailedException command failed exception
*/
public static Nonce execute(DeviceHandler device, short increment) throws YubiHSMErrorException, YubiHSMCommandFailedException {
byte[] result = CommandHandler.execute(device, Defines.YSM_NONCE_GET, Utils.leShortToByteArray(increment), true);

return parseResult(result);
}

/**
* Parse the response from the YubiHSM
*
* @param data the result from the YubiHSM
* @return returns a Nonce class
* @throws YubiHSMCommandFailedException command failed exception
*/
private static Nonce parseResult(byte[] data) throws YubiHSMCommandFailedException {
Nonce result = null;

if (data[0] == Defines.YSM_STATUS_OK) {
int vtile = Utils.leBAToBeInt(Utils.rangeOfByteArray(data, 1, 4));
int powerUpCount = Utils.leBAToBeShort(Utils.rangeOfByteArray(data, 5, 2));
int nonceInt = powerUpCount + vtile;
String nonce = Utils.byteArrayToHex(Utils.rangeOfByteArray(data, 1, Defines.YSM_AEAD_NONCE_SIZE));
result = new Nonce(vtile, powerUpCount, nonceInt, nonce);
} else {
throw new YubiHSMCommandFailedException("Command " + Defines.getCommandString(Defines.YSM_NONCE_GET) + " failed: " + Defines.getCommandStatus(data[0]));
}

return result;
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/unitedid/yhsm/utility/Utils.java
Expand Up @@ -18,6 +18,7 @@

package org.unitedid.yhsm.utility;

import com.sun.corba.se.spi.monitoring.StatisticMonitoredAttribute;
import org.unitedid.yhsm.internal.Defines;
import org.unitedid.yhsm.internal.YubiHSMErrorException;
import org.unitedid.yhsm.internal.YubiHSMInputException;
Expand Down Expand Up @@ -69,12 +70,27 @@ public static byte[] leIntToBA(int value) {
return buffer.array();
}

public static byte[] leShortToByteArray(short value) {
ByteBuffer buffer = ByteBuffer.allocate(2);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putShort(value);

return buffer.array();
}

public static int leBAToBeInt(byte[] data) {
ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);

return buffer.getInt();
}

public static int leBAToBeShort(byte[] data) {
ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);

return buffer.getShort();
}


public static byte[] rangeOfByteArray(byte[] data, int offset, int length) {
byte[] buffer = new byte[length];
for (int a = 0; a < length; a++, offset++) {
Expand Down
Expand Up @@ -31,7 +31,8 @@
SystemInfoCmdTest.class,
AESECBCmdTest.class,
LoadTemporaryKeyCmdTest.class,
OathHOTPCmdTest.class})
OathHOTPCmdTest.class,
NonceGetCmdTest.class})

public class InternalTestSuite {

Expand Down
61 changes: 61 additions & 0 deletions src/test/java/org/unitedid/yhsm/internal/NonceGetCmdTest.java
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2011 United ID. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Stefan Wold <stefan.wold@unitedid.org>
*/

package org.unitedid.yhsm.internal;

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 static junit.framework.Assert.assertEquals;

public class NonceGetCmdTest extends SetupCommon {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Before
public void setUp() throws Exception {
super.setUp();
}

@After
public void tearDown() throws Exception {
super.tearDown();
}

@Test
public void testNonceGet() throws YubiHSMErrorException {
Nonce nonce1 = hsm.getNonce((short) 1);
Nonce nonce2 = hsm.getNonce((short) 1);
assertEquals(nonce1.getNonceInt() + 1, nonce2.getNonceInt());

Nonce nonce3 = hsm.getNonce((short) 9);
assertEquals(nonce2.getNonceInt() +1, nonce3.getNonceInt());

Nonce nonce4 = hsm.getNonce((short) 1);
assertEquals(nonce3.getNonceInt() + 9, nonce4.getNonceInt());

Nonce nonce5 = hsm.getNonce((short) 0);
Nonce nonce6 = hsm.getNonce((short) 0);
assertEquals(nonce5.getNonceInt(), nonce6.getNonceInt());
}
}

0 comments on commit 47450aa

Please sign in to comment.