View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  package io.github.tonywasher.joceanus.gordianknot.impl.jca.agree;
19  
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
21  import io.github.tonywasher.joceanus.gordianknot.impl.core.agree.GordianCoreAgreementFactory;
22  import io.github.tonywasher.joceanus.gordianknot.impl.core.exc.GordianCryptoException;
23  import io.github.tonywasher.joceanus.gordianknot.impl.core.spec.agree.GordianCoreAgreementSpec;
24  import io.github.tonywasher.joceanus.gordianknot.impl.jca.agree.JcaAgreement.JcaAgreementBase;
25  import io.github.tonywasher.joceanus.gordianknot.impl.jca.keypair.JcaKeyPair.JcaPrivateKey;
26  import io.github.tonywasher.joceanus.gordianknot.impl.jca.keypair.JcaKeyPair.JcaPublicKey;
27  import org.bouncycastle.jcajce.spec.MQVParameterSpec;
28  
29  import javax.crypto.KeyAgreement;
30  import java.security.InvalidAlgorithmParameterException;
31  import java.security.InvalidKeyException;
32  
33  /**
34   * Jca MQV Agreement.
35   */
36  public class JcaMQVEngine
37          extends JcaAgreementBase {
38      /**
39       * Key Agreement.
40       */
41      private final KeyAgreement theAgreement;
42  
43      /**
44       * Constructor.
45       *
46       * @param pFactory   the security factory
47       * @param pSpec      the agreementSpec
48       * @param pAgreement the agreement
49       */
50      JcaMQVEngine(final GordianCoreAgreementFactory pFactory,
51                   final GordianCoreAgreementSpec pSpec,
52                   final KeyAgreement pAgreement) throws GordianException {
53          /* Initialize underlying class */
54          super(pFactory, pSpec);
55  
56          /* Store the agreement */
57          theAgreement = pAgreement;
58      }
59  
60      @Override
61      public void processClientHello() throws GordianException {
62          /* Protect against exceptions */
63          try {
64              /* Access keys */
65              final JcaPublicKey myClientPublic = (JcaPublicKey) getPublicKey(getClientKeyPair());
66              final JcaPublicKey myClientEphPublic = (JcaPublicKey) getPublicKey(getClientEphemeral());
67              final JcaPrivateKey myPrivate = (JcaPrivateKey) getPrivateKey(getServerKeyPair());
68              final JcaPublicKey myEphPublic = (JcaPublicKey) getPublicKey(getServerEphemeral());
69              final JcaPrivateKey myEphPrivate = (JcaPrivateKey) getPrivateKey(getServerEphemeral());
70  
71              /* Derive the secret */
72              final MQVParameterSpec myParams = new MQVParameterSpec(myEphPublic.getPublicKey(),
73                      myEphPrivate.getPrivateKey(), myClientEphPublic.getPublicKey(), getAdditional());
74              theAgreement.init(myPrivate.getPrivateKey(), myParams, getRandom());
75              theAgreement.doPhase(myClientPublic.getPublicKey(), true);
76              storeSecret(theAgreement.generateSecret());
77  
78          } catch (InvalidKeyException
79                   | InvalidAlgorithmParameterException e) {
80              throw new GordianCryptoException(JcaAgreement.ERR_AGREEMENT, e);
81          }
82      }
83  
84      @Override
85      public void processServerHello() throws GordianException {
86          /* Protect against exceptions */
87          try {
88              /* Access keys */
89              final JcaPublicKey myServerPublic = (JcaPublicKey) getPublicKey(getServerKeyPair());
90              final JcaPublicKey myServerEphPublic = (JcaPublicKey) getPublicKey(getServerEphemeral());
91              final JcaPrivateKey myPrivate = (JcaPrivateKey) getPrivateKey(getClientKeyPair());
92              final JcaPublicKey myEphPublic = (JcaPublicKey) getPublicKey(getClientEphemeral());
93              final JcaPrivateKey myEphPrivate = (JcaPrivateKey) getPrivateKey(getClientEphemeral());
94  
95              /* Derive the secret */
96              final MQVParameterSpec myParams = new MQVParameterSpec(myEphPublic.getPublicKey(),
97                      myEphPrivate.getPrivateKey(), myServerEphPublic.getPublicKey(), getAdditional());
98              theAgreement.init(myPrivate.getPrivateKey(), myParams);
99              theAgreement.doPhase(myServerPublic.getPublicKey(), true);
100             storeSecret(theAgreement.generateSecret());
101 
102         } catch (InvalidKeyException
103                  | InvalidAlgorithmParameterException e) {
104             throw new GordianCryptoException(JcaAgreement.ERR_AGREEMENT, e);
105         }
106     }
107 }