aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java
blob: 86cc95b85cf6144c2e8f74f123eac6a692b865e3 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package cz.crcs.ectester.standalone.libs.jni;

import cz.crcs.ectester.common.util.ECUtil;

import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECParameterSpec;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public abstract class NativeSignatureSpi extends SignatureSpi {
    private ECPublicKey verifyKey;
    private ECPrivateKey signKey;
    private ECParameterSpec params;

    private ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    @Override
    protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
        if (!(publicKey instanceof ECPublicKey)) {
            throw new InvalidKeyException
                    ("Key must be an instance of ECPublicKey");
        }
        verifyKey = (ECPublicKey) publicKey;
        params = verifyKey.getParams();
        buffer.reset();
    }

    @Override
    protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
        if (!(privateKey instanceof ECPrivateKey)) {
            throw new InvalidKeyException
                    ("Key must be an instance of ECPrivateKey");
        }
        signKey = (ECPrivateKey) privateKey;
        params = signKey.getParams();
        buffer.reset();
    }

    @Override
    protected void engineUpdate(byte b) throws SignatureException {
        buffer.write(b);
    }

    @Override
    protected void engineUpdate(byte[] b, int off, int len) throws SignatureException {
        buffer.write(b, off, len);
    }

    @Override
    protected byte[] engineSign() throws SignatureException {
        return sign(buffer.toByteArray(), ECUtil.toByteArray(signKey.getS(), params.getCurve().getField().getFieldSize()), params);
    }

    @Override
    protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
        return verify(sigBytes, buffer.toByteArray(), ECUtil.toX962Uncompressed(verifyKey.getW(), params), params);
    }

    @Override
    @Deprecated
    protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
        throw new UnsupportedOperationException("setParameter() not supported");
    }

    @Override
    @Deprecated
    protected Object engineGetParameter(String param) throws InvalidParameterException {
        throw new UnsupportedOperationException("getParameter() not supported");
    }

    abstract byte[] sign(byte[] data, byte[] privkey, ECParameterSpec params);

    abstract boolean verify(byte[] signature, byte[] data, byte[] pubkey, ECParameterSpec params);

    public static class TomCryptRaw extends NativeSignatureSpi {

        @Override
        native byte[] sign(byte[] data, byte[] privkey, ECParameterSpec params);

        @Override
        native boolean verify(byte[] signature, byte[] data, byte[] pubkey, ECParameterSpec params);
    }

    public abstract static class Botan extends NativeSignatureSpi {
        private String type;

        public Botan(String type) {
            this.type = type;
        }

        @Override
        native byte[] sign(byte[] data, byte[] privkey, ECParameterSpec params);

        @Override
        native boolean verify(byte[] signature, byte[] data, byte[] pubkey, ECParameterSpec params);
    }

    public static class BotanECDSAwithNONE extends Botan {

        public BotanECDSAwithNONE() {
            super("NONEwithECDSA");
        }
    }

    public static class BotanECDSAwithSHA1 extends Botan {

        public BotanECDSAwithSHA1() {
            super("SHA1withECDSA");
        }
    }

    public static class BotanECDSAwithSHA224 extends Botan {

        public BotanECDSAwithSHA224() {
            super("SHA224withECDSA");
        }
    }

    public static class BotanECDSAwithSHA256 extends Botan {

        public BotanECDSAwithSHA256() {
            super("SHA256withECDSA");
        }
    }

    public static class BotanECDSAwithSHA384 extends Botan {

        public BotanECDSAwithSHA384() {
            super("SHA384withECDSA");
        }
    }

    public static class BotanECDSAwithSHA512 extends Botan {

        public BotanECDSAwithSHA512() {
            super("SHA512withECDSA");
        }
    }

    public static class BotanECKCDSAwithNONE extends Botan {

        public BotanECKCDSAwithNONE() {
            super("NONEwithECKCDSA");
        }
    }

    public static class BotanECKCDSAwithSHA1 extends Botan {

        public BotanECKCDSAwithSHA1() {
            super("SHA1withECKCDSA");
        }
    }

    public static class BotanECKCDSAwithSHA224 extends Botan {

        public BotanECKCDSAwithSHA224() {
            super("SHA224withECKCDSA");
        }
    }

    public static class BotanECKCDSAwithSHA256 extends Botan {

        public BotanECKCDSAwithSHA256() {
            super("SHA256withECKCDSA");
        }
    }

    public static class BotanECKCDSAwithSHA384 extends Botan {

        public BotanECKCDSAwithSHA384() {
            super("SHA384withECKCDSA");
        }
    }

    public static class BotanECKCDSAwithSHA512 extends Botan {

        public BotanECKCDSAwithSHA512() {
            super("SHA512withECKCDSA");
        }
    }

    public static class BotanECGDSAwithNONE extends Botan {

        public BotanECGDSAwithNONE() {
            super("NONEwithECGDSA");
        }
    }

    public static class BotanECGDSAwithSHA1 extends Botan {

        public BotanECGDSAwithSHA1() {
            super("SHA1withECGDSA");
        }
    }

    public static class BotanECGDSAwithSHA224 extends Botan {

        public BotanECGDSAwithSHA224() {
            super("SHA224withECGDSA");
        }
    }

    public static class BotanECGDSAwithSHA256 extends Botan {

        public BotanECGDSAwithSHA256() {
            super("SHA256withECGDSA");
        }
    }

    public static class BotanECGDSAwithSHA384 extends Botan {

        public BotanECGDSAwithSHA384() {
            super("SHA384withECGDSA");
        }
    }

    public static class BotanECGDSAwithSHA512 extends Botan {

        public BotanECGDSAwithSHA512() {
            super("SHA512withECGDSA");
        }
    }

    public abstract static class Cryptopp extends NativeSignatureSpi {
        private String type;

        public Cryptopp(String type) {
            this.type = type;
        }

        @Override
        native byte[] sign(byte[] data, byte[] privkey, ECParameterSpec params);

        @Override
        native boolean verify(byte[] signature, byte[] data, byte[] pubkey, ECParameterSpec params);
    }

    public static class CryptoppECDSAwithSHA1 extends Cryptopp {

        public CryptoppECDSAwithSHA1() {
            super("SHA1withECDSA");
        }
    }

    public static class CryptoppECDSAwithSHA224 extends Cryptopp {

        public CryptoppECDSAwithSHA224() {
            super("SHA224withECDSA");
        }
    }

    public static class CryptoppECDSAwithSHA256 extends Cryptopp {

        public CryptoppECDSAwithSHA256() {
            super("SHA256withECDSA");
        }
    }

    public static class CryptoppECDSAwithSHA384 extends Cryptopp {

        public CryptoppECDSAwithSHA384() {
            super("SHA384withECDSA");
        }
    }

    public static class CryptoppECDSAwithSHA512 extends Cryptopp {

        public CryptoppECDSAwithSHA512() {
            super("SHA512withECDSA");
        }
    }

    public abstract static class Openssl extends NativeSignatureSpi {
        private String type;

        public Openssl(String type) {
            this.type = type;
        }

        @Override
        native byte[] sign(byte[] data, byte[] privkey, ECParameterSpec params);

        @Override
        native boolean verify(byte[] signature, byte[] data, byte[] pubkey, ECParameterSpec params);
    }

    public static class OpensslECDSAwithNONE extends Openssl {

        public OpensslECDSAwithNONE() {
            super("NONEwithECDSA");
        }
    }
}