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