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.bc.sign;
19  
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
21  import io.github.tonywasher.joceanus.gordianknot.api.sign.GordianNewSignParams;
22  import io.github.tonywasher.joceanus.gordianknot.api.sign.spec.GordianSignatureSpec;
23  import io.github.tonywasher.joceanus.gordianknot.impl.bc.keypair.BouncyEllipticKeyPair.BouncyECPrivateKey;
24  import io.github.tonywasher.joceanus.gordianknot.impl.bc.keypair.BouncyEllipticKeyPair.BouncyECPublicKey;
25  import io.github.tonywasher.joceanus.gordianknot.impl.bc.keypair.BouncyKeyPair;
26  import io.github.tonywasher.joceanus.gordianknot.impl.bc.sign.BouncySignature.BouncyDERCoder;
27  import io.github.tonywasher.joceanus.gordianknot.impl.core.base.GordianBaseFactory;
28  import org.bouncycastle.crypto.DSA;
29  import org.bouncycastle.crypto.params.ParametersWithRandom;
30  
31  import java.math.BigInteger;
32  
33  /**
34   * EC signer.
35   */
36  public class BouncyECSignature
37          extends BouncyDigestSignature {
38      /**
39       * The Signer.
40       */
41      private final DSA theSigner;
42  
43      /**
44       * The Coder.
45       */
46      private final BouncyDERCoder theCoder;
47  
48      /**
49       * Constructor.
50       *
51       * @param pFactory the factory
52       * @param pSpec    the signatureSpec.
53       * @throws GordianException on error
54       */
55      BouncyECSignature(final GordianBaseFactory pFactory,
56                        final GordianSignatureSpec pSpec) throws GordianException {
57          /* Initialise underlying class */
58          super(pFactory, pSpec);
59  
60          /* Create the signer and Coder */
61          theSigner = BouncySignature.getDSASigner(pFactory, pSpec);
62          theCoder = new BouncyDERCoder();
63      }
64  
65      @Override
66      public void initForSigning(final GordianNewSignParams pParams) throws GordianException {
67          /* Initialise detail */
68          super.initForSigning(pParams);
69          final BouncyKeyPair myPair = getKeyPair();
70          BouncyKeyPair.checkKeyPair(myPair);
71  
72          /* Initialise and set the signer */
73          final BouncyECPrivateKey myPrivate = (BouncyECPrivateKey) myPair.getPrivateKey();
74          final ParametersWithRandom myParms = new ParametersWithRandom(myPrivate.getPrivateKey(), getRandom());
75          theSigner.init(true, myParms);
76      }
77  
78      @Override
79      public void initForVerify(final GordianNewSignParams pParams) throws GordianException {
80          /* Initialise detail */
81          super.initForVerify(pParams);
82          final BouncyKeyPair myPair = getKeyPair();
83          BouncyKeyPair.checkKeyPair(myPair);
84  
85          /* Initialise and set the signer */
86          final BouncyECPublicKey myPublic = (BouncyECPublicKey) myPair.getPublicKey();
87          theSigner.init(false, myPublic.getPublicKey());
88      }
89  
90      @Override
91      public byte[] sign() throws GordianException {
92          /* Check that we are in signing mode */
93          checkMode(GordianSignatureMode.SIGN);
94  
95          /* Sign the message */
96          final BigInteger[] myValues = theSigner.generateSignature(getDigest());
97          return theCoder.dsaEncode(myValues[0], myValues[1]);
98      }
99  
100     @Override
101     public boolean verify(final byte[] pSignature) throws GordianException {
102         /* Check that we are in verify mode */
103         checkMode(GordianSignatureMode.VERIFY);
104 
105         /* Verify the message */
106         final BigInteger[] myValues = theCoder.dsaDecode(pSignature);
107         return theSigner.verifySignature(getDigest(), myValues[0], myValues[1]);
108     }
109 }