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) {
55              myParams = new GordianBlake2ParametersBuilder()
56                      .setKey(((KeyParameter) myParams).getKey())
57                      .setMaxOutputLen(-1L)
58                      .build();
59          }
60          if (!(myParams instanceof GordianBlake2Parameters)) {
61              throw new IllegalArgumentException("Invalid parameter passed to Blake2XMac init - "
62                      + pParams.getClass().getName());
63          }
64          final GordianBlake2Parameters myBlakeParams = (GordianBlake2Parameters) myParams;
65          if (myBlakeParams.getKey() == null) {
66              throw new IllegalArgumentException("Blake2XMac requires a key parameter.");
67          }
68  
69          /* Configure the xof */
70          theXof.init(myBlakeParams);
71      }
72  
73      @Override
74      public int getMacSize() {
75          return theXof.getDigestSize();
76      }
77  
78      @Override
79      public void update(final byte in) {
80          theXof.update(in);
81      }
82  
83      @Override
84      public void update(final byte[] in, final int inOff, final int len) {
85          theXof.update(in, inOff, len);
86      }
87  
88      @Override
89      public int doFinal(final byte[] out, final int outOff) {
90          return theXof.doFinal(out, outOff);
91      }
92  
93      @Override
94      public int doFinal(final byte[] out, final int outOff, final int outLen) {
95          return theXof.doFinal(out, outOff, outLen);
96      }
97  
98      @Override
99      public int doOutput(final byte[] out, final int outOff, final int outLen) {
100         return theXof.doOutput(out, outOff, outLen);
101     }
102 
103     @Override
104     public int getByteLength() {
105         return theXof.getByteLength();
106     }
107 
108     @Override
109     public int getDigestSize() {
110         return theXof.getDigestSize();
111     }
112 
113     @Override
114     public void reset() {
115         theXof.reset();
116     }
117 }