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.sign;
19  
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
21  import io.github.tonywasher.joceanus.gordianknot.api.keypair.GordianKeyPair;
22  import io.github.tonywasher.joceanus.gordianknot.api.sign.GordianNewSignParams;
23  import io.github.tonywasher.joceanus.gordianknot.api.sign.spec.GordianSignatureSpec;
24  import io.github.tonywasher.joceanus.gordianknot.impl.core.base.GordianBaseFactory;
25  import io.github.tonywasher.joceanus.gordianknot.impl.core.spec.keypair.GordianCoreEdwardsSpec;
26  import io.github.tonywasher.joceanus.gordianknot.impl.core.spec.keypair.GordianCoreKeyPairSpec;
27  import io.github.tonywasher.joceanus.gordianknot.impl.jca.keypair.JcaKeyPair;
28  
29  /**
30   * EdDSA signature.
31   */
32  public class JcaEdDSASignature
33          extends JcaSignature {
34      /**
35       * Constructor.
36       *
37       * @param pFactory       the factory
38       * @param pSignatureSpec the signatureSpec
39       */
40      JcaEdDSASignature(final GordianBaseFactory pFactory,
41                        final GordianSignatureSpec pSignatureSpec) {
42          /* Initialise class */
43          super(pFactory, pSignatureSpec);
44      }
45  
46      @Override
47      public void initForSigning(final GordianNewSignParams pParams) throws GordianException {
48          /* Determine the required signer */
49          final GordianKeyPair myPair = pParams.getKeyPair();
50          JcaKeyPair.checkKeyPair(myPair);
51          final String mySignName = getAlgorithmForKeyPair(myPair);
52          setSigner(getJavaSignature(mySignName, false));
53  
54          /* pass on call */
55          super.initForSigning(pParams);
56      }
57  
58      @Override
59      public void initForVerify(final GordianNewSignParams pParams) throws GordianException {
60          /* Determine the required signer */
61          final GordianKeyPair myPair = pParams.getKeyPair();
62          JcaKeyPair.checkKeyPair(myPair);
63          final String mySignName = getAlgorithmForKeyPair(myPair);
64          setSigner(getJavaSignature(mySignName, false));
65  
66          /* pass on call */
67          super.initForVerify(pParams);
68      }
69  
70      /**
71       * Obtain algorithmName for keyPair.
72       *
73       * @param pKeyPair the keyPair
74       * @return the name
75       */
76      private static String getAlgorithmForKeyPair(final GordianKeyPair pKeyPair) {
77          /* Determine the required signer */
78          final GordianCoreKeyPairSpec mySpec = (GordianCoreKeyPairSpec) pKeyPair.getKeyPairSpec();
79          final GordianCoreEdwardsSpec myEdwards = mySpec.getEdwardsSpec();
80          final boolean is25519 = myEdwards.is25519();
81  
82          /* Build the algorithm */
83          return is25519 ? "Ed25519" : "Ed448";
84      }
85  }