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