< prev index next >

src/share/classes/sun/security/ec/ECKeyPairGenerator.java

Print this page
rev 12543 : 8181048: Refactor existing providers to refer to the same constants for default values for key length
Reviewed-by: mullan, ahgross
   1 /*
   2  * Copyright (c) 2009, 2014, 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
  23  * questions.
  24  */
  25 
  26 package sun.security.ec;
  27 
  28 import java.math.BigInteger;
  29 import java.security.*;
  30 import java.security.spec.AlgorithmParameterSpec;
  31 import java.security.spec.ECGenParameterSpec;
  32 import java.security.spec.ECParameterSpec;
  33 import java.security.spec.ECPoint;
  34 
  35 import sun.security.ec.NamedCurve;
  36 import sun.security.ec.ECParameters;
  37 import sun.security.ec.ECPrivateKeyImpl;
  38 import sun.security.ec.ECPublicKeyImpl;
  39 import sun.security.jca.JCAUtil;
  40 import sun.security.util.ECUtil;

  41 
  42 /**
  43  * EC keypair generator.
  44  * Standard algorithm, minimum key length is 112 bits, maximum is 571 bits.
  45  *
  46  * @since 1.7
  47  */
  48 public final class ECKeyPairGenerator extends KeyPairGeneratorSpi {
  49 
  50     private static final int KEY_SIZE_MIN = 112; // min bits (see ecc_impl.h)
  51     private static final int KEY_SIZE_MAX = 571; // max bits (see ecc_impl.h)
  52     private static final int KEY_SIZE_DEFAULT = 256;
  53 
  54     // used to seed the keypair generator
  55     private SecureRandom random;
  56 
  57     // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
  58     private int keySize;
  59 
  60     // parameters specified via init, if any
  61     private AlgorithmParameterSpec params = null;
  62 
  63     /**
  64      * Constructs a new ECKeyPairGenerator.
  65      */
  66     public ECKeyPairGenerator() {
  67         // initialize to default in case the app does not call initialize()
  68         initialize(KEY_SIZE_DEFAULT, null);
  69     }
  70 
  71     // initialize the generator. See JCA doc
  72     @Override
  73     public void initialize(int keySize, SecureRandom random) {
  74 
  75         checkKeySize(keySize);
  76         this.params = ECUtil.getECParameterSpec(null, keySize);
  77         if (params == null) {
  78             throw new InvalidParameterException(
  79                 "No EC parameters available for key size " + keySize + " bits");
  80         }
  81         this.random = random;
  82     }
  83 
  84     // second initialize method. See JCA doc
  85     @Override
  86     public void initialize(AlgorithmParameterSpec params, SecureRandom random)
  87             throws InvalidAlgorithmParameterException {
  88 


   1 /*
   2  * Copyright (c) 2009, 2017, 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
  23  * questions.
  24  */
  25 
  26 package sun.security.ec;
  27 
  28 import java.math.BigInteger;
  29 import java.security.*;
  30 import java.security.spec.AlgorithmParameterSpec;
  31 import java.security.spec.ECGenParameterSpec;
  32 import java.security.spec.ECParameterSpec;
  33 import java.security.spec.ECPoint;
  34 
  35 import sun.security.ec.NamedCurve;
  36 import sun.security.ec.ECParameters;
  37 import sun.security.ec.ECPrivateKeyImpl;
  38 import sun.security.ec.ECPublicKeyImpl;
  39 import sun.security.jca.JCAUtil;
  40 import sun.security.util.ECUtil;
  41 import static sun.security.util.SecurityProviderConstants.DEF_EC_KEY_SIZE;
  42 
  43 /**
  44  * EC keypair generator.
  45  * Standard algorithm, minimum key length is 112 bits, maximum is 571 bits.
  46  *
  47  * @since 1.7
  48  */
  49 public final class ECKeyPairGenerator extends KeyPairGeneratorSpi {
  50 
  51     private static final int KEY_SIZE_MIN = 112; // min bits (see ecc_impl.h)
  52     private static final int KEY_SIZE_MAX = 571; // max bits (see ecc_impl.h)

  53 
  54     // used to seed the keypair generator
  55     private SecureRandom random;
  56 
  57     // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
  58     private int keySize;
  59 
  60     // parameters specified via init, if any
  61     private AlgorithmParameterSpec params = null;
  62 
  63     /**
  64      * Constructs a new ECKeyPairGenerator.
  65      */
  66     public ECKeyPairGenerator() {
  67         // initialize to default in case the app does not call initialize()
  68         initialize(DEF_EC_KEY_SIZE, null);
  69     }
  70 
  71     // initialize the generator. See JCA doc
  72     @Override
  73     public void initialize(int keySize, SecureRandom random) {
  74 
  75         checkKeySize(keySize);
  76         this.params = ECUtil.getECParameterSpec(null, keySize);
  77         if (params == null) {
  78             throw new InvalidParameterException(
  79                 "No EC parameters available for key size " + keySize + " bits");
  80         }
  81         this.random = random;
  82     }
  83 
  84     // second initialize method. See JCA doc
  85     @Override
  86     public void initialize(AlgorithmParameterSpec params, SecureRandom random)
  87             throws InvalidAlgorithmParameterException {
  88 


< prev index next >