Skip to content

Commit cf9d584

Browse files
committed
Cleanup after recent changes
1 parent 771dc69 commit cf9d584

File tree

13 files changed

+416
-204
lines changed

13 files changed

+416
-204
lines changed

crypto/src/asn1/nist/KMACwithSHAKE128_params.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,44 @@ namespace Org.BouncyCastle.Asn1.Nist
1010
/// customizationString OCTET STRING DEFAULT ''H
1111
/// }
1212
/// </summary>
13-
public class KMACwithSHAKE128_params : Asn1Encodable
13+
public class KMacWithShake128Params : Asn1Encodable
1414
{
15-
1615
private static readonly byte[] EMPTY_STRING = new byte[0];
1716
private static readonly int DEF_LENGTH = 256;
1817

1918
private readonly int outputLength;
2019
private readonly byte[] customizationString;
2120

22-
23-
public KMACwithSHAKE128_params(int outputLength)
21+
public KMacWithShake128Params(int outputLength)
2422
{
2523
this.outputLength = outputLength;
2624
this.customizationString = EMPTY_STRING;
2725
}
2826

29-
public KMACwithSHAKE128_params(int outputLength, byte[] customizationString)
27+
public KMacWithShake128Params(int outputLength, byte[] customizationString)
3028
{
3129
this.outputLength = outputLength;
3230
this.customizationString = Arrays.Clone(customizationString);
3331
}
3432

35-
36-
public static KMACwithSHAKE128_params getInstance(Object o)
33+
public static KMacWithShake128Params GetInstance(object o)
3734
{
38-
if (o is KMACwithSHAKE128_params)
35+
if (o is KMacWithShake128Params)
3936
{
40-
return (KMACwithSHAKE128_params)o;
37+
return (KMacWithShake128Params)o;
4138
}
4239
else if (o != null)
4340
{
44-
return new KMACwithSHAKE128_params(Asn1Sequence.GetInstance(o));
41+
return new KMacWithShake128Params(Asn1Sequence.GetInstance(o));
4542
}
4643

4744
return null;
4845
}
4946

50-
51-
private KMACwithSHAKE128_params(Asn1Sequence seq)
47+
private KMacWithShake128Params(Asn1Sequence seq)
5248
{
5349
if (seq.Count > 2)
54-
{
5550
throw new InvalidOperationException("sequence size greater than 2");
56-
}
5751

5852
if (seq.Count == 2)
5953
{
@@ -80,8 +74,6 @@ private KMACwithSHAKE128_params(Asn1Sequence seq)
8074
}
8175
}
8276

83-
84-
8577
public int OutputLength
8678
{
8779
get { return outputLength; }
@@ -92,7 +84,6 @@ public byte[] CustomizationString
9284
get { return Arrays.Clone(customizationString); }
9385
}
9486

95-
9687
public override Asn1Object ToAsn1Object()
9788
{
9889
Asn1EncodableVector v = new Asn1EncodableVector();

crypto/src/asn1/nist/KMACwithSHAKE256_params.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,44 @@ namespace Org.BouncyCastle.Asn1.Nist
99
/// customizationString OCTET STRING DEFAULT ''H
1010
/// }
1111
/// </summary>
12-
public class KMACwithSHAKE256_params : Asn1Encodable
12+
public class KMacWithShake256Params : Asn1Encodable
1313
{
14-
1514
private static readonly byte[] EMPTY_STRING = new byte[0];
1615
private static readonly int DEF_LENGTH = 512;
1716

1817
private readonly int outputLength;
1918
private readonly byte[] customizationString;
2019

21-
22-
public KMACwithSHAKE256_params(int outputLength)
20+
public KMacWithShake256Params(int outputLength)
2321
{
2422
this.outputLength = outputLength;
2523
this.customizationString = EMPTY_STRING;
2624
}
2725

28-
public KMACwithSHAKE256_params(int outputLength, byte[] customizationString)
26+
public KMacWithShake256Params(int outputLength, byte[] customizationString)
2927
{
3028
this.outputLength = outputLength;
3129
this.customizationString = Arrays.Clone(customizationString);
3230
}
3331

34-
35-
public static KMACwithSHAKE256_params getInstance(Object o)
32+
public static KMacWithShake256Params GetInstance(Object o)
3633
{
37-
if (o is KMACwithSHAKE256_params)
34+
if (o is KMacWithShake256Params)
3835
{
39-
return (KMACwithSHAKE256_params)o;
36+
return (KMacWithShake256Params)o;
4037
}
4138
else if (o != null)
4239
{
43-
return new KMACwithSHAKE256_params(Asn1Sequence.GetInstance(o));
40+
return new KMacWithShake256Params(Asn1Sequence.GetInstance(o));
4441
}
4542

4643
return null;
4744
}
4845

49-
50-
private KMACwithSHAKE256_params(Asn1Sequence seq)
46+
private KMacWithShake256Params(Asn1Sequence seq)
5147
{
5248
if (seq.Count > 2)
53-
{
5449
throw new InvalidOperationException("sequence size greater than 2");
55-
}
5650

5751
if (seq.Count == 2)
5852
{
@@ -79,8 +73,6 @@ private KMACwithSHAKE256_params(Asn1Sequence seq)
7973
}
8074
}
8175

82-
83-
8476
public int OutputLength
8577
{
8678
get { return outputLength; }
@@ -91,7 +83,6 @@ public byte[] CustomizationString
9183
get { return Arrays.Clone(customizationString); }
9284
}
9385

94-
9586
public override Asn1Object ToAsn1Object()
9687
{
9788
Asn1EncodableVector v = new Asn1EncodableVector();

crypto/src/crypto/digests/CSHAKEDigest.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using Org.BouncyCastle.Utilities;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
1+
using System;
2+
3+
using Org.BouncyCastle.Utilities;
64

75
namespace Org.BouncyCastle.Crypto.Digests
86
{
97
/// <summary>
108
/// Customizable SHAKE function.
119
/// </summary>
12-
public class CSHAKEDigest : ShakeDigest
10+
public class CShakeDigest : ShakeDigest
1311
{
1412
private static readonly byte[] padding = new byte[100];
1513
private readonly byte[] diff;
@@ -20,20 +18,19 @@ public class CSHAKEDigest : ShakeDigest
2018
/// <param name="bitLength">bit length of the underlying SHAKE function, 128 or 256.</param>
2119
/// <param name="N">the function name string, note this is reserved for use by NIST. Avoid using it if not required.</param>
2220
/// <param name="S">the customization string - available for local use.</param>
23-
public CSHAKEDigest(int bitLength, byte[] N, byte[] S) : base(bitLength)
21+
public CShakeDigest(int bitLength, byte[] N, byte[] S) : base(bitLength)
2422
{
2523
if ((N == null || N.Length == 0) && (S == null || S.Length == 0))
2624
{
2725
diff = null;
2826
}
2927
else
3028
{
31-
diff = Arrays.ConcatenateAll(XofUtils.leftEncode(rate / 8), encodeString(N), encodeString(S));
29+
diff = Arrays.ConcatenateAll(XofUtilities.LeftEncode(rate / 8), encodeString(N), encodeString(S));
3230
DiffPadAndAbsorb();
3331
}
3432
}
3533

36-
3734
// bytepad in SP 800-185
3835
private void DiffPadAndAbsorb()
3936
{
@@ -61,13 +58,16 @@ private byte[] encodeString(byte[] str)
6158
{
6259
if (str == null || str.Length == 0)
6360
{
64-
return XofUtils.leftEncode(0);
61+
return XofUtilities.LeftEncode(0);
6562
}
6663

67-
return Arrays.Concatenate(XofUtils.leftEncode(str.Length * 8L), str);
64+
return Arrays.Concatenate(XofUtilities.LeftEncode(str.Length * 8L), str);
6865
}
6966

70-
public override string AlgorithmName => "CSHAKE" + fixedOutputLength;
67+
public override string AlgorithmName
68+
{
69+
get { return "CSHAKE" + fixedOutputLength; }
70+
}
7171

7272
public override int DoFinal(byte[] output, int outOff)
7373
{
@@ -102,7 +102,7 @@ public override int DoOutput(byte[] output, int outOff, int outLen)
102102
}
103103
}
104104

105-
public void Reset()
105+
public override void Reset()
106106
{
107107
base.Reset();
108108

crypto/src/crypto/digests/XofUtils.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52

63
namespace Org.BouncyCastle.Crypto.Digests
74
{
8-
public class XofUtils
5+
internal class XofUtilities
96
{
10-
public static byte[] leftEncode(long strLen)
7+
internal static byte[] LeftEncode(long strLen)
118
{
129
byte n = 1;
1310

@@ -29,7 +26,7 @@ public static byte[] leftEncode(long strLen)
2926
return b;
3027
}
3128

32-
public static byte[] rightEncode(long strLen)
29+
internal static byte[] RightEncode(long strLen)
3330
{
3431
byte n = 1;
3532

crypto/src/crypto/macs/KMac.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
1-
using Org.BouncyCastle.Crypto.Digests;
1+
using System;
2+
using System.Text;
3+
4+
using Org.BouncyCastle.Crypto.Digests;
25
using Org.BouncyCastle.Crypto.Parameters;
36
using Org.BouncyCastle.Utilities;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Text;
87

98
namespace Org.BouncyCastle.Crypto.Macs
109
{
11-
public class KMac : IMac, IXof
10+
public class KMac
11+
: IMac, IXof
1212
{
13-
1413
private static readonly byte[] padding = new byte[100];
1514

16-
private readonly CSHAKEDigest cshake;
15+
private readonly CShakeDigest cshake;
1716
private readonly int bitLength;
1817
private readonly int outputLength;
1918

2019
private byte[] key;
2120
private bool initialised;
2221
private bool firstOutput;
2322

24-
2523
public KMac(int bitLength, byte[] S)
2624
{
27-
this.cshake = new CSHAKEDigest(bitLength, Encoding.ASCII.GetBytes("KMAC"),S);
25+
this.cshake = new CShakeDigest(bitLength, Encoding.ASCII.GetBytes("KMAC"), S);
2826
this.bitLength = bitLength;
2927
this.outputLength = bitLength * 2 / 8;
3028
}
3129

32-
33-
public string AlgorithmName => "KMAC" + cshake.AlgorithmName.Substring(6);
30+
public string AlgorithmName
31+
{
32+
get { return "KMAC" + cshake.AlgorithmName.Substring(6); }
33+
}
3434

3535
public void BlockUpdate(byte[] input, int inOff, int len)
3636
{
3737
if (!initialised)
38-
{
3938
throw new InvalidOperationException("KMAC not initialized");
40-
}
4139

4240
cshake.BlockUpdate(input, inOff, len);
4341
}
@@ -47,11 +45,9 @@ public int DoFinal(byte[] output, int outOff)
4745
if (firstOutput)
4846
{
4947
if (!initialised)
50-
{
5148
throw new InvalidOperationException("KMAC not initialized");
52-
}
5349

54-
byte[] encOut = XofUtils.rightEncode(GetMacSize() * 8);
50+
byte[] encOut = XofUtilities.RightEncode(GetMacSize() * 8);
5551

5652
cshake.BlockUpdate(encOut, 0, encOut.Length);
5753
}
@@ -68,11 +64,9 @@ public int DoFinal(byte[] output, int outOff, int outLen)
6864
if (firstOutput)
6965
{
7066
if (!initialised)
71-
{
7267
throw new InvalidOperationException("KMAC not initialized");
73-
}
7468

75-
byte[] encOut = XofUtils.rightEncode(outLen * 8);
69+
byte[] encOut = XofUtilities.RightEncode(outLen * 8);
7670

7771
cshake.BlockUpdate(encOut, 0, encOut.Length);
7872
}
@@ -89,11 +83,9 @@ public int DoOutput(byte[] output, int outOff, int outLen)
8983
if (firstOutput)
9084
{
9185
if (!initialised)
92-
{
9386
throw new InvalidOperationException("KMAC not initialized");
94-
}
9587

96-
byte[] encOut = XofUtils.rightEncode(0);
88+
byte[] encOut = XofUtilities.RightEncode(0);
9789

9890
cshake.BlockUpdate(encOut, 0, encOut.Length);
9991

@@ -147,7 +139,7 @@ public void Reset()
147139

148140
private void bytePad(byte[] X, int w)
149141
{
150-
byte[] bytes = XofUtils.leftEncode(w);
142+
byte[] bytes = XofUtilities.LeftEncode(w);
151143
BlockUpdate(bytes, 0, bytes.Length);
152144
byte[] encX = encode(X);
153145
BlockUpdate(encX, 0, encX.Length);
@@ -168,15 +160,13 @@ private void bytePad(byte[] X, int w)
168160

169161
private static byte[] encode(byte[] X)
170162
{
171-
return Arrays.Concatenate(XofUtils.leftEncode(X.Length * 8), X);
163+
return Arrays.Concatenate(XofUtilities.LeftEncode(X.Length * 8), X);
172164
}
173165

174166
public void Update(byte input)
175167
{
176168
if (!initialised)
177-
{
178169
throw new InvalidOperationException("KMAC not initialized");
179-
}
180170

181171
cshake.Update(input);
182172
}

0 commit comments

Comments
 (0)