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.GordianBlake3Digest;
20  import io.github.tonywasher.joceanus.gordianknot.impl.ext.params.GordianBlake3Parameters;
21  import org.bouncycastle.crypto.CipherParameters;
22  import org.bouncycastle.crypto.Mac;
23  import org.bouncycastle.crypto.Xof;
24  import org.bouncycastle.crypto.params.KeyParameter;
25  
26  /**
27   * Bouncy implementation of Blake3Mac.
28   */
29  public class GordianBlake3Mac
30          implements Mac, Xof {
31      /**
32       * Digest.
33       */
34      private final GordianBlake3Digest theDigest;
35  
36      /**
37       * Create a blake2Mac with the specified digest.
38       *
39       * @param pDigest the base digest.
40       */
41      public GordianBlake3Mac(final GordianBlake3Digest pDigest) {
42          /* Store the digest */
43          theDigest = pDigest;
44      }
45  
46      @Override
47      public String getAlgorithmName() {
48          return theDigest.getAlgorithmName() + "Mac";
49      }
50  
51      @Override
52      public void init(final CipherParameters pParams) {
53          CipherParameters myParams = pParams;
54          if (myParams instanceof KeyParameter keyParam) {
55              myParams = GordianBlake3Parameters.key(keyParam.getKey());
56          }
57          if (!(myParams instanceof GordianBlake3Parameters myBlakeParams)) {
58              throw new IllegalArgumentException("Invalid parameter passed to Blake3Mac init - "
59                      + pParams.getClass().getName());
60          }
61          if (myBlakeParams.getKey() == null) {
62              throw new IllegalArgumentException("Blake3Mac requires a key parameter.");
63          }
64  
65          /* Configure the digest */
66          theDigest.init(myBlakeParams);
67      }
68  
69      @Override
70      public int getMacSize() {
71          return theDigest.getDigestSize();
72      }
73  
74      @Override
75      public void update(final byte in) {
76          theDigest.update(in);
77      }
78  
79      @Override
80      public void update(final byte[] in, final int inOff, final int len) {
81          theDigest.update(in, inOff, len);
82      }
83  
84      @Override
85      public int doFinal(final byte[] out, final int outOff) {
86          return theDigest.doFinal(out, outOff);
87      }
88  
89      @Override
90      public void reset() {
91          theDigest.reset();
92      }
93  
94      @Override
95      public int doFinal(final byte[] out, final int outOff, final int outLen) {
96          return theDigest.doFinal(out, outOff, outLen);
97      }
98  
99      @Override
100     public int doOutput(final byte[] out, final int outOff, final int outLen) {
101         return theDigest.doOutput(out, outOff, outLen);
102     }
103 
104     @Override
105     public int getByteLength() {
106         return theDigest.getByteLength();
107     }
108 
109     @Override
110     public int getDigestSize() {
111         return theDigest.getDigestSize();
112     }
113 }