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.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
28
29 public class GordianBlake3Mac
30 implements Mac, Xof {
31
32
33
34 private final GordianBlake3Digest theDigest;
35
36
37
38
39
40
41 public GordianBlake3Mac(final GordianBlake3Digest pDigest) {
42
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) {
55 myParams = GordianBlake3Parameters.key(((KeyParameter) myParams).getKey());
56 }
57 if (!(myParams instanceof GordianBlake3Parameters)) {
58 throw new IllegalArgumentException("Invalid parameter passed to Blake3Mac init - "
59 + pParams.getClass().getName());
60 }
61 final GordianBlake3Parameters myBlakeParams = (GordianBlake3Parameters) myParams;
62 if (myBlakeParams.getKey() == null) {
63 throw new IllegalArgumentException("Blake3Mac requires a key parameter.");
64 }
65
66
67 theDigest.init(myBlakeParams);
68 }
69
70 @Override
71 public int getMacSize() {
72 return theDigest.getDigestSize();
73 }
74
75 @Override
76 public void update(final byte in) {
77 theDigest.update(in);
78 }
79
80 @Override
81 public void update(final byte[] in, final int inOff, final int len) {
82 theDigest.update(in, inOff, len);
83 }
84
85 @Override
86 public int doFinal(final byte[] out, final int outOff) {
87 return theDigest.doFinal(out, outOff);
88 }
89
90 @Override
91 public void reset() {
92 theDigest.reset();
93 }
94
95 @Override
96 public int doFinal(final byte[] out, final int outOff, final int outLen) {
97 return theDigest.doFinal(out, outOff, outLen);
98 }
99
100 @Override
101 public int doOutput(final byte[] out, final int outOff, final int outLen) {
102 return theDigest.doOutput(out, outOff, outLen);
103 }
104
105 @Override
106 public int getByteLength() {
107 return theDigest.getByteLength();
108 }
109
110 @Override
111 public int getDigestSize() {
112 return theDigest.getDigestSize();
113 }
114 }