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