| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package net.i2p.crypto.eddsa; |
| 13 |
|
| 14 |
import java.io.ByteArrayOutputStream; |
| 15 |
import java.nio.ByteBuffer; |
| 16 |
import java.security.InvalidAlgorithmParameterException; |
| 17 |
import java.security.InvalidKeyException; |
| 18 |
import java.security.MessageDigest; |
| 19 |
import java.security.NoSuchAlgorithmException; |
| 20 |
import java.security.PrivateKey; |
| 21 |
import java.security.PublicKey; |
| 22 |
import java.security.Signature; |
| 23 |
import java.security.SignatureException; |
| 24 |
import java.security.spec.AlgorithmParameterSpec; |
| 25 |
import java.security.spec.InvalidKeySpecException; |
| 26 |
import java.security.spec.X509EncodedKeySpec; |
| 27 |
import java.util.Arrays; |
| 28 |
|
| 29 |
import net.i2p.crypto.eddsa.math.Curve; |
| 30 |
import net.i2p.crypto.eddsa.math.GroupElement; |
| 31 |
import net.i2p.crypto.eddsa.math.ScalarOps; |
| 32 |
import sun.security.x509.X509Key; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public final class EdDSAEngine extends Signature { |
| 70 |
public static final String SIGNATURE_ALGORITHM = "NONEwithEdDSA"; |
| 71 |
|
| 72 |
private MessageDigest digest; |
| 73 |
private ByteArrayOutputStream baos; |
| 74 |
private EdDSAKey key; |
| 75 |
private boolean oneShotMode; |
| 76 |
private byte[] oneShotBytes; |
| 77 |
private int oneShotOffset; |
| 78 |
private int oneShotLength; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public static final AlgorithmParameterSpec ONE_SHOT_MODE = new OneShotSpec(); |
| 88 |
|
| 89 |
private static class OneShotSpec implements AlgorithmParameterSpec {} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public EdDSAEngine() { |
| 95 |
super(SIGNATURE_ALGORITHM); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public EdDSAEngine(MessageDigest digest) { |
| 103 |
this(); |
| 104 |
this.digest = digest; |
| 105 |
} |
| 106 |
|
| 107 |
private void reset() { |
| 108 |
if (digest != null) |
| 109 |
digest.reset(); |
| 110 |
if (baos != null) |
| 111 |
baos.reset(); |
| 112 |
oneShotMode = false; |
| 113 |
oneShotBytes = null; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException { |
| 118 |
reset(); |
| 119 |
if (privateKey instanceof EdDSAPrivateKey) { |
| 120 |
EdDSAPrivateKey privKey = (EdDSAPrivateKey) privateKey; |
| 121 |
key = privKey; |
| 122 |
|
| 123 |
if (digest == null) { |
| 124 |
|
| 125 |
try { |
| 126 |
digest = MessageDigest.getInstance(key.getParams().getHashAlgorithm()); |
| 127 |
} catch (NoSuchAlgorithmException e) { |
| 128 |
throw new InvalidKeyException("cannot get required digest " + key.getParams().getHashAlgorithm() + " for private key."); |
| 129 |
} |
| 130 |
} else if (!key.getParams().getHashAlgorithm().equals(digest.getAlgorithm())) |
| 131 |
throw new InvalidKeyException("Key hash algorithm does not match chosen digest"); |
| 132 |
digestInitSign(privKey); |
| 133 |
} else { |
| 134 |
throw new InvalidKeyException("cannot identify EdDSA private key: " + privateKey.getClass()); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
private void digestInitSign(EdDSAPrivateKey privKey) { |
| 139 |
|
| 140 |
|
| 141 |
int b = privKey.getParams().getCurve().getField().getb(); |
| 142 |
digest.update(privKey.getH(), b/8, b/4 - b/8); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException { |
| 147 |
reset(); |
| 148 |
if (publicKey instanceof EdDSAPublicKey) { |
| 149 |
key = (EdDSAPublicKey) publicKey; |
| 150 |
|
| 151 |
if (digest == null) { |
| 152 |
|
| 153 |
try { |
| 154 |
digest = MessageDigest.getInstance(key.getParams().getHashAlgorithm()); |
| 155 |
} catch (NoSuchAlgorithmException e) { |
| 156 |
throw new InvalidKeyException("cannot get required digest " + key.getParams().getHashAlgorithm() + " for private key."); |
| 157 |
} |
| 158 |
} else if (!key.getParams().getHashAlgorithm().equals(digest.getAlgorithm())) |
| 159 |
throw new InvalidKeyException("Key hash algorithm does not match chosen digest"); |
| 160 |
} else if (publicKey instanceof X509Key) { |
| 161 |
|
| 162 |
|
| 163 |
EdDSAPublicKey parsedPublicKey; |
| 164 |
try { |
| 165 |
parsedPublicKey = new EdDSAPublicKey(new X509EncodedKeySpec(publicKey.getEncoded())); |
| 166 |
} catch (InvalidKeySpecException ex) { |
| 167 |
throw new InvalidKeyException("cannot handle X.509 EdDSA public key: " + publicKey.getAlgorithm()); |
| 168 |
} |
| 169 |
engineInitVerify(parsedPublicKey); |
| 170 |
} else { |
| 171 |
throw new InvalidKeyException("cannot identify EdDSA public key: " + publicKey.getClass()); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@Override |
| 179 |
protected void engineUpdate(byte b) throws SignatureException { |
| 180 |
if (oneShotMode) |
| 181 |
throw new SignatureException("unsupported in one-shot mode"); |
| 182 |
if (baos == null) |
| 183 |
baos = new ByteArrayOutputStream(256); |
| 184 |
baos.write(b); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
@Override |
| 191 |
protected void engineUpdate(byte[] b, int off, int len) |
| 192 |
throws SignatureException { |
| 193 |
if (oneShotMode) { |
| 194 |
if (oneShotBytes != null) |
| 195 |
throw new SignatureException("update() already called"); |
| 196 |
oneShotBytes = b; |
| 197 |
oneShotOffset = off; |
| 198 |
oneShotLength = len; |
| 199 |
} else { |
| 200 |
if (baos == null) |
| 201 |
baos = new ByteArrayOutputStream(256); |
| 202 |
baos.write(b, off, len); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
protected byte[] engineSign() throws SignatureException { |
| 208 |
try { |
| 209 |
return x_engineSign(); |
| 210 |
} finally { |
| 211 |
reset(); |
| 212 |
|
| 213 |
|
| 214 |
EdDSAPrivateKey privKey = (EdDSAPrivateKey) key; |
| 215 |
digestInitSign(privKey); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
private byte[] x_engineSign() throws SignatureException { |
| 220 |
Curve curve = key.getParams().getCurve(); |
| 221 |
ScalarOps sc = key.getParams().getScalarOps(); |
| 222 |
byte[] a = ((EdDSAPrivateKey) key).geta(); |
| 223 |
|
| 224 |
byte[] message; |
| 225 |
int offset, length; |
| 226 |
if (oneShotMode) { |
| 227 |
if (oneShotBytes == null) |
| 228 |
throw new SignatureException("update() not called first"); |
| 229 |
message = oneShotBytes; |
| 230 |
offset = oneShotOffset; |
| 231 |
length = oneShotLength; |
| 232 |
} else { |
| 233 |
if (baos == null) |
| 234 |
message = new byte[0]; |
| 235 |
else |
| 236 |
message = baos.toByteArray(); |
| 237 |
offset = 0; |
| 238 |
length = message.length; |
| 239 |
} |
| 240 |
|
| 241 |
digest.update(message, offset, length); |
| 242 |
byte[] r = digest.digest(); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
r = sc.reduce(r); |
| 247 |
|
| 248 |
|
| 249 |
GroupElement R = key.getParams().getB().scalarMultiply(r); |
| 250 |
byte[] Rbyte = R.toByteArray(); |
| 251 |
|
| 252 |
|
| 253 |
digest.update(Rbyte); |
| 254 |
digest.update(((EdDSAPrivateKey) key).getAbyte()); |
| 255 |
digest.update(message, offset, length); |
| 256 |
byte[] h = digest.digest(); |
| 257 |
h = sc.reduce(h); |
| 258 |
byte[] S = sc.multiplyAndAdd(h, a, r); |
| 259 |
|
| 260 |
|
| 261 |
int b = curve.getField().getb(); |
| 262 |
ByteBuffer out = ByteBuffer.allocate(b/4); |
| 263 |
out.put(Rbyte).put(S); |
| 264 |
return out.array(); |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
protected boolean engineVerify(byte[] sigBytes) throws SignatureException { |
| 269 |
try { |
| 270 |
return x_engineVerify(sigBytes); |
| 271 |
} finally { |
| 272 |
reset(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
private boolean x_engineVerify(byte[] sigBytes) throws SignatureException { |
| 277 |
Curve curve = key.getParams().getCurve(); |
| 278 |
int b = curve.getField().getb(); |
| 279 |
if (sigBytes.length != b/4) |
| 280 |
throw new SignatureException("signature length is wrong"); |
| 281 |
|
| 282 |
|
| 283 |
digest.update(sigBytes, 0, b/8); |
| 284 |
digest.update(((EdDSAPublicKey) key).getAbyte()); |
| 285 |
|
| 286 |
byte[] message; |
| 287 |
int offset, length; |
| 288 |
if (oneShotMode) { |
| 289 |
if (oneShotBytes == null) |
| 290 |
throw new SignatureException("update() not called first"); |
| 291 |
message = oneShotBytes; |
| 292 |
offset = oneShotOffset; |
| 293 |
length = oneShotLength; |
| 294 |
} else { |
| 295 |
if (baos == null) |
| 296 |
message = new byte[0]; |
| 297 |
else |
| 298 |
message = baos.toByteArray(); |
| 299 |
offset = 0; |
| 300 |
length = message.length; |
| 301 |
} |
| 302 |
digest.update(message, offset, length); |
| 303 |
byte[] h = digest.digest(); |
| 304 |
|
| 305 |
|
| 306 |
h = key.getParams().getScalarOps().reduce(h); |
| 307 |
|
| 308 |
byte[] Sbyte = Arrays.copyOfRange(sigBytes, b/8, b/4); |
| 309 |
|
| 310 |
GroupElement R = key.getParams().getB().doubleScalarMultiplyVariableTime( |
| 311 |
((EdDSAPublicKey) key).getNegativeA(), h, Sbyte); |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
byte[] Rcalc = R.toByteArray(); |
| 316 |
for (int i = 0; i < Rcalc.length; i++) { |
| 317 |
if (Rcalc[i] != sigBytes[i]) |
| 318 |
return false; |
| 319 |
} |
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public byte[] signOneShot(byte[] data) throws SignatureException { |
| 340 |
return signOneShot(data, 0, data.length); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public byte[] signOneShot(byte[] data, int off, int len) throws SignatureException { |
| 362 |
oneShotMode = true; |
| 363 |
update(data, off, len); |
| 364 |
return sign(); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public boolean verifyOneShot(byte[] data, byte[] signature) throws SignatureException { |
| 385 |
return verifyOneShot(data, 0, data.length, signature, 0, signature.length); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public boolean verifyOneShot(byte[] data, int off, int len, byte[] signature) throws SignatureException { |
| 408 |
return verifyOneShot(data, off, len, signature, 0, signature.length); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public boolean verifyOneShot(byte[] data, byte[] signature, int sigoff, int siglen) throws SignatureException { |
| 431 |
return verifyOneShot(data, 0, data.length, signature, sigoff, siglen); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public boolean verifyOneShot(byte[] data, int off, int len, byte[] signature, int sigoff, int siglen) throws SignatureException { |
| 456 |
oneShotMode = true; |
| 457 |
update(data, off, len); |
| 458 |
return verify(signature, sigoff, siglen); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
@Override |
| 466 |
protected void engineSetParameter(AlgorithmParameterSpec spec) throws InvalidAlgorithmParameterException { |
| 467 |
if (spec.equals(ONE_SHOT_MODE)) { |
| 468 |
if (oneShotBytes != null || (baos != null && baos.size() > 0)) |
| 469 |
throw new InvalidAlgorithmParameterException("update() already called"); |
| 470 |
oneShotMode = true; |
| 471 |
} else { |
| 472 |
super.engineSetParameter(spec); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
@Override |
| 480 |
protected void engineSetParameter(String param, Object value) { |
| 481 |
throw new UnsupportedOperationException("engineSetParameter unsupported"); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
@Override |
| 488 |
protected Object engineGetParameter(String param) { |
| 489 |
throw new UnsupportedOperationException("engineSetParameter unsupported"); |
| 490 |
} |
| 491 |
} |