Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / mode / ModeFactory.java
blob0e949ed9e96d29b314de5f3f5b8829720265f23d
1 /* ModeFactory.java --
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
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.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;
49 import java.util.Set;
51 /**
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 // -------------------------------------------------------------------------
60 // Constructor(s)
61 // -------------------------------------------------------------------------
63 /** Trivial constructor to enforce Singleton pattern. */
64 private ModeFactory()
66 super();
69 // Class methods
70 // -------------------------------------------------------------------------
72 /**
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,
85 int cipherBlockSize)
87 if (mode == null || cipher == null)
89 return null;
92 mode = mode.trim();
93 cipher = cipher.trim();
95 IBlockCipher cipherImpl = CipherFactory.getInstance(cipher);
96 if (cipherImpl == null)
98 return null;
101 return getInstance(mode, cipherImpl, cipherBlockSize);
104 public static IMode getInstance(String mode, IBlockCipher cipher,
105 int cipherBlockSize)
107 // ensure that cipherBlockSize is valid for the chosen underlying cipher
108 boolean ok = false;
109 for (Iterator it = cipher.blockSizes(); it.hasNext();)
111 ok = (cipherBlockSize == ((Integer) it.next()).intValue());
112 if (ok)
114 break;
118 if (!ok)
120 throw new IllegalArgumentException("cipherBlockSize");
123 IMode result = null;
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());
158 return result;
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)
171 if (names == null)
173 HashSet hs = new HashSet();
174 hs.add(ECB_MODE);
175 hs.add(CTR_MODE);
176 hs.add(ICM_MODE);
177 hs.add(OFB_MODE);
178 hs.add(CBC_MODE);
179 hs.add(CFB_MODE);
180 hs.add(EAX_MODE);
182 names = Collections.unmodifiableSet(hs);
185 return names;
188 private static Set names;
190 // Instance methods
191 // -------------------------------------------------------------------------