Permalink
Browse files

Implemented hsm random generator command

  • Loading branch information...
1 parent 22c2d09 commit ebb409e44207418908ad44271c80d26dd84e07f2 @Ratler committed Aug 9, 2011
View
12 src/main/java/org/unitedid/yhsm/YubiHSM.java
@@ -403,6 +403,18 @@ public Nonce getNonce(short increment) throws YubiHSMErrorException, YubiHSMComm
}
/**
+ * Tell the YubiHSM to generate a number of random bytes.
+ *
+ * @param bytes the number of bytes to generate
+ * @return returns a byte array of random bytes
+ * @throws YubiHSMErrorException error exception
+ * @throws YubiHSMInputException invalid argument exception
+ */
+ public byte[] getRandom(int bytes) throws YubiHSMErrorException, YubiHSMInputException {
+ return RandomCmd.execute(deviceHandler, bytes);
+ }
+
+ /**
* Drain all remaining output from the YubiHSM, used for debugging.
*
* @return true if successful, false otherwise.
View
4 src/main/java/org/unitedid/yhsm/internal/Defines.java
@@ -65,6 +65,8 @@ private Defines() {}
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_RANDOM_GENERATE = 0x24;
+ final static public byte YSM_RANDOM_RESEED = 0x25;
final static public byte YSM_SYSTEM_INFO_QUERY = 0x26;
final static public byte YSM_KEY_STORAGE_UNLOCK = 0x27;
final static public byte YSM_MONITOR_EXIT = 0x7f;
@@ -91,6 +93,8 @@ private Defines() {}
put(0x21, "YSM_BUFFER_RANDOM_LOAD");
put(0x22, "YSM_NONCE_GET");
put(0x23, "YSM_ECHO");
+ put(0x24, "YSM_RANDOM_GENERATE");
+ put(0x25, "YSM_RANDOM_RESEED");
put(0x26, "YSM_SYSTEM_INFO_QUERY");
put(0x27, "YSM_KEY_STORAGE_UNLOCK");
}});
View
57 src/main/java/org/unitedid/yhsm/internal/RandomCmd.java
@@ -0,0 +1,57 @@
+/*
+ * 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>RandomCmd</code> implements YubiHSM random generator. */
+public class RandomCmd {
+
+ /** Private constructor */
+ private RandomCmd() {}
+
+ /**
+ * Tell the YubiHSM to generate a number of random bytes.
+ *
+ * @param device the YubiHSM device
+ * @param bytes the number of bytes to generate
+ * @return returns a byte array of random bytes
+ * @throws YubiHSMErrorException error exception
+ * @throws YubiHSMInputException invalid argument exception
+ */
+ public static byte[] execute(DeviceHandler device, int bytes) throws YubiHSMErrorException, YubiHSMInputException {
+ if (bytes > 0 && bytes > Defines.YSM_MAX_PKT_SIZE - 1) {
+ throw new YubiHSMInputException("'Bytes' int too large, expected less than or equal to " + (Defines.YSM_MAX_PKT_SIZE -1) + " but got " + bytes);
+ }
+ byte[] cmdBuffer = new byte[]{(byte) bytes};
+ byte[] result = CommandHandler.execute(device, Defines.YSM_RANDOM_GENERATE, cmdBuffer, true);
+ return parseResult(result);
+ }
+
+ /**
+ * Parse the response from the YubiHSM
+ *
+ * @param data the result from the YubiHSM
+ * @return returns a byte array of random bytes
+ */
+ private static byte[] parseResult(byte[] data) {
+ int bytes = data[0];
+ return Utils.rangeOfByteArray(data, 1, bytes);
+ }
+}
View
3 src/test/java/org/unitedid/yhsm/internal/InternalTestSuite.java
@@ -32,7 +32,8 @@
AESECBCmdTest.class,
LoadTemporaryKeyCmdTest.class,
OathHOTPCmdTest.class,
- NonceGetCmdTest.class})
+ NonceGetCmdTest.class,
+ RandomCmdTest.class})
public class InternalTestSuite {
View
50 src/test/java/org/unitedid/yhsm/internal/RandomCmdTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.Test;
+import org.unitedid.yhsm.SetupCommon;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+
+public class RandomCmdTest extends SetupCommon {
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testRandomness() throws YubiHSMErrorException, YubiHSMInputException {
+ byte[] random1 = hsm.getRandom(12);
+ byte[] random2 = hsm.getRandom(12);
+
+ assertNotSame(random1, random2);
+ assertEquals(12, random1.length);
+ }
+
+}

0 comments on commit ebb409e

Please sign in to comment.