4040public class EdDSAPublicKey implements EdDSAKey , PublicKey {
4141 private static final long serialVersionUID = 9837459837498475L ;
4242 private final GroupElement A ;
43- private final GroupElement Aneg ;
43+ private GroupElement Aneg = null ;
4444 private final byte [] Abyte ;
4545 private final EdDSAParameterSpec edDsaSpec ;
4646
@@ -52,14 +52,13 @@ public class EdDSAPublicKey implements EdDSAKey, PublicKey {
5252
5353 public EdDSAPublicKey (EdDSAPublicKeySpec spec ) {
5454 this .A = spec .getA ();
55- this .Aneg = spec .getNegativeA ();
5655 this .Abyte = this .A .toByteArray ();
5756 this .edDsaSpec = spec .getParams ();
5857 }
5958
6059 public EdDSAPublicKey (X509EncodedKeySpec spec ) throws InvalidKeySpecException {
6160 this (new EdDSAPublicKeySpec (decode (spec .getEncoded ()),
62- EdDSANamedCurveTable .getByName ( EdDSANamedCurveTable . ED_25519 ) ));
61+ EdDSANamedCurveTable .ED_25519_CURVE_SPEC ));
6362 }
6463
6564 @ Override
@@ -78,10 +77,10 @@ public String getFormat() {
7877 * This implements the following specs:
7978 *<ul><li>
8079 * General encoding: https://tools.ietf.org/html/draft-ietf-curdle-pkix-04
81- *</li></ li>
80+ *</li><li>
8281 * Key encoding: https://tools.ietf.org/html/rfc8032
8382 *</li></ul>
84- *</p>< p>
83+ *<p>
8584 * For keys in older formats, decoding and then re-encoding is sufficient to
8685 * migrate them to the canonical encoding.
8786 *</p>
@@ -113,7 +112,7 @@ public String getFormat() {
113112 */
114113 @ Override
115114 public byte [] getEncoded () {
116- if (!edDsaSpec .equals (EdDSANamedCurveTable .getByName ( EdDSANamedCurveTable . ED_25519 ) ))
115+ if (!edDsaSpec .equals (EdDSANamedCurveTable .ED_25519_CURVE_SPEC ))
117116 return null ;
118117 int totlen = 12 + Abyte .length ;
119118 byte [] rv = new byte [totlen ];
@@ -250,7 +249,13 @@ public GroupElement getA() {
250249 }
251250
252251 public GroupElement getNegativeA () {
253- return Aneg ;
252+ // Only read Aneg once, otherwise read re-ordering might occur between here and return. Requires all GroupElement's fields to be final.
253+ GroupElement ourAneg = Aneg ;
254+ if (ourAneg == null ) {
255+ ourAneg = A .negate ();
256+ Aneg = ourAneg ;
257+ }
258+ return ourAneg ;
254259 }
255260
256261 public byte [] getAbyte () {
0 commit comments