PageRenderTime 90ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gnu-crypto/source/gnu/testlet/gnu/crypto/cipher/TestOfBlowfish.java

https://github.com/clibrepo/04f6ea02286af71632b72d73554979a61da424cf681886674b21ecd595715c12
Java | 180 lines | 99 code | 17 blank | 64 comment | 3 complexity | d79088d887593f3c0b355ebbc8f7b9e0 MD5 | raw file
  1. package gnu.testlet.gnu.crypto.cipher;
  2. // ----------------------------------------------------------------------------
  3. // $Id: TestOfBlowfish.java,v 1.3 2003/09/27 00:04:20 raif Exp $
  4. //
  5. // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
  6. //
  7. // This file is part of GNU Crypto.
  8. //
  9. // GNU Crypto is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation; either version 2, or (at your option)
  12. // any later version.
  13. //
  14. // GNU Crypto is distributed in the hope that it will be useful, but
  15. // WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program; see the file COPYING. If not, write to the
  21. //
  22. // Free Software Foundation Inc.,
  23. // 59 Temple Place - Suite 330,
  24. // Boston, MA 02111-1307
  25. // USA
  26. //
  27. // Linking this library statically or dynamically with other modules is
  28. // making a combined work based on this library. Thus, the terms and
  29. // conditions of the GNU General Public License cover the whole
  30. // combination.
  31. //
  32. // As a special exception, the copyright holders of this library give
  33. // you permission to link this library with independent modules to
  34. // produce an executable, regardless of the license terms of these
  35. // independent modules, and to copy and distribute the resulting
  36. // executable under terms of your choice, provided that you also meet,
  37. // for each linked independent module, the terms and conditions of the
  38. // license of that module. An independent module is a module which is
  39. // not derived from or based on this library. If you modify this
  40. // library, you may extend this exception to your version of the
  41. // library, but you are not obligated to do so. If you do not wish to
  42. // do so, delete this exception statement from your version.
  43. // ----------------------------------------------------------------------------
  44. // Tags: GNU-CRYPTO
  45. // Uses: BaseCipherTestCase
  46. import gnu.crypto.cipher.Blowfish;
  47. import gnu.crypto.cipher.IBlockCipher;
  48. import gnu.crypto.util.Util;
  49. import gnu.testlet.TestHarness;
  50. import java.util.Arrays;
  51. import java.util.HashMap;
  52. /**
  53. * <p>Conformance test for the Blowfish cipher.</p>
  54. *
  55. * @version $Revision: 1.3 $
  56. */
  57. public class TestOfBlowfish extends BaseCipherTestCase {
  58. // Constants and variables.
  59. // -----------------------------------------------------------------------
  60. /**
  61. * Eric Young's Blowfish test vectors. See <a
  62. * href="http://www.counterpane.com/vectors.txt">http://www.counterpane.com/vectors.txt</a>.
  63. */
  64. static final String[][] TV = {
  65. // key bytes clear bytes cipher bytes
  66. { "0000000000000000", "0000000000000000", "4EF997456198DD78" },
  67. { "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "51866FD5B85ECB8A" },
  68. { "3000000000000000", "1000000000000001", "7D856F9A613063F2" },
  69. { "1111111111111111", "1111111111111111", "2466DD878B963C9D" },
  70. { "0123456789ABCDEF", "1111111111111111", "61F9C3802281B096" },
  71. { "1111111111111111", "0123456789ABCDEF", "7D0CC630AFDA1EC7" },
  72. { "0000000000000000", "0000000000000000", "4EF997456198DD78" },
  73. { "FEDCBA9876543210", "0123456789ABCDEF", "0ACEAB0FC6A0A28D" },
  74. { "7CA110454A1A6E57", "01A1D6D039776742", "59C68245EB05282B" },
  75. { "0131D9619DC1376E", "5CD54CA83DEF57DA", "B1B8CC0B250F09A0" },
  76. { "07A1133E4A0B2686", "0248D43806F67172", "1730E5778BEA1DA4" },
  77. { "3849674C2602319E", "51454B582DDF440A", "A25E7856CF2651EB" },
  78. { "04B915BA43FEB5B6", "42FD443059577FA2", "353882B109CE8F1A" },
  79. { "0113B970FD34F2CE", "059B5E0851CF143A", "48F4D0884C379918" },
  80. { "0170F175468FB5E6", "0756D8E0774761D2", "432193B78951FC98" },
  81. { "43297FAD38E373FE", "762514B829BF486A", "13F04154D69D1AE5" },
  82. { "07A7137045DA2A16", "3BDD119049372802", "2EEDDA93FFD39C79" },
  83. { "04689104C2FD3B2F", "26955F6835AF609A", "D887E0393C2DA6E3" },
  84. { "37D06BB516CB7546", "164D5E404F275232", "5F99D04F5B163969" },
  85. { "1F08260D1AC2465E", "6B056E18759F5CCA", "4A057A3B24D3977B" },
  86. { "584023641ABA6176", "004BD6EF09176062", "452031C1E4FADA8E" },
  87. { "025816164629B007", "480D39006EE762F2", "7555AE39F59B87BD" },
  88. { "49793EBC79B3258F", "437540C8698F3CFA", "53C55F9CB49FC019" },
  89. { "4FB05E1515AB73A7", "072D43A077075292", "7A8E7BFA937E89A3" },
  90. { "49E95D6D4CA229BF", "02FE55778117F12A", "CF9C5D7A4986ADB5" },
  91. { "018310DC409B26D6", "1D9D5C5018F728C2", "D1ABB290658BC778" },
  92. { "1C587F1C13924FEF", "305532286D6F295A", "55CB3774D13EF201" },
  93. { "0101010101010101", "0123456789ABCDEF", "FA34EC4847B268B2" },
  94. { "1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "A790795108EA3CAE" },
  95. { "E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "C39E072D9FAC631D" },
  96. { "0000000000000000", "FFFFFFFFFFFFFFFF", "014933E0CDAFF6E4" },
  97. { "FFFFFFFFFFFFFFFF", "0000000000000000", "F21E9A77B71C49BC" },
  98. { "0123456789ABCDEF", "0000000000000000", "245946885754369A" },
  99. { "FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "6B5C5A9C5D9E0A5A" }
  100. };
  101. // Constructors.
  102. // -----------------------------------------------------------------------
  103. // default 0-arguments constructor
  104. // Class methods.
  105. // -----------------------------------------------------------------------
  106. // Instance methods.
  107. // -----------------------------------------------------------------------
  108. public void test(TestHarness harness) {
  109. harness.checkPoint("TestOfBlowfish");
  110. cipher = new Blowfish();
  111. HashMap attrib = new HashMap();
  112. attrib.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(8));
  113. attrib.put(IBlockCipher.KEY_MATERIAL, new byte[16]);
  114. try {
  115. harness.check(validityTest(), "validityTest()");
  116. harness.check(cloneabilityTest(), "cloneabilityTest()");
  117. harness.check(vectorsTest(), "vectorsTest()");
  118. } catch (Exception x) {
  119. harness.debug(x);
  120. harness.fail("TestOfBlowfish");
  121. }
  122. }
  123. /** Test cloneability. */
  124. protected boolean cloneabilityTest() throws Exception {
  125. int blockSize = cipher.defaultBlockSize();
  126. int keySize = cipher.defaultKeySize();
  127. byte[] pt = new byte[blockSize];
  128. byte[] ct1 = new byte[blockSize];
  129. byte[] ct2 = new byte[blockSize];
  130. byte[] kb = new byte[keySize];
  131. HashMap attributes = new HashMap();
  132. attributes.put(IBlockCipher.KEY_MATERIAL, kb);
  133. cipher.reset();
  134. cipher.init(attributes);
  135. cipher.encryptBlock(pt, 0, pt, 0);
  136. IBlockCipher thomas = (IBlockCipher) cipher.clone();
  137. thomas.init(attributes);
  138. cipher.encryptBlock(pt, 0, ct1, 0);
  139. thomas.encryptBlock(pt, 0, ct2, 0);
  140. return Arrays.equals(ct1, ct2);
  141. }
  142. protected boolean vectorsTest() throws Exception {
  143. HashMap attrib = new HashMap();
  144. byte[] kb, pt, ct1, ct2 = new byte[8], cpt = new byte[8];
  145. for (int i = 0; i < TV.length; i++) {
  146. kb = Util.toBytesFromString(TV[i][0]);
  147. pt = Util.toBytesFromString(TV[i][1]);
  148. ct1 = Util.toBytesFromString(TV[i][2]);
  149. attrib.put(IBlockCipher.KEY_MATERIAL, kb);
  150. cipher.reset();
  151. cipher.init(attrib);
  152. cipher.encryptBlock(pt, 0, ct2, 0);
  153. if (!Arrays.equals(ct1, ct2)) {
  154. return false;
  155. }
  156. cipher.decryptBlock(ct2, 0, cpt, 0);
  157. if (!Arrays.equals(pt, cpt)) {
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. }