OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [javax/] [crypto/] [mode/] [ModeFactory.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* ModeFactory.java --
2
   Copyright (C) 2001, 2002, 2004, 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.java.security.Registry;
42
 
43
import gnu.javax.crypto.cipher.CipherFactory;
44
import gnu.javax.crypto.cipher.IBlockCipher;
45
 
46
import java.util.Collections;
47
import java.util.HashSet;
48
import java.util.Iterator;
49
import java.util.Set;
50
 
51
/**
52
 * A <i>Factory</i> to instantiate block cipher modes of operations.
53
 */
54
public class ModeFactory
55
    implements Registry
56
{
57
  private static Set names;
58
 
59
  /** Trivial constructor to enforce Singleton pattern. */
60
  private ModeFactory()
61
  {
62
    super();
63
  }
64
 
65
  /**
66
   * Returns an instance of a block cipher mode of operations given its name and
67
   * characteristics of the underlying block cipher.
68
   *
69
   * @param mode the case-insensitive name of the mode of operations.
70
   * @param cipher the case-insensitive name of the block cipher.
71
   * @param cipherBlockSize the block size, in bytes, of the underlying cipher.
72
   * @return an instance of the block cipher algorithm, operating in a given
73
   *         mode of operations, or <code>null</code> if none found.
74
   * @exception InternalError if either the mode or the underlying block cipher
75
   *              implementation does not pass its self-test.
76
   */
77
  public static IMode getInstance(String mode, String cipher,
78
                                  int cipherBlockSize)
79
  {
80
    if (mode == null || cipher == null)
81
      return null;
82
 
83
    mode = mode.trim();
84
    cipher = cipher.trim();
85
    IBlockCipher cipherImpl = CipherFactory.getInstance(cipher);
86
    if (cipherImpl == null)
87
      return null;
88
 
89
    return getInstance(mode, cipherImpl, cipherBlockSize);
90
  }
91
 
92
  public static IMode getInstance(String mode, IBlockCipher cipher,
93
                                  int cipherBlockSize)
94
  {
95
    // ensure that cipherBlockSize is valid for the chosen underlying cipher
96
    boolean ok = false;
97
    for (Iterator it = cipher.blockSizes(); it.hasNext();)
98
      {
99
        ok = (cipherBlockSize == ((Integer) it.next()).intValue());
100
        if (ok)
101
          break;
102
      }
103
    if (! ok)
104
      throw new IllegalArgumentException("cipherBlockSize");
105
    IMode result = null;
106
    if (mode.equalsIgnoreCase(ECB_MODE))
107
      result = new ECB(cipher, cipherBlockSize);
108
    else if (mode.equalsIgnoreCase(CTR_MODE))
109
      result = new CTR(cipher, cipherBlockSize);
110
    else if (mode.equalsIgnoreCase(ICM_MODE))
111
      result = new ICM(cipher, cipherBlockSize);
112
    else if (mode.equalsIgnoreCase(OFB_MODE))
113
      result = new OFB(cipher, cipherBlockSize);
114
    else if (mode.equalsIgnoreCase(CBC_MODE))
115
      result = new CBC(cipher, cipherBlockSize);
116
    else if (mode.equalsIgnoreCase(CFB_MODE))
117
      result = new CFB(cipher, cipherBlockSize);
118
    else if (mode.equalsIgnoreCase(EAX_MODE))
119
      result = new EAX(cipher, cipherBlockSize);
120
 
121
    if (result != null && ! result.selfTest())
122
      throw new InternalError(result.name());
123
 
124
    return result;
125
  }
126
 
127
  /**
128
   * Returns a {@link Set} of names of mode supported by this <i>Factory</i>.
129
   *
130
   * @return a {@link Set} of mode names (Strings).
131
   */
132
  public static final Set getNames()
133
  {
134
    synchronized (ModeFactory.class)
135
      {
136
        if (names == null)
137
          {
138
            HashSet hs = new HashSet();
139
            hs.add(ECB_MODE);
140
            hs.add(CTR_MODE);
141
            hs.add(ICM_MODE);
142
            hs.add(OFB_MODE);
143
            hs.add(CBC_MODE);
144
            hs.add(CFB_MODE);
145
            hs.add(EAX_MODE);
146
            names = Collections.unmodifiableSet(hs);
147
          }
148
      }
149
    return names;
150
  }
151
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.