PageRenderTime 127ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/java-1.7.0-openjdk/mauve-2008-10-22/gnu/testlet/gnu/javax/crypto/cipher/TestOfBlowfish.java

#
Java | 148 lines | 106 code | 11 blank | 31 comment | 3 complexity | d7d4e84a0a23af646c526e67ef62f963 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /* TestOfBlowfish.java --
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of Mauve.
  4. Mauve is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. Mauve is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Mauve; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. */
  17. // Tags: GNU-CRYPTO JDK1.4
  18. // Uses: BaseCipherTestCase
  19. package gnu.testlet.gnu.javax.crypto.cipher;
  20. import gnu.java.security.util.Util;
  21. import gnu.javax.crypto.cipher.Blowfish;
  22. import gnu.javax.crypto.cipher.IBlockCipher;
  23. import gnu.testlet.TestHarness;
  24. import java.util.Arrays;
  25. import java.util.HashMap;
  26. /**
  27. * Conformance test for the Blowfish cipher.
  28. */
  29. public class TestOfBlowfish extends BaseCipherTestCase
  30. {
  31. /**
  32. * Eric Young's Blowfish test vectors. See <a
  33. * href="http://www.counterpane.com/vectors.txt">http://www.counterpane.com/vectors.txt</a>.
  34. */
  35. static final String[][] TV = {
  36. // key bytes clear bytes cipher bytes
  37. { "0000000000000000", "0000000000000000", "4EF997456198DD78" },
  38. { "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "51866FD5B85ECB8A" },
  39. { "3000000000000000", "1000000000000001", "7D856F9A613063F2" },
  40. { "1111111111111111", "1111111111111111", "2466DD878B963C9D" },
  41. { "0123456789ABCDEF", "1111111111111111", "61F9C3802281B096" },
  42. { "1111111111111111", "0123456789ABCDEF", "7D0CC630AFDA1EC7" },
  43. { "0000000000000000", "0000000000000000", "4EF997456198DD78" },
  44. { "FEDCBA9876543210", "0123456789ABCDEF", "0ACEAB0FC6A0A28D" },
  45. { "7CA110454A1A6E57", "01A1D6D039776742", "59C68245EB05282B" },
  46. { "0131D9619DC1376E", "5CD54CA83DEF57DA", "B1B8CC0B250F09A0" },
  47. { "07A1133E4A0B2686", "0248D43806F67172", "1730E5778BEA1DA4" },
  48. { "3849674C2602319E", "51454B582DDF440A", "A25E7856CF2651EB" },
  49. { "04B915BA43FEB5B6", "42FD443059577FA2", "353882B109CE8F1A" },
  50. { "0113B970FD34F2CE", "059B5E0851CF143A", "48F4D0884C379918" },
  51. { "0170F175468FB5E6", "0756D8E0774761D2", "432193B78951FC98" },
  52. { "43297FAD38E373FE", "762514B829BF486A", "13F04154D69D1AE5" },
  53. { "07A7137045DA2A16", "3BDD119049372802", "2EEDDA93FFD39C79" },
  54. { "04689104C2FD3B2F", "26955F6835AF609A", "D887E0393C2DA6E3" },
  55. { "37D06BB516CB7546", "164D5E404F275232", "5F99D04F5B163969" },
  56. { "1F08260D1AC2465E", "6B056E18759F5CCA", "4A057A3B24D3977B" },
  57. { "584023641ABA6176", "004BD6EF09176062", "452031C1E4FADA8E" },
  58. { "025816164629B007", "480D39006EE762F2", "7555AE39F59B87BD" },
  59. { "49793EBC79B3258F", "437540C8698F3CFA", "53C55F9CB49FC019" },
  60. { "4FB05E1515AB73A7", "072D43A077075292", "7A8E7BFA937E89A3" },
  61. { "49E95D6D4CA229BF", "02FE55778117F12A", "CF9C5D7A4986ADB5" },
  62. { "018310DC409B26D6", "1D9D5C5018F728C2", "D1ABB290658BC778" },
  63. { "1C587F1C13924FEF", "305532286D6F295A", "55CB3774D13EF201" },
  64. { "0101010101010101", "0123456789ABCDEF", "FA34EC4847B268B2" },
  65. { "1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "A790795108EA3CAE" },
  66. { "E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "C39E072D9FAC631D" },
  67. { "0000000000000000", "FFFFFFFFFFFFFFFF", "014933E0CDAFF6E4" },
  68. { "FFFFFFFFFFFFFFFF", "0000000000000000", "F21E9A77B71C49BC" },
  69. { "0123456789ABCDEF", "0000000000000000", "245946885754369A" },
  70. { "FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "6B5C5A9C5D9E0A5A" } };
  71. public void test(TestHarness harness)
  72. {
  73. harness.checkPoint("TestOfBlowfish");
  74. cipher = new Blowfish();
  75. HashMap attrib = new HashMap();
  76. attrib.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(8));
  77. attrib.put(IBlockCipher.KEY_MATERIAL, new byte[16]);
  78. try
  79. {
  80. harness.check(validityTest(), "validityTest()");
  81. harness.check(cloneabilityTest(), "cloneabilityTest()");
  82. harness.check(vectorsTest(), "vectorsTest()");
  83. }
  84. catch (Exception x)
  85. {
  86. harness.debug(x);
  87. harness.fail("TestOfBlowfish");
  88. }
  89. }
  90. /** Test cloneability. */
  91. protected boolean cloneabilityTest() throws Exception
  92. {
  93. int blockSize = cipher.defaultBlockSize();
  94. int keySize = cipher.defaultKeySize();
  95. byte[] pt = new byte[blockSize];
  96. byte[] ct1 = new byte[blockSize];
  97. byte[] ct2 = new byte[blockSize];
  98. byte[] kb = new byte[keySize];
  99. HashMap attributes = new HashMap();
  100. attributes.put(IBlockCipher.KEY_MATERIAL, kb);
  101. cipher.reset();
  102. cipher.init(attributes);
  103. cipher.encryptBlock(pt, 0, pt, 0);
  104. IBlockCipher thomas = (IBlockCipher) cipher.clone();
  105. thomas.init(attributes);
  106. cipher.encryptBlock(pt, 0, ct1, 0);
  107. thomas.encryptBlock(pt, 0, ct2, 0);
  108. return Arrays.equals(ct1, ct2);
  109. }
  110. protected boolean vectorsTest() throws Exception
  111. {
  112. HashMap attrib = new HashMap();
  113. byte[] kb, pt, ct1, ct2 = new byte[8], cpt = new byte[8];
  114. for (int i = 0; i < TV.length; i++)
  115. {
  116. kb = Util.toBytesFromString(TV[i][0]);
  117. pt = Util.toBytesFromString(TV[i][1]);
  118. ct1 = Util.toBytesFromString(TV[i][2]);
  119. attrib.put(IBlockCipher.KEY_MATERIAL, kb);
  120. cipher.reset();
  121. cipher.init(attrib);
  122. cipher.encryptBlock(pt, 0, ct2, 0);
  123. if (!Arrays.equals(ct1, ct2))
  124. {
  125. return false;
  126. }
  127. cipher.decryptBlock(ct2, 0, cpt, 0);
  128. if (!Arrays.equals(pt, cpt))
  129. return false;
  130. }
  131. return true;
  132. }
  133. }