diff options
author | David Hook <dgh@cryptoworkshop.com> | 2013-05-08 12:19:48 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2013-05-08 12:19:48 +0400 |
commit | f2603a468508257728eae47e54f128dfbc19ea93 (patch) | |
tree | b171b82eb80bf07f584b1b3241376926039dddb3 | |
parent | 8e5e88f2d093db12f7d054612de82f9a0d7eea78 (diff) |
refactored over to alternate vector set.
4 files changed, 430 insertions, 297 deletions
diff --git a/src/main/java/org/bouncycastle/crypto/prng/HashSP800DRBG.java b/src/main/java/org/bouncycastle/crypto/prng/HashSP800DRBG.java index 4249fcee..45c87cd7 100644 --- a/src/main/java/org/bouncycastle/crypto/prng/HashSP800DRBG.java +++ b/src/main/java/org/bouncycastle/crypto/prng/HashSP800DRBG.java @@ -4,7 +4,6 @@ import java.util.Hashtable; import org.bouncycastle.crypto.Digest;
import org.bouncycastle.util.Integers;
-import org.bouncycastle.util.encoders.Hex;
public class HashSP800DRBG implements SP80090DRBG
{
@@ -54,9 +53,7 @@ public class HashSP800DRBG implements SP80090DRBG // 6. Return V, C, and reseed_counter as the initial_working_state
byte[] entropy = entropySource.getEntropy();
-
- System.out.println("Constructor Entropy: "+ new String(Hex.encode(entropy)));
-
+
byte[] seedMaterial = new byte[entropy.length + nonce.length + personalisationString.length];
System.arraycopy(entropy, 0, seedMaterial, 0, entropy.length);
@@ -64,11 +61,7 @@ public class HashSP800DRBG implements SP80090DRBG System.arraycopy(personalisationString, 0, seedMaterial, entropy.length + nonce.length,
personalisationString.length);
- System.out.println("Constructor SeedMaterial: "+ new String(Hex.encode(seedMaterial)));
-
byte[] seed = hash_df(seedMaterial, _seedLength);
-
- System.out.println("Constructor Seed: "+ new String(Hex.encode(seed)));
_V = seed;
byte[] subV = new byte[_V.length + 1];
@@ -76,8 +69,8 @@ public class HashSP800DRBG implements SP80090DRBG _C = hash_df(subV, _seedLength);
_reseedCounter = 1;
- System.out.println("Constructor V: "+ new String(Hex.encode(_V)));
- System.out.println("Constructor C: "+ new String(Hex.encode(_C)));
+// System.out.println("Constructor V: "+ new String(Hex.encode(_V)));
+// System.out.println("Constructor C: "+ new String(Hex.encode(_C)));
}
@@ -140,8 +133,8 @@ public class HashSP800DRBG implements SP80090DRBG _reseedCounter++;
System.arraycopy(rv, 0, output, 0, output.length);
- System.out.println("Generate V: "+ new String(Hex.encode(_V)));
- System.out.println("Generate C: "+ new String(Hex.encode(_C)));
+// System.out.println("Generate V: "+ new String(Hex.encode(_V)));
+// System.out.println("Generate C: "+ new String(Hex.encode(_C)));
return numberOfBits;
}
@@ -189,7 +182,7 @@ public class HashSP800DRBG implements SP80090DRBG byte[] entropy = _entropySource.getEntropy();
- System.out.println("Reseed Entropy: "+ new String(Hex.encode(entropy)));
+// System.out.println("Reseed Entropy: "+ new String(Hex.encode(entropy)));
byte[] seedMaterial = new byte[1+ _V.length + entropy.length + additionalInput.length];
@@ -201,11 +194,11 @@ public class HashSP800DRBG implements SP80090DRBG pos += entropy.length;
System.arraycopy(additionalInput, 0, seedMaterial, pos ,additionalInput.length);
- System.out.println("Reseed SeedMaterial: "+ new String(Hex.encode(seedMaterial)));
+// System.out.println("Reseed SeedMaterial: "+ new String(Hex.encode(seedMaterial)));
byte[] seed = hash_df(seedMaterial, _seedLength);
- System.out.println("Reseed Seed: "+ new String(Hex.encode(seed)));
+// System.out.println("Reseed Seed: "+ new String(Hex.encode(seed)));
_V = seed;
byte[] subV = new byte[_V.length + 1];
@@ -214,8 +207,8 @@ public class HashSP800DRBG implements SP80090DRBG _C = hash_df(subV, _seedLength);
_reseedCounter = 1;
- System.out.println("Reseed V: "+ new String(Hex.encode(_V)));
- System.out.println("Reseed C: "+ new String(Hex.encode(_C)));
+// System.out.println("Reseed V: "+ new String(Hex.encode(_V)));
+// System.out.println("Reseed C: "+ new String(Hex.encode(_C)));
}
diff --git a/src/test/java/org/bouncycastle/crypto/test/HashDRBGTest.java b/src/test/java/org/bouncycastle/crypto/test/HashDRBGTest.java new file mode 100644 index 00000000..c2dbc3fd --- /dev/null +++ b/src/test/java/org/bouncycastle/crypto/test/HashDRBGTest.java @@ -0,0 +1,419 @@ +package org.bouncycastle.crypto.test; + +import org.bouncycastle.crypto.digests.SHA1Digest; +import org.bouncycastle.crypto.digests.SHA256Digest; +import org.bouncycastle.crypto.digests.SHA384Digest; +import org.bouncycastle.crypto.digests.SHA512Digest; +import org.bouncycastle.crypto.prng.HashSP800DRBG; +import org.bouncycastle.crypto.prng.SP80090DRBG; +import org.bouncycastle.util.encoders.Hex; +import org.bouncycastle.util.test.SimpleTest; + +/** + * DRBG Test + */ +public class HashDRBGTest + extends SimpleTest +{ + public String getName() + { + return this.getClass().getName(); + } + + public static void main(String[] args) + { + runTest(new HashDRBGTest()); + } + + private DRBGTestVector[] createTestVectorData() + { + return new DRBGTestVector[] + { + new DRBGTestVector( + new SHA1Digest(), + new SHA1EntropyProvider().get(440), + false, + "2021222324", + 80, + new String[] + { + "9F7CFF1ECA23E750F66326969F11800F12088BA68E441D15D888B3FE12BF66FE057494F4546DE2F1", + "B77AA5C0CD55BBCEED7574AF223AFD988C7EEC8EFF4A94E5E89D26A04F58FA79F5E0D3702D7A9A6A" + } + ), + new DRBGTestVector( + new SHA1Digest(), + new SHA1EntropyProvider().get(440), + false, + "2021222324", + 80, + new String[] + { + "AB438BD3B01A0AF85CFEE29F7D7B71621C4908B909124D430E7B406FB1086EA994C582E0D656D989", + "29D9098F987E7005314A0F51B3DD2B8122F4AED706735DE6AD5DDBF223177C1E5F3AEBC52FAB90B9" + }) + .setPersonalizationString("404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F70717273747576"), + new DRBGTestVector( + new SHA1Digest(), + new SHA1EntropyProvider().get(440), + false, + "2021222324", + 80, + new String[] + { + "E76B4EDD5C865BC8AFD809A59B69B429AC7F4352A579BCF3F75E56249A3491F87C3CA6848B0FAB25", + "6577B6B4F87A93240B199FE51A3B335313683103DECE171E3256FB7E803586CA4E45DD242EB01F70" + }) + .addAdditionalInput("606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F90919293949596") + .addAdditionalInput("A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6"), + new DRBGTestVector( + new SHA1Digest(), + new SHA1EntropyProvider().get(440), + true, + "2021222324", + 80, + new String[] + { + "56EF4913373994D5539F4D7D17AFE7448CDF5E72416CC6A71A340059FA0D5AE526B23250C46C0944", + "575B37A2739814F966C63B60A2C4F149CA9ACC84FC4B25493289B085C67B2E30F5F0B99A2C349E2A" + }), + new DRBGTestVector( + new SHA1Digest(), + new SHA1EntropyProvider().get(440), + true, + "2021222324", + 80, + new String[] + { + "532CA1165DCFF21C55592687639884AF4BC4B057DF8F41DE653AB44E2ADEC7C9303E75ABE277EDBF", + "73C2C67C696D686D0C4DBCEB5C2AF7DDF6F020B6874FAE4390F102117ECAAFF54418529A367005A0" + }) + .setPersonalizationString("404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F70717273747576"), + new DRBGTestVector( + new SHA1Digest(), + new SHA1EntropyProvider().get(440), + true, + "2021222324", + 80, + new String[] + { + "183C242A1430E46C4ED70B4DBE1BF9AB0AB8721CDCA2A2D1820AD6F6C956858543B2AA191D8D1287", + "F196F9BD021C745CBD5AC7BFCE48EAAF0D0E7C091FBF436940E63A198EE770D9A4F0718669AF2BC9" + }) + .addAdditionalInput("606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F90919293949596") + .addAdditionalInput("A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6"), + new DRBGTestVector( + new SHA256Digest(), + new SHA256EntropyProvider().get(440), + false, + "2021222324252627", + 128, + new String[] + { + "77E05A0E7DC78AB5D8934D5E93E82C06" + + "A07C04CEE6C9C53045EEB485872777CF3B3E35C474F976B8" + + "94BF301A86FA651F463970E89D4A0534B2ECAD29EC044E7E", + "5FF4BA493C40CFFF3B01E472C575668C" + + "CE3880B9290B05BFEDE5EC96ED5E9B2898508B09BC800EEE" + + "099A3C90602ABD4B1D4F343D497C6055C87BB956D53BF351" + } + ), + new DRBGTestVector( + new SHA384Digest(), + new SHA384EntropyProvider().get(888), + false, + "202122232425262728292A2B", + 192, + new String[] + { + "04FF23AD15E78790ADD36B438BBC097C7A11747CC2CCEEDE" + + "2C978B23B3DC63B732C953061D7764990ABFEFC47A581B92" + + "1BC0428C4F12212460E406A0F0651E7F0CB9A90ABFDB07B5" + + "25565C74F0AA085082F6CF213AAFAD0C0646895078F1E1FE", + "4F35B85F95DEE3E873054905CFD02341653E18F529930CBE" + + "14D909F37FEAF2C790D22FAE7516B4590BE35D53E2FE1A35" + + "AFE4B6607CB358589C3B4D094A1D81FE0717F1DF5BDDEB3E" + + "114F130BB781E66C22B5B770E8AE115FF39F8ADAF66DEEDF" + } + ), + new DRBGTestVector( + new SHA512Digest(), + new SHA512EntropyProvider().get(888), + false, + "202122232425262728292A2B2C2D2E2F", + 256, + new String[] + { + "DA126CF95C6BF97E" + + "2F731F2137A907ACC70FD7AC9EBACD1C6E31C74029B052E3" + + "AABC48F3B00993F2B2381F7650A55322A968C86E05DE88E6" + + "367F6EF89A601DB4342E9086C7AC13B5E56C32E9E668040B" + + "73847893C5BFD38A1CF44F348B4EEE4CD68ADB7E7B8C837F" + + "19BC4F902761F7CFF24AB1D704FD11C4E929D8553753B55D", + "400B977CE8A2BB6A" + + "84C6FD1CF901459685ABF5408CFF4588CEDF52E2D2DC300A" + + "A9B4FAED8CD0161C2172B1FD269253195883D6EBF21020F2" + + "C20E5F2C81AE60C8595B834A229B1F5B726C1125717E6207" + + "8886EF38E61E32707AD5F8116C6393DFB6E7C7AE0E8E92BB" + + "D7E0C3D04BBA02F5169F2F569A58158915FEE4C9D28D45DB" + } + ) + .setPersonalizationString( + "404142434445464748494A4B4C4D4E" + + "4F505152535455565758595A5B5C5D5E5F60616263646566" + + "6768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" + + "7F808182838485868788898A8B8C8D8E8F90919293949596" + + "9798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAE") + .addAdditionalInput( + "606162636465666768696A6B6C6D6E" + + "6F707172737475767778797A7B7C7D7E7F80818283848586" + + "8788898A8B8C8D8E8F909192939495969798999A9B9C9D9E" + + "9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6" + + "B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCE") + .addAdditionalInput( + "A0A1A2A3A4A5A6A7A8A9AAABACADAE" + + "AFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6" + + "C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDE" + + "DFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6" + + "F7F8F9FAFBFCFDFEFF000102030405060708090A0B0C0D0E"), + new DRBGTestVector( + new SHA512Digest(), + new SHA512EntropyProvider().get(888), + true, + "202122232425262728292A2B2C2D2E2F", + 256, + new String[] + { + "F93CA6855590A77F" + + "07354097E90E026648B6115DF008FFEDBD9D9811F54E8286" + + "EF00FDD6BA1E58DF2535E3FBDD9A9BA3754A97F36EE83322" + + "1582060A1F37FCE4EE8826636B28EAD589593F4CA8B64738" + + "8F24EB3F0A34796968D21BDEE6F81FD5DF93536F935937B8" + + "025EC8CBF57DDB0C61F2E41463CC1516D657DA2829C6BF90", + "4817618F48C60FB1" + + "CE5BFBDA0CAF4591882A31F6EE3FE0F78779992A06EC60F3" + + "7FB9A8D6108C231F0A927754B0599FA4FA27A4E25E065EF0" + + "3085B892979DC0E7A1080883CAEBFDFD3665A8F2D061C521" + + "F7D6E3DA2AF8B97B6B43B6EC831AF515070A83BBB9AC95ED" + + "4EF49B756A2377A5F0833D847E27A88DDB0C2CE4AD782E7B " + } + ), + new DRBGTestVector( + new SHA512Digest(), + new SHA512EntropyProvider().get(888), + true, + "202122232425262728292A2B2C2D2E2F", + 256, + new String[] + { + "0455DD4AD7DBACB2" + + "410BE58DF7248D765A4547ABAEE1743B0BCAD37EBD06DA7C" + + "F7CE5E2216E525327E9E2005EBEF2CE53BD733B18128627D" + + "3FD6153089373AF2606A1584646A0EA488BFEF45228699A0" + + "89CEA8AEC44502D86D9591F3552C688B7F7B45FCB0C3C2B9" + + "43C1CD8A6FC63DF4D81C3DA543C9CF2843855EA84E4F959C", + "C047D46D7F614E4E" + + "4A7952C79A451F8F7ACA379967E2977C401C626A2ED70D74" + + "A63660579A354115BC8C8C8CC3AEA3050686A0CFCDB6FA9C" + + "F78D4C2165BAF851C6F9B1CD16A2E14C15C6DAAC56C16E75" + + "FC84A14D58B41622E88B0F1B1995587FD8BAA999CBA98025" + + "4C8AB9A9691DF7B84D88B639A9A3106DEABEB63748B99C09" + } + ) + .addAdditionalInput( + "606162636465666768696A6B6C6D6E" + + "6F707172737475767778797A7B7C7D7E7F80818283848586" + + "8788898A8B8C8D8E8F909192939495969798999A9B9C9D9E" + + "9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6" + + "B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCE") + .addAdditionalInput( + "A0A1A2A3A4A5A6A7A8A9AAABACADAE" + + "AFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6" + + "C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDE" + + "DFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6" + + "F7F8F9FAFBFCFDFEFF000102030405060708090A0B0C0D0E"), + new DRBGTestVector( + new SHA512Digest(), + new SHA512EntropyProvider().get(888), + true, + "202122232425262728292A2B2C2D2E2F", + 256, + new String[] + { + "22EB93A67911DA73" + + "85D9180C78127DE1A04FF713114C07C9C615F7CC5EF72744" + + "A2DDCD7C3CB85E65DED8EF5F240FBDCBEBBDE2BAAC8ECF7D" + + "CBC8AC333E54607AD41DC495D83DF72A05EF55B127C1441C" + + "9A0EFFDA2C7954DB6C2D04342EB812E5E0B11D6C395F41ED" + + "A2702ECE5BA479E2DFA18F953097492636C12FE30CE5C968", + "E66698CFBF1B3F2E" + + "919C03036E584EAA81CF1C6666240AF05F70637043733954" + + "D8A1E5A66A04C53C6900FDC145D4A3A80A31F5868ACE9AC9" + + "4E14E2051F624A05EEA1F8B684AA5410BCE315E76EA07C71" + + "5D6F34731320FF0DCF78D795E6EFA2DF92B98BE636CDFBA2" + + "9008DD392112AEC202F2E481CB9D83F987FEA69CD1B368BB" + } + ) + .setPersonalizationString( + "404142434445464748494A4B4C4D4E" + + "4F505152535455565758595A5B5C5D5E5F60616263646566" + + "6768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" + + "7F808182838485868788898A8B8C8D8E8F90919293949596" + + "9798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAE"), + new DRBGTestVector( + new SHA512Digest(), + new SHA512EntropyProvider().get(888), + true, + "202122232425262728292A2B2C2D2E2F", + 256, + new String[] + { + "7596A76372308BD5" + + "A5613439934678B35521A94D81ABFE63A21ACF61ABB88B61" + + "E86A12C37F308F2BBBE32BE4B38D03AE808386494D70EF52" + + "E9E1365DD18B7784CAB826F31D47579E4D57F69D8BF3152B" + + "95741946CEBE58571DF58ED39980D9AF44E69F01E8989759" + + "8E40171101A0E3302838E0AD9E849C01988993CF9F6E5263", + "DBE5EE36FCD85301" + + "303E1C3617C1AC5E23C08885D0BEFAAD0C85A0D89F85B9F1" + + "6ECE3D88A24EB96504F2F13EFA7049621782F5DE2C416A0D" + + "294CCFE53545C4E309C48E1E285A2B829A574B72B3C2FBE1" + + "34D01E3706B486F2401B9820E17298A342666918E15B8462" + + "87F8C5AF2D96B20FAF3D0BB392E15F4A06CDB0DECD1B6AD7" + } + ) + .setPersonalizationString( + "404142434445464748494A4B4C4D4E" + + "4F505152535455565758595A5B5C5D5E5F60616263646566" + + "6768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" + + "7F808182838485868788898A8B8C8D8E8F90919293949596" + + "9798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAE") + .addAdditionalInput( + "606162636465666768696A6B6C6D6E" + + "6F707172737475767778797A7B7C7D7E7F80818283848586" + + "8788898A8B8C8D8E8F909192939495969798999A9B9C9D9E" + + "9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6" + + "B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCE") + .addAdditionalInput( + "A0A1A2A3A4A5A6A7A8A9AAABACADAE" + + "AFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6" + + "C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDE" + + "DFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6" + + "F7F8F9FAFBFCFDFEFF000102030405060708090A0B0C0D0E") + }; + } + + public void performTest() + throws Exception + { + DRBGTestVector[] tests = createTestVectorData(); + + for (int i = 0; i != tests.length; i++) + { + DRBGTestVector tv = tests[i]; + + byte[] nonce = tv.nonce(); + byte[] personalisationString = tv.personalizationString(); + + SP80090DRBG d = new HashSP800DRBG(tv.getDigest(), tv.entropySource(), nonce, personalisationString, tv.securityStrength()); + + byte[] output = new byte[tv.expectedValue(0).length]; + + d.generate(output, tv.additionalInput(0), tv.predictionResistance()); + + byte[] expected = tv.expectedValue(0); + + if (!areEqual(expected, output)) + { + fail("Test #" + (i + 1) + ".1 failed, expected " + new String(Hex.encode(tv.expectedValue(0))) + " got " + new String(Hex.encode(output))); + } + + output = new byte[tv.expectedValue(0).length]; + + d.generate(output, tv.additionalInput(1), tv.predictionResistance()); + + expected = tv.expectedValue(1); + if (!areEqual(expected, output)) + { + fail("Test #" + (i + 1) + ".2 failed, expected " + new String(Hex.encode(tv.expectedValue(1))) + " got " + new String(Hex.encode(output))); + } + } + } + + private class SHA1EntropyProvider + extends TestEntropySourceProvider + { + SHA1EntropyProvider() + { + super( + Hex.decode( + "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F30313233343536" + + "808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6" + + "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6"), true); + } + } + + private class SHA256EntropyProvider + extends TestEntropySourceProvider + { + SHA256EntropyProvider() + { + super(Hex.decode( + "00010203040506" + + "0708090A0B0C0D0E0F101112131415161718191A1B1C1D1E" + + "1F202122232425262728292A2B2C2D2E2F30313233343536" + + "80818283848586" + + "8788898A8B8C8D8E8F909192939495969798999A9B9C9D9E" + + "9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6" + + "C0C1C2C3C4C5C6" + + "C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDE" + + "DFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6"), true); + } + } + + private class SHA384EntropyProvider + extends TestEntropySourceProvider + { + SHA384EntropyProvider() + { + super(Hex.decode( + "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223242526" + + "2728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F50515253545556" + + "5758595A5B5C5D5E5F606162636465666768696A6B6C6D6E" + + "808182838485868788898A8B8C8D8E" + + "8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6" + + "A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBE" + + "BFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6" + + "D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEE" + + "C0C1C2C3C4C5C6C7C8C9CACBCCCDCE" + + "CFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6" + + "E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFE" + + "FF000102030405060708090A0B0C0D0E0F10111213141516" + + "1718191A1B1C1D1E1F202122232425262728292A2B2C2D2E"), true); + } + } + + private class SHA512EntropyProvider + extends TestEntropySourceProvider + { + SHA512EntropyProvider() + { + super(Hex.decode( + "000102030405060708090A0B0C0D0E" + + "0F101112131415161718191A1B1C1D1E1F20212223242526" + + "2728292A2B2C2D2E2F303132333435363738393A3B3C3D3E" + + "3F404142434445464748494A4B4C4D4E4F50515253545556" + + "5758595A5B5C5D5E5F606162636465666768696A6B6C6D6E" + + "808182838485868788898A8B8C8D8E" + + "8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6" + + "A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBE" + + "BFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6" + + "D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEE" + + "C0C1C2C3C4C5C6C7C8C9CACBCCCDCE" + + "CFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6" + + "E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFE" + + "FF000102030405060708090A0B0C0D0E0F10111213141516" + + "1718191A1B1C1D1E1F202122232425262728292A2B2C2D2E"), true); + } + } +} diff --git a/src/test/java/org/bouncycastle/crypto/test/HashDRGBTest.java b/src/test/java/org/bouncycastle/crypto/test/HashDRGBTest.java deleted file mode 100644 index 933e57f5..00000000 --- a/src/test/java/org/bouncycastle/crypto/test/HashDRGBTest.java +++ /dev/null @@ -1,279 +0,0 @@ -package org.bouncycastle.crypto.test; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.bouncycastle.crypto.Digest; -import org.bouncycastle.crypto.digests.SHA1Digest; -import org.bouncycastle.crypto.prng.EntropySource; -import org.bouncycastle.crypto.prng.HashSP800DRBG; -import org.bouncycastle.crypto.prng.SP80090DRBG; -import org.bouncycastle.util.encoders.Hex; -import org.bouncycastle.util.test.SimpleTest; -import org.bouncycastle.util.test.TestResult; - -/** - * DRBG Test - */ -public class HashDRGBTest extends SimpleTest -{ - public String getName() - { - return this.getClass().getName(); - } - - public static void main(String[] args) - { - HashDRGBTest test = new HashDRGBTest(); - TestResult result = test.perform(); - - if (result.getException() != null) - { - result.getException().printStackTrace(); - } - else - { - System.out.println("TEST PASSSED: "+result); - } - } - - private class TestVector - { - - private String _entropy; - private boolean _pr; - private String _nonce; - private String _personalisation; - private int _ss; - private String _ev; - private boolean _magicalReseed = false; - private List _ai = new ArrayList(); - - public TestVector(String entropy, boolean predictionResistance, String nonce, int securityStrength, String expected) - { - _entropy = entropy; - _pr = predictionResistance; - _nonce = nonce; - _ss = securityStrength; - _ev = expected; - _personalisation = ""; - } - - public void setAdditionalInput(String input) - { - _ai.add(input); - } - - public void setPersonalisationString(String p) - { - _personalisation = p; - } - - public void setMagicalReseed() - { - _magicalReseed = true; - } - - // some of the test vectors need to force a reseed after the first generate. - // it seems the easiest way to do this is pretend that you want the generate to - // behave with prediction resistance (short of forcing reseed to go past RESEED_MAX) - public boolean reseed() - { - return _magicalReseed; - } - - public String entropy() - { - return _entropy; - } - - public boolean predictionResistance() - { - return _pr; - } - - public String nonce() - { - return _nonce; - } - - public String personalisation() - { - return _personalisation; - } - - public int securityStrength() - { - return _ss; - } - - public String expectedValue() - { - return _ev; - } - - public byte[] additionalInput(int position) - { - int len = _ai.size(); - byte[] rv; - if (position >= len) { - rv = null; - } - else { - rv = Hex.decode((String)(_ai.get(position))); - } - return rv; - } - - } - - private Collection createTestVectorData() - { - Collection rv = new ArrayList(); - - TestVector tv = null; - - // line 7 - tv = new TestVector( - "a37a3e08d8393feb01c4d78cb6a4d1e210c288c89e9838176bc78946745f1c5bea44cf15e061601bfd45f7b3b95be924", - true, - "8243299805c0877e", - 128, - "a05002f98d5676e1b2e3b3d4686bb9055a830a39"); - rv.add(tv); - - // line 27 - tv = new TestVector( - "28955584d8f219b7d23fa72c18e02d100af4889bdc691b9739ececa1dc77cb84aa87ff265d7db170d81cdd8ff05602bf", - true, - "72847c01a49c2686", - 128, - "085bfc190fdc6017c00c5beb6678878f6a12375a"); - rv.add(tv); - - // line 40 - tv = new TestVector( - "9a7ff1e2cc253c9699f307b82c14bddf67f18acf6e1bd8accb4bc3cacfcd81fd681138d4ff1a53baea0794a6216c443b", - true, - "1bef424a7dc0faea", - 128, - "eb829d215ac55efd6e964f671b7587e3c1439ce8"); - rv.add(tv); - - // line 217 of Hash_DRBG.txt - tv = new TestVector( - "55d201f2e3e2a6b42c95e73539ccf3ca51457ff8639e023be8a9c891ad318aa5b9bbf6b48d14d647d20708e7782bd7e0", // entropy - true, // prediction resistance - "438bc46d0de7db69", // nonce - 128, // security strength - "aed4af08f9c1bda495338c305946d4c94452a785"); // result - tv.setAdditionalInput("4bd19fd0f73d92373a3633375a367ee2"); // additional input - tv.setAdditionalInput("b247178c21b266432594738d2430cb1d"); // additional input - rv.add(tv); - - // line 230 of Hash_DRBG.txt - tv = new TestVector( - "78df90663fdf3a983cb8a3dec289e53e51500dd8fdd0d720cde174e7beeb6891f3b8b607e81274b03f69b87204c922a8", // entropy - true, // prediction resistance - "496caad48119623f", // nonce - 128, // security strength - "c2b224d922e5c5553143e8c72ea2482984506593"); // result - tv.setAdditionalInput("4bdc2db0916e671c1f6ede8d6af05cce"); // additional input - tv.setAdditionalInput("f79216128f3ec4d96194de0cfccba17d"); // additional input - rv.add(tv); - - // line 243 of Hash_DRBG.txt - tv = new TestVector( - "addcbba340a3ade464d16723e5df651c73a91ca11a672f3ffc29aa7bdc14d0ce17ef0235e1ce1779e38541b377dc6154", // entropy - true, // prediction resistance - "71856d19a46fe11d", // nonce - 128, // security strength - "d91b6fdedd5369ef50c743d24599e358b557711e"); // result - tv.setAdditionalInput("f6f8eb121f776e339ddfdbf47bbd7d4c"); // additional input - tv.setAdditionalInput("f3a685a580fb8b58c82162bdf49d5a75"); // additional input - rv.add(tv); - - // line 419 of Hash_DRBG.txt - tv = new TestVector( - "2616ae30ee1bc618f44cd700deabafb4602564ed770090946e25ffcd6d6de597709d0bf81e88c6af6b3996985c94feec", // entropy - true, // prediction resistance - "20c760bedbdc6f8e", // nonce - 128, // security strength - "dc675ef7de1a45b14bfc2169848e51fda0db1c9c"); // result - tv.setPersonalisationString("0bdd90e6bedfc8d611a3fd2409604086"); - rv.add(tv); - - // line 621 of Hash_DRBG.txt - tv = new TestVector( - "d236a5273173dd114f93bde231a59113c9839e16f61c0fb2ec6031a9cba9367a4e8c499b4a5c9b9c3aeefbd2aecd8cc4", // entropy - true, // prediction resistance - "b5b360eff76331f3", // nonce - 128, // security strength - "50b4b4cd6857fc2ec152ccf668a481ed7ee41d87"); // result - tv.setPersonalisationString("d4bb0210b271db81d6f04260daea7752"); - tv.setAdditionalInput("4dd26c87fb2c4fa68d1663226a51e3f8"); // additional input - tv.setAdditionalInput("f9e8d2721334956f1549479916031947"); // additional input - rv.add(tv); - - // line 823 of Hash_DRBG.txt - tv = new TestVector( - "8e898b45a75350dd7ae796d0e6b687f42d9e4ff91948f5e29d5d7c9b28735738", // entropy - false, // prediction resistance - "627c2efd05cefb2c", // nonce - 128, // security strength - "538c9cde9432015281eeec13f4ce712624731b72"); // result - tv.setMagicalReseed(); - rv.add(tv); - - // line 837 of Hash_DRBG.txt - tv = new TestVector( - "2a5d2262d53cc928c5d45e028e2e049ea46a8f1b5fcf69229089e045649b9988", // entropy - false, // prediction resistance - "176e55882bd7cd42", // nonce - 128, // security strength - "a1578253684465e65dae7872f1979edd5f961208"); // result - tv.setMagicalReseed(); - rv.add(tv); - - - return rv; - } - - public void performTest() throws Exception - { - Collection<TestVector> tests = createTestVectorData(); - - int c = 0; - for (TestVector tv : tests) - { - tv.entropy(); - Digest digest = new SHA1Digest(); - EntropySource tes = new TestEntropySourceProvider(Hex.decode(tv.entropy()), tv.predictionResistance()).get(tv.securityStrength()); - byte[] nonce = Hex.decode(tv.nonce()); - byte[] personalisationString = Hex.decode(tv.personalisation()); - int securityStrength = tv.securityStrength(); - SP80090DRBG d = new HashSP800DRBG(digest, tes, nonce, personalisationString, securityStrength); - - byte[] output = new byte[20]; - - d.generate(output, tv.additionalInput(0), tv.predictionResistance()); - String out = new String(Hex.encode(output)); - System.out.println(out); - d.generate(output, tv.additionalInput(1), tv.reseed() ? true : tv.predictionResistance()); - out = new String(Hex.encode(output)); - System.out.println(out); - - byte[] expected = Hex.decode(tv.expectedValue()); - - ++c; - if (!areEqual(expected, output)) - { - System.out.println("Test #"+c+" failed"); - throw new Exception("Test Vector Failed, expected "+tv.expectedValue()+" got "+out); - } - System.out.println("Test Vector Passed, expected ; "+tv.expectedValue()+" got "+out); - } - System.out.println("Total tests completed = "+c+" of "+tests.size()); - } -} diff --git a/src/test/java/org/bouncycastle/crypto/test/RegressionTest.java b/src/test/java/org/bouncycastle/crypto/test/RegressionTest.java index a8c5801c..1327ea80 100644 --- a/src/test/java/org/bouncycastle/crypto/test/RegressionTest.java +++ b/src/test/java/org/bouncycastle/crypto/test/RegressionTest.java @@ -113,7 +113,7 @@ public class RegressionTest new DSTU4145Test(), new SipHashTest(), new OCBTest(), - new HashDRGBTest(), + new HashDRBGTest(), new HMacDRBGTest(), new CTRDRGBTest(), new DualECDRBGTest() |