BaseCipher.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* BaseCipher.java --
  2. Copyright (C) 2001, 2002, 2003, 2006, 2010 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.cipher;
  32. import gnu.java.lang.CPStringBuilder;
  33. import gnu.java.security.Configuration;
  34. import java.security.InvalidKeyException;
  35. import java.util.Arrays;
  36. import java.util.Iterator;
  37. import java.util.Map;
  38. import java.util.logging.Level;
  39. import java.util.logging.Logger;
  40. /**
  41. * A basic abstract class to facilitate implementing symmetric key block
  42. * ciphers.
  43. */
  44. public abstract class BaseCipher
  45. implements IBlockCipher, IBlockCipherSpi
  46. {
  47. private static final Logger log = Configuration.DEBUG ?
  48. Logger.getLogger(BaseCipher.class.getName()) : null;
  49. /** The canonical name prefix of the cipher. */
  50. protected String name;
  51. /** The default block size, in bytes. */
  52. protected int defaultBlockSize;
  53. /** The default key size, in bytes. */
  54. protected int defaultKeySize;
  55. /** The current block size, in bytes. */
  56. protected int currentBlockSize;
  57. /** The session key for this instance. */
  58. protected transient Object currentKey;
  59. /** The instance lock. */
  60. protected Object lock = new Object();
  61. /**
  62. * Trivial constructor for use by concrete subclasses.
  63. *
  64. * @param name the canonical name prefix of this instance.
  65. * @param defaultBlockSize the default block size in bytes.
  66. * @param defaultKeySize the default key size in bytes.
  67. */
  68. protected BaseCipher(String name, int defaultBlockSize, int defaultKeySize)
  69. {
  70. super();
  71. this.name = name;
  72. this.defaultBlockSize = defaultBlockSize;
  73. this.defaultKeySize = defaultKeySize;
  74. }
  75. public abstract Object clone();
  76. public String name()
  77. {
  78. CPStringBuilder sb = new CPStringBuilder(name).append('-');
  79. if (currentKey == null)
  80. sb.append(String.valueOf(8 * defaultBlockSize));
  81. else
  82. sb.append(String.valueOf(8 * currentBlockSize));
  83. return sb.toString();
  84. }
  85. public int defaultBlockSize()
  86. {
  87. return defaultBlockSize;
  88. }
  89. public int defaultKeySize()
  90. {
  91. return defaultKeySize;
  92. }
  93. public void init(Map attributes) throws InvalidKeyException
  94. {
  95. synchronized (lock)
  96. {
  97. if (currentKey != null)
  98. throw new IllegalStateException();
  99. Integer bs = (Integer) attributes.get(CIPHER_BLOCK_SIZE);
  100. if (bs == null) // no block size was specified
  101. {
  102. if (currentBlockSize == 0) // happy birthday
  103. currentBlockSize = defaultBlockSize;
  104. // else it's a clone. use as is
  105. }
  106. else
  107. {
  108. currentBlockSize = bs.intValue();
  109. // ensure that value is valid
  110. Iterator it;
  111. boolean ok = false;
  112. for (it = blockSizes(); it.hasNext();)
  113. {
  114. ok = (currentBlockSize == ((Integer) it.next()).intValue());
  115. if (ok)
  116. break;
  117. }
  118. if (! ok)
  119. throw new IllegalArgumentException(IBlockCipher.CIPHER_BLOCK_SIZE);
  120. }
  121. byte[] k = (byte[]) attributes.get(KEY_MATERIAL);
  122. currentKey = makeKey(k, currentBlockSize);
  123. }
  124. }
  125. public int currentBlockSize()
  126. {
  127. if (currentKey == null)
  128. throw new IllegalStateException();
  129. return currentBlockSize;
  130. }
  131. public void reset()
  132. {
  133. synchronized (lock)
  134. {
  135. currentKey = null;
  136. }
  137. }
  138. public void encryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
  139. throws IllegalStateException
  140. {
  141. synchronized (lock)
  142. {
  143. if (currentKey == null)
  144. throw new IllegalStateException();
  145. encrypt(in, inOffset, out, outOffset, currentKey, currentBlockSize);
  146. }
  147. }
  148. public void decryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
  149. throws IllegalStateException
  150. {
  151. synchronized (lock)
  152. {
  153. if (currentKey == null)
  154. throw new IllegalStateException();
  155. decrypt(in, inOffset, out, outOffset, currentKey, currentBlockSize);
  156. }
  157. }
  158. public boolean selfTest()
  159. {
  160. int ks;
  161. Iterator bit;
  162. // do symmetry tests for all block-size/key-size combos
  163. for (Iterator kit = keySizes(); kit.hasNext();)
  164. {
  165. ks = ((Integer) kit.next()).intValue();
  166. for (bit = blockSizes(); bit.hasNext();)
  167. if (! testSymmetry(ks, ((Integer) bit.next()).intValue()))
  168. return false;
  169. }
  170. return true;
  171. }
  172. private boolean testSymmetry(int ks, int bs)
  173. {
  174. try
  175. {
  176. byte[] kb = new byte[ks];
  177. byte[] pt = new byte[bs];
  178. byte[] ct = new byte[bs];
  179. byte[] cpt = new byte[bs];
  180. int i;
  181. for (i = 0; i < ks; i++)
  182. kb[i] = (byte) i;
  183. for (i = 0; i < bs; i++)
  184. pt[i] = (byte) i;
  185. Object k = makeKey(kb, bs);
  186. encrypt(pt, 0, ct, 0, k, bs);
  187. decrypt(ct, 0, cpt, 0, k, bs);
  188. return Arrays.equals(pt, cpt);
  189. }
  190. catch (Exception x)
  191. {
  192. if (Configuration.DEBUG)
  193. log.log(Level.FINE, "Exception in testSymmetry() for " + name(), x);
  194. return false;
  195. }
  196. }
  197. protected boolean testKat(byte[] kb, byte[] ct)
  198. {
  199. return testKat(kb, ct, new byte[ct.length]); // all-zero plaintext
  200. }
  201. protected boolean testKat(byte[] kb, byte[] ct, byte[] pt)
  202. {
  203. try
  204. {
  205. int bs = pt.length;
  206. byte[] t = new byte[bs];
  207. Object k = makeKey(kb, bs);
  208. // test encryption
  209. encrypt(pt, 0, t, 0, k, bs);
  210. if (! Arrays.equals(t, ct))
  211. return false;
  212. // test decryption
  213. decrypt(t, 0, t, 0, k, bs);
  214. return Arrays.equals(t, pt);
  215. }
  216. catch (Exception x)
  217. {
  218. if (Configuration.DEBUG)
  219. log.log(Level.FINE, "Exception in testKat() for " + name(), x);
  220. return false;
  221. }
  222. }
  223. }