Frames | No Frames |
1: /* IMode.java -- 2: Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc. 3: 4: This file is a part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2 of the License, or (at 9: your option) any later version. 10: 11: GNU Classpath 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: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; if not, write to the Free Software 18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19: USA 20: 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: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.javax.crypto.mode; 40: 41: import gnu.javax.crypto.cipher.IBlockCipher; 42: 43: /** 44: * The basic visible methods of any block cipher mode. 45: * <p> 46: * Block ciphers encrypt plaintext in fixed size n-bit blocks. For messages 47: * larger than n bits, the simplest approach is to segment the message into 48: * n-bit blocks and process (encrypt and/or decrypt) each one separately 49: * (Electronic Codebook or ECB mode). But this approach has disadvantages in 50: * most applications. The block cipher modes of operations are one way of 51: * working around those disadvantages. 52: * <p> 53: * A <i>Mode</i> always employs an underlying block cipher for processing its 54: * input. For all intents and purposes, a <i>Mode</i> appears to behave as any 55: * other block cipher with the following differences: 56: * <ul> 57: * <li>Depending on the specifications of the mode, the block size may be 58: * different that that of the underlying cipher.</li> 59: * <li>While some modes of operations allow operations on block sizes that can 60: * be 1-bit long, this library will only deal with sizes that are multiple of 8 61: * bits. This is because the <tt>byte</tt> is the smallest, easy to handle, 62: * primitive type in Java.</li> 63: * <li>Some modes need an <i>Initialisation Vector</i> (IV) to be properly 64: * initialised.</li> 65: * </ul> 66: * <p> 67: * Possible additional initialisation values for an instance of that type are: 68: * <ul> 69: * <li>The block size in which to operate this mode instance. This value is 70: * <b>optional</b>, if unspecified, the underlying block cipher's configured 71: * block size shall be used.</li> 72: * <li>Whether this mode will be used for encryption or decryption. This value 73: * is <b>mandatory</b> and should be included in the initialisation parameters. 74: * If it isn't, a {@link java.lang.IllegalStateException} will be thrown if any 75: * method, other than <code>reset()</code> is invoked on the instance.</li> 76: * <li>The byte array containing the <i>initialisation vector</i>, if required 77: * by this type of mode.</li> 78: * </ul> 79: */ 80: public interface IMode 81: extends IBlockCipher 82: { 83: /** 84: * Property name of the state in which to operate this mode. The value 85: * associated to this property name is taken to be an {@link Integer} which 86: * value is either <code>ENCRYPTION</code> or <code>DECRYPTION</code>. 87: */ 88: String STATE = "gnu.crypto.mode.state"; 89: /** 90: * Property name of the block size in which to operate this mode. The value 91: * associated with this property name is taken to be an {@link Integer}. If 92: * it is not specified, the value of the block size of the underlying block 93: * cipher, used to construct the mode instance, shall be used. 94: */ 95: String MODE_BLOCK_SIZE = "gnu.crypto.mode.block.size"; 96: /** 97: * Property name of the initialisation vector to use, if required, with this 98: * instance. The value associated with this property name is taken to be a 99: * byte array. If the concrete instance needs such a parameter, and it has not 100: * been specified as part of the initialissation parameters, an all-zero byte 101: * array of the appropriate size shall be used. 102: */ 103: String IV = "gnu.crypto.mode.iv"; 104: /** Constant indicating the instance is being used for <i>encryption</i>. */ 105: int ENCRYPTION = 1; 106: /** Constant indicating the instance is being used for <i>decryption</i>. */ 107: int DECRYPTION = 2; 108: 109: /** 110: * A convenience method. Effectively invokes the <code>encryptBlock()</code> 111: * or <code>decryptBlock()</code> method depending on the operational state 112: * of the instance. 113: * 114: * @param in the plaintext. 115: * @param inOffset index of <code>in</code> from which to start considering 116: * data. 117: * @param out the ciphertext. 118: * @param outOffset index of <code>out</code> from which to store result. 119: * @exception IllegalStateException if the instance is not initialised. 120: */ 121: void update(byte[] in, int inOffset, byte[] out, int outOffset) 122: throws IllegalStateException; 123: }