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.GordianBlake2Xof;
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.Xof;
25  import org.bouncycastle.crypto.params.KeyParameter;
26  
27  /**
28   * Bouncy implementation of Blake2Mac.
29   */
30  public class GordianBlake2XMac
31          implements Mac, Xof {
32      /**
33       * Digest.
34       */
35      private final GordianBlake2Xof theXof;
36  
37      /**
38       * Create a blake2XMac with the specified xof.
39       *
40       * @param pXof the base xof.
41       */
42      public GordianBlake2XMac(final GordianBlake2Xof pXof) {
43          theXof = pXof;
44      }
45  
46      @Override
47      public String getAlgorithmName() {
48          return theXof.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 = new GordianBlake2ParametersBuilder()
56                      .setKey(keyParam.getKey())
57                      .setMaxOutputLen(-1L)
58                      .build();
59          }
60          if (!(myParams instanceof GordianBlake2Parameters myBlakeParams)) {
61              throw new IllegalArgumentException("Invalid parameter passed to Blake2XMac init - "
62                      + pParams.getClass().getName());
63          }
64          if (myBlakeParams.getKey() == null) {
65              throw new IllegalArgumentException("Blake2XMac requires a key parameter.");
66          }
67  
68          /* Configure the xof */
69          theXof.init(myBlakeParams);
70      }
71  
72      @Override
73      public int getMacSize() {
74          return theXof.getDigestSize();
75      }
76  
77      @Override
78      public void update(final byte in) {
79          theXof.update(in);
80      }
81  
82      @Override
83      public void update(final byte[] in, final int inOff, final int len) {
84          theXof.update(in, inOff, len);
85      }
86  
87      @Override
88      public int doFinal(final byte[] out, final int outOff) {
89          return theXof.doFinal(out, outOff);
90      }
91  
92      @Override
93      public int doFinal(final byte[] out, final int outOff, final int outLen) {
94          return theXof.doFinal(out, outOff, outLen);
95      }
96  
97      @Override
98      public int doOutput(final byte[] out, final int outOff, final int outLen) {
99          return theXof.doOutput(out, outOff, outLen);
100     }
101 
102     @Override
103     public int getByteLength() {
104         return theXof.getByteLength();
105     }
106 
107     @Override
108     public int getDigestSize() {
109         return theXof.getDigestSize();
110     }
111 
112     @Override
113     public void reset() {
114         theXof.reset();
115     }
116 }