1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  */
23 package com.sun.org.apache.xml.internal.security.algorithms.implementations;
24 
25 import java.io.IOException;
26 import java.math.BigInteger;
27 import java.security.interfaces.ECPublicKey;
28 import java.security.spec.*;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32 
33 public final class ECDSAUtils {
34 
ECDSAUtils()35     private ECDSAUtils() {
36         // complete
37     }
38 
39     /**
40      * Converts an ASN.1 ECDSA value to a XML Signature ECDSA Value.
41      * <p></p>
42      * The JAVA JCE ECDSA Signature algorithm creates ASN.1 encoded (r, s) value
43      * pairs; the XML Signature requires the core BigInteger values.
44      *
45      * @param asn1Bytes
46      * @return the decode bytes
47      * @throws IOException
48      * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
49      * @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
50      */
convertASN1toXMLDSIG(byte asn1Bytes[])51     public static byte[] convertASN1toXMLDSIG(byte asn1Bytes[]) throws IOException {
52 
53         if (asn1Bytes.length < 8 || asn1Bytes[0] != 48) {
54             throw new IOException("Invalid ASN.1 format of ECDSA signature");
55         }
56         int offset;
57         if (asn1Bytes[1] > 0) {
58             offset = 2;
59         } else if (asn1Bytes[1] == (byte) 0x81) {
60             offset = 3;
61         } else {
62             throw new IOException("Invalid ASN.1 format of ECDSA signature");
63         }
64 
65         byte rLength = asn1Bytes[offset + 1];
66         int i;
67 
68         for (i = rLength; i > 0 && asn1Bytes[offset + 2 + rLength - i] == 0; i--); //NOPMD
69 
70         byte sLength = asn1Bytes[offset + 2 + rLength + 1];
71         int j;
72 
73         for (j = sLength; j > 0 && asn1Bytes[offset + 2 + rLength + 2 + sLength - j] == 0; j--); //NOPMD
74 
75         int rawLen = Math.max(i, j);
76 
77         if ((asn1Bytes[offset - 1] & 0xff) != asn1Bytes.length - offset
78                 || (asn1Bytes[offset - 1] & 0xff) != 2 + rLength + 2 + sLength
79                 || asn1Bytes[offset] != 2
80                 || asn1Bytes[offset + 2 + rLength] != 2) {
81             throw new IOException("Invalid ASN.1 format of ECDSA signature");
82         }
83         byte xmldsigBytes[] = new byte[2 * rawLen];
84 
85         System.arraycopy(asn1Bytes, offset + 2 + rLength - i, xmldsigBytes, rawLen - i, i);
86         System.arraycopy(asn1Bytes, offset + 2 + rLength + 2 + sLength - j, xmldsigBytes,
87                 2 * rawLen - j, j);
88 
89         return xmldsigBytes;
90     }
91 
92     /**
93      * Converts a XML Signature ECDSA Value to an ASN.1 DSA value.
94      * <p></p>
95      * The JAVA JCE ECDSA Signature algorithm creates ASN.1 encoded (r, s) value
96      * pairs; the XML Signature requires the core BigInteger values.
97      *
98      * @param xmldsigBytes
99      * @return the encoded ASN.1 bytes
100      * @throws IOException
101      * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
102      * @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
103      */
convertXMLDSIGtoASN1(byte xmldsigBytes[])104     public static byte[] convertXMLDSIGtoASN1(byte xmldsigBytes[]) throws IOException {
105 
106         int rawLen = xmldsigBytes.length / 2;
107 
108         int i;
109 
110         for (i = rawLen; i > 0 && xmldsigBytes[rawLen - i] == 0; i--); //NOPMD
111 
112         int j = i;
113 
114         if (xmldsigBytes[rawLen - i] < 0) {
115             j += 1;
116         }
117 
118         int k;
119 
120         for (k = rawLen; k > 0 && xmldsigBytes[2 * rawLen - k] == 0; k--); //NOPMD
121 
122         int l = k;
123 
124         if (xmldsigBytes[2 * rawLen - k] < 0) {
125             l += 1;
126         }
127 
128         int len = 2 + j + 2 + l;
129         if (len > 255) {
130             throw new IOException("Invalid XMLDSIG format of ECDSA signature");
131         }
132         int offset;
133         byte asn1Bytes[];
134         if (len < 128) {
135             asn1Bytes = new byte[2 + 2 + j + 2 + l];
136             offset = 1;
137         } else {
138             asn1Bytes = new byte[3 + 2 + j + 2 + l];
139             asn1Bytes[1] = (byte) 0x81;
140             offset = 2;
141         }
142         asn1Bytes[0] = 48;
143         asn1Bytes[offset++] = (byte) len;
144         asn1Bytes[offset++] = 2;
145         asn1Bytes[offset++] = (byte) j;
146 
147         System.arraycopy(xmldsigBytes, rawLen - i, asn1Bytes, offset + j - i, i);
148 
149         offset += j;
150 
151         asn1Bytes[offset++] = 2;
152         asn1Bytes[offset++] = (byte) l;
153 
154         System.arraycopy(xmldsigBytes, 2 * rawLen - k, asn1Bytes, offset + l - k, k);
155 
156         return asn1Bytes;
157     }
158 
159     private static final List<ECCurveDefinition> ecCurveDefinitions = new ArrayList<>();
160 
161     static {
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )162         ecCurveDefinitions.add(
163                 new ECCurveDefinition(
164                         "secp112r1",
165                         "1.3.132.0.6",
166                         "db7c2abf62e35e668076bead208b",
167                         "db7c2abf62e35e668076bead2088",
168                         "659ef8ba043916eede8911702b22",
169                         "09487239995a5ee76b55f9c2f098",
170                         "a89ce5af8724c0a23e0e0ff77500",
171                         "db7c2abf62e35e7628dfac6561c5",
172                         1)
173         );
174 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )175         ecCurveDefinitions.add(
176                 new ECCurveDefinition(
177                         "secp112r2",
178                         "1.3.132.0.7",
179                         "db7c2abf62e35e668076bead208b",
180                         "6127c24c05f38a0aaaf65c0ef02c",
181                         "51def1815db5ed74fcc34c85d709",
182                         "4ba30ab5e892b4e1649dd0928643",
183                         "adcd46f5882e3747def36e956e97",
184                         "36df0aafd8b8d7597ca10520d04b",
185                         4)
186         );
187 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )188         ecCurveDefinitions.add(
189                 new ECCurveDefinition(
190                         "secp128r1",
191                         "1.3.132.0.28",
192                         "fffffffdffffffffffffffffffffffff",
193                         "fffffffdfffffffffffffffffffffffc",
194                         "e87579c11079f43dd824993c2cee5ed3",
195                         "161ff7528b899b2d0c28607ca52c5b86",
196                         "cf5ac8395bafeb13c02da292dded7a83",
197                         "fffffffe0000000075a30d1b9038a115",
198                         1)
199         );
200 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )201         ecCurveDefinitions.add(
202                 new ECCurveDefinition(
203                         "secp128r2",
204                         "1.3.132.0.29",
205                         "fffffffdffffffffffffffffffffffff",
206                         "d6031998d1b3bbfebf59cc9bbff9aee1",
207                         "5eeefca380d02919dc2c6558bb6d8a5d",
208                         "7b6aa5d85e572983e6fb32a7cdebc140",
209                         "27b6916a894d3aee7106fe805fc34b44",
210                         "3fffffff7fffffffbe0024720613b5a3",
211                         4)
212         );
213 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )214         ecCurveDefinitions.add(
215                 new ECCurveDefinition(
216                         "secp160k1",
217                         "1.3.132.0.9",
218                         "fffffffffffffffffffffffffffffffeffffac73",
219                         "0000000000000000000000000000000000000000",
220                         "0000000000000000000000000000000000000007",
221                         "3b4c382ce37aa192a4019e763036f4f5dd4d7ebb",
222                         "938cf935318fdced6bc28286531733c3f03c4fee",
223                         "0100000000000000000001b8fa16dfab9aca16b6b3",
224                         1)
225         );
226 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )227         ecCurveDefinitions.add(
228                 new ECCurveDefinition(
229                         "secp160r1",
230                         "1.3.132.0.8",
231                         "ffffffffffffffffffffffffffffffff7fffffff",
232                         "ffffffffffffffffffffffffffffffff7ffffffc",
233                         "1c97befc54bd7a8b65acf89f81d4d4adc565fa45",
234                         "4a96b5688ef573284664698968c38bb913cbfc82",
235                         "23a628553168947d59dcc912042351377ac5fb32",
236                         "0100000000000000000001f4c8f927aed3ca752257",
237                         1)
238         );
239 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )240         ecCurveDefinitions.add(
241                 new ECCurveDefinition(
242                         "secp160r2",
243                         "1.3.132.0.30",
244                         "fffffffffffffffffffffffffffffffeffffac73",
245                         "fffffffffffffffffffffffffffffffeffffac70",
246                         "b4e134d3fb59eb8bab57274904664d5af50388ba",
247                         "52dcb034293a117e1f4ff11b30f7199d3144ce6d",
248                         "feaffef2e331f296e071fa0df9982cfea7d43f2e",
249                         "0100000000000000000000351ee786a818f3a1a16b",
250                         1)
251         );
252 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )253         ecCurveDefinitions.add(
254                 new ECCurveDefinition(
255                         "secp192k1",
256                         "1.3.132.0.31",
257                         "fffffffffffffffffffffffffffffffffffffffeffffee37",
258                         "000000000000000000000000000000000000000000000000",
259                         "000000000000000000000000000000000000000000000003",
260                         "db4ff10ec057e9ae26b07d0280b7f4341da5d1b1eae06c7d",
261                         "9b2f2f6d9c5628a7844163d015be86344082aa88d95e2f9d",
262                         "fffffffffffffffffffffffe26f2fc170f69466a74defd8d",
263                         1)
264         );
265 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )266         ecCurveDefinitions.add(
267                 new ECCurveDefinition(
268                         "secp192r1 [NIST P-192, X9.62 prime192v1]",
269                         "1.2.840.10045.3.1.1",
270                         "fffffffffffffffffffffffffffffffeffffffffffffffff",
271                         "fffffffffffffffffffffffffffffffefffffffffffffffc",
272                         "64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1",
273                         "188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012",
274                         "07192b95ffc8da78631011ed6b24cdd573f977a11e794811",
275                         "ffffffffffffffffffffffff99def836146bc9b1b4d22831",
276                         1)
277         );
278 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )279         ecCurveDefinitions.add(
280                 new ECCurveDefinition(
281                         "secp224k1",
282                         "1.3.132.0.32",
283                         "fffffffffffffffffffffffffffffffffffffffffffffffeffffe56d",
284                         "00000000000000000000000000000000000000000000000000000000",
285                         "00000000000000000000000000000000000000000000000000000005",
286                         "a1455b334df099df30fc28a169a467e9e47075a90f7e650eb6b7a45c",
287                         "7e089fed7fba344282cafbd6f7e319f7c0b0bd59e2ca4bdb556d61a5",
288                         "010000000000000000000000000001dce8d2ec6184caf0a971769fb1f7",
289                         1)
290         );
291 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )292         ecCurveDefinitions.add(
293                 new ECCurveDefinition(
294                         "secp224r1 [NIST P-224]",
295                         "1.3.132.0.33",
296                         "ffffffffffffffffffffffffffffffff000000000000000000000001",
297                         "fffffffffffffffffffffffffffffffefffffffffffffffffffffffe",
298                         "b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4",
299                         "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21",
300                         "bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34",
301                         "ffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d",
302                         1)
303         );
304 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )305         ecCurveDefinitions.add(
306                 new ECCurveDefinition(
307                         "secp256k1",
308                         "1.3.132.0.10",
309                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
310                         "0000000000000000000000000000000000000000000000000000000000000000",
311                         "0000000000000000000000000000000000000000000000000000000000000007",
312                         "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
313                         "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8",
314                         "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
315                         1)
316         );
317 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )318         ecCurveDefinitions.add(
319                 new ECCurveDefinition(
320                         "secp256r1 [NIST P-256, X9.62 prime256v1]",
321                         "1.2.840.10045.3.1.7",
322                         "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
323                         "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
324                         "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
325                         "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
326                         "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
327                         "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
328                         1)
329         );
330 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )331         ecCurveDefinitions.add(
332                 new ECCurveDefinition(
333                         "secp384r1 [NIST P-384]",
334                         "1.3.132.0.34",
335                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff",
336                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc",
337                         "b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef",
338                         "aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7",
339                         "3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f",
340                         "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973",
341                         1)
342         );
343 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )344         ecCurveDefinitions.add(
345                 new ECCurveDefinition(
346                         "secp521r1 [NIST P-521]",
347                         "1.3.132.0.35",
348                         "01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
349                         "01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
350                         "0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
351                         "00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
352                         "011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
353                         "01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
354                         1)
355         );
356 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )357         ecCurveDefinitions.add(
358                 new ECCurveDefinition(
359                         "X9.62 prime192v2",
360                         "1.2.840.10045.3.1.2",
361                         "fffffffffffffffffffffffffffffffeffffffffffffffff",
362                         "fffffffffffffffffffffffffffffffefffffffffffffffc",
363                         "cc22d6dfb95c6b25e49c0d6364a4e5980c393aa21668d953",
364                         "eea2bae7e1497842f2de7769cfe9c989c072ad696f48034a",
365                         "6574d11d69b6ec7a672bb82a083df2f2b0847de970b2de15",
366                         "fffffffffffffffffffffffe5fb1a724dc80418648d8dd31",
367                         1)
368         );
369 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )370         ecCurveDefinitions.add(
371                 new ECCurveDefinition(
372                         "X9.62 prime192v3",
373                         "1.2.840.10045.3.1.3",
374                         "fffffffffffffffffffffffffffffffeffffffffffffffff",
375                         "fffffffffffffffffffffffffffffffefffffffffffffffc",
376                         "22123dc2395a05caa7423daeccc94760a7d462256bd56916",
377                         "7d29778100c65a1da1783716588dce2b8b4aee8e228f1896",
378                         "38a90f22637337334b49dcb66a6dc8f9978aca7648a943b0",
379                         "ffffffffffffffffffffffff7a62d031c83f4294f640ec13",
380                         1)
381         );
382 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )383         ecCurveDefinitions.add(
384                 new ECCurveDefinition(
385                         "X9.62 prime239v1",
386                         "1.2.840.10045.3.1.4",
387                         "7fffffffffffffffffffffff7fffffffffff8000000000007fffffffffff",
388                         "7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc",
389                         "6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a",
390                         "0ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf",
391                         "7debe8e4e90a5dae6e4054ca530ba04654b36818ce226b39fccb7b02f1ae",
392                         "7fffffffffffffffffffffff7fffff9e5e9a9f5d9071fbd1522688909d0b",
393                         1)
394         );
395 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )396         ecCurveDefinitions.add(
397                 new ECCurveDefinition(
398                         "X9.62 prime239v2",
399                         "1.2.840.10045.3.1.5",
400                         "7fffffffffffffffffffffff7fffffffffff8000000000007fffffffffff",
401                         "7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc",
402                         "617fab6832576cbbfed50d99f0249c3fee58b94ba0038c7ae84c8c832f2c",
403                         "38af09d98727705120c921bb5e9e26296a3cdcf2f35757a0eafd87b830e7",
404                         "5b0125e4dbea0ec7206da0fc01d9b081329fb555de6ef460237dff8be4ba",
405                         "7fffffffffffffffffffffff800000cfa7e8594377d414c03821bc582063",
406                         1)
407         );
408 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 1) )409         ecCurveDefinitions.add(
410                 new ECCurveDefinition(
411                         "X9.62 prime239v3",
412                         "1.2.840.10045.3.1.6",
413                         "7fffffffffffffffffffffff7fffffffffff8000000000007fffffffffff",
414                         "7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc",
415                         "255705fa2a306654b1f4cb03d6a750a30c250102d4988717d9ba15ab6d3e",
416                         "6768ae8e18bb92cfcf005c949aa2c6d94853d0e660bbf854b1c9505fe95a",
417                         "1607e6898f390c06bc1d552bad226f3b6fcfe48b6e818499af18e3ed6cf3",
418                         "7fffffffffffffffffffffff7fffff975deb41b3a6057c3c432146526551",
419                         1)
420         );
421 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )422         ecCurveDefinitions.add(
423                 new ECCurveDefinition(
424                         "sect113r1",
425                         "1.3.132.0.4",
426                         "020000000000000000000000000201",
427                         "003088250ca6e7c7fe649ce85820f7",
428                         "00e8bee4d3e2260744188be0e9c723",
429                         "009d73616f35f4ab1407d73562c10f",
430                         "00a52830277958ee84d1315ed31886",
431                         "0100000000000000d9ccec8a39e56f",
432                         2)
433         );
434 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )435         ecCurveDefinitions.add(
436                 new ECCurveDefinition(
437                         "sect113r2",
438                         "1.3.132.0.5",
439                         "020000000000000000000000000201",
440                         "00689918dbec7e5a0dd6dfc0aa55c7",
441                         "0095e9a9ec9b297bd4bf36e059184f",
442                         "01a57a6a7b26ca5ef52fcdb8164797",
443                         "00b3adc94ed1fe674c06e695baba1d",
444                         "010000000000000108789b2496af93",
445                         2)
446         );
447 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )448         ecCurveDefinitions.add(
449                 new ECCurveDefinition(
450                         "sect131r1",
451                         "1.3.132.0.22",
452                         "080000000000000000000000000000010d",
453                         "07a11b09a76b562144418ff3ff8c2570b8",
454                         "0217c05610884b63b9c6c7291678f9d341",
455                         "0081baf91fdf9833c40f9c181343638399",
456                         "078c6e7ea38c001f73c8134b1b4ef9e150",
457                         "0400000000000000023123953a9464b54d",
458                         2)
459         );
460 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )461         ecCurveDefinitions.add(
462                 new ECCurveDefinition(
463                         "sect131r2",
464                         "1.3.132.0.23",
465                         "080000000000000000000000000000010d",
466                         "03e5a88919d7cafcbf415f07c2176573b2",
467                         "04b8266a46c55657ac734ce38f018f2192",
468                         "0356dcd8f2f95031ad652d23951bb366a8",
469                         "0648f06d867940a5366d9e265de9eb240f",
470                         "0400000000000000016954a233049ba98f",
471                         2)
472         );
473 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )474         ecCurveDefinitions.add(
475                 new ECCurveDefinition(
476                         "sect163k1 [NIST K-163]",
477                         "1.3.132.0.1",
478                         "0800000000000000000000000000000000000000c9",
479                         "000000000000000000000000000000000000000001",
480                         "000000000000000000000000000000000000000001",
481                         "02fe13c0537bbc11acaa07d793de4e6d5e5c94eee8",
482                         "0289070fb05d38ff58321f2e800536d538ccdaa3d9",
483                         "04000000000000000000020108a2e0cc0d99f8a5ef",
484                         2)
485         );
486 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )487         ecCurveDefinitions.add(
488                 new ECCurveDefinition(
489                         "sect163r1",
490                         "1.3.132.0.2",
491                         "0800000000000000000000000000000000000000c9",
492                         "07b6882caaefa84f9554ff8428bd88e246d2782ae2",
493                         "0713612dcddcb40aab946bda29ca91f73af958afd9",
494                         "0369979697ab43897789566789567f787a7876a654",
495                         "00435edb42efafb2989d51fefce3c80988f41ff883",
496                         "03ffffffffffffffffffff48aab689c29ca710279b",
497                         2)
498         );
499 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )500         ecCurveDefinitions.add(
501                 new ECCurveDefinition(
502                         "sect163r2 [NIST B-163]",
503                         "1.3.132.0.15",
504                         "0800000000000000000000000000000000000000c9",
505                         "000000000000000000000000000000000000000001",
506                         "020a601907b8c953ca1481eb10512f78744a3205fd",
507                         "03f0eba16286a2d57ea0991168d4994637e8343e36",
508                         "00d51fbc6c71a0094fa2cdd545b11c5c0c797324f1",
509                         "040000000000000000000292fe77e70c12a4234c33",
510                         2)
511         );
512 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )513         ecCurveDefinitions.add(
514                 new ECCurveDefinition(
515                         "sect193r1",
516                         "1.3.132.0.24",
517                         "02000000000000000000000000000000000000000000008001",
518                         "0017858feb7a98975169e171f77b4087de098ac8a911df7b01",
519                         "00fdfb49bfe6c3a89facadaa7a1e5bbc7cc1c2e5d831478814",
520                         "01f481bc5f0ff84a74ad6cdf6fdef4bf6179625372d8c0c5e1",
521                         "0025e399f2903712ccf3ea9e3a1ad17fb0b3201b6af7ce1b05",
522                         "01000000000000000000000000c7f34a778f443acc920eba49",
523                         2)
524         );
525 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )526         ecCurveDefinitions.add(
527                 new ECCurveDefinition(
528                         "sect193r2",
529                         "1.3.132.0.25",
530                         "02000000000000000000000000000000000000000000008001",
531                         "0163f35a5137c2ce3ea6ed8667190b0bc43ecd69977702709b",
532                         "00c9bb9e8927d4d64c377e2ab2856a5b16e3efb7f61d4316ae",
533                         "00d9b67d192e0367c803f39e1a7e82ca14a651350aae617e8f",
534                         "01ce94335607c304ac29e7defbd9ca01f596f927224cdecf6c",
535                         "010000000000000000000000015aab561b005413ccd4ee99d5",
536                         2)
537         );
538 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )539         ecCurveDefinitions.add(
540                 new ECCurveDefinition(
541                         "sect233k1 [NIST K-233]",
542                         "1.3.132.0.26",
543                         "020000000000000000000000000000000000000004000000000000000001",
544                         "000000000000000000000000000000000000000000000000000000000000",
545                         "000000000000000000000000000000000000000000000000000000000001",
546                         "017232ba853a7e731af129f22ff4149563a419c26bf50a4c9d6eefad6126",
547                         "01db537dece819b7f70f555a67c427a8cd9bf18aeb9b56e0c11056fae6a3",
548                         "008000000000000000000000000000069d5bb915bcd46efb1ad5f173abdf",
549                         4)
550         );
551 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )552         ecCurveDefinitions.add(
553                 new ECCurveDefinition(
554                         "sect233r1 [NIST B-233]",
555                         "1.3.132.0.27",
556                         "020000000000000000000000000000000000000004000000000000000001",
557                         "000000000000000000000000000000000000000000000000000000000001",
558                         "0066647ede6c332c7f8c0923bb58213b333b20e9ce4281fe115f7d8f90ad",
559                         "00fac9dfcbac8313bb2139f1bb755fef65bc391f8b36f8f8eb7371fd558b",
560                         "01006a08a41903350678e58528bebf8a0beff867a7ca36716f7e01f81052",
561                         "01000000000000000000000000000013e974e72f8a6922031d2603cfe0d7",
562                         2)
563         );
564 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )565         ecCurveDefinitions.add(
566                 new ECCurveDefinition(
567                         "sect239k1",
568                         "1.3.132.0.3",
569                         "800000000000000000004000000000000000000000000000000000000001",
570                         "000000000000000000000000000000000000000000000000000000000000",
571                         "000000000000000000000000000000000000000000000000000000000001",
572                         "29a0b6a887a983e9730988a68727a8b2d126c44cc2cc7b2a6555193035dc",
573                         "76310804f12e549bdb011c103089e73510acb275fc312a5dc6b76553f0ca",
574                         "2000000000000000000000000000005a79fec67cb6e91f1c1da800e478a5",
575                         4)
576         );
577 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )578         ecCurveDefinitions.add(
579                 new ECCurveDefinition(
580                         "sect283k1 [NIST K-283]",
581                         "1.3.132.0.16",
582                         "0800000000000000000000000000000000000000000000000000000000000000000010a1",
583                         "000000000000000000000000000000000000000000000000000000000000000000000000",
584                         "000000000000000000000000000000000000000000000000000000000000000000000001",
585                         "0503213f78ca44883f1a3b8162f188e553cd265f23c1567a16876913b0c2ac2458492836",
586                         "01ccda380f1c9e318d90f95d07e5426fe87e45c0e8184698e45962364e34116177dd2259",
587                         "01ffffffffffffffffffffffffffffffffffe9ae2ed07577265dff7f94451e061e163c61",
588                         4)
589         );
590 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )591         ecCurveDefinitions.add(
592                 new ECCurveDefinition(
593                         "sect283r1 [NIST B-283]",
594                         "1.3.132.0.17",
595                         "0800000000000000000000000000000000000000000000000000000000000000000010a1",
596                         "000000000000000000000000000000000000000000000000000000000000000000000001",
597                         "027b680ac8b8596da5a4af8a19a0303fca97fd7645309fa2a581485af6263e313b79a2f5",
598                         "05f939258db7dd90e1934f8c70b0dfec2eed25b8557eac9c80e2e198f8cdbecd86b12053",
599                         "03676854fe24141cb98fe6d4b20d02b4516ff702350eddb0826779c813f0df45be8112f4",
600                         "03ffffffffffffffffffffffffffffffffffef90399660fc938a90165b042a7cefadb307",
601                         2)
602         );
603 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )604         ecCurveDefinitions.add(
605                 new ECCurveDefinition(
606                         "sect409k1 [NIST K-409]",
607                         "1.3.132.0.36",
608                         "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
609                         "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
610                         "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
611                         "0060f05f658f49c1ad3ab1890f7184210efd0987e307c84c27accfb8f9f67cc2c460189eb5aaaa62ee222eb1b35540cfe9023746",
612                         "01e369050b7c4e42acba1dacbf04299c3460782f918ea427e6325165e9ea10e3da5f6c42e9c55215aa9ca27a5863ec48d8e0286b",
613                         "007ffffffffffffffffffffffffffffffffffffffffffffffffffe5f83b2d4ea20400ec4557d5ed3e3e7ca5b4b5c83b8e01e5fcf",
614                         4)
615         );
616 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )617         ecCurveDefinitions.add(
618                 new ECCurveDefinition(
619                         "sect409r1 [NIST B-409]",
620                         "1.3.132.0.37",
621                         "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
622                         "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
623                         "0021a5c2c8ee9feb5c4b9a753b7b476b7fd6422ef1f3dd674761fa99d6ac27c8a9a197b272822f6cd57a55aa4f50ae317b13545f",
624                         "015d4860d088ddb3496b0c6064756260441cde4af1771d4db01ffe5b34e59703dc255a868a1180515603aeab60794e54bb7996a7",
625                         "0061b1cfab6be5f32bbfa78324ed106a7636b9c5a7bd198d0158aa4f5488d08f38514f1fdf4b4f40d2181b3681c364ba0273c706",
626                         "010000000000000000000000000000000000000000000000000001e2aad6a612f33307be5fa47c3c9e052f838164cd37d9a21173",
627                         2)
628         );
629 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )630         ecCurveDefinitions.add(
631                 new ECCurveDefinition(
632                         "sect571k1 [NIST K-571]",
633                         "1.3.132.0.38",
634                         "080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
635                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
636                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
637                         "026eb7a859923fbc82189631f8103fe4ac9ca2970012d5d46024804801841ca44370958493b205e647da304db4ceb08cbbd1ba39494776fb988b47174dca88c7e2945283a01c8972",
638                         "0349dc807f4fbf374f4aeade3bca95314dd58cec9f307a54ffc61efc006d8a2c9d4979c0ac44aea74fbebbb9f772aedcb620b01a7ba7af1b320430c8591984f601cd4c143ef1c7a3",
639                         "020000000000000000000000000000000000000000000000000000000000000000000000131850e1f19a63e4b391a8db917f4138b630d84be5d639381e91deb45cfe778f637c1001",
640                         4)
641         );
642 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )643         ecCurveDefinitions.add(
644                 new ECCurveDefinition(
645                         "sect571r1 [NIST B-571]",
646                         "1.3.132.0.39",
647                         "080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
648                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
649                         "02f40e7e2221f295de297117b7f3d62f5c6a97ffcb8ceff1cd6ba8ce4a9a18ad84ffabbd8efa59332be7ad6756a66e294afd185a78ff12aa520e4de739baca0c7ffeff7f2955727a",
650                         "0303001d34b856296c16c0d40d3cd7750a93d1d2955fa80aa5f40fc8db7b2abdbde53950f4c0d293cdd711a35b67fb1499ae60038614f1394abfa3b4c850d927e1e7769c8eec2d19",
651                         "037bf27342da639b6dccfffeb73d69d78c6c27a6009cbbca1980f8533921e8a684423e43bab08a576291af8f461bb2a8b3531d2f0485c19b16e2f1516e23dd3c1a4827af1b8ac15b",
652                         "03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe661ce18ff55987308059b186823851ec7dd9ca1161de93d5174d66e8382e9bb2fe84e47",
653                         2)
654         );
655 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 2) )656         ecCurveDefinitions.add(
657                 new ECCurveDefinition(
658                         "X9.62 c2tnb191v1",
659                         "1.2.840.10045.3.0.5",
660                         "800000000000000000000000000000000000000000000201",
661                         "2866537b676752636a68f56554e12640276b649ef7526267",
662                         "2e45ef571f00786f67b0081b9495a3d95462f5de0aa185ec",
663                         "36b3daf8a23206f9c4f299d7b21a9c369137f2c84ae1aa0d",
664                         "765be73433b3f95e332932e70ea245ca2418ea0ef98018fb",
665                         "40000000000000000000000004a20e90c39067c893bbb9a5",
666                         2)
667         );
668 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )669         ecCurveDefinitions.add(
670                 new ECCurveDefinition(
671                         "X9.62 c2tnb191v2",
672                         "1.2.840.10045.3.0.6",
673                         "800000000000000000000000000000000000000000000201",
674                         "401028774d7777c7b7666d1366ea432071274f89ff01e718",
675                         "0620048d28bcbd03b6249c99182b7c8cd19700c362c46a01",
676                         "3809b2b7cc1b28cc5a87926aad83fd28789e81e2c9e3bf10",
677                         "17434386626d14f3dbf01760d9213a3e1cf37aec437d668a",
678                         "20000000000000000000000050508cb89f652824e06b8173",
679                         4)
680         );
681 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 6) )682         ecCurveDefinitions.add(
683                 new ECCurveDefinition(
684                         "X9.62 c2tnb191v3",
685                         "1.2.840.10045.3.0.7",
686                         "800000000000000000000000000000000000000000000201",
687                         "6c01074756099122221056911c77d77e77a777e7e7e77fcb",
688                         "71fe1af926cf847989efef8db459f66394d90f32ad3f15e8",
689                         "375d4ce24fde434489de8746e71786015009e66e38a926dd",
690                         "545a39176196575d985999366e6ad34ce0a77cd7127b06be",
691                         "155555555555555555555555610c0b196812bfb6288a3ea3",
692                         6)
693         );
694 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 4) )695         ecCurveDefinitions.add(
696                 new ECCurveDefinition(
697                         "X9.62 c2tnb239v1",
698                         "1.2.840.10045.3.0.11",
699                         "800000000000000000000000000000000000000000000000001000000001",
700                         "32010857077c5431123a46b808906756f543423e8d27877578125778ac76",
701                         "790408f2eedaf392b012edefb3392f30f4327c0ca3f31fc383c422aa8c16",
702                         "57927098fa932e7c0a96d3fd5b706ef7e5f5c156e16b7e7c86038552e91d",
703                         "61d8ee5077c33fecf6f1a16b268de469c3c7744ea9a971649fc7a9616305",
704                         "2000000000000000000000000000000f4d42ffe1492a4993f1cad666e447",
705                         4)
706         );
707 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 6) )708         ecCurveDefinitions.add(
709                 new ECCurveDefinition(
710                         "X9.62 c2tnb239v2",
711                         "1.2.840.10045.3.0.12",
712                         "800000000000000000000000000000000000000000000000001000000001",
713                         "4230017757a767fae42398569b746325d45313af0766266479b75654e65f",
714                         "5037ea654196cff0cd82b2c14a2fcf2e3ff8775285b545722f03eacdb74b",
715                         "28f9d04e900069c8dc47a08534fe76d2b900b7d7ef31f5709f200c4ca205",
716                         "5667334c45aff3b5a03bad9dd75e2c71a99362567d5453f7fa6e227ec833",
717                         "1555555555555555555555555555553c6f2885259c31e3fcdf154624522d",
718                         6)
719         );
720 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 0xA) )721         ecCurveDefinitions.add(
722                 new ECCurveDefinition(
723                         "X9.62 c2tnb239v3",
724                         "1.2.840.10045.3.0.13",
725                         "800000000000000000000000000000000000000000000000001000000001",
726                         "01238774666a67766d6676f778e676b66999176666e687666d8766c66a9f",
727                         "6a941977ba9f6a435199acfc51067ed587f519c5ecb541b8e44111de1d40",
728                         "70f6e9d04d289c4e89913ce3530bfde903977d42b146d539bf1bde4e9c92",
729                         "2e5a0eaf6e5e1305b9004dce5c0ed7fe59a35608f33837c816d80b79f461",
730                         "0cccccccccccccccccccccccccccccac4912d2d9df903ef9888b8a0e4cff",
731                         0xA)
732         );
733 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 0x4C) )734         ecCurveDefinitions.add(
735                 new ECCurveDefinition(
736                         "X9.62 c2tnb359v1",
737                         "1.2.840.10045.3.0.18",
738                         "800000000000000000000000000000000000000000000000000000000000000000000000100000000000000001",
739                         "5667676a654b20754f356ea92017d946567c46675556f19556a04616b567d223a5e05656fb549016a96656a557",
740                         "2472e2d0197c49363f1fe7f5b6db075d52b6947d135d8ca445805d39bc345626089687742b6329e70680231988",
741                         "3c258ef3047767e7ede0f1fdaa79daee3841366a132e163aced4ed2401df9c6bdcde98e8e707c07a2239b1b097",
742                         "53d7e08529547048121e9c95f3791dd804963948f34fae7bf44ea82365dc7868fe57e4ae2de211305a407104bd",
743                         "01af286bca1af286bca1af286bca1af286bca1af286bc9fb8f6b85c556892c20a7eb964fe7719e74f490758d3b",
744                         0x4C)
745         );
746 
ecCurveDefinitions.add( new ECCurveDefinition( R, R, R, R, R, R, R, R, 0x2760) )747         ecCurveDefinitions.add(
748                 new ECCurveDefinition(
749                         "X9.62 c2tnb431r1",
750                         "1.2.840.10045.3.0.20",
751                         "800000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000001",
752                         "1a827ef00dd6fc0e234caf046c6a5d8a85395b236cc4ad2cf32a0cadbdc9ddf620b0eb9906d0957f6c6feacd615468df104de296cd8f",
753                         "10d9b4a3d9047d8b154359abfb1b7f5485b04ceb868237ddc9deda982a679a5a919b626d4e50a8dd731b107a9962381fb5d807bf2618",
754                         "120fc05d3c67a99de161d2f4092622feca701be4f50f4758714e8a87bbf2a658ef8c21e7c5efe965361f6c2999c0c247b0dbd70ce6b7",
755                         "20d0af8903a96f8d5fa2c255745d3c451b302c9346d9b7e485e7bce41f6b591f3e8f6addcbb0bc4c2f947a7de1a89b625d6a598b3760",
756                         "0340340340340340340340340340340340340340340340340340340323c313fab50589703b5ec68d3587fec60d161cc149c1ad4a91",
757                         0x2760)
758         );
759     }
760 
getOIDFromPublicKey(ECPublicKey ecPublicKey)761     public static String getOIDFromPublicKey(ECPublicKey ecPublicKey) {
762         ECParameterSpec ecParameterSpec = ecPublicKey.getParams();
763         BigInteger order = ecParameterSpec.getOrder();
764         BigInteger affineX = ecParameterSpec.getGenerator().getAffineX();
765         BigInteger affineY = ecParameterSpec.getGenerator().getAffineY();
766         BigInteger a = ecParameterSpec.getCurve().getA();
767         BigInteger b = ecParameterSpec.getCurve().getB();
768         int h = ecParameterSpec.getCofactor();
769         ECField ecField = ecParameterSpec.getCurve().getField();
770         BigInteger field;
771         if (ecField instanceof ECFieldFp) {
772             ECFieldFp ecFieldFp = (ECFieldFp) ecField;
773             field = ecFieldFp.getP();
774         } else {
775             ECFieldF2m ecFieldF2m = (ECFieldF2m) ecField;
776             field = ecFieldF2m.getReductionPolynomial();
777         }
778 
779         Iterator<ECCurveDefinition> ecCurveDefinitionIterator = ecCurveDefinitions.iterator();
780         while (ecCurveDefinitionIterator.hasNext()) {
781             ECCurveDefinition ecCurveDefinition = ecCurveDefinitionIterator.next();
782             String oid = ecCurveDefinition.equals(field, a, b, affineX, affineY, order, h);
783             if (oid != null) {
784                 return oid;
785             }
786         }
787         return null;
788     }
789 
getECCurveDefinition(String oid)790     public static ECCurveDefinition getECCurveDefinition(String oid) {
791         Iterator<ECCurveDefinition> ecCurveDefinitionIterator = ecCurveDefinitions.iterator();
792         while (ecCurveDefinitionIterator.hasNext()) {
793             ECCurveDefinition ecCurveDefinition = ecCurveDefinitionIterator.next();
794             if (ecCurveDefinition.getOid().equals(oid)) {
795                 return ecCurveDefinition;
796             }
797         }
798         return null;
799     }
800 
801     public static class ECCurveDefinition {
802 
803         private final String name;
804         private final String oid;
805         private final String field;
806         private final String a;
807         private final String b;
808         private final String x;
809         private final String y;
810         private final String n;
811         private final int h;
812 
ECCurveDefinition(String name, String oid, String field, String a, String b, String x, String y, String n, int h)813         public ECCurveDefinition(String name, String oid, String field, String a, String b, String x, String y, String n, int h) {
814             this.name = name;
815             this.oid = oid;
816             this.field = field;
817             this.a = a;
818             this.b = b;
819             this.x = x;
820             this.y = y;
821             this.n = n;
822             this.h = h;
823         }
824 
825         /**
826          * returns the ec oid if parameter are equal to this definition
827          */
equals(BigInteger field, BigInteger a, BigInteger b, BigInteger x, BigInteger y, BigInteger n, int h)828         public String equals(BigInteger field, BigInteger a, BigInteger b, BigInteger x, BigInteger y, BigInteger n, int h) {
829             if (this.field.equals(field.toString(16))
830                     && this.a.equals(a.toString(16))
831                     && this.b.equals(b.toString(16))
832                     && this.x.equals(x.toString(16))
833                     && this.y.equals(y.toString(16))
834                     && this.n.equals(n.toString(16))
835                     && this.h == h) {
836                 return this.oid;
837             }
838             return null;
839         }
840 
getName()841         public String getName() {
842             return name;
843         }
844 
getOid()845         public String getOid() {
846             return oid;
847         }
848 
getField()849         public String getField() {
850             return field;
851         }
852 
getA()853         public String getA() {
854             return a;
855         }
856 
getB()857         public String getB() {
858             return b;
859         }
860 
getX()861         public String getX() {
862             return x;
863         }
864 
getY()865         public String getY() {
866             return y;
867         }
868 
getN()869         public String getN() {
870             return n;
871         }
872 
getH()873         public int getH() {
874             return h;
875         }
876     }
877 
encodePoint(ECPoint ecPoint, EllipticCurve ellipticCurve)878     public static byte[] encodePoint(ECPoint ecPoint, EllipticCurve ellipticCurve) {
879         int size = (ellipticCurve.getField().getFieldSize() + 7) / 8;
880         byte affineXBytes[] = stripLeadingZeros(ecPoint.getAffineX().toByteArray());
881         byte affineYBytes[] = stripLeadingZeros(ecPoint.getAffineY().toByteArray());
882         byte encodedBytes[] = new byte[size * 2 + 1];
883         encodedBytes[0] = 0x04; //uncompressed
884         System.arraycopy(affineXBytes, 0, encodedBytes, size - affineXBytes.length + 1, affineXBytes.length);
885         System.arraycopy(affineYBytes, 0, encodedBytes, encodedBytes.length - affineYBytes.length, affineYBytes.length);
886         return encodedBytes;
887     }
888 
decodePoint(byte[] encodedBytes, EllipticCurve elliptiCcurve)889     public static ECPoint decodePoint(byte[] encodedBytes, EllipticCurve elliptiCcurve) {
890         if (encodedBytes[0] != 0x04) {
891             throw new IllegalArgumentException("Only uncompressed format is supported");
892         }
893 
894         int size = (elliptiCcurve.getField().getFieldSize() + 7) / 8;
895         byte affineXBytes[] = new byte[size];
896         byte affineYBytes[] = new byte[size];
897         System.arraycopy(encodedBytes, 1, affineXBytes, 0, size);
898         System.arraycopy(encodedBytes, size + 1, affineYBytes, 0, size);
899         return new ECPoint(new BigInteger(1, affineXBytes), new BigInteger(1, affineYBytes));
900     }
901 
stripLeadingZeros(byte[] bytes)902     public static byte[] stripLeadingZeros(byte[] bytes) {
903         int i;
904         for (i = 0; i < bytes.length - 1; i++) {
905             if (bytes[i] != 0) {
906                 break;
907             }
908         }
909 
910         if (i == 0) {
911             return bytes;
912         } else {
913             byte stripped[] = new byte[bytes.length - i];
914             System.arraycopy(bytes, i, stripped, 0, stripped.length);
915             return stripped;
916         }
917     }
918 }
919