jdk/src/share/classes/sun/security/ec/ECKeyPairGenerator.java
changeset 17491 7a33824ec8c5
parent 9508 310b4f6c8e61
child 23010 6dadb192ad81
equal deleted inserted replaced
17467:374c1cceefff 17491:7a33824ec8c5
     1 /*
     1 /*
     2  * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    35 import sun.security.ec.NamedCurve;
    35 import sun.security.ec.NamedCurve;
    36 import sun.security.ec.ECParameters;
    36 import sun.security.ec.ECParameters;
    37 import sun.security.ec.ECPrivateKeyImpl;
    37 import sun.security.ec.ECPrivateKeyImpl;
    38 import sun.security.ec.ECPublicKeyImpl;
    38 import sun.security.ec.ECPublicKeyImpl;
    39 import sun.security.jca.JCAUtil;
    39 import sun.security.jca.JCAUtil;
       
    40 import sun.security.util.ECUtil;
    40 
    41 
    41 /**
    42 /**
    42  * EC keypair generator.
    43  * EC keypair generator.
    43  * Standard algorithm, minimum key length is 112 bits, maximum is 571 bits.
    44  * Standard algorithm, minimum key length is 112 bits, maximum is 571 bits.
    44  *
    45  *
    70     // initialize the generator. See JCA doc
    71     // initialize the generator. See JCA doc
    71     @Override
    72     @Override
    72     public void initialize(int keySize, SecureRandom random) {
    73     public void initialize(int keySize, SecureRandom random) {
    73 
    74 
    74         checkKeySize(keySize);
    75         checkKeySize(keySize);
    75         this.params = NamedCurve.getECParameterSpec(keySize);
    76         this.params = ECUtil.getECParameterSpec(null, keySize);
    76         if (params == null) {
    77         if (params == null) {
    77             throw new InvalidParameterException(
    78             throw new InvalidParameterException(
    78                 "No EC parameters available for key size " + keySize + " bits");
    79                 "No EC parameters available for key size " + keySize + " bits");
    79         }
    80         }
    80         this.random = random;
    81         this.random = random;
    84     @Override
    85     @Override
    85     public void initialize(AlgorithmParameterSpec params, SecureRandom random)
    86     public void initialize(AlgorithmParameterSpec params, SecureRandom random)
    86             throws InvalidAlgorithmParameterException {
    87             throws InvalidAlgorithmParameterException {
    87 
    88 
    88         if (params instanceof ECParameterSpec) {
    89         if (params instanceof ECParameterSpec) {
    89             this.params = ECParameters.getNamedCurve((ECParameterSpec)params);
    90             this.params = ECUtil.getECParameterSpec(null,
       
    91                                                     (ECParameterSpec)params);
    90             if (this.params == null) {
    92             if (this.params == null) {
    91                 throw new InvalidAlgorithmParameterException(
    93                 throw new InvalidAlgorithmParameterException(
    92                     "Unsupported curve: " + params);
    94                     "Unsupported curve: " + params);
    93             }
    95             }
    94         } else if (params instanceof ECGenParameterSpec) {
    96         } else if (params instanceof ECGenParameterSpec) {
    95             String name = ((ECGenParameterSpec)params).getName();
    97             String name = ((ECGenParameterSpec)params).getName();
    96             this.params = NamedCurve.getECParameterSpec(name);
    98             this.params = ECUtil.getECParameterSpec(null, name);
    97             if (this.params == null) {
    99             if (this.params == null) {
    98                 throw new InvalidAlgorithmParameterException(
   100                 throw new InvalidAlgorithmParameterException(
    99                     "Unknown curve name: " + name);
   101                     "Unknown curve name: " + name);
   100             }
   102             }
   101         } else {
   103         } else {
   110     // generate the keypair. See JCA doc
   112     // generate the keypair. See JCA doc
   111     @Override
   113     @Override
   112     public KeyPair generateKeyPair() {
   114     public KeyPair generateKeyPair() {
   113 
   115 
   114         byte[] encodedParams =
   116         byte[] encodedParams =
   115             ECParameters.encodeParameters((ECParameterSpec)params);
   117             ECUtil.encodeECParameterSpec(null, (ECParameterSpec)params);
   116 
   118 
   117         // seed is twice the key size (in bytes) plus 1
   119         // seed is twice the key size (in bytes) plus 1
   118         byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
   120         byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
   119         if (random == null) {
   121         if (random == null) {
   120             random = JCAUtil.getSecureRandom();
   122             random = JCAUtil.getSecureRandom();
   133 
   135 
   134             PrivateKey privateKey =
   136             PrivateKey privateKey =
   135                 new ECPrivateKeyImpl(s, (ECParameterSpec)params);
   137                 new ECPrivateKeyImpl(s, (ECParameterSpec)params);
   136 
   138 
   137             // handles[1] points to the native public key
   139             // handles[1] points to the native public key
   138             ECPoint w = ECParameters.decodePoint(getEncodedBytes(handles[1]),
   140             ECPoint w = ECUtil.decodePoint(getEncodedBytes(handles[1]),
   139                 ((ECParameterSpec)params).getCurve());
   141                 ((ECParameterSpec)params).getCurve());
   140             PublicKey publicKey =
   142             PublicKey publicKey =
   141                 new ECPublicKeyImpl(w, (ECParameterSpec)params);
   143                 new ECPublicKeyImpl(w, (ECParameterSpec)params);
   142 
   144 
   143             return new KeyPair(publicKey, privateKey);
   145             return new KeyPair(publicKey, privateKey);