Debian Codegraph @

ext/ed25519_jruby/net/i2p/crypto/eddsa/spec/EdDSAParameterSpec.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.AlgorithmParameterSpec;
17
18 import net.i2p.crypto.eddsa.math.Curve;
19 import net.i2p.crypto.eddsa.math.GroupElement;
20 import net.i2p.crypto.eddsa.math.ScalarOps;
21
22 import java.io.Serializable;
23
24 /**
25 * Parameter specification for an EdDSA algorithm.
26 * @author str4d
27 *
28 */
29 public class EdDSAParameterSpec implements AlgorithmParameterSpec, Serializable {
30 private static final long serialVersionUID = 8274987108472012L;
31 private final Curve curve;
32 private final String hashAlgo;
33 private final ScalarOps sc;
34 private final GroupElement B;
35
36 /**
37 * @param curve the curve
38 * @param hashAlgo the JCA string for the hash algorithm
39 * @param sc the parameter L represented as ScalarOps
40 * @param B the parameter B
41 * @throws IllegalArgumentException if hash algorithm is unsupported or length is wrong
42 */
43 public EdDSAParameterSpec(Curve curve, String hashAlgo,
44 ScalarOps sc, GroupElement B) {
45 try {
46 MessageDigest hash = MessageDigest.getInstance(hashAlgo);
47 // EdDSA hash function must produce 2b-bit output
48 if (curve.getField().getb()/4 != hash.getDigestLength())
49 throw new IllegalArgumentException("Hash output is not 2b-bit");
50 } catch (NoSuchAlgorithmException e) {
51 throw new IllegalArgumentException("Unsupported hash algorithm");
52 }
53
54 this.curve = curve;
55 this.hashAlgo = hashAlgo;
56 this.sc = sc;
57 this.B = B;
58 }
59
60 public Curve getCurve() {
61 return curve;
62 }
63
64 public String getHashAlgorithm() {
65 return hashAlgo;
66 }
67
68 public ScalarOps getScalarOps() {
69 return sc;
70 }
71
72 /**
73 * @return the base (generator)
74 */
75 public GroupElement getB() {
76 return B;
77 }
78
79 @Override
80 public int hashCode() {
81 return hashAlgo.hashCode() ^
82 curve.hashCode() ^
83 B.hashCode();
84 }
85
86 @Override
87 public boolean equals(Object o) {
88 if (o == this)
89 return true;
90 if (!(o instanceof EdDSAParameterSpec))
91 return false;
92 EdDSAParameterSpec s = (EdDSAParameterSpec) o;
93 return hashAlgo.equals(s.getHashAlgorithm()) &&
94 curve.equals(s.getCurve()) &&
95 B.equals(s.getB());
96 }
97 }