Debian Codegraph @

ext/ed25519_jruby/net/i2p/crypto/eddsa/EdDSAPublicKey.java

raw symbols SCIP
1 /**
2 * EdDSA-Java by str4d
3 *
4 * To the extent possible under law, the person who associated CC0 with
5 * EdDSA-Java has waived all copyright and related or neighboring rights
6 * to EdDSA-Java.
7 *
8 * You should have received a copy of the CC0 legalcode along with this
9 * work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
10 *
11 */
12 package net.i2p.crypto.eddsa;
13
14 import java.security.PublicKey;
15 import java.security.spec.InvalidKeySpecException;
16 import java.security.spec.X509EncodedKeySpec;
17 import java.util.Arrays;
18
19 import net.i2p.crypto.eddsa.math.GroupElement;
20 import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
21 import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec;
22 import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
23
24 /**
25 * An EdDSA public key.
26 *<p>
27 * Warning: Public key encoding is is based on the current curdle WG draft,
28 * and is subject to change. See getEncoded().
29 *</p><p>
30 * For compatibility with older releases, decoding supports both the old and new
31 * draft specifications. See decode().
32 *</p><p>
33 * Ref: https://tools.ietf.org/html/draft-ietf-curdle-pkix-04
34 *</p><p>
35 * Old Ref: https://tools.ietf.org/html/draft-josefsson-pkix-eddsa-04
36 *</p>
37 * @author str4d
38 *
39 */
40 public class EdDSAPublicKey implements EdDSAKey, PublicKey {
41 private static final long serialVersionUID = 9837459837498475L;
42 private final GroupElement A;
43 private final GroupElement Aneg;
44 private final byte[] Abyte;
45 private final EdDSAParameterSpec edDsaSpec;
46
47 // OID 1.3.101.xxx
48 private static final int OID_OLD = 100;
49 private static final int OID_ED25519 = 112;
50 private static final int OID_BYTE = 8;
51 private static final int IDLEN_BYTE = 3;
52
53 public EdDSAPublicKey(EdDSAPublicKeySpec spec) {
54 this.A = spec.getA();
55 this.Aneg = spec.getNegativeA();
56 this.Abyte = this.A.toByteArray();
57 this.edDsaSpec = spec.getParams();
58 }
59
60 public EdDSAPublicKey(X509EncodedKeySpec spec) throws InvalidKeySpecException {
61 this(new EdDSAPublicKeySpec(decode(spec.getEncoded()),
62 EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519)));
63 }
64
65 @Override
66 public String getAlgorithm() {
67 return KEY_ALGORITHM;
68 }
69
70 @Override
71 public String getFormat() {
72 return "X.509";
73 }
74
75 /**
76 * Returns the public key in its canonical encoding.
77 *<p>
78 * This implements the following specs:
79 *<ul><li>
80 * General encoding: https://tools.ietf.org/html/draft-ietf-curdle-pkix-04
81 *</li></li>
82 * Key encoding: https://tools.ietf.org/html/rfc8032
83 *</li></ul>
84 *</p><p>
85 * For keys in older formats, decoding and then re-encoding is sufficient to
86 * migrate them to the canonical encoding.
87 *</p>
88 * Relevant spec quotes:
89 *<pre>
90 * In the X.509 certificate, the subjectPublicKeyInfo field has the
91 * SubjectPublicKeyInfo type, which has the following ASN.1 syntax:
92 *
93 * SubjectPublicKeyInfo ::= SEQUENCE {
94 * algorithm AlgorithmIdentifier,
95 * subjectPublicKey BIT STRING
96 * }
97 *</pre>
98 *
99 *<pre>
100 * AlgorithmIdentifier ::= SEQUENCE {
101 * algorithm OBJECT IDENTIFIER,
102 * parameters ANY DEFINED BY algorithm OPTIONAL
103 * }
104 *
105 * For all of the OIDs, the parameters MUST be absent.
106 *</pre>
107 *
108 *<pre>
109 * id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }
110 *</pre>
111 *
112 * @return 44 bytes for Ed25519, null for other curves
113 */
114 @Override
115 public byte[] getEncoded() {
116 if (!edDsaSpec.equals(EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519)))
117 return null;
118 int totlen = 12 + Abyte.length;
119 byte[] rv = new byte[totlen];
120 int idx = 0;
121 // sequence
122 rv[idx++] = 0x30;
123 rv[idx++] = (byte) (totlen - 2);
124 // Algorithm Identifier
125 // sequence
126 rv[idx++] = 0x30;
127 rv[idx++] = 5;
128 // OID
129 // https://msdn.microsoft.com/en-us/library/windows/desktop/bb540809%28v=vs.85%29.aspx
130 rv[idx++] = 0x06;
131 rv[idx++] = 3;
132 rv[idx++] = (1 * 40) + 3;
133 rv[idx++] = 101;
134 rv[idx++] = (byte) OID_ED25519;
135 // params - absent
136 // the key
137 rv[idx++] = 0x03; // bit string
138 rv[idx++] = (byte) (1 + Abyte.length);
139 rv[idx++] = 0; // number of trailing unused bits
140 System.arraycopy(Abyte, 0, rv, idx, Abyte.length);
141 return rv;
142 }
143
144 /**
145 * Extracts the public key bytes from the provided encoding.
146 *<p>
147 * This will decode data conforming to the current spec at
148 * https://tools.ietf.org/html/draft-ietf-curdle-pkix-04
149 * or the old spec at
150 * https://tools.ietf.org/html/draft-josefsson-pkix-eddsa-04.
151 *</p><p>
152 * Contrary to draft-ietf-curdle-pkix-04, it WILL accept a parameter value
153 * of NULL, as it is required for interoperability with the default Java
154 * keystore. Other implementations MUST NOT copy this behaviour from here
155 * unless they also need to read keys from the default Java keystore.
156 *</p><p>
157 * This is really dumb for now. It does not use a general-purpose ASN.1 decoder.
158 * See also getEncoded().
159 *</p>
160 *
161 * @return 32 bytes for Ed25519, throws for other curves
162 */
163 private static byte[] decode(byte[] d) throws InvalidKeySpecException {
164 try {
165 //
166 // Setup and OID check
167 //
168 int totlen = 44;
169 int idlen = 5;
170 int doid = d[OID_BYTE];
171 if (doid == OID_OLD) {
172 totlen = 47;
173 idlen = 8;
174 } else if (doid == OID_ED25519) {
175 // Detect parameter value of NULL
176 if (d[IDLEN_BYTE] == 7) {
177 totlen = 46;
178 idlen = 7;
179 }
180 } else {
181 throw new InvalidKeySpecException("unsupported key spec");
182 }
183
184 //
185 // Pre-decoding check
186 //
187 if (d.length != totlen) {
188 throw new InvalidKeySpecException("invalid key spec length");
189 }
190
191 //
192 // Decoding
193 //
194 int idx = 0;
195 if (d[idx++] != 0x30 ||
196 d[idx++] != (totlen - 2) ||
197 d[idx++] != 0x30 ||
198 d[idx++] != idlen ||
199 d[idx++] != 0x06 ||
200 d[idx++] != 3 ||
201 d[idx++] != (1 * 40) + 3 ||
202 d[idx++] != 101) {
203 throw new InvalidKeySpecException("unsupported key spec");
204 }
205 idx++; // OID, checked above
206 // parameters only with old OID
207 if (doid == OID_OLD) {
208 if (d[idx++] != 0x0a ||
209 d[idx++] != 1 ||
210 d[idx++] != 1) {
211 throw new InvalidKeySpecException("unsupported key spec");
212 }
213 } else {
214 // Handle parameter value of NULL
215 //
216 // Quote https://tools.ietf.org/html/draft-ietf-curdle-pkix-04 :
217 // For all of the OIDs, the parameters MUST be absent.
218 // Regardless of the defect in the original 1997 syntax,
219 // implementations MUST NOT accept a parameters value of NULL.
220 //
221 // But Java's default keystore puts it in (when decoding as
222 // PKCS8 and then re-encoding to pass on), so we must accept it.
223 if (idlen == 7) {
224 if (d[idx++] != 0x05 ||
225 d[idx++] != 0) {
226 throw new InvalidKeySpecException("unsupported key spec");
227 }
228 }
229 }
230 if (d[idx++] != 0x03 ||
231 d[idx++] != 33 ||
232 d[idx++] != 0) {
233 throw new InvalidKeySpecException("unsupported key spec");
234 }
235 byte[] rv = new byte[32];
236 System.arraycopy(d, idx, rv, 0, 32);
237 return rv;
238 } catch (IndexOutOfBoundsException ioobe) {
239 throw new InvalidKeySpecException(ioobe);
240 }
241 }
242
243 @Override
244 public EdDSAParameterSpec getParams() {
245 return edDsaSpec;
246 }
247
248 public GroupElement getA() {
249 return A;
250 }
251
252 public GroupElement getNegativeA() {
253 return Aneg;
254 }
255
256 public byte[] getAbyte() {
257 return Abyte;
258 }
259
260 @Override
261 public int hashCode() {
262 return Arrays.hashCode(Abyte);
263 }
264
265 @Override
266 public boolean equals(Object o) {
267 if (o == this)
268 return true;
269 if (!(o instanceof EdDSAPublicKey))
270 return false;
271 EdDSAPublicKey pk = (EdDSAPublicKey) o;
272 return Arrays.equals(Abyte, pk.getAbyte()) &&
273 edDsaSpec.equals(pk.getParams());
274 }
275 }