src/java.base/share/classes/com/sun/crypto/provider/KeyGeneratorCore.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  94         this.random = random;
  95     }
  96 
  97     // implementation for engineInit(), see JCE doc
  98     // generate the key
  99     SecretKey implGenerateKey() {
 100         if (random == null) {
 101             random = SunJCE.getRandom();
 102         }
 103         byte[] b = new byte[(keySize + 7) >> 3];
 104         random.nextBytes(b);
 105         return new SecretKeySpec(b, name);
 106     }
 107 
 108     // nested static classes for the HmacSHA-2 family of key generator
 109     abstract static class HmacSHA2KG extends KeyGeneratorSpi {
 110         private final KeyGeneratorCore core;
 111         protected HmacSHA2KG(String algoName, int len) {
 112             core = new KeyGeneratorCore(algoName, len);
 113         }

 114         protected void engineInit(SecureRandom random) {
 115             core.implInit(random);
 116         }

 117         protected void engineInit(AlgorithmParameterSpec params,
 118                 SecureRandom random) throws InvalidAlgorithmParameterException {
 119             core.implInit(params, random);
 120         }

 121         protected void engineInit(int keySize, SecureRandom random) {
 122             core.implInit(keySize, random);
 123         }

 124         protected SecretKey engineGenerateKey() {
 125             return core.implGenerateKey();
 126         }
 127 
 128         public static final class SHA224 extends HmacSHA2KG {
 129             public SHA224() {
 130                 super("HmacSHA224", 224);
 131             }
 132         }
 133         public static final class SHA256 extends HmacSHA2KG {
 134             public SHA256() {
 135                 super("HmacSHA256", 256);
 136             }
 137         }
 138         public static final class SHA384 extends HmacSHA2KG {
 139             public SHA384() {
 140                 super("HmacSHA384", 384);
 141             }
 142         }
 143         public static final class SHA512 extends HmacSHA2KG {
 144             public SHA512() {
 145                 super("HmacSHA512", 512);
 146             }
 147         }
 148     }
 149 
 150     // nested static class for the RC2 key generator
 151     public static final class RC2KeyGenerator extends KeyGeneratorSpi {
 152         private final KeyGeneratorCore core;
 153         public RC2KeyGenerator() {
 154             core = new KeyGeneratorCore("RC2", 128);
 155         }

 156         protected void engineInit(SecureRandom random) {
 157             core.implInit(random);
 158         }

 159         protected void engineInit(AlgorithmParameterSpec params,
 160                 SecureRandom random) throws InvalidAlgorithmParameterException {
 161             core.implInit(params, random);
 162         }

 163         protected void engineInit(int keySize, SecureRandom random) {
 164             if ((keySize < 40) || (keySize > 1024)) {
 165                 throw new InvalidParameterException("Key length for RC2"
 166                     + " must be between 40 and 1024 bits");
 167             }
 168             core.implInit(keySize, random);
 169         }

 170         protected SecretKey engineGenerateKey() {
 171             return core.implGenerateKey();
 172         }
 173     }
 174 
 175     // nested static class for the ARCFOUR (RC4) key generator
 176     public static final class ARCFOURKeyGenerator extends KeyGeneratorSpi {
 177         private final KeyGeneratorCore core;
 178         public ARCFOURKeyGenerator() {
 179             core = new KeyGeneratorCore("ARCFOUR", 128);
 180         }

 181         protected void engineInit(SecureRandom random) {
 182             core.implInit(random);
 183         }

 184         protected void engineInit(AlgorithmParameterSpec params,
 185                 SecureRandom random) throws InvalidAlgorithmParameterException {
 186             core.implInit(params, random);
 187         }

 188         protected void engineInit(int keySize, SecureRandom random) {
 189             if ((keySize < 40) || (keySize > 1024)) {
 190                 throw new InvalidParameterException("Key length for ARCFOUR"
 191                     + " must be between 40 and 1024 bits");
 192             }
 193             core.implInit(keySize, random);
 194         }

 195         protected SecretKey engineGenerateKey() {
 196             return core.implGenerateKey();
 197         }
 198     }
 199 




























 200 }
   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  94         this.random = random;
  95     }
  96 
  97     // implementation for engineInit(), see JCE doc
  98     // generate the key
  99     SecretKey implGenerateKey() {
 100         if (random == null) {
 101             random = SunJCE.getRandom();
 102         }
 103         byte[] b = new byte[(keySize + 7) >> 3];
 104         random.nextBytes(b);
 105         return new SecretKeySpec(b, name);
 106     }
 107 
 108     // nested static classes for the HmacSHA-2 family of key generator
 109     abstract static class HmacSHA2KG extends KeyGeneratorSpi {
 110         private final KeyGeneratorCore core;
 111         protected HmacSHA2KG(String algoName, int len) {
 112             core = new KeyGeneratorCore(algoName, len);
 113         }
 114         @Override
 115         protected void engineInit(SecureRandom random) {
 116             core.implInit(random);
 117         }
 118         @Override
 119         protected void engineInit(AlgorithmParameterSpec params,
 120                 SecureRandom random) throws InvalidAlgorithmParameterException {
 121             core.implInit(params, random);
 122         }
 123         @Override
 124         protected void engineInit(int keySize, SecureRandom random) {
 125             core.implInit(keySize, random);
 126         }
 127         @Override
 128         protected SecretKey engineGenerateKey() {
 129             return core.implGenerateKey();
 130         }
 131 
 132         public static final class SHA224 extends HmacSHA2KG {
 133             public SHA224() {
 134                 super("HmacSHA224", 224);
 135             }
 136         }
 137         public static final class SHA256 extends HmacSHA2KG {
 138             public SHA256() {
 139                 super("HmacSHA256", 256);
 140             }
 141         }
 142         public static final class SHA384 extends HmacSHA2KG {
 143             public SHA384() {
 144                 super("HmacSHA384", 384);
 145             }
 146         }
 147         public static final class SHA512 extends HmacSHA2KG {
 148             public SHA512() {
 149                 super("HmacSHA512", 512);
 150             }
 151         }
 152     }
 153 
 154     // nested static class for the RC2 key generator
 155     public static final class RC2KeyGenerator extends KeyGeneratorSpi {
 156         private final KeyGeneratorCore core;
 157         public RC2KeyGenerator() {
 158             core = new KeyGeneratorCore("RC2", 128);
 159         }
 160         @Override
 161         protected void engineInit(SecureRandom random) {
 162             core.implInit(random);
 163         }
 164         @Override
 165         protected void engineInit(AlgorithmParameterSpec params,
 166                 SecureRandom random) throws InvalidAlgorithmParameterException {
 167             core.implInit(params, random);
 168         }
 169         @Override
 170         protected void engineInit(int keySize, SecureRandom random) {
 171             if ((keySize < 40) || (keySize > 1024)) {
 172                 throw new InvalidParameterException("Key length for RC2"
 173                     + " must be between 40 and 1024 bits");
 174             }
 175             core.implInit(keySize, random);
 176         }
 177         @Override
 178         protected SecretKey engineGenerateKey() {
 179             return core.implGenerateKey();
 180         }
 181     }
 182 
 183     // nested static class for the ARCFOUR (RC4) key generator
 184     public static final class ARCFOURKeyGenerator extends KeyGeneratorSpi {
 185         private final KeyGeneratorCore core;
 186         public ARCFOURKeyGenerator() {
 187             core = new KeyGeneratorCore("ARCFOUR", 128);
 188         }
 189         @Override
 190         protected void engineInit(SecureRandom random) {
 191             core.implInit(random);
 192         }
 193         @Override
 194         protected void engineInit(AlgorithmParameterSpec params,
 195                 SecureRandom random) throws InvalidAlgorithmParameterException {
 196             core.implInit(params, random);
 197         }
 198         @Override
 199         protected void engineInit(int keySize, SecureRandom random) {
 200             if ((keySize < 40) || (keySize > 1024)) {
 201                 throw new InvalidParameterException("Key length for ARCFOUR"
 202                     + " must be between 40 and 1024 bits");
 203             }
 204             core.implInit(keySize, random);
 205         }
 206         @Override
 207         protected SecretKey engineGenerateKey() {
 208             return core.implGenerateKey();
 209         }
 210     }
 211 
 212     // nested static class for the ChaCha20 key generator
 213     public static final class ChaCha20KeyGenerator extends KeyGeneratorSpi {
 214         private final KeyGeneratorCore core;
 215         public ChaCha20KeyGenerator() {
 216             core = new KeyGeneratorCore("ChaCha20", 256);
 217         }
 218         @Override
 219         protected void engineInit(SecureRandom random) {
 220             core.implInit(random);
 221         }
 222         @Override
 223         protected void engineInit(AlgorithmParameterSpec params,
 224                 SecureRandom random) throws InvalidAlgorithmParameterException {
 225             core.implInit(params, random);
 226         }
 227         @Override
 228         protected void engineInit(int keySize, SecureRandom random) {
 229             if (keySize != 256) {
 230                 throw new InvalidParameterException(
 231                         "Key length for ChaCha20 must be 256 bits");
 232             }
 233             core.implInit(keySize, random);
 234         }
 235         @Override
 236         protected SecretKey engineGenerateKey() {
 237             return core.implGenerateKey();
 238         }
 239     }
 240 }