2 Copyright (C) 2001, 2002, 2004, 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
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
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
.mode
;
41 import gnu
.java
.security
.Registry
;
43 import gnu
.javax
.crypto
.cipher
.CipherFactory
;
44 import gnu
.javax
.crypto
.cipher
.IBlockCipher
;
46 import java
.util
.Collections
;
47 import java
.util
.HashSet
;
48 import java
.util
.Iterator
;
52 * <p>A <i>Factory</i> to instantiate block cipher modes of operations.</p>
54 public class ModeFactory
implements Registry
57 // Constants and variables
58 // -------------------------------------------------------------------------
61 // -------------------------------------------------------------------------
63 /** Trivial constructor to enforce Singleton pattern. */
70 // -------------------------------------------------------------------------
73 * <p>Returns an instance of a block cipher mode of operations given its name
74 * and characteristics of the underlying block cipher.</p>
76 * @param mode the case-insensitive name of the mode of operations.
77 * @param cipher the case-insensitive name of the block cipher.
78 * @param cipherBlockSize the block size, in bytes, of the underlying cipher.
79 * @return an instance of the block cipher algorithm, operating in a given
80 * mode of operations, or <code>null</code> if none found.
81 * @exception InternalError if either the mode or the underlying block cipher
82 * implementation does not pass its self-test.
84 public static IMode
getInstance(String mode
, String cipher
,
87 if (mode
== null || cipher
== null)
93 cipher
= cipher
.trim();
95 IBlockCipher cipherImpl
= CipherFactory
.getInstance(cipher
);
96 if (cipherImpl
== null)
101 return getInstance(mode
, cipherImpl
, cipherBlockSize
);
104 public static IMode
getInstance(String mode
, IBlockCipher cipher
,
107 // ensure that cipherBlockSize is valid for the chosen underlying cipher
109 for (Iterator it
= cipher
.blockSizes(); it
.hasNext();)
111 ok
= (cipherBlockSize
== ((Integer
) it
.next()).intValue());
120 throw new IllegalArgumentException("cipherBlockSize");
124 if (mode
.equalsIgnoreCase(ECB_MODE
))
126 result
= new ECB(cipher
, cipherBlockSize
);
128 else if (mode
.equalsIgnoreCase(CTR_MODE
))
130 result
= new CTR(cipher
, cipherBlockSize
);
132 else if (mode
.equalsIgnoreCase(ICM_MODE
))
134 result
= new ICM(cipher
, cipherBlockSize
);
136 else if (mode
.equalsIgnoreCase(OFB_MODE
))
138 result
= new OFB(cipher
, cipherBlockSize
);
140 else if (mode
.equalsIgnoreCase(CBC_MODE
))
142 result
= new CBC(cipher
, cipherBlockSize
);
144 else if (mode
.equalsIgnoreCase(CFB_MODE
))
146 result
= new CFB(cipher
, cipherBlockSize
);
148 else if (mode
.equalsIgnoreCase(EAX_MODE
))
150 result
= new EAX(cipher
, cipherBlockSize
);
153 if (result
!= null && !result
.selfTest())
155 throw new InternalError(result
.name());
162 * <p>Returns a {@link java.util.Set} of names of mode supported by this
163 * <i>Factory</i>.</p>
165 * @return a {@link java.util.Set} of mode names (Strings).
167 public static final Set
getNames()
169 synchronized (ModeFactory
.class)
173 HashSet hs
= new HashSet();
182 names
= Collections
.unmodifiableSet(hs
);
188 private static Set names
;
191 // -------------------------------------------------------------------------