| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package net.i2p.crypto.eddsa.spec; |
| 13 |
|
| 14 |
import java.security.MessageDigest; |
| 15 |
import java.security.NoSuchAlgorithmException; |
| 16 |
import java.security.spec.KeySpec; |
| 17 |
import java.util.Arrays; |
| 18 |
|
| 19 |
import net.i2p.crypto.eddsa.math.GroupElement; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class EdDSAPrivateKeySpec implements KeySpec { |
| 26 |
private final byte[] seed; |
| 27 |
private final byte[] h; |
| 28 |
private final byte[] a; |
| 29 |
private final GroupElement A; |
| 30 |
private final EdDSAParameterSpec spec; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public EdDSAPrivateKeySpec(byte[] seed, EdDSAParameterSpec spec) { |
| 38 |
if (seed.length != spec.getCurve().getField().getb()/8) |
| 39 |
throw new IllegalArgumentException("seed length is wrong"); |
| 40 |
|
| 41 |
this.spec = spec; |
| 42 |
this.seed = seed; |
| 43 |
|
| 44 |
try { |
| 45 |
MessageDigest hash = MessageDigest.getInstance(spec.getHashAlgorithm()); |
| 46 |
int b = spec.getCurve().getField().getb(); |
| 47 |
|
| 48 |
|
| 49 |
h = hash.digest(seed); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
h[0] &= 248; |
| 58 |
h[(b/8)-1] &= 63; |
| 59 |
h[(b/8)-1] |= 64; |
| 60 |
a = Arrays.copyOfRange(h, 0, b/8); |
| 61 |
|
| 62 |
A = spec.getB().scalarMultiply(a); |
| 63 |
} catch (NoSuchAlgorithmException e) { |
| 64 |
throw new IllegalArgumentException("Unsupported hash algorithm"); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public EdDSAPrivateKeySpec(EdDSAParameterSpec spec, byte[] h) { |
| 78 |
if (h.length != spec.getCurve().getField().getb()/4) |
| 79 |
throw new IllegalArgumentException("hash length is wrong"); |
| 80 |
|
| 81 |
this.seed = null; |
| 82 |
this.h = h; |
| 83 |
this.spec = spec; |
| 84 |
int b = spec.getCurve().getField().getb(); |
| 85 |
|
| 86 |
h[0] &= 248; |
| 87 |
h[(b/8)-1] &= 63; |
| 88 |
h[(b/8)-1] |= 64; |
| 89 |
a = Arrays.copyOfRange(h, 0, b/8); |
| 90 |
|
| 91 |
A = spec.getB().scalarMultiply(a); |
| 92 |
} |
| 93 |
|
| 94 |
public EdDSAPrivateKeySpec(byte[] seed, byte[] h, byte[] a, GroupElement A, EdDSAParameterSpec spec) { |
| 95 |
this.seed = seed; |
| 96 |
this.h = h; |
| 97 |
this.a = a; |
| 98 |
this.A = A; |
| 99 |
this.spec = spec; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public byte[] getSeed() { |
| 106 |
return seed; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public byte[] getH() { |
| 113 |
return h; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public byte[] geta() { |
| 120 |
return a; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public GroupElement getA() { |
| 127 |
return A; |
| 128 |
} |
| 129 |
|
| 130 |
public EdDSAParameterSpec getParams() { |
| 131 |
return spec; |
| 132 |
} |
| 133 |
} |