Source for gnu.javax.crypto.jce.prng.UMacRandomSpi

   1: /* UMacRandomSpi.java -- 
   2:    Copyright (C) 2001, 2002, 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.jce.prng;
  40: 
  41: import gnu.java.security.Configuration;
  42: import gnu.java.security.Registry;
  43: import gnu.java.security.prng.LimitReachedException;
  44: import gnu.javax.crypto.cipher.IBlockCipher;
  45: import gnu.javax.crypto.prng.UMacGenerator;
  46: 
  47: import java.security.SecureRandomSpi;
  48: import java.util.HashMap;
  49: import java.util.Random;
  50: import java.util.logging.Logger;
  51: 
  52: /**
  53:  * An <em>Adapter</em> class around {@link UMacGenerator} to allow using this
  54:  * algorithm as a JCE {@link java.security.SecureRandom}.
  55:  */
  56: public class UMacRandomSpi
  57:     extends SecureRandomSpi
  58: {
  59:   private static final Logger log = Logger.getLogger(UMacRandomSpi.class.getName());
  60:   /** Class-wide prng to generate random material for the underlying prng. */
  61:   private static final UMacGenerator prng; // blank final
  62:   static
  63:     {
  64:       prng = new UMacGenerator();
  65:       resetLocalPRNG();
  66:     }
  67:   // error messages
  68:   private static final String MSG = "Exception while setting up a "
  69:                                     + Registry.UMAC_PRNG + " SPI: ";
  70:   private static final String RETRY = "Retry...";
  71:   /** Our underlying prng instance. */
  72:   private UMacGenerator adaptee = new UMacGenerator();
  73: 
  74:   // default 0-arguments constructor
  75: 
  76:   private static void resetLocalPRNG()
  77:   {
  78:     HashMap attributes = new HashMap();
  79:     attributes.put(UMacGenerator.CIPHER, Registry.AES_CIPHER);
  80:     byte[] key = new byte[128 / 8]; // AES default key size
  81:     Random rand = new Random(System.currentTimeMillis());
  82:     rand.nextBytes(key);
  83:     attributes.put(IBlockCipher.KEY_MATERIAL, key);
  84:     int index = rand.nextInt() & 0xFF;
  85:     attributes.put(UMacGenerator.INDEX, Integer.valueOf(index));
  86:     prng.setup(attributes);
  87:   }
  88: 
  89:   public byte[] engineGenerateSeed(int numBytes)
  90:   {
  91:     if (numBytes < 1)
  92:       return new byte[0];
  93:     byte[] result = new byte[numBytes];
  94:     this.engineNextBytes(result);
  95:     return result;
  96:   }
  97: 
  98:   public void engineNextBytes(byte[] bytes)
  99:   {
 100:     if (! adaptee.isInitialised())
 101:       this.engineSetSeed(new byte[0]);
 102:     while (true)
 103:       {
 104:         try
 105:           {
 106:             adaptee.nextBytes(bytes, 0, bytes.length);
 107:             break;
 108:           }
 109:         catch (LimitReachedException x)
 110:           { // reseed the generator
 111:             resetLocalPRNG();
 112:           }
 113:       }
 114:   }
 115: 
 116:   public void engineSetSeed(byte[] seed)
 117:   {
 118:     // compute the total number of random bytes required to setup adaptee
 119:     int materialLength = 0;
 120:     materialLength += 16; // key material size
 121:     materialLength++; // index size
 122:     byte[] material = new byte[materialLength];
 123:     // use as much as possible bytes from the seed
 124:     int materialOffset = 0;
 125:     int materialLeft = material.length;
 126:     if (seed.length > 0)
 127:       { // copy some bytes into key and update indices
 128:         int lenToCopy = Math.min(materialLength, seed.length);
 129:         System.arraycopy(seed, 0, material, 0, lenToCopy);
 130:         materialOffset += lenToCopy;
 131:         materialLeft -= lenToCopy;
 132:       }
 133:     if (materialOffset > 0) // generate the rest
 134:       {
 135:         while (true)
 136:           {
 137:             try
 138:               {
 139:                 prng.nextBytes(material, materialOffset, materialLeft);
 140:                 break;
 141:               }
 142:             catch (IllegalStateException x) // should not happen
 143:               {
 144:                 throw new InternalError(MSG + String.valueOf(x));
 145:               }
 146:             catch (LimitReachedException x)
 147:               {
 148:                 if (Configuration.DEBUG)
 149:                   {
 150:                     log.fine(MSG + String.valueOf(x));
 151:                     log.fine(RETRY);
 152:                   }
 153:               }
 154:           }
 155:       }
 156:     // setup the underlying adaptee instance
 157:     HashMap attributes = new HashMap();
 158:     // use AES cipher with 128-bit block size
 159:     attributes.put(UMacGenerator.CIPHER, Registry.AES_CIPHER);
 160:     // specify the key
 161:     byte[] key = new byte[16];
 162:     System.arraycopy(material, 0, key, 0, 16);
 163:     attributes.put(IBlockCipher.KEY_MATERIAL, key);
 164:     // use a 1-byte index
 165:     attributes.put(UMacGenerator.INDEX, Integer.valueOf(material[16] & 0xFF));
 166:     adaptee.init(attributes);
 167:   }
 168: }