View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 2012-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  package io.github.tonywasher.joceanus.gordianknot.impl.ext.macs;
18  
19  import io.github.tonywasher.joceanus.gordianknot.impl.ext.digests.GordianBlake2Base;
20  import io.github.tonywasher.joceanus.gordianknot.impl.ext.params.GordianBlake2Parameters;
21  import io.github.tonywasher.joceanus.gordianknot.impl.ext.params.GordianBlake2Parameters.GordianBlake2ParametersBuilder;
22  import org.bouncycastle.crypto.CipherParameters;
23  import org.bouncycastle.crypto.Mac;
24  import org.bouncycastle.crypto.params.KeyParameter;
25  
26  /**
27   * Bouncy implementation of Blake2Mac.
28   */
29  public class GordianBlake2Mac
30          implements Mac {
31      /**
32       * Digest.
33       */
34      private final GordianBlake2Base theDigest;
35  
36      /**
37       * Create a blake2Mac with the specified digest.
38       *
39       * @param pDigest the base digest.
40       */
41      public GordianBlake2Mac(final GordianBlake2Base pDigest) {
42          theDigest = pDigest;
43      }
44  
45      @Override
46      public String getAlgorithmName() {
47          return theDigest.getAlgorithmName() + "Mac";
48      }
49  
50      @Override
51      public void init(final CipherParameters pParams) {
52          CipherParameters myParams = pParams;
53          if (myParams instanceof KeyParameter) {
54              myParams = new GordianBlake2ParametersBuilder()
55                      .setKey(((KeyParameter) myParams).getKey())
56                      .build();
57          }
58          if (!(myParams instanceof GordianBlake2Parameters)) {
59              throw new IllegalArgumentException("Invalid parameter passed to Blake2Mac init - "
60                      + pParams.getClass().getName());
61          }
62          final GordianBlake2Parameters myBlakeParams = (GordianBlake2Parameters) myParams;
63          if (myBlakeParams.getKey() == null) {
64              throw new IllegalArgumentException("Blake2Mac requires a key parameter.");
65          }
66  
67          /* Configure the digest */
68          theDigest.init(myBlakeParams);
69      }
70  
71      @Override
72      public int getMacSize() {
73          return theDigest.getDigestSize();
74      }
75  
76      @Override
77      public void update(final byte in) {
78          theDigest.update(in);
79      }
80  
81      @Override
82      public void update(final byte[] in, final int inOff, final int len) {
83          theDigest.update(in, inOff, len);
84      }
85  
86      @Override
87      public int doFinal(final byte[] out, final int outOff) {
88          return theDigest.doFinal(out, outOff);
89      }
90  
91      @Override
92      public void reset() {
93          theDigest.reset();
94      }
95  }