summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/SignatureSpi.java
blob: 26811d18ea6ea1b54c6859fd6ed9e8b17ff3cbce (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package org.bouncycastle.jcajce.provider.asymmetric.ec;

import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.PublicKey;

import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DSA;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.NullDigest;
// BEGIN android-added
import org.bouncycastle.crypto.digests.AndroidDigestFactory;
// END android-added
// BEGIN android-removed
// import org.bouncycastle.crypto.digests.RIPEMD160Digest;
// import org.bouncycastle.crypto.digests.SHA1Digest;
// import org.bouncycastle.crypto.digests.SHA224Digest;
// import org.bouncycastle.crypto.digests.SHA256Digest;
// import org.bouncycastle.crypto.digests.SHA384Digest;
// import org.bouncycastle.crypto.digests.SHA512Digest;
// END android-removed
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.signers.ECDSASigner;
// BEGIN android-removed
// import org.bouncycastle.crypto.signers.ECNRSigner;
// import org.bouncycastle.crypto.signers.HMacDSAKCalculator;
// END android-removed
import org.bouncycastle.jcajce.provider.asymmetric.util.DSABase;
import org.bouncycastle.jcajce.provider.asymmetric.util.DSAEncoder;
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;

public class SignatureSpi
    extends DSABase
{
    SignatureSpi(Digest digest, DSA signer, DSAEncoder encoder)
    {
        super(digest, signer, encoder);
    }

    protected void engineInitVerify(PublicKey publicKey)
        throws InvalidKeyException
    {
        CipherParameters param = ECUtil.generatePublicKeyParameter(publicKey);

        digest.reset();
        signer.init(false, param);
    }

    protected void engineInitSign(
        PrivateKey privateKey)
        throws InvalidKeyException
    {
        CipherParameters param = ECUtil.generatePrivateKeyParameter(privateKey);

        digest.reset();

        if (appRandom != null)
        {
            signer.init(true, new ParametersWithRandom(param, appRandom));
        }
        else
        {
            signer.init(true, param);
        }
    }

    static public class ecDSA
        extends SignatureSpi
    {
        public ecDSA()
        {
            // BEGIN android-changed
            super(AndroidDigestFactory.getSHA1(), new ECDSASigner(), new StdDSAEncoder());
            // END android-changed
        }
    }

    // BEGIN android-removed
    // static public class ecDetDSA
    //     extends SignatureSpi
    // {
    //     public ecDetDSA()
    //     {
    //         super(new SHA1Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA1Digest())), new StdDSAEncoder());
    //     }
    // }
    // END android-removed

    static public class ecDSAnone
        extends SignatureSpi
    {
        public ecDSAnone()
        {
            super(new NullDigest(), new ECDSASigner(), new StdDSAEncoder());
        }
    }

    static public class ecDSA224
        extends SignatureSpi
    {
        public ecDSA224()
        {
            // BEGIN android-changed
            super(AndroidDigestFactory.getSHA224(), new ECDSASigner(), new StdDSAEncoder());
            // END android-changed
        }
    }

    // BEGIN android-removed
    // static public class ecDetDSA224
    //     extends SignatureSpi
    // {
    //     public ecDetDSA224()
    //     {
    //         super(new SHA224Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA224Digest())), new StdDSAEncoder());
    //     }
    // }
    // END android-removed

    static public class ecDSA256
        extends SignatureSpi
    {
        public ecDSA256()
        {
            // BEGIN android-changed
            super(AndroidDigestFactory.getSHA256(), new ECDSASigner(), new StdDSAEncoder());
            // END android-changed
        }
    }

    // BEGIN android-removed
    // static public class ecDetDSA256
    //     extends SignatureSpi
    // {
    //     public ecDetDSA256()
    //     {
    //         super(new SHA256Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())), new StdDSAEncoder());
    //     }
    // }
    // END android-removed

    static public class ecDSA384
        extends SignatureSpi
    {
        public ecDSA384()
        {
            // BEGIN android-changed
            super(AndroidDigestFactory.getSHA384(), new ECDSASigner(), new StdDSAEncoder());
            // END android-changed
        }
    }

    // BEGIN android-removed
    // static public class ecDetDSA384
    //     extends SignatureSpi
    // {
    //     public ecDetDSA384()
    //     {
    //         super(new SHA384Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA384Digest())), new StdDSAEncoder());
    //     }
    // }
    // END android-removed

    static public class ecDSA512
        extends SignatureSpi
    {
        public ecDSA512()
        {
            // BEGIN android-changed
            super(AndroidDigestFactory.getSHA512(), new ECDSASigner(), new StdDSAEncoder());
            // END android-changed
        }
    }

    // BEGIN android-removed
    // static public class ecDetDSA512
    //     extends SignatureSpi
    // {
    //     public ecDetDSA512()
    //     {
    //         super(new SHA512Digest(), new ECDSASigner(new HMacDSAKCalculator(new SHA512Digest())), new StdDSAEncoder());
    //     }
    // }
    //
    // static public class ecDSARipeMD160
    //     extends SignatureSpi
    // {
    //     public ecDSARipeMD160()
    //     {
    //         super(new RIPEMD160Digest(), new ECDSASigner(), new StdDSAEncoder());
    //     }
    // }
    //
    // static public class ecNR
    //     extends SignatureSpi
    // {
    //     public ecNR()
    //     {
    //         super(new SHA1Digest(), new ECNRSigner(), new StdDSAEncoder());
    //     }
    // }
    //
    // static public class ecNR224
    //     extends SignatureSpi
    // {
    //     public ecNR224()
    //     {
    //         super(new SHA224Digest(), new ECNRSigner(), new StdDSAEncoder());
    //     }
    // }
    //
    // static public class ecNR256
    //     extends SignatureSpi
    // {
    //     public ecNR256()
    //     {
    //         super(new SHA256Digest(), new ECNRSigner(), new StdDSAEncoder());
    //     }
    // }
    //
    // static public class ecNR384
    //     extends SignatureSpi
    // {
    //     public ecNR384()
    //     {
    //         super(new SHA384Digest(), new ECNRSigner(), new StdDSAEncoder());
    //     }
    // }
    //
    // static public class ecNR512
    //     extends SignatureSpi
    // {
    //     public ecNR512()
    //     {
    //         super(new SHA512Digest(), new ECNRSigner(), new StdDSAEncoder());
    //     }
    // }
    //
    // static public class ecCVCDSA
    //     extends SignatureSpi
    // {
    //     public ecCVCDSA()
    //     {
    //         super(new SHA1Digest(), new ECDSASigner(), new PlainDSAEncoder());
    //     }
    // }
    //
    // static public class ecCVCDSA224
    //     extends SignatureSpi
    // {
    //     public ecCVCDSA224()
    //     {
    //         super(new SHA224Digest(), new ECDSASigner(), new PlainDSAEncoder());
    //     }
    // }
    //
    // static public class ecCVCDSA256
    //     extends SignatureSpi
    // {
    //     public ecCVCDSA256()
    //     {
    //         super(new SHA256Digest(), new ECDSASigner(), new PlainDSAEncoder());
    //     }
    // }
    //
    // static public class ecCVCDSA384
    //     extends SignatureSpi
    // {
    //     public ecCVCDSA384()
    //     {
    //         super(new SHA384Digest(), new ECDSASigner(), new PlainDSAEncoder());
    //     }
    // }
    //
    // static public class ecCVCDSA512
    //     extends SignatureSpi
    // {
    //     public ecCVCDSA512()
    //     {
    //         super(new SHA512Digest(), new ECDSASigner(), new PlainDSAEncoder());
    //     }
    // }
    //
    // static public class ecPlainDSARP160
    //     extends SignatureSpi
    // {
    //     public ecPlainDSARP160()
    //     {
    //         super(new RIPEMD160Digest(), new ECDSASigner(), new PlainDSAEncoder());
    //     }
    // }
    // END android-removed

    private static class StdDSAEncoder
        implements DSAEncoder
    {
        public byte[] encode(
            BigInteger r,
            BigInteger s)
            throws IOException
        {
            ASN1EncodableVector v = new ASN1EncodableVector();

            v.add(new ASN1Integer(r));
            v.add(new ASN1Integer(s));

            return new DERSequence(v).getEncoded(ASN1Encoding.DER);
        }

        public BigInteger[] decode(
            byte[] encoding)
            throws IOException
        {
            ASN1Sequence s = (ASN1Sequence)ASN1Primitive.fromByteArray(encoding);
            BigInteger[] sig = new BigInteger[2];

            sig[0] = ASN1Integer.getInstance(s.getObjectAt(0)).getValue();
            sig[1] = ASN1Integer.getInstance(s.getObjectAt(1)).getValue();

            return sig;
        }
    }

    // BEGIN android-removed
    // private static class PlainDSAEncoder
    //     implements DSAEncoder
    // {
    //     public byte[] encode(
    //         BigInteger r,
    //         BigInteger s)
    //         throws IOException
    //     {
    //         byte[] first = makeUnsigned(r);
    //         byte[] second = makeUnsigned(s);
    //         byte[] res;
    //
    //         if (first.length > second.length)
    //         {
    //             res = new byte[first.length * 2];
    //         }
    //         else
    //         {
    //             res = new byte[second.length * 2];
    //         }
    //
    //         System.arraycopy(first, 0, res, res.length / 2 - first.length, first.length);
    //         System.arraycopy(second, 0, res, res.length - second.length, second.length);
    //
    //         return res;
    //     }
    //
    //
    //     private byte[] makeUnsigned(BigInteger val)
    //     {
    //         byte[] res = val.toByteArray();
    //
    //         if (res[0] == 0)
    //         {
    //             byte[] tmp = new byte[res.length - 1];
    //
    //             System.arraycopy(res, 1, tmp, 0, tmp.length);
    //
    //             return tmp;
    //         }
    //
    //         return res;
    //     }
    //
    //     public BigInteger[] decode(
    //         byte[] encoding)
    //         throws IOException
    //     {
    //         BigInteger[] sig = new BigInteger[2];
    //
    //         byte[] first = new byte[encoding.length / 2];
    //         byte[] second = new byte[encoding.length / 2];
    //
    //         System.arraycopy(encoding, 0, first, 0, first.length);
    //         System.arraycopy(encoding, first.length, second, 0, second.length);
    //
    //         sig[0] = new BigInteger(1, first);
    //         sig[1] = new BigInteger(1, second);
    //
    //         return sig;
    //     }
    // }
    // END android-removed
}