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) {
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
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 }