PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gnu-crypto/source/gnu/testlet/gnu/crypto/assembly/TestOfAssembly.java

https://github.com/clibrepo/04f6ea02286af71632b72d73554979a61da424cf681886674b21ecd595715c12
Java | 198 lines | 105 code | 28 blank | 65 comment | 1 complexity | 1166db364883b6f20040d4f3716a3d33 MD5 | raw file
  1. package gnu.testlet.gnu.crypto.assembly;
  2. // ----------------------------------------------------------------------------
  3. // $Id: TestOfAssembly.java,v 1.3 2003/09/27 00:03:53 raif Exp $
  4. //
  5. // Copyright (C) 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. import gnu.crypto.Registry;
  46. import gnu.crypto.assembly.Assembly;
  47. import gnu.crypto.assembly.Cascade;
  48. import gnu.crypto.assembly.Direction;
  49. import gnu.crypto.assembly.Stage;
  50. import gnu.crypto.assembly.Transformer;
  51. import gnu.crypto.assembly.TransformerException;
  52. import gnu.crypto.cipher.Blowfish;
  53. import gnu.crypto.cipher.IBlockCipher;
  54. import gnu.crypto.mode.IMode;
  55. import gnu.crypto.mode.ModeFactory;
  56. import gnu.crypto.pad.IPad;
  57. import gnu.crypto.pad.PadFactory;
  58. import gnu.testlet.TestHarness;
  59. import gnu.testlet.Testlet;
  60. import java.io.ByteArrayOutputStream;
  61. import java.util.Arrays;
  62. import java.util.HashMap;
  63. /**
  64. * <p>Simple symmetry tests for 3 assembly constructions.</p>
  65. *
  66. * @version $Revision: 1.3 $
  67. */
  68. public class TestOfAssembly implements Testlet {
  69. // Constants and variables
  70. // -------------------------------------------------------------------------
  71. private Assembly asm;
  72. private HashMap attributes = new HashMap();
  73. private HashMap modeAttributes = new HashMap();
  74. // Constructor(s)
  75. // -------------------------------------------------------------------------
  76. public TestOfAssembly() {
  77. super();
  78. }
  79. // Class methods
  80. // -------------------------------------------------------------------------
  81. // Instance methods
  82. // -------------------------------------------------------------------------
  83. public void test(TestHarness harness) {
  84. TestOfAssembly testcase = new TestOfAssembly();
  85. // build an OFB-Blowfish cascade
  86. Cascade ofbBlowfish = new Cascade();
  87. Object modeNdx = ofbBlowfish.append(
  88. Stage.getInstance(
  89. ModeFactory.getInstance(Registry.OFB_MODE, new Blowfish(), 8),
  90. Direction.FORWARD));
  91. testcase.attributes.put(modeNdx, testcase.modeAttributes);
  92. IPad pkcs7 = PadFactory.getInstance(Registry.PKCS7_PAD);
  93. testcase.asm = new Assembly();
  94. testcase.asm.addPreTransformer(Transformer.getCascadeTransformer(ofbBlowfish));
  95. testcase.asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
  96. testcase.testSymmetry(harness, 1);
  97. // add a compression transformer.
  98. // the resulting assembly encrypts + pad first and compresses later
  99. // testcase.asm = new Assembly();
  100. // testcase.asm.addPreTransformer(Transformer.getCascadeTransformer(ofbBlowfish));
  101. // testcase.asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
  102. testcase.asm.addPostTransformer(Transformer.getDeflateTransformer());
  103. testcase.testSymmetry(harness, 2);
  104. // now build an assembly that compresses first and encrypts + pads later
  105. testcase.asm = new Assembly();
  106. testcase.asm.addPreTransformer(Transformer.getCascadeTransformer(ofbBlowfish));
  107. testcase.asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
  108. testcase.asm.addPreTransformer(Transformer.getDeflateTransformer());
  109. testcase.testSymmetry(harness, 3);
  110. }
  111. private void testSymmetry(TestHarness harness, int ndx) {
  112. harness.checkPoint("TestOfAssembly.testSymmetry#"+ndx);
  113. byte[] km = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8};
  114. byte[] iv = new byte[] {-1, -2, -3, -4, -5, -6, -7, -8, -9};
  115. byte[] pt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  116. byte[] tpt = new byte[11 * pt.length];
  117. // forward
  118. modeAttributes.put(IBlockCipher.KEY_MATERIAL, km);
  119. modeAttributes.put(IMode.IV, iv);
  120. attributes.put(Assembly.DIRECTION, Direction.FORWARD);
  121. try {
  122. asm.init(attributes);
  123. } catch (TransformerException x) {
  124. harness.debug(x);
  125. harness.fail("Forward initialisation");
  126. return;
  127. }
  128. byte[] ct = null;
  129. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  130. try {
  131. for (int i = 0; i < 10; i++) { // transform in parts of 12-byte a time
  132. System.arraycopy(pt, 0, tpt, i * pt.length, pt.length);
  133. ct = asm.update(pt);
  134. baos.write(ct, 0, ct.length);
  135. }
  136. } catch (TransformerException x) {
  137. harness.debug(x);
  138. harness.fail("Forward transformation");
  139. return;
  140. }
  141. try {
  142. System.arraycopy(pt, 0, tpt, 10 * pt.length, pt.length);
  143. ct = asm.lastUpdate(pt);
  144. } catch (TransformerException x) {
  145. harness.debug(x);
  146. harness.fail("Forward last transformation");
  147. return;
  148. }
  149. baos.write(ct, 0, ct.length);
  150. ct = baos.toByteArray();
  151. // reversed
  152. attributes.put(Assembly.DIRECTION, Direction.REVERSED);
  153. try {
  154. asm.init(attributes);
  155. } catch (TransformerException x) {
  156. harness.debug(x);
  157. harness.fail("Reverse initialisation");
  158. return;
  159. }
  160. byte[] ot;
  161. try {
  162. ot = asm.lastUpdate(ct); // transform the lot in one go
  163. } catch (TransformerException x) {
  164. harness.debug(x);
  165. harness.fail("Reverse transformation");
  166. return;
  167. }
  168. harness.check(Arrays.equals(ot, tpt), "symmetric test");
  169. }
  170. }