PageRenderTime 94ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/rg/pkg/kaffe/libraries/javalib/gnu/crypto/assembly/package.html

https://github.com/GunioRobot/MI424WR_GEN2_Rev_E-F
HTML | 253 lines | 173 code | 35 blank | 45 comment | 0 complexity | b1cd097a68df2a77d31388a2b12def45 MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html><head>
  3. <!--
  4. $Id: package.html,v 1.2 2005/10/20 12:04:23 alexa Exp $
  5. Copyright (C) 2003, Free Software Foundation, Inc.
  6. This file is part of GNU Crypto.
  7. GNU Crypto is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. GNU Crypto is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; see the file COPYING. If not, write to the
  17. Free Software Foundation Inc.,
  18. 51 Franklin Street, Fifth Floor,
  19. Boston, MA 02110-1301
  20. USA
  21. Linking this library statically or dynamically with other modules is
  22. making a combined work based on this library. Thus, the terms and
  23. conditions of the GNU General Public License cover the whole
  24. combination.
  25. As a special exception, the copyright holders of this library give
  26. you permission to link this library with independent modules to
  27. produce an executable, regardless of the license terms of these
  28. independent modules, and to copy and distribute the resulting
  29. executable under terms of your choice, provided that you also meet,
  30. for each linked independent module, the terms and conditions of the
  31. license of that module. An independent module is a module which is
  32. not derived from or based on this library. If you modify this
  33. library, you may extend this exception to your version of the
  34. library, but you are not obligated to do so. If you do not wish to
  35. do so, delete this exception statement from your version.
  36. -->
  37. </head><body>
  38. Provides a high-level API for combining and using GNU Crypto cipher, mode,
  39. and padding primitives.
  40. <h2>Package overview</h2>
  41. <p>This package describes two patterns implemneted by the GNU Crypto
  42. library that allow users to combine the basic cipher (and other) primitives
  43. into higher level components in order to offer more flexible functionalities.
  44. These two patterns are: <i>Cascade</i> and <i>Assembly</i>.</p>
  45. <p>The <i>Cascade</i> is a means of assembling block cipher Modes of
  46. Operations into an ordered sequence of <i>stages</i>. A <i>stage</i>
  47. is a representation of a Mode (of Operations) wired in a designated
  48. <i>direction</i>: FORWARD or REVERSED. A Mode staged in the FORWARD
  49. direction would encrypt input blocks, producing ciphertext, while the
  50. same Mode, wired in the REVERSED direction would do the opposite; i.e.
  51. decrypt an input text producing a plaintext.</p>
  52. <p>In the simplest case, all stages in a <i>Cascade</i> have <i>k</i>-bit
  53. keys, and the stage inputs and outputs are all <i>n</i>-bit quantities.
  54. The stage ciphers may differ (general cascade of ciphers), or all be
  55. identical (cascade of identical ciphers).</p>
  56. <p>An <i>Assembly</i> is a construction of an ordered set of <i>Transformer</i>
  57. objects. Each <i>Transformer</i> is wired to operate in PRE_PROCESSING or
  58. POST_PROCESSING mode --the Transformer's <i>operation</i>. In
  59. PRE_PROCESSING, the input is first processed by the <i>Transformer</i>
  60. before being passed to the rest of the chain, while in POST_PROCESSING
  61. state, the <i>Transformer</i> first passes the input to the rest of the
  62. chain and only processes the output of the returned data.</p>
  63. <h3>Cascade pattern</h3>
  64. <p>The following diagram shows the important classes participating in the
  65. <i>Cascade</i> pattern:</p>
  66. <p><img src="{@docRoot}/../diagrams/cascade_class_diag.png"
  67. width=525 height=195 border=0></p>
  68. <p>Here is an example of how a <i>Cascade</i> is used to construct a
  69. <i>DES-EDE</i> symetric-key block cipher from three independent
  70. <i>DES</i> cipher instances:</p>
  71. <pre>
  72. HashMap map = new HashMap();
  73. HashMap map1 = new HashMap();
  74. HashMap map2 = new HashMap();
  75. HashMap map3 = new HashMap();
  76. Cascade new3DES = new Cascade();
  77. Object des1 = new3DES.append(
  78. Stage.getInstance(
  79. ModeFactory.getInstance(Registry.ECB_MODE, new DES(), 8),
  80. Direction.FORWARD));
  81. Object des2 = new3DES.append(
  82. Stage.getInstance(
  83. ModeFactory.getInstance(Registry.ECB_MODE, new DES(), 8),
  84. Direction.REVERSED));
  85. Object des3 = new3DES.append(
  86. Stage.getInstance(
  87. ModeFactory.getInstance(Registry.ECB_MODE, new DES(), 8),
  88. Direction.FORWARD));
  89. map.put(des1, map1);
  90. map.put(des2, map2);
  91. map.put(des3, map3);
  92. map1.put(IBlockCipher.KEY_MATERIAL, key1material);
  93. map2.put(IBlockCipher.KEY_MATERIAL, key2material);
  94. map3.put(IBlockCipher.KEY_MATERIAL, key3material);
  95. // encryption
  96. map.put(Cascade.DIRECTION, Direction.FORWARD);
  97. byte[] pt = ...; // some plaintext to encrypt
  98. byte[] ct = new byte[pt.length]; // where ciphertext is returned
  99. try
  100. {
  101. new3DES.init(map);
  102. new3DES.update(pt, 0, ct, 0);
  103. }
  104. catch (InvalidKeyException x)
  105. {
  106. x.printStackTrace(System.err);
  107. }
  108. </pre>
  109. <h3>Assembly pattern</h3>
  110. <p>The following diagram shows the important classes participating in the
  111. <i>Assembly</i> pattern:</p>
  112. <p><img src="{@docRoot}/../diagrams/assembly_class_diag.png"
  113. width=455 height=313 border=0></p>
  114. <p>Here is an example of how to compress and encrypt a stream of
  115. plaintext:</p>
  116. <pre>
  117. import gnu.crypto.Registry;
  118. import gnu.crypto.util.Util;
  119. import gnu.crypto.assembly.Assembly;
  120. import gnu.crypto.assembly.Cascade;
  121. import gnu.crypto.assembly.Direction;
  122. import gnu.crypto.assembly.Stage;
  123. import gnu.crypto.assembly.Transformer;
  124. import gnu.crypto.assembly.TransformerException;
  125. import gnu.crypto.cipher.Blowfish;
  126. import gnu.crypto.cipher.IBlockCipher;
  127. import gnu.crypto.mode.IMode;
  128. import gnu.crypto.mode.ModeFactory;
  129. import gnu.crypto.pad.IPad;
  130. import gnu.crypto.pad.PadFactory;
  131. HashMap attributes = new HashMap();
  132. HashMap modeAttributes = new HashMap();
  133. Cascade ofbBlowfish = new Cascade();
  134. Object modeNdx = ofbBlowfish.append(
  135. Stage.getInstance(
  136. ModeFactory.getInstance(Registry.OFB_MODE, new Blowfish(), 8),
  137. Direction.FORWARD));
  138. attributes.put(modeNdx, modeAttributes);
  139. IPad pkcs7 = PadFactory.getInstance(Registry.PKCS7_PAD);
  140. Assembly asm = new Assembly();
  141. asm.addPreTransformer(Transformer.getCascadeTransformer(ofbBlowfish));
  142. asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
  143. asm.addPreTransformer(Transformer.getDeflateTransformer());
  144. // plaintext and key material
  145. byte[] km = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8};
  146. byte[] iv = new byte[] {-1, -2, -3, -4, -5, -6, -7, -8, -9};
  147. byte[] pt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  148. byte[] tpt = new byte[11 * pt.length];
  149. // forward transformation
  150. modeAttributes.put(IBlockCipher.KEY_MATERIAL, km);
  151. modeAttributes.put(IMode.IV, iv);
  152. attributes.put(Assembly.DIRECTION, Direction.FORWARD);
  153. try
  154. {
  155. asm.init(attributes);
  156. }
  157. catch (TransformerException x)
  158. {
  159. x.printStackTrace(System.err);
  160. }
  161. byte[] ct = null;
  162. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  163. try
  164. {
  165. for (int i = 0; i < 10; i++)
  166. { // transform in parts of 12-byte a time
  167. System.arraycopy(pt, 0, tpt, i * pt.length, pt.length);
  168. ct = asm.update(pt);
  169. baos.write(ct, 0, ct.length);
  170. }
  171. }
  172. catch (TransformerException x)
  173. {
  174. x.printStackTrace(System.err);
  175. }
  176. try
  177. {
  178. System.arraycopy(pt, 0, tpt, 10 * pt.length, pt.length);
  179. ct = asm.lastUpdate(pt);
  180. }
  181. catch (TransformerException x)
  182. {
  183. x.printStackTrace(System.err);
  184. }
  185. baos.write(ct, 0, ct.length);
  186. ct = baos.toByteArray();
  187. // reversed transformation
  188. attributes.put(Assembly.DIRECTION, Direction.REVERSED);
  189. try
  190. {
  191. asm.init(attributes);
  192. }
  193. catch (TransformerException x)
  194. {
  195. x.printStackTrace(System.err);
  196. }
  197. byte[] ot = null;
  198. try
  199. {
  200. ot = asm.lastUpdate(ct); // transform the lot in one go
  201. }
  202. catch (TransformerException x)
  203. {
  204. x.printStackTrace(System.err);
  205. }
  206. </pre>
  207. <!-- $Revision: 1.2 $ -->
  208. </body></html>