Debian Codegraph @

ext/ed25519_jruby/net/i2p/crypto/eddsa/spec/EdDSAPrivateKeySpec.java

raw symbols SCIP
1 /**
2 * EdDSA-Java by str4d
3 *
4 * To the extent possible under law, the person who associated CC0 with
5 * EdDSA-Java has waived all copyright and related or neighboring rights
6 * to EdDSA-Java.
7 *
8 * You should have received a copy of the CC0 legalcode along with this
9 * work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
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 * @author str4d
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 * @param seed the private key
34 * @param spec the parameter specification for this key
35 * @throws IllegalArgumentException if seed length is wrong or hash algorithm is unsupported
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 // H(k)
49 h = hash.digest(seed);
50
51 /*a = BigInteger.valueOf(2).pow(b-2);
52 for (int i=3;i<(b-2);i++) {
53 a = a.add(BigInteger.valueOf(2).pow(i).multiply(BigInteger.valueOf(Utils.bit(h,i))));
54 }*/
55 // Saves ~0.4ms per key when running signing tests.
56 // TODO: are these bitflips the same for any hash function?
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 * Initialize directly from the hash.
70 * getSeed() will return null if this constructor is used.
71 *
72 * @param spec the parameter specification for this key
73 * @param h the private key
74 * @throws IllegalArgumentException if hash length is wrong
75 * @since 0.1.1
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 * @return will be null if constructed directly from the private key
104 */
105 public byte[] getSeed() {
106 return seed;
107 }
108
109 /**
110 * @return the hash
111 */
112 public byte[] getH() {
113 return h;
114 }
115
116 /**
117 * @return the private key
118 */
119 public byte[] geta() {
120 return a;
121 }
122
123 /**
124 * @return the public key
125 */
126 public GroupElement getA() {
127 return A;
128 }
129
130 public EdDSAParameterSpec getParams() {
131 return spec;
132 }
133 }