IMode.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* IMode.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.mode;
  32. import gnu.javax.crypto.cipher.IBlockCipher;
  33. /**
  34. * The basic visible methods of any block cipher mode.
  35. * <p>
  36. * Block ciphers encrypt plaintext in fixed size n-bit blocks. For messages
  37. * larger than n bits, the simplest approach is to segment the message into
  38. * n-bit blocks and process (encrypt and/or decrypt) each one separately
  39. * (Electronic Codebook or ECB mode). But this approach has disadvantages in
  40. * most applications. The block cipher modes of operations are one way of
  41. * working around those disadvantages.
  42. * <p>
  43. * A <i>Mode</i> always employs an underlying block cipher for processing its
  44. * input. For all intents and purposes, a <i>Mode</i> appears to behave as any
  45. * other block cipher with the following differences:
  46. * <ul>
  47. * <li>Depending on the specifications of the mode, the block size may be
  48. * different that that of the underlying cipher.</li>
  49. * <li>While some modes of operations allow operations on block sizes that can
  50. * be 1-bit long, this library will only deal with sizes that are multiple of 8
  51. * bits. This is because the <tt>byte</tt> is the smallest, easy to handle,
  52. * primitive type in Java.</li>
  53. * <li>Some modes need an <i>Initialisation Vector</i> (IV) to be properly
  54. * initialised.</li>
  55. * </ul>
  56. * <p>
  57. * Possible additional initialisation values for an instance of that type are:
  58. * <ul>
  59. * <li>The block size in which to operate this mode instance. This value is
  60. * <b>optional</b>, if unspecified, the underlying block cipher's configured
  61. * block size shall be used.</li>
  62. * <li>Whether this mode will be used for encryption or decryption. This value
  63. * is <b>mandatory</b> and should be included in the initialisation parameters.
  64. * If it isn't, a {@link java.lang.IllegalStateException} will be thrown if any
  65. * method, other than <code>reset()</code> is invoked on the instance.</li>
  66. * <li>The byte array containing the <i>initialisation vector</i>, if required
  67. * by this type of mode.</li>
  68. * </ul>
  69. */
  70. public interface IMode
  71. extends IBlockCipher
  72. {
  73. /**
  74. * Property name of the state in which to operate this mode. The value
  75. * associated to this property name is taken to be an {@link Integer} which
  76. * value is either <code>ENCRYPTION</code> or <code>DECRYPTION</code>.
  77. */
  78. String STATE = "gnu.crypto.mode.state";
  79. /**
  80. * Property name of the block size in which to operate this mode. The value
  81. * associated with this property name is taken to be an {@link Integer}. If
  82. * it is not specified, the value of the block size of the underlying block
  83. * cipher, used to construct the mode instance, shall be used.
  84. */
  85. String MODE_BLOCK_SIZE = "gnu.crypto.mode.block.size";
  86. /**
  87. * Property name of the initialisation vector to use, if required, with this
  88. * instance. The value associated with this property name is taken to be a
  89. * byte array. If the concrete instance needs such a parameter, and it has not
  90. * been specified as part of the initialissation parameters, an all-zero byte
  91. * array of the appropriate size shall be used.
  92. */
  93. String IV = "gnu.crypto.mode.iv";
  94. /** Constant indicating the instance is being used for <i>encryption</i>. */
  95. int ENCRYPTION = 1;
  96. /** Constant indicating the instance is being used for <i>decryption</i>. */
  97. int DECRYPTION = 2;
  98. /**
  99. * A convenience method. Effectively invokes the <code>encryptBlock()</code>
  100. * or <code>decryptBlock()</code> method depending on the operational state
  101. * of the instance.
  102. *
  103. * @param in the plaintext.
  104. * @param inOffset index of <code>in</code> from which to start considering
  105. * data.
  106. * @param out the ciphertext.
  107. * @param outOffset index of <code>out</code> from which to store result.
  108. * @exception IllegalStateException if the instance is not initialised.
  109. */
  110. void update(byte[] in, int inOffset, byte[] out, int outOffset)
  111. throws IllegalStateException;
  112. }