author | bpb |
Fri, 14 Sep 2018 09:00:22 -0700 | |
changeset 51746 | 07ae9da7a230 |
parent 50204 | 3195a713e24d |
child 52946 | 752e57845ad2 |
permissions | -rw-r--r-- |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1 |
/* |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved. |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
10 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
15 |
* accompanied this code). |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
16 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
20 |
* |
5506 | 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. |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
24 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
25 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
26 |
package sun.security.ec; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
27 |
|
39771
ab558f578f30
8161571: Verifying ECDSA signatures permits trailing bytes
vinnie
parents:
28974
diff
changeset
|
28 |
import java.io.IOException; |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
29 |
import java.nio.ByteBuffer; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
30 |
import java.math.BigInteger; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
31 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
32 |
import java.security.*; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
33 |
import java.security.interfaces.*; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
34 |
import java.security.spec.*; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
35 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
36 |
import sun.security.jca.JCAUtil; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
37 |
import sun.security.util.*; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
38 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
39 |
/** |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
40 |
* ECDSA signature implementation. This class currently supports the |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
41 |
* following algorithm names: |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
42 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
43 |
* . "NONEwithECDSA" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
44 |
* . "SHA1withECDSA" |
12685 | 45 |
* . "SHA224withECDSA" |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
46 |
* . "SHA256withECDSA" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
47 |
* . "SHA384withECDSA" |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
48 |
* . "SHA512withECDSA" |
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
49 |
* . "NONEwithECDSAinP1363Format" |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
50 |
* . "SHA1withECDSAinP1363Format" |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
51 |
* . "SHA224withECDSAinP1363Format" |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
52 |
* . "SHA256withECDSAinP1363Format" |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
53 |
* . "SHA384withECDSAinP1363Format" |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
54 |
* . "SHA512withECDSAinP1363Format" |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
55 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
56 |
* @since 1.7 |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
57 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
58 |
abstract class ECDSASignature extends SignatureSpi { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
59 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
60 |
// message digest implementation we use |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
61 |
private final MessageDigest messageDigest; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
62 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
63 |
// supplied entropy |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
64 |
private SecureRandom random; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
65 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
66 |
// flag indicating whether the digest has been reset |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
67 |
private boolean needsReset; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
68 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
69 |
// private key, if initialized for signing |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
70 |
private ECPrivateKey privateKey; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
71 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
72 |
// public key, if initialized for verifying |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
73 |
private ECPublicKey publicKey; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
74 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
75 |
// The format. true for the IEEE P1363 format. false (default) for ASN.1 |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
76 |
private final boolean p1363Format; |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
77 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
78 |
/** |
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
79 |
* Constructs a new ECDSASignature. |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
80 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
81 |
* @exception ProviderException if the native ECC library is unavailable. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
82 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
83 |
ECDSASignature() { |
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
84 |
this(false); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
85 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
86 |
|
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
87 |
/** |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
88 |
* Constructs a new ECDSASignature that will use the specified |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
89 |
* signature format. {@code p1363Format} should be {@code true} to |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
90 |
* use the IEEE P1363 format. If {@code p1363Format} is {@code false}, |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
91 |
* the DER-encoded ASN.1 format will be used. This constructor is |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
92 |
* used by the RawECDSA subclasses. |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
93 |
*/ |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
94 |
ECDSASignature(boolean p1363Format) { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
95 |
this.messageDigest = null; |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
96 |
this.p1363Format = p1363Format; |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
97 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
98 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
99 |
/** |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
100 |
* Constructs a new ECDSASignature. Used by subclasses. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
101 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
102 |
ECDSASignature(String digestName) { |
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
103 |
this(digestName, false); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
104 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
105 |
|
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
106 |
/** |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
107 |
* Constructs a new ECDSASignature that will use the specified |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
108 |
* digest and signature format. {@code p1363Format} should be |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
109 |
* {@code true} to use the IEEE P1363 format. If {@code p1363Format} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
110 |
* is {@code false}, the DER-encoded ASN.1 format will be used. This |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
111 |
* constructor is used by subclasses. |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
112 |
*/ |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
113 |
ECDSASignature(String digestName, boolean p1363Format) { |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
114 |
try { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
115 |
messageDigest = MessageDigest.getInstance(digestName); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
116 |
} catch (NoSuchAlgorithmException e) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
117 |
throw new ProviderException(e); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
118 |
} |
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
119 |
this.needsReset = false; |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
120 |
this.p1363Format = p1363Format; |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
121 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
122 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
123 |
// Class for Raw ECDSA signatures. |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
124 |
static class RawECDSA extends ECDSASignature { |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
125 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
126 |
// the longest supported digest is 512 bits (SHA-512) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
127 |
private static final int RAW_ECDSA_MAX = 64; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
128 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
129 |
private final byte[] precomputedDigest; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
130 |
private int offset = 0; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
131 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
132 |
RawECDSA(boolean p1363Format) { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
133 |
super(p1363Format); |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
134 |
precomputedDigest = new byte[RAW_ECDSA_MAX]; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
135 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
136 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
137 |
// Stores the precomputed message digest value. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
138 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
139 |
protected void engineUpdate(byte b) throws SignatureException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
140 |
if (offset >= precomputedDigest.length) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
141 |
offset = RAW_ECDSA_MAX + 1; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
142 |
return; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
143 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
144 |
precomputedDigest[offset++] = b; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
145 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
146 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
147 |
// Stores the precomputed message digest value. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
148 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
149 |
protected void engineUpdate(byte[] b, int off, int len) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
150 |
throws SignatureException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
151 |
if (offset >= precomputedDigest.length) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
152 |
offset = RAW_ECDSA_MAX + 1; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
153 |
return; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
154 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
155 |
System.arraycopy(b, off, precomputedDigest, offset, len); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
156 |
offset += len; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
157 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
158 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
159 |
// Stores the precomputed message digest value. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
160 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
161 |
protected void engineUpdate(ByteBuffer byteBuffer) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
162 |
int len = byteBuffer.remaining(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
163 |
if (len <= 0) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
164 |
return; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
165 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
166 |
if (offset + len >= precomputedDigest.length) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
167 |
offset = RAW_ECDSA_MAX + 1; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
168 |
return; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
169 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
170 |
byteBuffer.get(precomputedDigest, offset, len); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
171 |
offset += len; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
172 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
173 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
174 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
175 |
protected void resetDigest(){ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
176 |
offset = 0; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
177 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
178 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
179 |
// Returns the precomputed message digest value. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
180 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
181 |
protected byte[] getDigestValue() throws SignatureException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
182 |
if (offset > RAW_ECDSA_MAX) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
183 |
throw new SignatureException("Message digest is too long"); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
184 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
185 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
186 |
byte[] result = new byte[offset]; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
187 |
System.arraycopy(precomputedDigest, 0, result, 0, offset); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
188 |
offset = 0; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
189 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
190 |
return result; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
191 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
192 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
193 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
194 |
// Nested class for NONEwithECDSA signatures |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
195 |
public static final class Raw extends RawECDSA { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
196 |
public Raw() { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
197 |
super(false); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
198 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
199 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
200 |
|
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
201 |
// Nested class for NONEwithECDSAinP1363Format signatures |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
202 |
public static final class RawinP1363Format extends RawECDSA { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
203 |
public RawinP1363Format() { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
204 |
super(true); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
205 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
206 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
207 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
208 |
// Nested class for SHA1withECDSA signatures |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
209 |
public static final class SHA1 extends ECDSASignature { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
210 |
public SHA1() { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
211 |
super("SHA1"); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
212 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
213 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
214 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
215 |
// Nested class for SHA1withECDSAinP1363Format signatures |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
216 |
public static final class SHA1inP1363Format extends ECDSASignature { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
217 |
public SHA1inP1363Format() { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
218 |
super("SHA1", true); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
219 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
220 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
221 |
|
12685 | 222 |
// Nested class for SHA224withECDSA signatures |
223 |
public static final class SHA224 extends ECDSASignature { |
|
224 |
public SHA224() { |
|
225 |
super("SHA-224"); |
|
226 |
} |
|
227 |
} |
|
228 |
||
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
229 |
// Nested class for SHA224withECDSAinP1363Format signatures |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
230 |
public static final class SHA224inP1363Format extends ECDSASignature { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
231 |
public SHA224inP1363Format() { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
232 |
super("SHA-224", true); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
233 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
234 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
235 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
236 |
// Nested class for SHA256withECDSA signatures |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
237 |
public static final class SHA256 extends ECDSASignature { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
238 |
public SHA256() { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
239 |
super("SHA-256"); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
240 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
241 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
242 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
243 |
// Nested class for SHA256withECDSAinP1363Format signatures |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
244 |
public static final class SHA256inP1363Format extends ECDSASignature { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
245 |
public SHA256inP1363Format() { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
246 |
super("SHA-256", true); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
247 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
248 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
249 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
250 |
// Nested class for SHA384withECDSA signatures |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
251 |
public static final class SHA384 extends ECDSASignature { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
252 |
public SHA384() { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
253 |
super("SHA-384"); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
254 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
255 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
256 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
257 |
// Nested class for SHA384withECDSAinP1363Format signatures |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
258 |
public static final class SHA384inP1363Format extends ECDSASignature { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
259 |
public SHA384inP1363Format() { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
260 |
super("SHA-384", true); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
261 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
262 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
263 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
264 |
// Nested class for SHA512withECDSA signatures |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
265 |
public static final class SHA512 extends ECDSASignature { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
266 |
public SHA512() { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
267 |
super("SHA-512"); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
268 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
269 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
270 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
271 |
// Nested class for SHA512withECDSAinP1363Format signatures |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
272 |
public static final class SHA512inP1363Format extends ECDSASignature { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
273 |
public SHA512inP1363Format() { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
274 |
super("SHA-512", true); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
275 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
276 |
} |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
277 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
278 |
// initialize for verification. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
279 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
280 |
protected void engineInitVerify(PublicKey publicKey) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
281 |
throws InvalidKeyException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
282 |
this.publicKey = (ECPublicKey) ECKeyFactory.toECKey(publicKey); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
283 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
284 |
// Should check that the supplied key is appropriate for signature |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
285 |
// algorithm (e.g. P-256 for SHA256withECDSA) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
286 |
this.privateKey = null; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
287 |
resetDigest(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
288 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
289 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
290 |
// initialize for signing. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
291 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
292 |
protected void engineInitSign(PrivateKey privateKey) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
293 |
throws InvalidKeyException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
294 |
engineInitSign(privateKey, null); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
295 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
296 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
297 |
// initialize for signing. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
298 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
299 |
protected void engineInitSign(PrivateKey privateKey, SecureRandom random) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
300 |
throws InvalidKeyException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
301 |
this.privateKey = (ECPrivateKey) ECKeyFactory.toECKey(privateKey); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
302 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
303 |
// Should check that the supplied key is appropriate for signature |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
304 |
// algorithm (e.g. P-256 for SHA256withECDSA) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
305 |
this.publicKey = null; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
306 |
this.random = random; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
307 |
resetDigest(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
308 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
309 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
310 |
/** |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
311 |
* Resets the message digest if needed. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
312 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
313 |
protected void resetDigest() { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
314 |
if (needsReset) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
315 |
if (messageDigest != null) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
316 |
messageDigest.reset(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
317 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
318 |
needsReset = false; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
319 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
320 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
321 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
322 |
/** |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
323 |
* Returns the message digest value. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
324 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
325 |
protected byte[] getDigestValue() throws SignatureException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
326 |
needsReset = false; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
327 |
return messageDigest.digest(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
328 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
329 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
330 |
// update the signature with the plaintext data. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
331 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
332 |
protected void engineUpdate(byte b) throws SignatureException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
333 |
messageDigest.update(b); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
334 |
needsReset = true; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
335 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
336 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
337 |
// update the signature with the plaintext data. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
338 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
339 |
protected void engineUpdate(byte[] b, int off, int len) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
340 |
throws SignatureException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
341 |
messageDigest.update(b, off, len); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
342 |
needsReset = true; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
343 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
344 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
345 |
// update the signature with the plaintext data. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
346 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
347 |
protected void engineUpdate(ByteBuffer byteBuffer) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
348 |
int len = byteBuffer.remaining(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
349 |
if (len <= 0) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
350 |
return; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
351 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
352 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
353 |
messageDigest.update(byteBuffer); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
354 |
needsReset = true; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
355 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
356 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
357 |
// sign the data and return the signature. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
358 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
359 |
protected byte[] engineSign() throws SignatureException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
360 |
byte[] s = privateKey.getS().toByteArray(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
361 |
ECParameterSpec params = privateKey.getParams(); |
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
12685
diff
changeset
|
362 |
// DER OID |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
12685
diff
changeset
|
363 |
byte[] encodedParams = ECUtil.encodeECParameterSpec(null, params); |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
364 |
int keySize = params.getCurve().getField().getFieldSize(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
365 |
|
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
3492
diff
changeset
|
366 |
// seed is twice the key size (in bytes) plus 1 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
3492
diff
changeset
|
367 |
byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2]; |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
368 |
if (random == null) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
369 |
random = JCAUtil.getSecureRandom(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
370 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
371 |
random.nextBytes(seed); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
372 |
|
45993 | 373 |
// random bits needed for timing countermeasures |
374 |
int timingArgument = random.nextInt(); |
|
375 |
// values must be non-zero to enable countermeasures |
|
376 |
timingArgument |= 1; |
|
377 |
||
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
378 |
byte[] sig; |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
379 |
try { |
45993 | 380 |
sig = signDigest(getDigestValue(), s, encodedParams, seed, |
381 |
timingArgument); |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
382 |
} catch (GeneralSecurityException e) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
383 |
throw new SignatureException("Could not sign data", e); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
384 |
} |
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
385 |
|
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
386 |
if (p1363Format) { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
387 |
return sig; |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
388 |
} else { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
389 |
return encodeSignature(sig); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
390 |
} |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
391 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
392 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
393 |
// verify the data and return the result. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
394 |
@Override |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
395 |
protected boolean engineVerify(byte[] signature) throws SignatureException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
396 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
397 |
byte[] w; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
398 |
ECParameterSpec params = publicKey.getParams(); |
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
12685
diff
changeset
|
399 |
// DER OID |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
12685
diff
changeset
|
400 |
byte[] encodedParams = ECUtil.encodeECParameterSpec(null, params); |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
401 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
402 |
if (publicKey instanceof ECPublicKeyImpl) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
403 |
w = ((ECPublicKeyImpl)publicKey).getEncodedPublicValue(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
404 |
} else { // instanceof ECPublicKey |
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
12685
diff
changeset
|
405 |
w = ECUtil.encodePoint(publicKey.getW(), params.getCurve()); |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
406 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
407 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
408 |
byte[] sig; |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
409 |
if (p1363Format) { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
410 |
sig = signature; |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
411 |
} else { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
412 |
sig = decodeSignature(signature); |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
413 |
} |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
414 |
|
28974
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
415 |
try { |
71fe221460f5
8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents:
25859
diff
changeset
|
416 |
return verifySignedDigest(sig, getDigestValue(), w, encodedParams); |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
417 |
} catch (GeneralSecurityException e) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
418 |
throw new SignatureException("Could not verify signature", e); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
419 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
420 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
421 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
422 |
// set parameter, not supported. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
423 |
@Override |
19209 | 424 |
@Deprecated |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
425 |
protected void engineSetParameter(String param, Object value) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
426 |
throws InvalidParameterException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
427 |
throw new UnsupportedOperationException("setParameter() not supported"); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
428 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
429 |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
430 |
@Override |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
431 |
protected void engineSetParameter(AlgorithmParameterSpec params) |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
432 |
throws InvalidAlgorithmParameterException { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
433 |
if (params != null) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
434 |
throw new InvalidAlgorithmParameterException("No parameter accepted"); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
435 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
436 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
437 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
438 |
// get parameter, not supported. See JCA doc |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
439 |
@Override |
19209 | 440 |
@Deprecated |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
441 |
protected Object engineGetParameter(String param) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
442 |
throws InvalidParameterException { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
443 |
throw new UnsupportedOperationException("getParameter() not supported"); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
444 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
445 |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
446 |
@Override |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
447 |
protected AlgorithmParameters engineGetParameters() { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
448 |
return null; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
449 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
450 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
451 |
// Convert the concatenation of R and S into their DER encoding |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
452 |
private byte[] encodeSignature(byte[] signature) throws SignatureException { |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
3492
diff
changeset
|
453 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
454 |
try { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
455 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
456 |
int n = signature.length >> 1; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
457 |
byte[] bytes = new byte[n]; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
458 |
System.arraycopy(signature, 0, bytes, 0, n); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
459 |
BigInteger r = new BigInteger(1, bytes); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
460 |
System.arraycopy(signature, n, bytes, 0, n); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
461 |
BigInteger s = new BigInteger(1, bytes); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
462 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
463 |
DerOutputStream out = new DerOutputStream(signature.length + 10); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
464 |
out.putInteger(r); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
465 |
out.putInteger(s); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
466 |
DerValue result = |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
467 |
new DerValue(DerValue.tag_Sequence, out.toByteArray()); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
468 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
469 |
return result.toByteArray(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
470 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
471 |
} catch (Exception e) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
472 |
throw new SignatureException("Could not encode signature", e); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
473 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
474 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
475 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
476 |
// Convert the DER encoding of R and S into a concatenation of R and S |
43214 | 477 |
private byte[] decodeSignature(byte[] sig) throws SignatureException { |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
478 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
479 |
try { |
43214 | 480 |
// Enforce strict DER checking for signatures |
481 |
DerInputStream in = new DerInputStream(sig, 0, sig.length, false); |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
482 |
DerValue[] values = in.getSequence(2); |
43214 | 483 |
|
484 |
// check number of components in the read sequence |
|
485 |
// and trailing data |
|
486 |
if ((values.length != 2) || (in.available() != 0)) { |
|
487 |
throw new IOException("Invalid encoding for signature"); |
|
488 |
} |
|
489 |
||
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
490 |
BigInteger r = values[0].getPositiveBigInteger(); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
491 |
BigInteger s = values[1].getPositiveBigInteger(); |
39771
ab558f578f30
8161571: Verifying ECDSA signatures permits trailing bytes
vinnie
parents:
28974
diff
changeset
|
492 |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
493 |
// trim leading zeroes |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
494 |
byte[] rBytes = trimZeroes(r.toByteArray()); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
495 |
byte[] sBytes = trimZeroes(s.toByteArray()); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
496 |
int k = Math.max(rBytes.length, sBytes.length); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
497 |
// r and s each occupy half the array |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
498 |
byte[] result = new byte[k << 1]; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
499 |
System.arraycopy(rBytes, 0, result, k - rBytes.length, |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
500 |
rBytes.length); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
501 |
System.arraycopy(sBytes, 0, result, result.length - sBytes.length, |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
502 |
sBytes.length); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
503 |
return result; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
504 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
505 |
} catch (Exception e) { |
43214 | 506 |
throw new SignatureException("Invalid encoding for signature", e); |
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
507 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
508 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
509 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
510 |
// trim leading (most significant) zeroes from the result |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
511 |
private static byte[] trimZeroes(byte[] b) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
512 |
int i = 0; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
513 |
while ((i < b.length - 1) && (b[i] == 0)) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
514 |
i++; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
515 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
516 |
if (i == 0) { |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
517 |
return b; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
518 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
519 |
byte[] t = new byte[b.length - i]; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
520 |
System.arraycopy(b, i, t, 0, t.length); |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
521 |
return t; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
522 |
} |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
523 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
524 |
/** |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
525 |
* Signs the digest using the private key. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
526 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
527 |
* @param digest the digest to be signed. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
528 |
* @param s the private key's S value. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
529 |
* @param encodedParams the curve's DER encoded object identifier. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
530 |
* @param seed the random seed. |
45993 | 531 |
* @param timing When non-zero, the implmentation will use timing |
532 |
* countermeasures to hide secrets from timing channels. The EC |
|
533 |
* implementation will disable the countermeasures when this value is |
|
534 |
* zero, because the underlying EC functions are shared by several |
|
535 |
* crypto operations, some of which do not use the countermeasures. |
|
536 |
* The high-order 31 bits must be uniformly random. The entropy from |
|
537 |
* these bits is used by the countermeasures. |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
538 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
539 |
* @return byte[] the signature. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
540 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
541 |
private static native byte[] signDigest(byte[] digest, byte[] s, |
45993 | 542 |
byte[] encodedParams, byte[] seed, int timing) |
543 |
throws GeneralSecurityException; |
|
3492
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
544 |
|
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
545 |
/** |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
546 |
* Verifies the signed digest using the public key. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
547 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
548 |
* @param signedDigest the signature to be verified. It is encoded |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
549 |
* as a concatenation of the key's R and S values. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
550 |
* @param digest the digest to be used. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
551 |
* @param w the public key's W point (in uncompressed form). |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
552 |
* @param encodedParams the curve's DER encoded object identifier. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
553 |
* |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
554 |
* @return boolean true if the signature is successfully verified. |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
555 |
*/ |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
556 |
private static native boolean verifySignedDigest(byte[] signature, |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
557 |
byte[] digest, byte[] w, byte[] encodedParams) |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
558 |
throws GeneralSecurityException; |
e549cea58864
6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
559 |
} |