Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / sasl / srp / CALG.java
blob6215783d6a9f29dbf27f7be26b6b2bfda345c69c
1 /* CALG.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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.
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. */
39 package gnu.javax.crypto.sasl.srp;
41 import gnu.java.security.Registry;
42 import gnu.javax.crypto.assembly.Assembly;
43 import gnu.javax.crypto.assembly.Cascade;
44 import gnu.javax.crypto.assembly.Direction;
45 import gnu.javax.crypto.assembly.Stage;
46 import gnu.javax.crypto.assembly.Transformer;
47 import gnu.javax.crypto.assembly.TransformerException;
48 import gnu.javax.crypto.cipher.CipherFactory;
49 import gnu.javax.crypto.cipher.IBlockCipher;
50 import gnu.javax.crypto.mode.IMode;
51 import gnu.javax.crypto.mode.ModeFactory;
52 import gnu.javax.crypto.pad.IPad;
53 import gnu.javax.crypto.pad.PadFactory;
54 import gnu.javax.crypto.sasl.ConfidentialityException;
56 import java.util.HashMap;
58 import javax.security.sasl.SaslException;
60 /**
61 * <p>A Factory class that returns CALG (Confidentiality Algorithm) instances
62 * that operate as described in the draft-burdis-cat-sasl-srp-08.</p>
64 * <p>The designated CALG block cipher should be used in OFB (Output Feedback
65 * Block) mode in the ISO variant, as described in <i>The Handbook of Applied
66 * Cryptography</i>, algorithm 7.20.</p>
68 * <p>Let <code>k</code> be the block size of the chosen symmetric key block
69 * cipher algorithm; e.g. for AES this is <code>128</code> bits or <code>16</code>
70 * octets. The OFB mode used shall be of length/size <code>k</code>.</p>
72 * <p>It is recommended that block ciphers operating in OFB mode be used with an
73 * Initial Vector (the mode's IV). In such a mode of operation - OFB with key
74 * re-use - the IV need not be secret. For the mechanism in question the IVs
75 * shall be a random octet sequence of <code>k</code> bytes.</p>
77 * The input data to the confidentiality protection algorithm shall be
78 * a multiple of the symmetric cipher block size <code>k</code>. When the input
79 * length is not a multiple of <code>k</code> octets, the data shall be padded
80 * according to the following scheme:</p>
82 * <p>Assuming the length of the input is <code>l</code> octets,
83 * <code>(k - (l mod k))</code> octets, all having the value
84 * <code>(k - (l mod k))</code>, shall be appended to the original data. In
85 * other words, the input is padded at the trailing end with one of the
86 * following sequences:</p>
88 * <pre>
90 * 01 -- if l mod k = k-1
91 * 02 02 -- if l mod k = k-2
92 * ...
93 * ...
94 * ...
95 * k k ... k k -- if l mod k = 0
96 *</pre>
98 * <p>The padding can be removed unambiguously since all input is padded and no
99 * padding sequence is a suffix of another. This padding method is well-defined
100 * if and only if <code>k &lt; 256</code> octets, which is the case with
101 * symmetric key block ciphers today, and in the forseeable future.</p>
103 public final class CALG
106 // Constants and variables
107 // --------------------------------------------------------------------------
109 private Assembly assembly;
111 private Object modeNdx; // initialisation key of the cascade's attributes
113 private int blockSize; // the underlying cipher's blocksize == IV length
115 private int keySize; // the underlying cipher's key size (in bytes).
117 // Constructor(s)
118 // --------------------------------------------------------------------------
120 /** Private constructor to enforce instantiation through Factory method. */
121 private CALG(final int blockSize, final int keySize, final Object modeNdx,
122 final Assembly assembly)
124 super();
126 this.blockSize = blockSize;
127 this.keySize = keySize;
128 this.modeNdx = modeNdx;
129 this.assembly = assembly;
132 // Class methods
133 // -------------------------------------------------------------------------
136 * <p>Returns an instance of a SASL-SRP CALG implementation.</p>
138 * @param algorithm the name of the symmetric cipher algorithm.
139 * @return an instance of this object.
141 static synchronized CALG getInstance(final String algorithm)
143 final IBlockCipher cipher = CipherFactory.getInstance(algorithm);
144 final int blockSize = cipher.defaultBlockSize();
145 final int keySize = cipher.defaultKeySize();
146 final Cascade ofbCipher = new Cascade();
147 final Object modeNdx = ofbCipher.append(Stage.getInstance(
148 ModeFactory.getInstance(
149 Registry.OFB_MODE,
150 cipher,
151 blockSize),
152 Direction.FORWARD));
153 final IPad pkcs7 = PadFactory.getInstance(Registry.PKCS7_PAD);
154 // the passed IV may be longer that what we need. ensure correct length
155 // byte[] realIV = null;
156 // if (iv.length == blockSize) {
157 // realIV = iv;
158 // } else {
159 // realIV = new byte[blockSize];
160 // if (iv.length > blockSize) {
161 // System.arraycopy(iv, 0, realIV, 0, blockSize);
162 // } else { // shouldnt happen
163 // System.arraycopy(iv, 0, realIV, 0, iv.length);
164 // }
165 // }
167 // HashMap modeAttributes = new HashMap();
168 // modeAttributes.put(IBlockCipher.KEY_MATERIAL, K.clone());
169 // modeAttributes.put(IMode.IV, realIV);
171 final Assembly asm = new Assembly();
172 asm.addPreTransformer(Transformer.getCascadeTransformer(ofbCipher));
173 asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
175 // HashMap attributes = new HashMap();
176 // attributes.put(Assembly.DIRECTION, dir);
177 // attributes.put(modeNdx, modeAttributes);
178 // try {
179 // asm.init(attributes);
180 // } catch (TransformerException x) {
181 // throw new SaslException("getInstance()", x);
182 // }
184 return new CALG(blockSize, keySize, modeNdx, asm);
187 // Instance methods
188 // -------------------------------------------------------------------------
191 * <p>Initialises a SASL-SRP CALG implementation.</p>
193 * @param kdf the key derivation function.
194 * @param iv the initial vector value to use.
195 * @param dir whether this CALG is used for encryption or decryption.
197 // public void init(byte[] K, byte[] iv, Direction dir) throws SaslException {
198 public void init(final KDF kdf, final byte[] iv, final Direction dir)
199 throws SaslException
201 // IBlockCipher cipher = CipherFactory.getInstance(algorithm);
202 // int blockSize = cipher.defaultBlockSize();
203 // Cascade ofbCipher = new Cascade();
204 // Object modeNdx = ofbCipher.append(
205 // Stage.getInstace(
206 // ModeFactory.getInstance(Registry.OFB_MODE, cipher, blockSize),
207 // Direction.FORWARD));
208 // IPad pkcs7 = PadFactory.getInstance(Registry.PKCS7_PAD);
209 // the passed IV may be longer that what we need. ensure correct length
210 final byte[] realIV;
211 if (iv.length == blockSize)
213 realIV = iv;
215 else
217 realIV = new byte[blockSize];
218 if (iv.length > blockSize)
220 System.arraycopy(iv, 0, realIV, 0, blockSize);
222 else
223 { // shouldnt happen
224 System.arraycopy(iv, 0, realIV, 0, iv.length);
228 final HashMap modeAttributes = new HashMap();
229 // modeAttributes.put(IBlockCipher.KEY_MATERIAL, K.clone());
230 final byte[] sk = kdf.derive(keySize);
231 modeAttributes.put(IBlockCipher.KEY_MATERIAL, sk);
232 //System.out.println("**** Initialised CALG with: "+gnu.crypto.util.Util.dumpString(sk));
233 modeAttributes.put(IMode.IV, realIV);
235 // Assembly asm = new Assembly();
236 // asm.addPreTransformer(Transformer.getCascadeTransformer(ofbCipher));
237 // asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));
239 final HashMap attributes = new HashMap();
240 attributes.put(Assembly.DIRECTION, dir);
241 attributes.put(modeNdx, modeAttributes);
244 // asm.init(attributes);
245 assembly.init(attributes);
247 catch (TransformerException x)
249 throw new SaslException("getInstance()", x);
252 // return new CALG(asm);
256 * <p>Encrypts or decrypts, depending on the mode already set, a designated
257 * array of bytes and returns the result.</p>
259 * @param data the data to encrypt/decrypt.
260 * @return the decrypted/encrypted result.
261 * @throws ConfidentialityException if an exception occurs duirng the process.
263 public byte[] doFinal(final byte[] data) throws ConfidentialityException
265 return doFinal(data, 0, data.length);
269 * <p>Encrypts or decrypts, depending on the mode already set, a designated
270 * array of bytes and returns the result.</p>
272 * @param data the data to encrypt/decrypt.
273 * @param offset where to start in <code>data</code>.
274 * @param length how many bytes to consider in <code>data</code>.
275 * @return the decrypted/encrypted result.
276 * @throws ConfidentialityException if an exception occurs duirng the process.
278 public byte[] doFinal(final byte[] data, final int offset, final int length)
279 throws ConfidentialityException
281 final byte[] result;
284 result = assembly.lastUpdate(data, offset, length);
286 catch (TransformerException x)
288 throw new ConfidentialityException("doFinal()", x);
290 return result;