summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/gnu/javax/crypto/sasl/srp/CALG.java
blob: 6215783d6a9f29dbf27f7be26b6b2bfda345c69c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* CALG.java -- 
   Copyright (C) 2003, 2006 Free Software Foundation, Inc.

This file is a part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.  */


package gnu.javax.crypto.sasl.srp;

import gnu.java.security.Registry;
import gnu.javax.crypto.assembly.Assembly;
import gnu.javax.crypto.assembly.Cascade;
import gnu.javax.crypto.assembly.Direction;
import gnu.javax.crypto.assembly.Stage;
import gnu.javax.crypto.assembly.Transformer;
import gnu.javax.crypto.assembly.TransformerException;
import gnu.javax.crypto.cipher.CipherFactory;
import gnu.javax.crypto.cipher.IBlockCipher;
import gnu.javax.crypto.mode.IMode;
import gnu.javax.crypto.mode.ModeFactory;
import gnu.javax.crypto.pad.IPad;
import gnu.javax.crypto.pad.PadFactory;
import gnu.javax.crypto.sasl.ConfidentialityException;

import java.util.HashMap;

import javax.security.sasl.SaslException;

/**
 * <p>A Factory class that returns CALG (Confidentiality Algorithm) instances
 * that operate as described in the draft-burdis-cat-sasl-srp-08.</p>
 *
 * <p>The designated CALG block cipher should be used in OFB (Output Feedback
 * Block) mode in the ISO variant, as described in <i>The Handbook of Applied
 * Cryptography</i>, algorithm 7.20.</p>
 *
 * <p>Let <code>k</code> be the block size of the chosen symmetric key block
 * cipher algorithm; e.g. for AES this is <code>128</code> bits or <code>16</code>
 * octets.  The OFB mode used shall be of length/size <code>k</code>.</p>
 *
 * <p>It is recommended that block ciphers operating in OFB mode be used with an
 * Initial Vector (the mode's IV). In such a mode of operation - OFB with key
 * re-use - the IV need not be secret. For the mechanism in question the IVs
 * shall be a random octet sequence of <code>k</code> bytes.</p>
 *
 * The input data to the confidentiality protection algorithm shall be
 * a multiple of the symmetric cipher block size <code>k</code>. When the input
 * length is not a multiple of <code>k</code> octets, the data shall be padded
 * according to the following scheme:</p>
 *
 * <p>Assuming the length of the input is <code>l</code> octets,
 * <code>(k - (l mod k))</code> octets, all having the value
 * <code>(k - (l mod k))</code>, shall be appended to the original data. In
 * other words, the input is padded at the trailing end with one of the
 * following sequences:</p>
 *
 * <pre>
 *
 *                    01 -- if l mod k = k-1
 *                   02 02 -- if l mod k = k-2
 *                             ...
 *                             ...
 *                             ...
 *                 k k ... k k -- if l mod k = 0
 *</pre>
 *
 * <p>The padding can be removed unambiguously since all input is padded and no
 * padding sequence is a suffix of another. This padding method is well-defined
 * if and only if <code>k &lt; 256</code> octets, which is the case with
 * symmetric key block ciphers today, and in the forseeable future.</p>
 */
public final class CALG
{

  // Constants and variables
  // --------------------------------------------------------------------------

  private Assembly assembly;

  private Object modeNdx; // initialisation key of the cascade's attributes

  private int blockSize; // the underlying cipher's blocksize == IV length

  private int keySize; // the underlying cipher's key size (in bytes).

  // Constructor(s)
  // --------------------------------------------------------------------------

  /** Private constructor to enforce instantiation through Factory method. */
  private CALG(final int blockSize, final int keySize, final Object modeNdx,
               final Assembly assembly)
  {
    super();

    this.blockSize = blockSize;
    this.keySize = keySize;
    this.modeNdx = modeNdx;
    this.assembly = assembly;
  }

  // Class methods
  // -------------------------------------------------------------------------

  /**
   * <p>Returns an instance of a SASL-SRP CALG implementation.</p>
   *
   * @param algorithm the name of the symmetric cipher algorithm.
   * @return an instance of this object.
   */
  static synchronized CALG getInstance(final String algorithm)
  {
    final IBlockCipher cipher = CipherFactory.getInstance(algorithm);
    final int blockSize = cipher.defaultBlockSize();
    final int keySize = cipher.defaultKeySize();
    final Cascade ofbCipher = new Cascade();
    final Object modeNdx = ofbCipher.append(Stage.getInstance(
                                                              ModeFactory.getInstance(
                                                                                      Registry.OFB_MODE,
                                                                                      cipher,
                                                                                      blockSize),
                                                              Direction.FORWARD));
    final IPad pkcs7 = PadFactory.getInstance(Registry.PKCS7_PAD);
    // the passed IV may be longer that what we need. ensure correct length
    //      byte[] realIV = null;
    //      if (iv.length == blockSize) {
    //         realIV = iv;
    //      } else {
    //         realIV = new byte[blockSize];
    //         if (iv.length > blockSize) {
    //            System.arraycopy(iv, 0, realIV, 0, blockSize);
    //         } else { // shouldnt happen
    //            System.arraycopy(iv, 0, realIV, 0, iv.length);
    //         }
    //      }

    //      HashMap modeAttributes = new HashMap();
    //      modeAttributes.put(IBlockCipher.KEY_MATERIAL, K.clone());
    //      modeAttributes.put(IMode.IV, realIV);

    final Assembly asm = new Assembly();
    asm.addPreTransformer(Transformer.getCascadeTransformer(ofbCipher));
    asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));

    //      HashMap attributes = new HashMap();
    //      attributes.put(Assembly.DIRECTION, dir);
    //      attributes.put(modeNdx, modeAttributes);
    //      try {
    //         asm.init(attributes);
    //      } catch (TransformerException x) {
    //         throw new SaslException("getInstance()", x);
    //      }

    return new CALG(blockSize, keySize, modeNdx, asm);
  }

  // Instance methods
  // -------------------------------------------------------------------------

  /**
   * <p>Initialises a SASL-SRP CALG implementation.</p>
   *
   * @param kdf the key derivation function.
   * @param iv the initial vector value to use.
   * @param dir whether this CALG is used for encryption or decryption.
   */
  //   public void init(byte[] K, byte[] iv, Direction dir) throws SaslException {
  public void init(final KDF kdf, final byte[] iv, final Direction dir)
      throws SaslException
  {
    //      IBlockCipher cipher = CipherFactory.getInstance(algorithm);
    //      int blockSize = cipher.defaultBlockSize();
    //      Cascade ofbCipher = new Cascade();
    //      Object modeNdx = ofbCipher.append(
    //            Stage.getInstace(
    //                  ModeFactory.getInstance(Registry.OFB_MODE, cipher, blockSize),
    //                  Direction.FORWARD));
    //      IPad pkcs7 = PadFactory.getInstance(Registry.PKCS7_PAD);
    // the passed IV may be longer that what we need. ensure correct length
    final byte[] realIV;
    if (iv.length == blockSize)
      {
        realIV = iv;
      }
    else
      {
        realIV = new byte[blockSize];
        if (iv.length > blockSize)
          {
            System.arraycopy(iv, 0, realIV, 0, blockSize);
          }
        else
          { // shouldnt happen
            System.arraycopy(iv, 0, realIV, 0, iv.length);
          }
      }

    final HashMap modeAttributes = new HashMap();
    //      modeAttributes.put(IBlockCipher.KEY_MATERIAL, K.clone());
    final byte[] sk = kdf.derive(keySize);
    modeAttributes.put(IBlockCipher.KEY_MATERIAL, sk);
    //System.out.println("**** Initialised CALG with: "+gnu.crypto.util.Util.dumpString(sk));
    modeAttributes.put(IMode.IV, realIV);

    //      Assembly asm = new Assembly();
    //      asm.addPreTransformer(Transformer.getCascadeTransformer(ofbCipher));
    //      asm.addPreTransformer(Transformer.getPaddingTransformer(pkcs7));

    final HashMap attributes = new HashMap();
    attributes.put(Assembly.DIRECTION, dir);
    attributes.put(modeNdx, modeAttributes);
    try
      {
        //         asm.init(attributes);
        assembly.init(attributes);
      }
    catch (TransformerException x)
      {
        throw new SaslException("getInstance()", x);
      }

    //      return new CALG(asm);
  }

  /**
   * <p>Encrypts or decrypts, depending on the mode already set, a designated
   * array of bytes and returns the result.</p>
   *
   * @param data the data to encrypt/decrypt.
   * @return the decrypted/encrypted result.
   * @throws ConfidentialityException if an exception occurs duirng the process.
   */
  public byte[] doFinal(final byte[] data) throws ConfidentialityException
  {
    return doFinal(data, 0, data.length);
  }

  /**
   * <p>Encrypts or decrypts, depending on the mode already set, a designated
   * array of bytes and returns the result.</p>
   *
   * @param data the data to encrypt/decrypt.
   * @param offset where to start in <code>data</code>.
   * @param length how many bytes to consider in <code>data</code>.
   * @return the decrypted/encrypted result.
   * @throws ConfidentialityException if an exception occurs duirng the process.
   */
  public byte[] doFinal(final byte[] data, final int offset, final int length)
      throws ConfidentialityException
  {
    final byte[] result;
    try
      {
        result = assembly.lastUpdate(data, offset, length);
      }
    catch (TransformerException x)
      {
        throw new ConfidentialityException("doFinal()", x);
      }
    return result;
  }
}
OpenPOWER on IntegriCloud