History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/security/ec/ECKeyPairGenerator.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/security/ec/ECKeyPairGenerator.java+173−0
@@ -0,0 +1,173 @@
1/*
2 * Copyright (c) 2009, 2011, 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
26package sun.security.ec;
27
28import java.math.BigInteger;
29import java.security.*;
30import java.security.spec.AlgorithmParameterSpec;
31import java.security.spec.ECGenParameterSpec;
32import java.security.spec.ECParameterSpec;
33import java.security.spec.ECPoint;
34
35import sun.security.ec.NamedCurve;
36import sun.security.ec.ECParameters;
37import sun.security.ec.ECPrivateKeyImpl;
38import sun.security.ec.ECPublicKeyImpl;
39import sun.security.jca.JCAUtil;
40
41/**
42 * EC keypair generator.
43 * Standard algorithm, minimum key length is 112 bits, maximum is 571 bits.
44 *
45 * @since 1.7
46 */
47public final class ECKeyPairGenerator extends KeyPairGeneratorSpi {
48
49 private static final int KEY_SIZE_MIN = 112; // min bits (see ecc_impl.h)
50 private static final int KEY_SIZE_MAX = 571; // max bits (see ecc_impl.h)
51 private static final int KEY_SIZE_DEFAULT = 256;
52
53 // used to seed the keypair generator
54 private SecureRandom random;
55
56 // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
57 private int keySize;
58
59 // parameters specified via init, if any
60 private AlgorithmParameterSpec params = null;
61
62 /**
63 * Constructs a new ECKeyPairGenerator.
64 */
65 public ECKeyPairGenerator() {
66 // initialize to default in case the app does not call initialize()
67 initialize(KEY_SIZE_DEFAULT, null);
68 }
69
70 // initialize the generator. See JCA doc
71 @Override
72 public void initialize(int keySize, SecureRandom random) {
73
74 checkKeySize(keySize);
75 this.params = NamedCurve.getECParameterSpec(keySize);
76 if (params == null) {
77 throw new InvalidParameterException(
78 "No EC parameters available for key size " + keySize + " bits");
79 }
80 this.random = random;
81 }
82
83 // second initialize method. See JCA doc
84 @Override
85 public void initialize(AlgorithmParameterSpec params, SecureRandom random)
86 throws InvalidAlgorithmParameterException {
87
88 if (params instanceof ECParameterSpec) {
89 this.params = ECParameters.getNamedCurve((ECParameterSpec)params);
90 if (this.params == null) {
91 throw new InvalidAlgorithmParameterException(
92 "Unsupported curve: " + params);
93 }
94 } else if (params instanceof ECGenParameterSpec) {
95 String name = ((ECGenParameterSpec)params).getName();
96 this.params = NamedCurve.getECParameterSpec(name);
97 if (this.params == null) {
98 throw new InvalidAlgorithmParameterException(
99 "Unknown curve name: " + name);
100 }
101 } else {
102 throw new InvalidAlgorithmParameterException(
103 "ECParameterSpec or ECGenParameterSpec required for EC");
104 }
105 this.keySize =
106 ((ECParameterSpec)this.params).getCurve().getField().getFieldSize();
107 this.random = random;
108 }
109
110 // generate the keypair. See JCA doc
111 @Override
112 public KeyPair generateKeyPair() {
113
114 byte[] encodedParams =
115 ECParameters.encodeParameters((ECParameterSpec)params);
116
117 // seed is twice the key size (in bytes) plus 1
118 byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
119 if (random == null) {
120 random = JCAUtil.getSecureRandom();
121 }
122 random.nextBytes(seed);
123
124 try {
125
126 long[] handles = generateECKeyPair(keySize, encodedParams, seed);
127
128 // The 'params' object supplied above is equivalent to the native
129 // one so there is no need to fetch it.
130
131 // handles[0] points to the native private key
132 BigInteger s = new BigInteger(1, getEncodedBytes(handles[0]));
133
134 PrivateKey privateKey =
135 new ECPrivateKeyImpl(s, (ECParameterSpec)params);
136
137 // handles[1] points to the native public key
138 ECPoint w = ECParameters.decodePoint(getEncodedBytes(handles[1]),
139 ((ECParameterSpec)params).getCurve());
140 PublicKey publicKey =
141 new ECPublicKeyImpl(w, (ECParameterSpec)params);
142
143 return new KeyPair(publicKey, privateKey);
144
145 } catch (Exception e) {
146 throw new ProviderException(e);
147 }
148 }
149
150 private void checkKeySize(int keySize) throws InvalidParameterException {
151 if (keySize < KEY_SIZE_MIN) {
152 throw new InvalidParameterException
153 ("Key size must be at least " + KEY_SIZE_MIN + " bits");
154 }
155 if (keySize > KEY_SIZE_MAX) {
156 throw new InvalidParameterException
157 ("Key size must be at most " + KEY_SIZE_MAX + " bits");
158 }
159 this.keySize = keySize;
160 }
161
162 /*
163 * Generates the keypair and returns a 2-element array of handles.
164 * The first handle points to the private key, the second to the public key.
165 */
166 private static native long[] generateECKeyPair(int keySize,
167 byte[] encodedParams, byte[] seed) throws GeneralSecurityException;
168
169 /*
170 * Extracts the encoded key data using the supplied handle.
171 */
172 private static native byte[] getEncodedBytes(long handle);
173}