ModeFactory.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* ModeFactory.java --
  2. Copyright (C) 2001, 2002, 2004, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.mode;
  32. import gnu.java.security.Registry;
  33. import gnu.javax.crypto.cipher.CipherFactory;
  34. import gnu.javax.crypto.cipher.IBlockCipher;
  35. import java.util.Collections;
  36. import java.util.HashSet;
  37. import java.util.Iterator;
  38. import java.util.Set;
  39. /**
  40. * A <i>Factory</i> to instantiate block cipher modes of operations.
  41. */
  42. public class ModeFactory
  43. implements Registry
  44. {
  45. private static Set names;
  46. /** Trivial constructor to enforce Singleton pattern. */
  47. private ModeFactory()
  48. {
  49. super();
  50. }
  51. /**
  52. * Returns an instance of a block cipher mode of operations given its name and
  53. * characteristics of the underlying block cipher.
  54. *
  55. * @param mode the case-insensitive name of the mode of operations.
  56. * @param cipher the case-insensitive name of the block cipher.
  57. * @param cipherBlockSize the block size, in bytes, of the underlying cipher.
  58. * @return an instance of the block cipher algorithm, operating in a given
  59. * mode of operations, or <code>null</code> if none found.
  60. * @exception InternalError if either the mode or the underlying block cipher
  61. * implementation does not pass its self-test.
  62. */
  63. public static IMode getInstance(String mode, String cipher,
  64. int cipherBlockSize)
  65. {
  66. if (mode == null || cipher == null)
  67. return null;
  68. mode = mode.trim();
  69. cipher = cipher.trim();
  70. IBlockCipher cipherImpl = CipherFactory.getInstance(cipher);
  71. if (cipherImpl == null)
  72. return null;
  73. return getInstance(mode, cipherImpl, cipherBlockSize);
  74. }
  75. public static IMode getInstance(String mode, IBlockCipher cipher,
  76. int cipherBlockSize)
  77. {
  78. // ensure that cipherBlockSize is valid for the chosen underlying cipher
  79. boolean ok = false;
  80. for (Iterator it = cipher.blockSizes(); it.hasNext();)
  81. {
  82. ok = (cipherBlockSize == ((Integer) it.next()).intValue());
  83. if (ok)
  84. break;
  85. }
  86. if (! ok)
  87. throw new IllegalArgumentException("cipherBlockSize");
  88. IMode result = null;
  89. if (mode.equalsIgnoreCase(ECB_MODE))
  90. result = new ECB(cipher, cipherBlockSize);
  91. else if (mode.equalsIgnoreCase(CTR_MODE))
  92. result = new CTR(cipher, cipherBlockSize);
  93. else if (mode.equalsIgnoreCase(ICM_MODE))
  94. result = new ICM(cipher, cipherBlockSize);
  95. else if (mode.equalsIgnoreCase(OFB_MODE))
  96. result = new OFB(cipher, cipherBlockSize);
  97. else if (mode.equalsIgnoreCase(CBC_MODE))
  98. result = new CBC(cipher, cipherBlockSize);
  99. else if (mode.equalsIgnoreCase(CFB_MODE))
  100. result = new CFB(cipher, cipherBlockSize);
  101. else if (mode.equalsIgnoreCase(EAX_MODE))
  102. result = new EAX(cipher, cipherBlockSize);
  103. if (result != null && ! result.selfTest())
  104. throw new InternalError(result.name());
  105. return result;
  106. }
  107. /**
  108. * Returns a {@link Set} of names of mode supported by this <i>Factory</i>.
  109. *
  110. * @return a {@link Set} of mode names (Strings).
  111. */
  112. public static final Set getNames()
  113. {
  114. synchronized (ModeFactory.class)
  115. {
  116. if (names == null)
  117. {
  118. HashSet hs = new HashSet();
  119. hs.add(ECB_MODE);
  120. hs.add(CTR_MODE);
  121. hs.add(ICM_MODE);
  122. hs.add(OFB_MODE);
  123. hs.add(CBC_MODE);
  124. hs.add(CFB_MODE);
  125. hs.add(EAX_MODE);
  126. names = Collections.unmodifiableSet(hs);
  127. }
  128. }
  129. return names;
  130. }
  131. }