forked from UnitedID/YubiHSM-java-api
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- New utility methods to handle datatype short - Unit tests for YSM_NONCE_GET
- Loading branch information
Showing
7 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/org/unitedid/yhsm/internal/NonceGetCmdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |