| 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
 | 
 | 
package com.sun.crypto.provider;  | 
 | 
 | 
 | 
import java.security.*;  | 
 | 
import java.security.spec.AlgorithmParameterSpec;  | 
 | 
 | 
 | 
import javax.crypto.*;  | 
 | 
import javax.crypto.spec.SecretKeySpec;  | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
final class KeyGeneratorCore { | 
 | 
 | 
 | 
      | 
 | 
    private final String name;  | 
 | 
 | 
 | 
      | 
 | 
    private final int defaultKeySize;  | 
 | 
 | 
 | 
      | 
 | 
    private int keySize;  | 
 | 
 | 
 | 
      | 
 | 
    private SecureRandom random;  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    KeyGeneratorCore(String name, int defaultKeySize) { | 
 | 
        this.name = name;  | 
 | 
        this.defaultKeySize = defaultKeySize;  | 
 | 
        implInit(null);  | 
 | 
    }  | 
 | 
 | 
 | 
    // implementation for engineInit(), see JCE doc  | 
 | 
      | 
 | 
    void implInit(SecureRandom random) { | 
 | 
        this.keySize = defaultKeySize;  | 
 | 
        this.random = random;  | 
 | 
    }  | 
 | 
 | 
 | 
    // implementation for engineInit(), see JCE doc  | 
 | 
      | 
 | 
    void implInit(AlgorithmParameterSpec params, SecureRandom random)  | 
 | 
            throws InvalidAlgorithmParameterException { | 
 | 
        throw new InvalidAlgorithmParameterException  | 
 | 
            (name + " key generation does not take any parameters");  | 
 | 
    }  | 
 | 
 | 
 | 
    // implementation for engineInit(), see JCE doc  | 
 | 
      | 
 | 
    void implInit(int keysize, SecureRandom random) { | 
 | 
        if (keysize < 40) { | 
 | 
            throw new InvalidParameterException  | 
 | 
                ("Key length must be at least 40 bits"); | 
 | 
        }  | 
 | 
        this.keySize = keysize;  | 
 | 
        this.random = random;  | 
 | 
    }  | 
 | 
 | 
 | 
    // implementation for engineInit(), see JCE doc  | 
 | 
      | 
 | 
    SecretKey implGenerateKey() { | 
 | 
        if (random == null) { | 
 | 
            random = SunJCE.getRandom();  | 
 | 
        }  | 
 | 
        byte[] b = new byte[(keySize + 7) >> 3];  | 
 | 
        random.nextBytes(b);  | 
 | 
        return new SecretKeySpec(b, name);  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    abstract static class HmacSHA2KG extends KeyGeneratorSpi { | 
 | 
        private final KeyGeneratorCore core;  | 
 | 
        protected HmacSHA2KG(String algoName, int len) { | 
 | 
            core = new KeyGeneratorCore(algoName, len);  | 
 | 
        }  | 
 | 
        protected void engineInit(SecureRandom random) { | 
 | 
            core.implInit(random);  | 
 | 
        }  | 
 | 
        protected void engineInit(AlgorithmParameterSpec params,  | 
 | 
                SecureRandom random) throws InvalidAlgorithmParameterException { | 
 | 
            core.implInit(params, random);  | 
 | 
        }  | 
 | 
        protected void engineInit(int keySize, SecureRandom random) { | 
 | 
            core.implInit(keySize, random);  | 
 | 
        }  | 
 | 
        protected SecretKey engineGenerateKey() { | 
 | 
            return core.implGenerateKey();  | 
 | 
        }  | 
 | 
 | 
 | 
        public static final class SHA224 extends HmacSHA2KG { | 
 | 
            public SHA224() { | 
 | 
                super("HmacSHA224", 224); | 
 | 
            }  | 
 | 
        }  | 
 | 
        public static final class SHA256 extends HmacSHA2KG { | 
 | 
            public SHA256() { | 
 | 
                super("HmacSHA256", 256); | 
 | 
            }  | 
 | 
        }  | 
 | 
        public static final class SHA384 extends HmacSHA2KG { | 
 | 
            public SHA384() { | 
 | 
                super("HmacSHA384", 384); | 
 | 
            }  | 
 | 
        }  | 
 | 
        public static final class SHA512 extends HmacSHA2KG { | 
 | 
            public SHA512() { | 
 | 
                super("HmacSHA512", 512); | 
 | 
            }  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    public static final class RC2KeyGenerator extends KeyGeneratorSpi { | 
 | 
        private final KeyGeneratorCore core;  | 
 | 
        public RC2KeyGenerator() { | 
 | 
            core = new KeyGeneratorCore("RC2", 128); | 
 | 
        }  | 
 | 
        protected void engineInit(SecureRandom random) { | 
 | 
            core.implInit(random);  | 
 | 
        }  | 
 | 
        protected void engineInit(AlgorithmParameterSpec params,  | 
 | 
                SecureRandom random) throws InvalidAlgorithmParameterException { | 
 | 
            core.implInit(params, random);  | 
 | 
        }  | 
 | 
        protected void engineInit(int keySize, SecureRandom random) { | 
 | 
            if ((keySize < 40) || (keySize > 1024)) { | 
 | 
                throw new InvalidParameterException("Key length for RC2" | 
 | 
                    + " must be between 40 and 1024 bits");  | 
 | 
            }  | 
 | 
            core.implInit(keySize, random);  | 
 | 
        }  | 
 | 
        protected SecretKey engineGenerateKey() { | 
 | 
            return core.implGenerateKey();  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    public static final class ARCFOURKeyGenerator extends KeyGeneratorSpi { | 
 | 
        private final KeyGeneratorCore core;  | 
 | 
        public ARCFOURKeyGenerator() { | 
 | 
            core = new KeyGeneratorCore("ARCFOUR", 128); | 
 | 
        }  | 
 | 
        protected void engineInit(SecureRandom random) { | 
 | 
            core.implInit(random);  | 
 | 
        }  | 
 | 
        protected void engineInit(AlgorithmParameterSpec params,  | 
 | 
                SecureRandom random) throws InvalidAlgorithmParameterException { | 
 | 
            core.implInit(params, random);  | 
 | 
        }  | 
 | 
        protected void engineInit(int keySize, SecureRandom random) { | 
 | 
            if ((keySize < 40) || (keySize > 1024)) { | 
 | 
                throw new InvalidParameterException("Key length for ARCFOUR" | 
 | 
                    + " must be between 40 and 1024 bits");  | 
 | 
            }  | 
 | 
            core.implInit(keySize, random);  | 
 | 
        }  | 
 | 
        protected SecretKey engineGenerateKey() { | 
 | 
            return core.implGenerateKey();  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
}  |