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

Print this page




 106         }
 107         this.keySize =
 108             ((ECParameterSpec)this.params).getCurve().getField().getFieldSize();
 109         this.random = random;
 110     }
 111 
 112     // generate the keypair. See JCA doc
 113     @Override
 114     public KeyPair generateKeyPair() {
 115 
 116         byte[] encodedParams =
 117             ECUtil.encodeECParameterSpec(null, (ECParameterSpec)params);
 118 
 119         // seed is twice the key size (in bytes) plus 1
 120         byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
 121         if (random == null) {
 122             random = JCAUtil.getSecureRandom();
 123         }
 124         random.nextBytes(seed);
 125 

 126         try {
 127 
 128             long[] handles = generateECKeyPair(keySize, encodedParams, seed);
 129 
 130             // The 'params' object supplied above is equivalent to the native
 131             // one so there is no need to fetch it.
 132 
 133             // handles[0] points to the native private key
 134             BigInteger s = new BigInteger(1, getEncodedBytes(handles[0]));
 135 
 136             PrivateKey privateKey =
 137                 new ECPrivateKeyImpl(s, (ECParameterSpec)params);
 138 
 139             // handles[1] points to the native public key
 140             ECPoint w = ECUtil.decodePoint(getEncodedBytes(handles[1]),
 141                 ((ECParameterSpec)params).getCurve());
 142             PublicKey publicKey =
 143                 new ECPublicKeyImpl(w, (ECParameterSpec)params);
 144 
 145             return new KeyPair(publicKey, privateKey);
 146 
 147         } catch (Exception e) {
 148             throw new ProviderException(e);






 149         }
 150     }
 151 
 152     private void checkKeySize(int keySize) throws InvalidParameterException {
 153         if (keySize < KEY_SIZE_MIN) {
 154             throw new InvalidParameterException
 155                 ("Key size must be at least " + KEY_SIZE_MIN + " bits");
 156         }
 157         if (keySize > KEY_SIZE_MAX) {
 158             throw new InvalidParameterException
 159                 ("Key size must be at most " + KEY_SIZE_MAX + " bits");
 160         }
 161         this.keySize = keySize;
 162     }
 163 
 164     /*
 165      * Generates the keypair and returns a 2-element array of handles.
 166      * The first handle points to the private key, the second to the public key.
 167      */
 168     private static native long[] generateECKeyPair(int keySize,
 169         byte[] encodedParams, byte[] seed) throws GeneralSecurityException;






 170 
 171     /*
 172      * Extracts the encoded key data using the supplied handle.
 173      */
 174     private static native byte[] getEncodedBytes(long handle);
 175 }


 106         }
 107         this.keySize =
 108             ((ECParameterSpec)this.params).getCurve().getField().getFieldSize();
 109         this.random = random;
 110     }
 111 
 112     // generate the keypair. See JCA doc
 113     @Override
 114     public KeyPair generateKeyPair() {
 115 
 116         byte[] encodedParams =
 117             ECUtil.encodeECParameterSpec(null, (ECParameterSpec)params);
 118 
 119         // seed is twice the key size (in bytes) plus 1
 120         byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
 121         if (random == null) {
 122             random = JCAUtil.getSecureRandom();
 123         }
 124         random.nextBytes(seed);
 125 
 126         long[] handles = null;
 127         try {
 128 
 129             handles = generateECKeyPair(keySize, encodedParams, seed);
 130 
 131             // The 'params' object supplied above is equivalent to the native
 132             // one so there is no need to fetch it.
 133 
 134             // handles[0] points to the native private key
 135             BigInteger s = new BigInteger(1, getEncodedBytes(handles[0]));
 136 
 137             PrivateKey privateKey =
 138                 new ECPrivateKeyImpl(s, (ECParameterSpec)params);
 139 
 140             // handles[1] points to the native public key
 141             ECPoint w = ECUtil.decodePoint(getEncodedBytes(handles[1]),
 142                 ((ECParameterSpec)params).getCurve());
 143             PublicKey publicKey =
 144                 new ECPublicKeyImpl(w, (ECParameterSpec)params);
 145 
 146             return new KeyPair(publicKey, privateKey);
 147 
 148         } catch (Exception e) {
 149             throw new ProviderException(e);
 150         } finally {
 151             if (handles != null) {
 152                 // handles[2] points to the data structure containing
 153                 // both the native public and private key.
 154                 releaseECKeyPairData(handles[2]);
 155             }
 156         }
 157     }
 158 
 159     private void checkKeySize(int keySize) throws InvalidParameterException {
 160         if (keySize < KEY_SIZE_MIN) {
 161             throw new InvalidParameterException
 162                 ("Key size must be at least " + KEY_SIZE_MIN + " bits");
 163         }
 164         if (keySize > KEY_SIZE_MAX) {
 165             throw new InvalidParameterException
 166                 ("Key size must be at most " + KEY_SIZE_MAX + " bits");
 167         }
 168         this.keySize = keySize;
 169     }
 170 
 171     /*
 172      * Generates the keypair and returns a 2-element array of handles.
 173      * The first handle points to the private key, the second to the public key.
 174      */
 175     private static native long[] generateECKeyPair(int keySize,
 176         byte[] encodedParams, byte[] seed) throws GeneralSecurityException;
 177 
 178 
 179     /*
 180      * Releases the native memory allocated by generateECKeyPair.
 181      */
 182     private static native void releaseECKeyPairData(long privKeyHandle);
 183 
 184     /*
 185      * Extracts the encoded key data using the supplied handle.
 186      */
 187     private static native byte[] getEncodedBytes(long handle);
 188 }