| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 122 |
rv[idx++] = 0x30; |
| 123 |
rv[idx++] = (byte) (totlen - 2); |
| 124 |
|
| 125 |
|
| 126 |
rv[idx++] = 0x30; |
| 127 |
rv[idx++] = 5; |
| 128 |
|
| 129 |
|
| 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 |
|
| 136 |
|
| 137 |
rv[idx++] = 0x03; |
| 138 |
rv[idx++] = (byte) (1 + Abyte.length); |
| 139 |
rv[idx++] = 0; |
| 140 |
System.arraycopy(Abyte, 0, rv, idx, Abyte.length); |
| 141 |
return rv; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private static byte[] decode(byte[] d) throws InvalidKeySpecException { |
| 164 |
try { |
| 165 |
|
| 166 |
|
| 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 |
|
| 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 |
|
| 186 |
|
| 187 |
if (d.length != totlen) { |
| 188 |
throw new InvalidKeySpecException("invalid key spec length"); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 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++; |
| 206 |
|
| 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 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 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 |
} |