Source for gnu.javax.crypto.cipher.BaseCipher

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