IBlockCipher.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* IBlockCipher.java --
  2. Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath 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 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath 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 GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.cipher;
  32. import java.security.InvalidKeyException;
  33. import java.util.Iterator;
  34. import java.util.Map;
  35. /**
  36. * The basic visible methods of any symmetric key block cipher.
  37. * <p>
  38. * A symmetric key block cipher is a function that maps n-bit plaintext blocks
  39. * to n-bit ciphertext blocks; n being the cipher's <i>block size</i>. This
  40. * encryption function is parameterised by a k-bit key, and is invertible. Its
  41. * inverse is the decryption function.
  42. * <p>
  43. * Possible initialisation values for an instance of this type are:
  44. * <ul>
  45. * <li>The block size in which to operate this block cipher instance. This
  46. * value is <b>optional</b>, if unspecified, the block cipher's default block
  47. * size shall be used.</li>
  48. * <li>The byte array containing the user supplied key material to use for
  49. * generating the cipher's session key(s). This value is <b>mandatory</b> and
  50. * should be included in the initialisation parameters. If it isn't, an
  51. * {@link IllegalStateException} will be thrown if any method, other than
  52. * <code>reset()</code> is invoked on the instance. Furthermore, the size of
  53. * this key material shall be taken as an indication on the key size in which to
  54. * operate this instance.</li>
  55. * </ul>
  56. * <p>
  57. * <b>IMPLEMENTATION NOTE</b>: Although all the concrete classes in this
  58. * package implement the {@link Cloneable} interface, it is important to note
  59. * here that such an operation <b>DOES NOT</b> clone any session key material
  60. * that may have been used in initialising the source cipher (the instance to be
  61. * cloned). Instead a clone of an already initialised cipher is another instance
  62. * that operates with the <b>same block size</b> but without any knowledge of
  63. * neither key material nor key size.
  64. */
  65. public interface IBlockCipher
  66. extends Cloneable
  67. {
  68. /**
  69. * Property name of the block size in which to operate a block cipher. The
  70. * value associated with this property name is taken to be an {@link Integer}.
  71. */
  72. String CIPHER_BLOCK_SIZE = "gnu.crypto.cipher.block.size";
  73. /**
  74. * Property name of the user-supplied key material. The value associated to
  75. * this property name is taken to be a byte array.
  76. */
  77. String KEY_MATERIAL = "gnu.crypto.cipher.key.material";
  78. /**
  79. * Returns the canonical name of this instance.
  80. *
  81. * @return the canonical name of this instance.
  82. */
  83. String name();
  84. /**
  85. * Returns the default value, in bytes, of the algorithm's block size.
  86. *
  87. * @return the default value, in bytes, of the algorithm's block size.
  88. */
  89. int defaultBlockSize();
  90. /**
  91. * Returns the default value, in bytes, of the algorithm's key size.
  92. *
  93. * @return the default value, in bytes, of the algorithm's key size.
  94. */
  95. int defaultKeySize();
  96. /**
  97. * Returns an {@link Iterator} over the supported block sizes. Each element
  98. * returned by this object is an {@link Integer}.
  99. *
  100. * @return an {@link Iterator} over the supported block sizes.
  101. */
  102. Iterator blockSizes();
  103. /**
  104. * Returns an {@link Iterator} over the supported key sizes. Each element
  105. * returned by this object is an {@link Integer}.
  106. *
  107. * @return an {@link Iterator} over the supported key sizes.
  108. */
  109. Iterator keySizes();
  110. /**
  111. * Returns a clone of this instance.
  112. *
  113. * @return a clone copy of this instance.
  114. */
  115. Object clone();
  116. /**
  117. * Initialises the algorithm with designated attributes. Permissible names and
  118. * values are described in the class documentation above.
  119. *
  120. * @param attributes a set of name-value pairs that describes the desired
  121. * future behaviour of this instance.
  122. * @exception InvalidKeyException if the key data is invalid.
  123. * @exception IllegalStateException if the instance is already initialised.
  124. * @see #KEY_MATERIAL
  125. * @see #CIPHER_BLOCK_SIZE
  126. */
  127. void init(Map attributes) throws InvalidKeyException, IllegalStateException;
  128. /**
  129. * Returns the currently set block size for this instance.
  130. *
  131. * @return the current block size for this instance.
  132. * @exception IllegalStateException if the instance is not initialised.
  133. */
  134. int currentBlockSize() throws IllegalStateException;
  135. /**
  136. * Resets the algorithm instance for re-initialisation and use with other
  137. * characteristics. This method always succeeds.
  138. */
  139. void reset();
  140. /**
  141. * Encrypts exactly one block of plaintext.
  142. *
  143. * @param in the plaintext.
  144. * @param inOffset index of <code>in</code> from which to start considering
  145. * data.
  146. * @param out the ciphertext.
  147. * @param outOffset index of <code>out</code> from which to store result.
  148. * @exception IllegalStateException if the instance is not initialised.
  149. */
  150. void encryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
  151. throws IllegalStateException;
  152. /**
  153. * Decrypts exactly one block of ciphertext.
  154. *
  155. * @param in the plaintext.
  156. * @param inOffset index of <code>in</code> from which to start considering
  157. * data.
  158. * @param out the ciphertext.
  159. * @param outOffset index of <code>out</code> from which to store result.
  160. * @exception IllegalStateException if the instance is not initialised.
  161. */
  162. void decryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
  163. throws IllegalStateException;
  164. /**
  165. * A <i>correctness</i> test that consists of basic symmetric encryption /
  166. * decryption test(s) for all supported block and key sizes, as well as one
  167. * (1) variable key Known Answer Test (KAT).
  168. *
  169. * @return <code>true</code> if the implementation passes simple
  170. * <i>correctness</i> tests. Returns <code>false</code> otherwise.
  171. */
  172. boolean selfTest();
  173. }