summaryrefslogtreecommitdiff
path: root/bcprov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java
blob: 6e1eda2d291c501d7ac5219f209dedda37f45a0e (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package org.bouncycastle.jcajce.provider.asymmetric.ec;

import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;

import org.bouncycastle.asn1.x9.X9IntegerConverter;
import org.bouncycastle.crypto.BasicAgreement;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DerivationFunction;
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
// BEGIN android-removed
// import org.bouncycastle.crypto.agreement.ECDHCBasicAgreement;
// import org.bouncycastle.crypto.agreement.ECMQVBasicAgreement;
// import org.bouncycastle.crypto.agreement.kdf.ConcatenationKDFGenerator;
// import org.bouncycastle.crypto.generators.KDF2BytesGenerator;
// END android-removed
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
// BEGIN android-removed
// import org.bouncycastle.crypto.params.MQVPrivateParameters;
// import org.bouncycastle.crypto.params.MQVPublicParameters;
// import org.bouncycastle.crypto.util.DigestFactory;
// END android-removed
import org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi;
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
// BEGIN android-removed
// import org.bouncycastle.jcajce.spec.MQVParameterSpec;
// END android-removed
import org.bouncycastle.jcajce.spec.UserKeyingMaterialSpec;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
// BEGIN android-removed
// import org.bouncycastle.jce.interfaces.MQVPrivateKey;
// import org.bouncycastle.jce.interfaces.MQVPublicKey;
// END android-removed

/**
 * Diffie-Hellman key agreement using elliptic curve keys, ala IEEE P1363
 * both the simple one, and the simple one with cofactors are supported.
 *
 * Also, MQV key agreement per SEC-1
 */
public class KeyAgreementSpi
    extends BaseAgreementSpi
{
    private static final X9IntegerConverter converter = new X9IntegerConverter();

    private String                 kaAlgorithm;

    private ECDomainParameters     parameters;
    private BasicAgreement         agreement;

    // BEGIN android-removed
    // private MQVParameterSpec       mqvParameters;
    // END android-removed
    private BigInteger             result;

    protected KeyAgreementSpi(
        String kaAlgorithm,
        BasicAgreement agreement,
        DerivationFunction kdf)
    {
        super(kaAlgorithm, kdf);

        this.kaAlgorithm = kaAlgorithm;
        this.agreement = agreement;
    }

    protected byte[] bigIntToBytes(
        BigInteger    r)
    {
        return converter.integerToBytes(r, converter.getByteLength(parameters.getCurve()));
    }

    protected Key engineDoPhase(
        Key     key,
        boolean lastPhase) 
        throws InvalidKeyException, IllegalStateException
    {
        if (parameters == null)
        {
            throw new IllegalStateException(kaAlgorithm + " not initialised.");
        }

        if (!lastPhase)
        {
            throw new IllegalStateException(kaAlgorithm + " can only be between two parties.");
        }

        CipherParameters pubKey;        
        // BEGIN android-removed
        // if (agreement instanceof ECMQVBasicAgreement)
        // {
        //     if (!(key instanceof MQVPublicKey))
        //     {
        //         ECPublicKeyParameters staticKey = (ECPublicKeyParameters)
        //             ECUtils.generatePublicKeyParameter((PublicKey)key);
        //         ECPublicKeyParameters ephemKey = (ECPublicKeyParameters)
        //             ECUtils.generatePublicKeyParameter(mqvParameters.getOtherPartyEphemeralKey());
        //
        //         pubKey = new MQVPublicParameters(staticKey, ephemKey);
        //     }
        //     else
        //     {
        //         MQVPublicKey mqvPubKey = (MQVPublicKey)key;
        //         ECPublicKeyParameters staticKey = (ECPublicKeyParameters)
        //             ECUtils.generatePublicKeyParameter(mqvPubKey.getStaticKey());
        //         ECPublicKeyParameters ephemKey = (ECPublicKeyParameters)
        //             ECUtils.generatePublicKeyParameter(mqvPubKey.getEphemeralKey());
        //
        //         pubKey = new MQVPublicParameters(staticKey, ephemKey);
        //     }
        // }
        // else
        // END android-removed
        {
            if (!(key instanceof PublicKey))
            {
                throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
                    + getSimpleName(ECPublicKey.class) + " for doPhase");
            }

            pubKey = ECUtils.generatePublicKeyParameter((PublicKey)key);
        }

        try
        {
            result = agreement.calculateAgreement(pubKey);
        } catch (final Exception e) {
            throw new InvalidKeyException("calculation failed: " + e.getMessage())
            {
                public Throwable getCause()
                            {
                                return e;
                            }
            };
        }
        return null;
    }

    protected void engineInit(
        Key                     key,
        AlgorithmParameterSpec  params,
        SecureRandom            random) 
        throws InvalidKeyException, InvalidAlgorithmParameterException
    {
        // BEGIN android-changed
        if (params != null && !(params instanceof UserKeyingMaterialSpec))
        // END android-changed
        {
            throw new InvalidAlgorithmParameterException("No algorithm parameters supported");
        }

        initFromKey(key, params);
    }

    protected void engineInit(
        Key             key,
        SecureRandom    random)
        throws InvalidKeyException
    {
        initFromKey(key, null);
    }

    private void initFromKey(Key key, AlgorithmParameterSpec parameterSpec)
        throws InvalidKeyException
    {
        // BEGIN android-removed
        // if (agreement instanceof ECMQVBasicAgreement)
        // {
        //     mqvParameters = null;
        //     if (!(key instanceof MQVPrivateKey) && !(parameterSpec instanceof MQVParameterSpec))
        //     {
        //         throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
        //             + getSimpleName(MQVParameterSpec.class) + " for initialisation");
        //     }
        //
        //     ECPrivateKeyParameters staticPrivKey;
        //     ECPrivateKeyParameters ephemPrivKey;
        //     ECPublicKeyParameters ephemPubKey;
        //     if (key instanceof MQVPrivateKey)
        //     {
        //         MQVPrivateKey mqvPrivKey = (MQVPrivateKey)key;
        //         staticPrivKey = (ECPrivateKeyParameters)
        //             ECUtil.generatePrivateKeyParameter(mqvPrivKey.getStaticPrivateKey());
        //         ephemPrivKey = (ECPrivateKeyParameters)
        //             ECUtil.generatePrivateKeyParameter(mqvPrivKey.getEphemeralPrivateKey());
        //
        //         ephemPubKey = null;
        //         if (mqvPrivKey.getEphemeralPublicKey() != null)
        //         {
        //             ephemPubKey = (ECPublicKeyParameters)
        //                 ECUtils.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey());
        //         }
        //     }
        //     else
        //     {
        //         MQVParameterSpec mqvParameterSpec = (MQVParameterSpec)parameterSpec;
        //
        //         staticPrivKey = (ECPrivateKeyParameters)
        //             ECUtil.generatePrivateKeyParameter((PrivateKey)key);
        //         ephemPrivKey = (ECPrivateKeyParameters)
        //             ECUtil.generatePrivateKeyParameter(mqvParameterSpec.getEphemeralPrivateKey());
        //
        //         ephemPubKey = null;
        //         if (mqvParameterSpec.getEphemeralPublicKey() != null)
        //         {
        //             ephemPubKey = (ECPublicKeyParameters)
        //                 ECUtils.generatePublicKeyParameter(mqvParameterSpec.getEphemeralPublicKey());
        //         }
        //         mqvParameters = mqvParameterSpec;
        //         ukmParameters = mqvParameterSpec.getUserKeyingMaterial();
        //     }
        //
        //     MQVPrivateParameters localParams = new MQVPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
        //     this.parameters = staticPrivKey.getParameters();
        //
        //     // TODO Validate that all the keys are using the same parameters?
        //
        //     agreement.init(localParams);
        // }
        // else
        // END android-removed
        {
            if (!(key instanceof PrivateKey))
            {
                throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
                    + getSimpleName(ECPrivateKey.class) + " for initialisation");
            }

            ECPrivateKeyParameters privKey = (ECPrivateKeyParameters)ECUtil.generatePrivateKeyParameter((PrivateKey)key);
            this.parameters = privKey.getParameters();
            ukmParameters = (parameterSpec instanceof UserKeyingMaterialSpec) ? ((UserKeyingMaterialSpec)parameterSpec).getUserKeyingMaterial() : null;
            agreement.init(privKey);
        }
    }

    private static String getSimpleName(Class clazz)
    {
        String fullName = clazz.getName();

        return fullName.substring(fullName.lastIndexOf('.') + 1);
    }


    protected byte[] calcSecret()
    {
        return bigIntToBytes(result);
    }

    public static class DH
        extends KeyAgreementSpi
    {
        public DH()
        {
            super("ECDH", new ECDHBasicAgreement(), null);
        }
    }

    // BEGIN android-removed
    // public static class DHC
    //     extends KeyAgreementSpi
    // {
    //     public DHC()
    //     {
    //         super("ECDHC", new ECDHCBasicAgreement(), null);
    //     }
    // }

    // public static class MQV
    //     extends KeyAgreementSpi
    // {
    //     public MQV()
    //     {
    //         super("ECMQV", new ECMQVBasicAgreement(), null);
    //     }
    // }

    // public static class DHwithSHA1KDF
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA1KDF()
    //     {
    //         super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1()));
    //     }
    // }

    // public static class DHwithSHA1KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA1KDFAndSharedInfo()
    //     {
    //         super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1()));
    //     }
    // }

    // public static class CDHwithSHA1KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public CDHwithSHA1KDFAndSharedInfo()
    //     {
    //         super("ECCDHwithSHA1KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1()));
    //     }
    // }

    // public static class DHwithSHA224KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA224KDFAndSharedInfo()
    //     {
    //         super("ECDHwithSHA224KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA224()));
    //     }
    // }

    // public static class CDHwithSHA224KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public CDHwithSHA224KDFAndSharedInfo()
    //     {
    //         super("ECCDHwithSHA224KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA224()));
    //     }
    // }

    // public static class DHwithSHA256KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA256KDFAndSharedInfo()
    //     {
    //         super("ECDHwithSHA256KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA256()));
    //     }
    // }

    // public static class CDHwithSHA256KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public CDHwithSHA256KDFAndSharedInfo()
    //     {
    //         super("ECCDHwithSHA256KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA256()));
    //     }
    // }

    // public static class DHwithSHA384KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA384KDFAndSharedInfo()
    //     {
    //         super("ECDHwithSHA384KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA384()));
    //     }
    // }

    // public static class CDHwithSHA384KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public CDHwithSHA384KDFAndSharedInfo()
    //     {
    //         super("ECCDHwithSHA384KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA384()));
    //     }
    // }

    // public static class DHwithSHA512KDFAndSharedInfo
    //      extends KeyAgreementSpi
    //  {
    //      public DHwithSHA512KDFAndSharedInfo()
    //      {
    //          super("ECDHwithSHA512KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA512()));
    //      }
    //  }

    //  public static class CDHwithSHA512KDFAndSharedInfo
    //      extends KeyAgreementSpi
    //  {
    //      public CDHwithSHA512KDFAndSharedInfo()
    //      {
    //          super("ECCDHwithSHA512KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA512()));
    //      }
    //  }

    // public static class MQVwithSHA1KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA1KDFAndSharedInfo()
    //     {
    //         super("ECMQVwithSHA1KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA1()));
    //     }
    // }

    // public static class MQVwithSHA224KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA224KDFAndSharedInfo()
    //     {
    //         super("ECMQVwithSHA224KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA224()));
    //     }
    // }

    // public static class MQVwithSHA256KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA256KDFAndSharedInfo()
    //     {
    //         super("ECMQVwithSHA256KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA256()));
    //     }
    // }

    // public static class MQVwithSHA384KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA384KDFAndSharedInfo()
    //     {
    //         super("ECMQVwithSHA384KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA384()));
    //     }
    // }

    // public static class MQVwithSHA512KDFAndSharedInfo
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA512KDFAndSharedInfo()
    //     {
    //         super("ECMQVwithSHA512KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(DigestFactory.createSHA512()));
    //     }
    // }

    // public static class DHwithSHA1CKDF
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA1CKDF()
    //     {
    //         super("ECDHwithSHA1CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA1()));
    //     }
    // }

    // public static class DHwithSHA256CKDF
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA256CKDF()
    //     {
    //         super("ECDHwithSHA256CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA256()));
    //     }
    // }

    // public static class DHwithSHA384CKDF
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA384CKDF()
    //     {
    //         super("ECDHwithSHA384CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA384()));
    //     }
    // }

    // public static class DHwithSHA512CKDF
    //     extends KeyAgreementSpi
    // {
    //     public DHwithSHA512CKDF()
    //     {
    //         super("ECDHwithSHA512CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA512()));
    //     }
    // }

    // public static class MQVwithSHA1CKDF
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA1CKDF()
    //     {
    //         super("ECMQVwithSHA1CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA1()));
    //     }
    // }

    // public static class MQVwithSHA224CKDF
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA224CKDF()
    //     {
    //         super("ECMQVwithSHA224CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA224()));
    //     }
    // }

    // public static class MQVwithSHA256CKDF
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA256CKDF()
    //     {
    //         super("ECMQVwithSHA256CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA256()));
    //     }
    // }

    // public static class MQVwithSHA384CKDF
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA384CKDF()
    //     {
    //         super("ECMQVwithSHA384CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA384()));
    //     }
    // }

    // public static class MQVwithSHA512CKDF
    //     extends KeyAgreementSpi
    // {
    //     public MQVwithSHA512CKDF()
    //     {
    //         super("ECMQVwithSHA512CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(DigestFactory.createSHA512()));
    //     }
    // }
    // END android-removed
}