1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
29
30 public class GordianBlake2XMac
31 implements Mac, Xof {
32
33
34
35 private final GordianBlake2Xof theXof;
36
37
38
39
40
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
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 }