aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/test/base/KeyAgreementTestable.java
blob: 1382c281c831dbf5b940b2113178f8f845e59621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
70
71
72
73
74
75
76
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cz.crcs.ectester.standalone.test.base;

import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.ECParameterSpec;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class KeyAgreementTestable extends StandaloneTestable<KeyAgreementTestable.KeyAgreementStage> {
    private KeyAgreement ka;
    private ECPrivateKey privateKey;
    private ECPublicKey publicKey;
    private KeyGeneratorTestable kgtPrivate;
    private KeyGeneratorTestable kgtPublic;
    private AlgorithmParameterSpec spec;
    private String keyAlgo;
    private byte[] secret;
    private SecretKey derived;

    public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey) {
        this.ka = ka;
        this.privateKey = privateKey;
        this.publicKey = publicKey;
    }

    public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey, String keyAlgo) {
        this(ka, privateKey, publicKey);
        this.keyAlgo = keyAlgo;
    }

    public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey, ECParameterSpec spec) {
        this(ka, privateKey, publicKey);
        this.spec = spec;
    }

    public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey, ECParameterSpec spec, String keyAlgo) {
        this(ka, privateKey, publicKey, spec);
        this.keyAlgo = keyAlgo;
    }

    public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable kgt, ECPrivateKey privateKey, ECParameterSpec spec) {
        this(ka, privateKey, null, spec);
        this.kgtPublic = kgt;
    }

    public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable kgt, ECPrivateKey privateKey, ECParameterSpec spec, String keyAlgo) {
        this(ka, kgt, privateKey, spec);
        this.keyAlgo = keyAlgo;
    }

    public KeyAgreementTestable(KeyAgreement ka, ECPublicKey publicKey, KeyGeneratorTestable kgt, ECParameterSpec spec) {
        this(ka, null, publicKey, spec);
        this.kgtPrivate = kgt;
    }

    public KeyAgreementTestable(KeyAgreement ka, ECPublicKey publicKey, KeyGeneratorTestable kgt, ECParameterSpec spec, String keyAlgo) {
        this(ka, publicKey, kgt, spec);
        this.keyAlgo = keyAlgo;
    }

    public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable privKgt, KeyGeneratorTestable pubKgt, ECParameterSpec spec) {
        this(ka, (ECPrivateKey) null, null, spec);
        this.kgtPrivate = privKgt;
        this.kgtPublic = pubKgt;
    }

    public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable privKgt, KeyGeneratorTestable pubKgt, ECParameterSpec spec, String keyAlgo) {
        this(ka, privKgt, pubKgt, spec);
        this.keyAlgo = keyAlgo;
    }

    public String getKeyAlgorithm() {
        return keyAlgo;
    }

    public KeyAgreement getKa() {
        return ka;
    }

    public ECPublicKey getPublicKey() {
        return publicKey;
    }

    public ECPrivateKey getPrivateKey() {
        return privateKey;
    }

    public byte[] getSecret() {
        if (!hasRun) {
            return null;
        }
        return secret;
    }

    public SecretKey getDerivedKey() {
        if (!hasRun) {
            return null;
        }
        return derived;
    }

    @Override
    public void run() {
        try {
            stage = KeyAgreementStage.GetPrivate;
            if (kgtPrivate != null) {
                privateKey = (ECPrivateKey) kgtPrivate.getKeyPair().getPrivate();
            }

            stage = KeyAgreementStage.GetPublic;
            if (kgtPublic != null) {
                publicKey = (ECPublicKey) kgtPublic.getKeyPair().getPublic();
            }

            stage = KeyAgreementStage.Init;
            try {
                if (spec != null) {
                    ka.init(privateKey, spec);
                } else {
                    ka.init(privateKey);
                }
            } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
                failOnException(e);
                return;
            }

            stage = KeyAgreementStage.DoPhase;
            try {
                ka.doPhase(publicKey, true);
            } catch (IllegalStateException | InvalidKeyException e) {
                failOnException(e);
                return;
            }

            stage = KeyAgreementStage.GenerateSecret;
            try {
                if (keyAlgo != null) {
                    derived = ka.generateSecret(keyAlgo);
                    secret = derived.getEncoded();
                } else {
                    secret = ka.generateSecret();
                }
            } catch (IllegalStateException | UnsupportedOperationException e) {
                failOnException(e);
                return;
            }

            ok = true;
        } catch (Exception ex) {
            ok = false;
            error = true;
            errorCause = ex;
        }
        hasRun = true;
    }

    public enum KeyAgreementStage {
        GetPrivate,
        GetPublic,
        Init,
        DoPhase,
        GenerateSecret
    }
}