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

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