| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package net.i2p.crypto.eddsa; |
| 13 |
|
| 14 |
import java.security.PrivateKey; |
| 15 |
import java.security.spec.InvalidKeySpecException; |
| 16 |
import java.security.spec.PKCS8EncodedKeySpec; |
| 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.EdDSAPrivateKeySpec; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class EdDSAPrivateKey implements EdDSAKey, PrivateKey { |
| 41 |
private static final long serialVersionUID = 23495873459878957L; |
| 42 |
private final byte[] seed; |
| 43 |
private final byte[] h; |
| 44 |
private final byte[] a; |
| 45 |
private final GroupElement A; |
| 46 |
private final byte[] Abyte; |
| 47 |
private final EdDSAParameterSpec edDsaSpec; |
| 48 |
|
| 49 |
|
| 50 |
private static final int OID_OLD = 100; |
| 51 |
private static final int OID_ED25519 = 112; |
| 52 |
private static final int OID_BYTE = 11; |
| 53 |
private static final int IDLEN_BYTE = 6; |
| 54 |
|
| 55 |
public EdDSAPrivateKey(EdDSAPrivateKeySpec spec) { |
| 56 |
this.seed = spec.getSeed(); |
| 57 |
this.h = spec.getH(); |
| 58 |
this.a = spec.geta(); |
| 59 |
this.A = spec.getA(); |
| 60 |
this.Abyte = this.A.toByteArray(); |
| 61 |
this.edDsaSpec = spec.getParams(); |
| 62 |
} |
| 63 |
|
| 64 |
public EdDSAPrivateKey(PKCS8EncodedKeySpec spec) throws InvalidKeySpecException { |
| 65 |
this(new EdDSAPrivateKeySpec(decode(spec.getEncoded()), |
| 66 |
EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519))); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public String getAlgorithm() { |
| 71 |
return KEY_ALGORITHM; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public String getFormat() { |
| 76 |
return "PKCS#8"; |
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@Override |
| 138 |
public byte[] getEncoded() { |
| 139 |
if (!edDsaSpec.equals(EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519))) |
| 140 |
return null; |
| 141 |
if (seed == null) |
| 142 |
return null; |
| 143 |
int totlen = 16 + seed.length; |
| 144 |
byte[] rv = new byte[totlen]; |
| 145 |
int idx = 0; |
| 146 |
|
| 147 |
rv[idx++] = 0x30; |
| 148 |
rv[idx++] = (byte) (totlen - 2); |
| 149 |
|
| 150 |
rv[idx++] = 0x02; |
| 151 |
rv[idx++] = 1; |
| 152 |
|
| 153 |
rv[idx++] = 0; |
| 154 |
|
| 155 |
|
| 156 |
rv[idx++] = 0x30; |
| 157 |
rv[idx++] = 5; |
| 158 |
|
| 159 |
|
| 160 |
rv[idx++] = 0x06; |
| 161 |
rv[idx++] = 3; |
| 162 |
rv[idx++] = (1 * 40) + 3; |
| 163 |
rv[idx++] = 101; |
| 164 |
rv[idx++] = (byte) OID_ED25519; |
| 165 |
|
| 166 |
|
| 167 |
rv[idx++] = 0x04; |
| 168 |
rv[idx++] = (byte) (2 + seed.length); |
| 169 |
|
| 170 |
rv[idx++] = 0x04; |
| 171 |
rv[idx++] = (byte) seed.length; |
| 172 |
|
| 173 |
System.arraycopy(seed, 0, rv, idx, seed.length); |
| 174 |
return rv; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private static byte[] decode(byte[] d) throws InvalidKeySpecException { |
| 196 |
try { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
int totlen = 48; |
| 201 |
int idlen = 5; |
| 202 |
int doid = d[OID_BYTE]; |
| 203 |
if (doid == OID_OLD) { |
| 204 |
totlen = 49; |
| 205 |
idlen = 8; |
| 206 |
} else if (doid == OID_ED25519) { |
| 207 |
|
| 208 |
if (d[IDLEN_BYTE] == 7) { |
| 209 |
totlen = 50; |
| 210 |
idlen = 7; |
| 211 |
} |
| 212 |
} else { |
| 213 |
throw new InvalidKeySpecException("unsupported key spec"); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if (d.length != totlen) { |
| 220 |
throw new InvalidKeySpecException("invalid key spec length"); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
int idx = 0; |
| 227 |
if (d[idx++] != 0x30 || |
| 228 |
d[idx++] != (totlen - 2) || |
| 229 |
d[idx++] != 0x02 || |
| 230 |
d[idx++] != 1 || |
| 231 |
d[idx++] != 0 || |
| 232 |
d[idx++] != 0x30 || |
| 233 |
d[idx++] != idlen || |
| 234 |
d[idx++] != 0x06 || |
| 235 |
d[idx++] != 3 || |
| 236 |
d[idx++] != (1 * 40) + 3 || |
| 237 |
d[idx++] != 101) { |
| 238 |
throw new InvalidKeySpecException("unsupported key spec"); |
| 239 |
} |
| 240 |
idx++; |
| 241 |
|
| 242 |
if (doid == OID_OLD) { |
| 243 |
if (d[idx++] != 0x0a || |
| 244 |
d[idx++] != 1 || |
| 245 |
d[idx++] != 1) { |
| 246 |
throw new InvalidKeySpecException("unsupported key spec"); |
| 247 |
} |
| 248 |
} else { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
if (idlen == 7) { |
| 259 |
if (d[idx++] != 0x05 || |
| 260 |
d[idx++] != 0) { |
| 261 |
throw new InvalidKeySpecException("unsupported key spec"); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if (d[idx++] != 0x04 || |
| 266 |
d[idx++] != 34) { |
| 267 |
throw new InvalidKeySpecException("unsupported key spec"); |
| 268 |
} |
| 269 |
} |
| 270 |
if (d[idx++] != 0x04 || |
| 271 |
d[idx++] != 32) { |
| 272 |
throw new InvalidKeySpecException("unsupported key spec"); |
| 273 |
} |
| 274 |
byte[] rv = new byte[32]; |
| 275 |
System.arraycopy(d, idx, rv, 0, 32); |
| 276 |
return rv; |
| 277 |
} catch (IndexOutOfBoundsException ioobe) { |
| 278 |
throw new InvalidKeySpecException(ioobe); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public EdDSAParameterSpec getParams() { |
| 284 |
return edDsaSpec; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public byte[] getSeed() { |
| 292 |
return seed; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public byte[] getH() { |
| 299 |
return h; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public byte[] geta() { |
| 306 |
return a; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public GroupElement getA() { |
| 313 |
return A; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public byte[] getAbyte() { |
| 320 |
return Abyte; |
| 321 |
} |
| 322 |
|
| 323 |
@Override |
| 324 |
public int hashCode() { |
| 325 |
return Arrays.hashCode(seed); |
| 326 |
} |
| 327 |
|
| 328 |
@Override |
| 329 |
public boolean equals(Object o) { |
| 330 |
if (o == this) |
| 331 |
return true; |
| 332 |
if (!(o instanceof EdDSAPrivateKey)) |
| 333 |
return false; |
| 334 |
EdDSAPrivateKey pk = (EdDSAPrivateKey) o; |
| 335 |
return Arrays.equals(seed, pk.getSeed()) && |
| 336 |
edDsaSpec.equals(pk.getParams()); |
| 337 |
} |
| 338 |
} |