Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2ab98bf

Browse files
committed
Merge pull request #2780 from dotnet-bot/from-tfs
Merge changes from TFS
2 parents 416019f + 75b2ca3 commit 2ab98bf

File tree

6 files changed

+184
-15
lines changed

6 files changed

+184
-15
lines changed

src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<Compile Include="System\Security\Cryptography\SHA256.cs" />
3434
<Compile Include="System\Security\Cryptography\SHA384.cs" />
3535
<Compile Include="System\Security\Cryptography\SHA512.cs" />
36+
<Compile Include="System\Security\Cryptography\HMACMD5.cs" />
3637
<Compile Include="System\Security\Cryptography\HMACSHA1.cs" />
3738
<Compile Include="System\Security\Cryptography\HMACSHA256.cs" />
3839
<Compile Include="System\Security\Cryptography\HMACSHA384.cs" />
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.Security.Cryptography;
7+
8+
using Internal.Cryptography;
9+
10+
namespace System.Security.Cryptography
11+
{
12+
//
13+
// If you change anything in this class, you must make the same change in the other HMAC* classes. This is a pain but given that the
14+
// preexisting contract from the desktop locks all of these into deriving directly from HMAC, it can't be helped.
15+
//
16+
17+
public class HMACMD5 : HMAC
18+
{
19+
public HMACMD5()
20+
: this(Helpers.GenerateRandom(64))
21+
{
22+
}
23+
24+
public HMACMD5(byte[] key)
25+
{
26+
this.HashName = HashAlgorithmNames.MD5;
27+
_hMacCommon = new HMACCommon(HashAlgorithmNames.MD5, key);
28+
base.Key = key;
29+
}
30+
31+
public override int HashSize
32+
{
33+
get
34+
{
35+
return _hMacCommon.HashSizeInBits;
36+
}
37+
}
38+
39+
public override byte[] Key
40+
{
41+
get
42+
{
43+
return base.Key;
44+
}
45+
set
46+
{
47+
base.Key = value;
48+
_hMacCommon.ChangeKey(value);
49+
}
50+
}
51+
52+
protected override void HashCore(byte[] rgb, int ib, int cb)
53+
{
54+
_hMacCommon.AppendHashData(rgb, ib, cb);
55+
}
56+
57+
protected override byte[] HashFinal()
58+
{
59+
return _hMacCommon.FinalizeHashAndReset();
60+
}
61+
62+
public override void Initialize()
63+
{
64+
// Nothing to do here. We expect HashAlgorithm to invoke HashFinal() and Initialize() as a pair. This reflects the
65+
// reality that our native crypto providers (e.g. CNG) expose hash finalization and object reinitialization as an atomic operation.
66+
return;
67+
}
68+
69+
protected override void Dispose(bool disposing)
70+
{
71+
if (disposing)
72+
{
73+
HMACCommon hMacCommon = _hMacCommon;
74+
_hMacCommon = null;
75+
if (hMacCommon != null)
76+
hMacCommon.Dispose(disposing);
77+
}
78+
base.Dispose(disposing);
79+
}
80+
81+
private HMACCommon _hMacCommon;
82+
}
83+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Test.Cryptography;
5+
using Xunit;
6+
7+
namespace System.Security.Cryptography.Hashing.Algorithms.Tests
8+
{
9+
public class HmacMD5Tests : Rfc2202HmacTests
10+
{
11+
private static readonly byte[][] s_testKeys2202 =
12+
{
13+
null,
14+
ByteUtils.RepeatByte(0x0b, 16),
15+
ByteUtils.AsciiBytes("Jefe"),
16+
ByteUtils.RepeatByte(0xaa, 16),
17+
ByteUtils.HexToByteArray("0102030405060708090a0b0c0d0e0f10111213141516171819"),
18+
ByteUtils.RepeatByte(0x0c, 16),
19+
ByteUtils.RepeatByte(0xaa, 80),
20+
ByteUtils.RepeatByte(0xaa, 80),
21+
};
22+
23+
public HmacMD5Tests()
24+
: base(s_testKeys2202)
25+
{
26+
}
27+
28+
protected override HMAC Create()
29+
{
30+
return new HMACMD5();
31+
}
32+
33+
[Fact]
34+
public void HmacMD5_Rfc2202_1()
35+
{
36+
VerifyHmac(1, "9294727a3638bb1c13f48ef8158bfc9d");
37+
}
38+
39+
[Fact]
40+
public void HmacMD5_Rfc2202_2()
41+
{
42+
VerifyHmac(2, "750c783e6ab0b503eaa86e310a5db738");
43+
}
44+
45+
[Fact]
46+
public void HmacMD5_Rfc2202_3()
47+
{
48+
VerifyHmac(3, "56be34521d144c88dbb8c733f0e8b3f6");
49+
}
50+
51+
[Fact]
52+
public void HmacMD5_Rfc2202_4()
53+
{
54+
VerifyHmac(4, "697eaf0aca3a3aea3a75164746ffaa79");
55+
}
56+
57+
[Fact]
58+
public void HmacMD5_Rfc2202_5()
59+
{
60+
VerifyHmac(5, "56461ef2342edc00f9bab995690efd4c");
61+
}
62+
63+
[Fact]
64+
public void HmacMD5_Rfc2202_6()
65+
{
66+
VerifyHmac(6, "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd");
67+
}
68+
69+
[Fact]
70+
public void HmacMD5_Rfc2202_7()
71+
{
72+
VerifyHmac(7, "6f630fad67cda0ee1fb1f562db3aa53e");
73+
}
74+
}
75+
}

src/System.Security.Cryptography.Algorithms/tests/HmacSha1Tests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using Test.Cryptography;
45
using Xunit;
56

67
namespace System.Security.Cryptography.Hashing.Algorithms.Tests
78
{
89
public class HmacSha1Tests : Rfc2202HmacTests
910
{
11+
private static readonly byte[][] s_testKeys2202 =
12+
{
13+
null,
14+
ByteUtils.RepeatByte(0x0b, 20),
15+
ByteUtils.AsciiBytes("Jefe"),
16+
ByteUtils.RepeatByte(0xaa, 20),
17+
ByteUtils.HexToByteArray("0102030405060708090a0b0c0d0e0f10111213141516171819"),
18+
ByteUtils.RepeatByte(0x0c, 20),
19+
ByteUtils.RepeatByte(0xaa, 80),
20+
ByteUtils.RepeatByte(0xaa, 80),
21+
};
22+
23+
public HmacSha1Tests()
24+
: base(s_testKeys2202)
25+
{
26+
}
27+
1028
protected override HMAC Create()
1129
{
1230
return new HMACSHA1();
@@ -54,4 +72,4 @@ public void HmacSha1_Rfc2202_7()
5472
VerifyHmac(7, "e8e99d0f45237d786d6bbaa7965c7808bbff1a91");
5573
}
5674
}
57-
}
75+
}

src/System.Security.Cryptography.Algorithms/tests/Rfc2202HmacTests.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ namespace System.Security.Cryptography.Hashing.Algorithms.Tests
77
{
88
public abstract class Rfc2202HmacTests : HmacTests
99
{
10-
private static readonly byte[][] s_testKeys2202 =
11-
{
12-
null,
13-
ByteUtils.RepeatByte(0x0b, 20),
14-
ByteUtils.AsciiBytes("Jefe"),
15-
ByteUtils.RepeatByte(0xaa, 20),
16-
ByteUtils.HexToByteArray("0102030405060708090a0b0c0d0e0f10111213141516171819"),
17-
ByteUtils.HexToByteArray("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"),
18-
ByteUtils.RepeatByte(0xaa, 80),
19-
ByteUtils.RepeatByte(0xaa, 80),
20-
};
21-
2210
private static readonly byte[][] s_testData2202 =
2311
{
2412
null,
@@ -31,8 +19,11 @@ public abstract class Rfc2202HmacTests : HmacTests
3119
ByteUtils.AsciiBytes("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"),
3220
};
3321

34-
protected Rfc2202HmacTests() :
35-
base(s_testKeys2202, s_testData2202)
22+
// The keys for test cases 1, 3, and 5 for RFC2202 are sized to match the
23+
// algorithm (16 bytes for MD5, 20 for SHA-1), so they need to be provided by
24+
// the more derived type.
25+
protected Rfc2202HmacTests(byte[][] testKeys) :
26+
base(testKeys, s_testData2202)
3627
{
3728
}
3829
}

src/System.Security.Cryptography.Algorithms/tests/System.Security.Cryptography.Algorithms.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<Compile Include="DecryptorReusability.cs" />
2828
<Compile Include="HashAlgorithmTest.cs" />
2929
<Compile Include="InvalidUsageTests.cs" />
30+
<Compile Include="HmacMD5Tests.cs" />
3031
<Compile Include="HmacSha1Tests.cs" />
3132
<Compile Include="HmacSha256Tests.cs" />
3233
<Compile Include="HmacSha384Tests.cs" />

0 commit comments

Comments
 (0)