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

/java-1.7.0-openjdk/mauve-2008-10-22/gnu/testlet/gnu/javax/crypto/assembly/TestOfAssembly.java

#
Java | 183 lines | 125 code | 25 blank | 33 comment | 1 complexity | db6dbc63340d77ab45e516d2ad161c6b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /* TestOfAssembly.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. package gnu.testlet.gnu.javax.crypto.assembly;
  19. import gnu.java.security.Registry;
  20. import gnu.javax.crypto.assembly.Assembly;
  21. import gnu.javax.crypto.assembly.Cascade;
  22. import gnu.javax.crypto.assembly.Direction;
  23. import gnu.javax.crypto.assembly.Stage;
  24. import gnu.javax.crypto.assembly.Transformer;
  25. import gnu.javax.crypto.assembly.TransformerException;
  26. import gnu.javax.crypto.cipher.Blowfish;
  27. import gnu.javax.crypto.cipher.IBlockCipher;
  28. import gnu.javax.crypto.mode.IMode;
  29. import gnu.javax.crypto.mode.ModeFactory;
  30. import gnu.javax.crypto.pad.IPad;
  31. import gnu.javax.crypto.pad.PadFactory;
  32. import gnu.testlet.TestHarness;
  33. import gnu.testlet.Testlet;
  34. import java.io.ByteArrayOutputStream;
  35. import java.util.Arrays;
  36. import java.util.HashMap;
  37. /**
  38. * Simple symmetry tests for 3 assembly constructions.
  39. */
  40. public class TestOfAssembly implements Testlet
  41. {
  42. private Assembly asm;
  43. private HashMap attributes = new HashMap();
  44. private HashMap modeAttributes = new HashMap();
  45. public TestOfAssembly()
  46. {
  47. super();
  48. }
  49. public void test(TestHarness harness)
  50. {
  51. TestOfAssembly testcase = new TestOfAssembly();
  52. // build an OFB-Blowfish cascade
  53. Cascade ofbBlowfish = new Cascade();
  54. Object modeNdx = ofbBlowfish.append(Stage.getInstance(
  55. ModeFactory.getInstance(
  56. Registry.OFB_MODE, new Blowfish(), 8),
  57. Direction.FORWARD));
  58. testcase.attributes.put(modeNdx, testcase.modeAttributes);
  59. IPad pkcs7 = PadFactory.getInstance(Registry.PKCS7_PAD);
  60. testcase.asm = new Assembly();
  61. testcase.asm.addPreTransformer(Transformer.getCascadeTransformer(ofbBlowfish));
  62. testcase.asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
  63. testcase.testSymmetry(harness, 1);
  64. // add a compression transformer.
  65. // the resulting assembly encrypts + pad first and compresses later
  66. // testcase.asm = new Assembly();
  67. // testcase.asm.addPreTransformer(Transformer.getCascadeTransformer(ofbBlowfish));
  68. // testcase.asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
  69. testcase.asm.addPostTransformer(Transformer.getDeflateTransformer());
  70. testcase.testSymmetry(harness, 2);
  71. // now build an assembly that compresses first and encrypts + pads later
  72. testcase.asm = new Assembly();
  73. testcase.asm.addPreTransformer(Transformer.getCascadeTransformer(ofbBlowfish));
  74. testcase.asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
  75. testcase.asm.addPreTransformer(Transformer.getDeflateTransformer());
  76. testcase.testSymmetry(harness, 3);
  77. }
  78. private void testSymmetry(TestHarness harness, int ndx)
  79. {
  80. harness.checkPoint("TestOfAssembly.testSymmetry#" + ndx);
  81. byte[] km = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
  82. byte[] iv = new byte[] { -1, -2, -3, -4, -5, -6, -7, -8, -9 };
  83. byte[] pt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  84. byte[] tpt = new byte[11 * pt.length];
  85. // forward
  86. modeAttributes.put(IBlockCipher.KEY_MATERIAL, km);
  87. modeAttributes.put(IMode.IV, iv);
  88. attributes.put(Assembly.DIRECTION, Direction.FORWARD);
  89. try
  90. {
  91. asm.init(attributes);
  92. }
  93. catch (TransformerException x)
  94. {
  95. harness.debug(x);
  96. harness.fail("Forward initialisation");
  97. return;
  98. }
  99. byte[] ct = null;
  100. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  101. try
  102. {
  103. for (int i = 0; i < 10; i++)
  104. { // transform in parts of 12-byte a time
  105. System.arraycopy(pt, 0, tpt, i * pt.length, pt.length);
  106. ct = asm.update(pt);
  107. baos.write(ct, 0, ct.length);
  108. }
  109. }
  110. catch (TransformerException x)
  111. {
  112. harness.debug(x);
  113. harness.fail("Forward transformation");
  114. return;
  115. }
  116. try
  117. {
  118. System.arraycopy(pt, 0, tpt, 10 * pt.length, pt.length);
  119. ct = asm.lastUpdate(pt);
  120. }
  121. catch (TransformerException x)
  122. {
  123. harness.debug(x);
  124. harness.fail("Forward last transformation");
  125. return;
  126. }
  127. baos.write(ct, 0, ct.length);
  128. ct = baos.toByteArray();
  129. // reversed
  130. attributes.put(Assembly.DIRECTION, Direction.REVERSED);
  131. try
  132. {
  133. asm.init(attributes);
  134. }
  135. catch (TransformerException x)
  136. {
  137. harness.debug(x);
  138. harness.fail("Reverse initialisation");
  139. return;
  140. }
  141. byte[] ot;
  142. try
  143. {
  144. ot = asm.lastUpdate(ct); // transform the lot in one go
  145. }
  146. catch (TransformerException x)
  147. {
  148. harness.debug(x);
  149. harness.fail("Reverse transformation");
  150. return;
  151. }
  152. harness.check(Arrays.equals(ot, tpt), "symmetric test");
  153. }
  154. }