29
30 import java.security.*;
31 import java.security.interfaces.*;
32 import java.security.spec.*;
33 import java.util.Optional;
34
35 import sun.security.jca.JCAUtil;
36 import sun.security.util.*;
37 import static sun.security.ec.ECOperations.IntermediateValueException;
38
39 /**
40 * ECDSA signature implementation. This class currently supports the
41 * following algorithm names:
42 *
43 * . "NONEwithECDSA"
44 * . "SHA1withECDSA"
45 * . "SHA224withECDSA"
46 * . "SHA256withECDSA"
47 * . "SHA384withECDSA"
48 * . "SHA512withECDSA"
49 * . "NONEwithECDSAinP1363Format"
50 * . "SHA1withECDSAinP1363Format"
51 * . "SHA224withECDSAinP1363Format"
52 * . "SHA256withECDSAinP1363Format"
53 * . "SHA384withECDSAinP1363Format"
54 * . "SHA512withECDSAinP1363Format"
55 *
56 * @since 1.7
57 */
58 abstract class ECDSASignature extends SignatureSpi {
59
60 // message digest implementation we use
61 private final MessageDigest messageDigest;
62
63 // supplied entropy
64 private SecureRandom random;
65
66 // flag indicating whether the digest has been reset
67 private boolean needsReset;
68
69 // private key, if initialized for signing
70 private ECPrivateKey privateKey;
71
72 // public key, if initialized for verifying
73 private ECPublicKey publicKey;
74
261 public static final class SHA384inP1363Format extends ECDSASignature {
262 public SHA384inP1363Format() {
263 super("SHA-384", true);
264 }
265 }
266
267 // Nested class for SHA512withECDSA signatures
268 public static final class SHA512 extends ECDSASignature {
269 public SHA512() {
270 super("SHA-512");
271 }
272 }
273
274 // Nested class for SHA512withECDSAinP1363Format signatures
275 public static final class SHA512inP1363Format extends ECDSASignature {
276 public SHA512inP1363Format() {
277 super("SHA-512", true);
278 }
279 }
280
281 // initialize for verification. See JCA doc
282 @Override
283 protected void engineInitVerify(PublicKey publicKey)
284 throws InvalidKeyException {
285 ECPublicKey key = (ECPublicKey) ECKeyFactory.toECKey(publicKey);
286 if (!isCompatible(this.sigParams, key.getParams())) {
287 throw new InvalidKeyException("Key params does not match signature params");
288 }
289
290 // Should check that the supplied key is appropriate for signature
291 // algorithm (e.g. P-256 for SHA256withECDSA)
292 this.publicKey = key;
293 this.privateKey = null;
294 resetDigest();
295 }
296
297 // initialize for signing. See JCA doc
298 @Override
299 protected void engineInitSign(PrivateKey privateKey)
300 throws InvalidKeyException {
|
29
30 import java.security.*;
31 import java.security.interfaces.*;
32 import java.security.spec.*;
33 import java.util.Optional;
34
35 import sun.security.jca.JCAUtil;
36 import sun.security.util.*;
37 import static sun.security.ec.ECOperations.IntermediateValueException;
38
39 /**
40 * ECDSA signature implementation. This class currently supports the
41 * following algorithm names:
42 *
43 * . "NONEwithECDSA"
44 * . "SHA1withECDSA"
45 * . "SHA224withECDSA"
46 * . "SHA256withECDSA"
47 * . "SHA384withECDSA"
48 * . "SHA512withECDSA"
49 * . "SHA3-224withECDSA"
50 * . "SHA3-256withECDSA"
51 * . "SHA3-384withECDSA"
52 * . "SHA3-512withECDSA"
53 * . "NONEwithECDSAinP1363Format"
54 * . "SHA1withECDSAinP1363Format"
55 * . "SHA224withECDSAinP1363Format"
56 * . "SHA256withECDSAinP1363Format"
57 * . "SHA384withECDSAinP1363Format"
58 * . "SHA512withECDSAinP1363Format"
59 * . "SHA3-224withECDSAinP1363Format"
60 * . "SHA3-256withECDSAinP1363Format"
61 * . "SHA3-384withECDSAinP1363Format"
62 * . "SHA3-512withECDSAinP1363Format"
63 *
64 * @since 1.7
65 */
66 abstract class ECDSASignature extends SignatureSpi {
67
68 // message digest implementation we use
69 private final MessageDigest messageDigest;
70
71 // supplied entropy
72 private SecureRandom random;
73
74 // flag indicating whether the digest has been reset
75 private boolean needsReset;
76
77 // private key, if initialized for signing
78 private ECPrivateKey privateKey;
79
80 // public key, if initialized for verifying
81 private ECPublicKey publicKey;
82
269 public static final class SHA384inP1363Format extends ECDSASignature {
270 public SHA384inP1363Format() {
271 super("SHA-384", true);
272 }
273 }
274
275 // Nested class for SHA512withECDSA signatures
276 public static final class SHA512 extends ECDSASignature {
277 public SHA512() {
278 super("SHA-512");
279 }
280 }
281
282 // Nested class for SHA512withECDSAinP1363Format signatures
283 public static final class SHA512inP1363Format extends ECDSASignature {
284 public SHA512inP1363Format() {
285 super("SHA-512", true);
286 }
287 }
288
289 // Nested class for SHA3_224withECDSA signatures
290 public static final class SHA3_224 extends ECDSASignature {
291 public SHA3_224() {
292 super("SHA3-224");
293 }
294 }
295
296 // Nested class for SHA3_224withECDSAinP1363Format signatures
297 public static final class SHA3_224inP1363Format extends ECDSASignature {
298 public SHA3_224inP1363Format() {
299 super("SHA3-224", true);
300 }
301 }
302
303 // Nested class for SHA3_256withECDSA signatures
304 public static final class SHA3_256 extends ECDSASignature {
305 public SHA3_256() {
306 super("SHA3-256");
307 }
308 }
309
310 // Nested class for SHA3_256withECDSAinP1363Format signatures
311 public static final class SHA3_256inP1363Format extends ECDSASignature {
312 public SHA3_256inP1363Format() {
313 super("SHA3-256", true);
314 }
315 }
316
317 // Nested class for SHA3_384withECDSA signatures
318 public static final class SHA3_384 extends ECDSASignature {
319 public SHA3_384() {
320 super("SHA3-384");
321 }
322 }
323
324 // Nested class for SHA3_384withECDSAinP1363Format signatures
325 public static final class SHA3_384inP1363Format extends ECDSASignature {
326 public SHA3_384inP1363Format() {
327 super("SHA3-384", true);
328 }
329 }
330
331 // Nested class for SHA3_512withECDSA signatures
332 public static final class SHA3_512 extends ECDSASignature {
333 public SHA3_512() {
334 super("SHA3-512");
335 }
336 }
337
338 // Nested class for SHA3_512withECDSAinP1363Format signatures
339 public static final class SHA3_512inP1363Format extends ECDSASignature {
340 public SHA3_512inP1363Format() {
341 super("SHA3-512", true);
342 }
343 }
344
345 // initialize for verification. See JCA doc
346 @Override
347 protected void engineInitVerify(PublicKey publicKey)
348 throws InvalidKeyException {
349 ECPublicKey key = (ECPublicKey) ECKeyFactory.toECKey(publicKey);
350 if (!isCompatible(this.sigParams, key.getParams())) {
351 throw new InvalidKeyException("Key params does not match signature params");
352 }
353
354 // Should check that the supplied key is appropriate for signature
355 // algorithm (e.g. P-256 for SHA256withECDSA)
356 this.publicKey = key;
357 this.privateKey = null;
358 resetDigest();
359 }
360
361 // initialize for signing. See JCA doc
362 @Override
363 protected void engineInitSign(PrivateKey privateKey)
364 throws InvalidKeyException {
|