Skip to content

Commit 610322d

Browse files
committed
Add NonMemoableDigest and tests
1 parent 6a939be commit 610322d

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

crypto/crypto.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -3323,6 +3323,11 @@
33233323
SubType = "Code"
33243324
BuildAction = "Compile"
33253325
/>
3326+
<File
3327+
RelPath = "src\crypto\digests\NonMemoableDigest.cs"
3328+
SubType = "Code"
3329+
BuildAction = "Compile"
3330+
/>
33263331
<File
33273332
RelPath = "src\crypto\digests\NullDigest.cs"
33283333
SubType = "Code"
@@ -11489,6 +11494,11 @@
1148911494
SubType = "Code"
1149011495
BuildAction = "Compile"
1149111496
/>
11497+
<File
11498+
RelPath = "test\src\crypto\test\NonMemoableDigestTest.cs"
11499+
SubType = "Code"
11500+
BuildAction = "Compile"
11501+
/>
1149211502
<File
1149311503
RelPath = "test\src\crypto\test\NoekeonTest.cs"
1149411504
SubType = "Code"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
3+
namespace Org.BouncyCastle.Crypto.Digests
4+
{
5+
/**
6+
* Wrapper removes exposure to the IMemoable interface on an IDigest implementation.
7+
*/
8+
public class NonMemoableDigest
9+
: IDigest
10+
{
11+
protected readonly IDigest mBaseDigest;
12+
13+
/**
14+
* Base constructor.
15+
*
16+
* @param baseDigest underlying digest to use.
17+
* @exception IllegalArgumentException if baseDigest is null
18+
*/
19+
public NonMemoableDigest(IDigest baseDigest)
20+
{
21+
if (baseDigest == null)
22+
throw new ArgumentNullException("baseDigest");
23+
24+
this.mBaseDigest = baseDigest;
25+
}
26+
27+
public virtual string AlgorithmName
28+
{
29+
get { return mBaseDigest.AlgorithmName; }
30+
}
31+
32+
public virtual int GetDigestSize()
33+
{
34+
return mBaseDigest.GetDigestSize();
35+
}
36+
37+
public virtual void Update(byte input)
38+
{
39+
mBaseDigest.Update(input);
40+
}
41+
42+
public virtual void BlockUpdate(byte[] input, int inOff, int len)
43+
{
44+
mBaseDigest.BlockUpdate(input, inOff, len);
45+
}
46+
47+
public virtual int DoFinal(byte[] output, int outOff)
48+
{
49+
return mBaseDigest.DoFinal(output, outOff);
50+
}
51+
52+
public virtual void Reset()
53+
{
54+
mBaseDigest.Reset();
55+
}
56+
57+
public virtual int GetByteLength()
58+
{
59+
return mBaseDigest.GetByteLength();
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.Text;
3+
4+
using NUnit.Framework;
5+
6+
using Org.BouncyCastle.Crypto.Digests;
7+
using Org.BouncyCastle.Crypto.Macs;
8+
using Org.BouncyCastle.Crypto.Parameters;
9+
using Org.BouncyCastle.Utilities;
10+
using Org.BouncyCastle.Utilities.Encoders;
11+
using Org.BouncyCastle.Utilities.Test;
12+
13+
namespace Org.BouncyCastle.Crypto.Tests
14+
{
15+
/**
16+
* SHA1 HMac Test, test vectors from RFC 2202
17+
*/
18+
public class NonMemoableDigestTest
19+
: SimpleTest
20+
{
21+
private static readonly string[] keys = {
22+
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
23+
"4a656665",
24+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
25+
"0102030405060708090a0b0c0d0e0f10111213141516171819",
26+
"0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
27+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
28+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
29+
};
30+
31+
private static readonly string[] digests = {
32+
"b617318655057264e28bc0b6fb378c8ef146be00",
33+
"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
34+
"125d7342b9ac11cd91a39af48aa17b4f63f175d3",
35+
"4c9007f4026250c6bc8414f9bf50c86c2d7235da",
36+
"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
37+
"aa4ae5e15272d00e95705637ce8a3b55ed402112",
38+
"e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
39+
"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
40+
"aa4ae5e15272d00e95705637ce8a3b55ed402112",
41+
"e8e99d0f45237d786d6bbaa7965c7808bbff1a91"
42+
};
43+
44+
private static readonly string[] messages = {
45+
"Hi There",
46+
"what do ya want for nothing?",
47+
"0xdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
48+
"0xcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
49+
"Test With Truncation",
50+
"Test Using Larger Than Block-Size Key - Hash Key First",
51+
"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
52+
};
53+
54+
public override string Name
55+
{
56+
get { return "NonMemoableDigest"; }
57+
}
58+
59+
public override void PerformTest()
60+
{
61+
HMac hmac = new HMac(new NonMemoableDigest(new Sha1Digest()));
62+
byte[] resBuf = new byte[hmac.GetMacSize()];
63+
64+
for (int i = 0; i < messages.Length; i++)
65+
{
66+
byte[] m = Strings.ToByteArray(messages[i]);
67+
if (messages[i].StartsWith("0x"))
68+
{
69+
m = Hex.Decode(messages[i].Substring(2));
70+
}
71+
hmac.Init(new KeyParameter(Hex.Decode(keys[i])));
72+
hmac.BlockUpdate(m, 0, m.Length);
73+
hmac.DoFinal(resBuf, 0);
74+
75+
if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i])))
76+
{
77+
Fail(Name + ": Vector " + i + " failed");
78+
}
79+
}
80+
81+
//
82+
// test reset
83+
//
84+
{
85+
int vector = 0; // vector used for test
86+
byte[] m = Strings.ToByteArray(messages[vector]);
87+
if (messages[vector].StartsWith("0x"))
88+
{
89+
m = Hex.Decode(messages[vector].Substring(2));
90+
}
91+
hmac.Init(new KeyParameter(Hex.Decode(keys[vector])));
92+
hmac.BlockUpdate(m, 0, m.Length);
93+
hmac.DoFinal(resBuf, 0);
94+
hmac.Reset();
95+
hmac.BlockUpdate(m, 0, m.Length);
96+
hmac.DoFinal(resBuf, 0);
97+
98+
if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[vector])))
99+
{
100+
Fail(Name + ": Reset with vector " + vector + " failed");
101+
}
102+
}
103+
}
104+
105+
public static void Main(
106+
string[] args)
107+
{
108+
RunTest(new NonMemoableDigestTest());
109+
}
110+
111+
[Test]
112+
public void TestFunction()
113+
{
114+
string resultText = Perform().ToString();
115+
116+
Assert.AreEqual(Name + ": Okay", resultText);
117+
}
118+
}
119+
}

crypto/test/src/crypto/test/RegressionTest.cs

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public class RegressionTest
119119
new SipHashTest(),
120120
new Poly1305Test(),
121121
new OcbTest(),
122+
new NonMemoableDigestTest(),
122123
new SM3DigestTest(),
123124
new X931SignerTest(),
124125
new KeccakDigestTest(),

0 commit comments

Comments
 (0)