Skip to content

Commit 6536ed9

Browse files
committed
Added KMAC Params and test
1 parent 64754bc commit 6536ed9

File tree

4 files changed

+330
-22
lines changed

4 files changed

+330
-22
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using Org.BouncyCastle.Utilities;
2+
using System;
3+
4+
5+
namespace Org.BouncyCastle.Asn1.Nist
6+
{
7+
/// <summary>
8+
/// KMACwithSHAKE128-params ::= SEQUENCE {
9+
/// kMACOutputLength INTEGER DEFAULT 256, -- Output length in bits
10+
/// customizationString OCTET STRING DEFAULT ''H
11+
/// }
12+
/// </summary>
13+
public class KMACwithSHAKE128_params : Asn1Encodable
14+
{
15+
16+
private static readonly byte[] EMPTY_STRING = new byte[0];
17+
private static readonly int DEF_LENGTH = 256;
18+
19+
private readonly int outputLength;
20+
private readonly byte[] customizationString;
21+
22+
23+
public KMACwithSHAKE128_params(int outputLength)
24+
{
25+
this.outputLength = outputLength;
26+
this.customizationString = EMPTY_STRING;
27+
}
28+
29+
public KMACwithSHAKE128_params(int outputLength, byte[] customizationString)
30+
{
31+
this.outputLength = outputLength;
32+
this.customizationString = Arrays.Clone(customizationString);
33+
}
34+
35+
36+
public static KMACwithSHAKE128_params getInstance(Object o)
37+
{
38+
if (o is KMACwithSHAKE128_params)
39+
{
40+
return (KMACwithSHAKE128_params)o;
41+
}
42+
else if (o != null)
43+
{
44+
return new KMACwithSHAKE128_params(Asn1Sequence.GetInstance(o));
45+
}
46+
47+
return null;
48+
}
49+
50+
51+
private KMACwithSHAKE128_params(Asn1Sequence seq)
52+
{
53+
if (seq.Count > 2)
54+
{
55+
throw new InvalidOperationException("sequence size greater than 2");
56+
}
57+
58+
if (seq.Count == 2)
59+
{
60+
this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact;
61+
this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[1]).GetOctets());
62+
}
63+
else if (seq.Count == 1)
64+
{
65+
if (seq[0] is DerInteger)
66+
{
67+
this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact;
68+
this.customizationString = EMPTY_STRING;
69+
}
70+
else
71+
{
72+
this.outputLength = DEF_LENGTH;
73+
this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[0]).GetOctets());
74+
}
75+
}
76+
else
77+
{
78+
this.outputLength = DEF_LENGTH;
79+
this.customizationString = EMPTY_STRING;
80+
}
81+
}
82+
83+
84+
85+
public int OutputLength
86+
{
87+
get { return outputLength; }
88+
}
89+
90+
public byte[] CustomizationString
91+
{
92+
get { return Arrays.Clone(customizationString); }
93+
}
94+
95+
96+
public override Asn1Object ToAsn1Object()
97+
{
98+
Asn1EncodableVector v = new Asn1EncodableVector();
99+
if (outputLength != DEF_LENGTH)
100+
{
101+
v.Add(new DerInteger(outputLength));
102+
}
103+
104+
if (customizationString.Length != 0)
105+
{
106+
v.Add(new DerOctetString(CustomizationString));
107+
}
108+
109+
return new DerSequence(v);
110+
}
111+
}
112+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using Org.BouncyCastle.Utilities;
2+
using System;
3+
4+
namespace Org.BouncyCastle.Asn1.Nist
5+
{
6+
/// <summary>
7+
/// KMACwithSHAKE256-params ::= SEQUENCE {
8+
/// kMACOutputLength INTEGER DEFAULT 512, -- Output length in bits
9+
/// customizationString OCTET STRING DEFAULT ''H
10+
/// }
11+
/// </summary>
12+
public class KMACwithSHAKE256_params : Asn1Encodable
13+
{
14+
15+
private static readonly byte[] EMPTY_STRING = new byte[0];
16+
private static readonly int DEF_LENGTH = 512;
17+
18+
private readonly int outputLength;
19+
private readonly byte[] customizationString;
20+
21+
22+
public KMACwithSHAKE256_params(int outputLength)
23+
{
24+
this.outputLength = outputLength;
25+
this.customizationString = EMPTY_STRING;
26+
}
27+
28+
public KMACwithSHAKE256_params(int outputLength, byte[] customizationString)
29+
{
30+
this.outputLength = outputLength;
31+
this.customizationString = Arrays.Clone(customizationString);
32+
}
33+
34+
35+
public static KMACwithSHAKE256_params getInstance(Object o)
36+
{
37+
if (o is KMACwithSHAKE256_params)
38+
{
39+
return (KMACwithSHAKE256_params)o;
40+
}
41+
else if (o != null)
42+
{
43+
return new KMACwithSHAKE256_params(Asn1Sequence.GetInstance(o));
44+
}
45+
46+
return null;
47+
}
48+
49+
50+
private KMACwithSHAKE256_params(Asn1Sequence seq)
51+
{
52+
if (seq.Count > 2)
53+
{
54+
throw new InvalidOperationException("sequence size greater than 2");
55+
}
56+
57+
if (seq.Count == 2)
58+
{
59+
this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact;
60+
this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[1]).GetOctets());
61+
}
62+
else if (seq.Count == 1)
63+
{
64+
if (seq[0] is DerInteger)
65+
{
66+
this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact;
67+
this.customizationString = EMPTY_STRING;
68+
}
69+
else
70+
{
71+
this.outputLength = DEF_LENGTH;
72+
this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[0]).GetOctets());
73+
}
74+
}
75+
else
76+
{
77+
this.outputLength = DEF_LENGTH;
78+
this.customizationString = EMPTY_STRING;
79+
}
80+
}
81+
82+
83+
84+
public int OutputLength
85+
{
86+
get { return outputLength; }
87+
}
88+
89+
public byte[] CustomizationString
90+
{
91+
get { return Arrays.Clone(customizationString); }
92+
}
93+
94+
95+
public override Asn1Object ToAsn1Object()
96+
{
97+
Asn1EncodableVector v = new Asn1EncodableVector();
98+
if (outputLength != DEF_LENGTH)
99+
{
100+
v.Add(new DerInteger(outputLength));
101+
}
102+
103+
if (customizationString.Length != 0)
104+
{
105+
v.Add(new DerOctetString(CustomizationString));
106+
}
107+
108+
return new DerSequence(v);
109+
}
110+
}
111+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using NUnit.Framework;
2+
using Org.BouncyCastle.Asn1.Nist;
3+
using Org.BouncyCastle.Utilities;
4+
using Org.BouncyCastle.Utilities.Test;
5+
6+
namespace Org.BouncyCastle.Asn1.Tests
7+
{
8+
[TestFixture]
9+
public class KMacParamsTest :SimpleTest
10+
{
11+
public override string Name => "KMacParamsTest";
12+
13+
14+
public override void PerformTest()
15+
{
16+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256).GetEncoded(), new DerSequence().GetEncoded()));
17+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512).GetEncoded(), new DerSequence().GetEncoded()));
18+
19+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512).GetEncoded(), new DerSequence(new DerInteger(512)).GetEncoded()));
20+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256).GetEncoded(), new DerSequence(new DerInteger(256)).GetEncoded()));
21+
22+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512).GetEncoded(), KMACwithSHAKE128_params.getInstance(new DerSequence(new DerInteger(512))).GetEncoded()));
23+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256).GetEncoded(), KMACwithSHAKE256_params.getInstance(new DerSequence(new DerInteger(256))).GetEncoded()));
24+
25+
byte[] customizationString = Strings.ToByteArray("hello, world!");
26+
27+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512, customizationString).GetEncoded(), new DerSequence(
28+
new Asn1Encodable[] { new DerInteger(512), new DerOctetString(customizationString) }).GetEncoded()));
29+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256, customizationString).GetEncoded(), new DerSequence(
30+
new Asn1Encodable[] { new DerInteger(256), new DerOctetString(customizationString) }).GetEncoded()));
31+
32+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512, customizationString).GetEncoded(),
33+
KMACwithSHAKE128_params.getInstance(
34+
new DerSequence(new Asn1Encodable[] { new DerInteger(512), new DerOctetString(customizationString) })).GetEncoded()));
35+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256, customizationString).GetEncoded(),
36+
KMACwithSHAKE256_params.getInstance(new DerSequence(
37+
new Asn1Encodable[] { new DerInteger(256), new DerOctetString(customizationString) })).GetEncoded()));
38+
39+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256, customizationString).GetEncoded(), new DerSequence(
40+
new Asn1Encodable[] { new DerOctetString(customizationString) }).GetEncoded()));
41+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512, customizationString).GetEncoded(), new DerSequence(
42+
new Asn1Encodable[] { new DerOctetString(customizationString) }).GetEncoded()));
43+
44+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256, customizationString).GetEncoded(),
45+
KMACwithSHAKE128_params.getInstance(
46+
new DerSequence(new Asn1Encodable[] { new DerOctetString(customizationString) })).GetEncoded()));
47+
Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512, customizationString).GetEncoded(),
48+
KMACwithSHAKE256_params.getInstance(new DerSequence(
49+
new Asn1Encodable[] { new DerOctetString(customizationString) })).GetEncoded()));
50+
51+
KMACwithSHAKE128_params p128 = new KMACwithSHAKE128_params(256, customizationString);
52+
Assert.AreEqual(256, p128.OutputLength);
53+
Assert.IsTrue(Arrays.AreEqual(customizationString, p128.CustomizationString));
54+
Assert.IsTrue(p128 == KMACwithSHAKE128_params.getInstance(p128));
55+
56+
KMACwithSHAKE256_params p256 = new KMACwithSHAKE256_params(512, customizationString);
57+
Assert.AreEqual(512, p256.OutputLength);
58+
Assert.IsTrue(Arrays.AreEqual(customizationString, p256.CustomizationString));
59+
Assert.IsTrue(p256 == KMACwithSHAKE256_params.getInstance(p256));
60+
61+
p128 = new KMACwithSHAKE128_params(512);
62+
Assert.AreEqual(512, p128.OutputLength);
63+
Assert.IsTrue(Arrays.AreEqual(new byte[0], p128.CustomizationString));
64+
65+
p256 = new KMACwithSHAKE256_params(256);
66+
Assert.AreEqual(256, p256.OutputLength);
67+
Assert.IsTrue(Arrays.AreEqual(new byte[0], p256.CustomizationString));
68+
}
69+
70+
public static void Main(
71+
string[] args)
72+
{
73+
RunTest(new KMacParamsTest());
74+
}
75+
76+
[Test]
77+
public void TestFunction()
78+
{
79+
string resultText = Perform().ToString();
80+
81+
Assert.AreEqual(resultText, Name + ": Okay", resultText);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)