Skip to content

Commit 5c6bc72

Browse files
authored
Merge pull request #1 from carljohnsen/performance/mac_addr_fix
Performance/Changed the way a device's MAC address is found
2 parents fd536c0 + dbbefce commit 5c6bc72

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

src/Constants.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,33 @@ internal static class Constants
5353
/// </summary>
5454
/// <remarks>This number is required to be 8192 by the AESCrypt specification</remarks>
5555
internal const int KEY_HASH_ITERATIONS = 8192;
56+
57+
/// <summary>
58+
/// Helper function for setting the `FIRST_MAC_ADDRESS` varible. It retrieves the first MAC address of the system. If no MAC address is found, or one of the system functions throws an exception, a default MAC address is used.
59+
/// </summary>
60+
/// <remarks>The default MAC address used is 01:23:45:67:89:ab</remarks>
61+
/// <returns>The MAC address of the first network interface that has a MAC address, or the default MAC address if no such interface is found.</returns>
62+
private static byte[] GetFirstMacAddress()
63+
{
64+
byte[] default_mac = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab];
65+
try {
66+
return System.Net.NetworkInformation.NetworkInterface
67+
.GetAllNetworkInterfaces()
68+
.Select(ni => { try { return ni.GetPhysicalAddress().GetAddressBytes(); } catch { return []; } })
69+
.Where(mac => mac.Length > 0 && !mac.All(b => b == 0))
70+
.FirstOrDefault(default_mac);
71+
} catch {
72+
return default_mac;
73+
}
74+
}
75+
76+
/// <summary>
77+
/// The MAC address of the first network interface that has a MAC address.
78+
/// </summary>
79+
/// <remarks>If no such interface is found, a default MAC address is used (01:23:45:67:89:ab)</remarks>
80+
internal static readonly ulong FIRST_MAC_ADDRESS = System.Buffers.Binary.BinaryPrimitives.ReadUInt64BigEndian(
81+
[
82+
.. GetFirstMacAddress(),
83+
.. new byte[2],
84+
]);
5685
}

src/SetupHelper.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ namespace SharpAESCrypt;
99
/// </summary>
1010
internal class SetupHelper : IDisposable
1111
{
12-
/// <summary>
13-
/// The MAC adress to use in case the network interface enumeration fails
14-
/// </summary>
15-
private static readonly ulong DEFAULT_MAC
16-
= BinaryPrimitives.ReadUInt64BigEndian([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
17-
1812
/// <summary>
1913
/// The encryption algorithm used
2014
/// </summary>
@@ -145,26 +139,10 @@ private static byte[] EncodePassword(string password)
145139
/// <returns>A random IV</returns>
146140
private ReadOnlyMemory<byte> GenerateHeaderIV()
147141
{
148-
var mac = DEFAULT_MAC;
149-
try
150-
{
151-
var interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
152-
for (int i = 0; i < interfaces.Length; i++)
153-
if (i != System.Net.NetworkInformation.NetworkInterface.LoopbackInterfaceIndex)
154-
{
155-
mac = BinaryPrimitives.ReadUInt64BigEndian(interfaces[i].GetPhysicalAddress().GetAddressBytes());
156-
break;
157-
}
158-
}
159-
catch
160-
{
161-
//Not much to do, just go with default MAC
162-
}
163-
164142
// Build some initial entropy
165143
var iv = new byte[Constants.IV_SIZE];
166144
BinaryPrimitives.WriteInt64BigEndian(iv.AsSpan(), DateTime.Now.Ticks);
167-
BinaryPrimitives.WriteUInt64BigEndian(iv.AsSpan(8), mac);
145+
BinaryPrimitives.WriteUInt64BigEndian(iv.AsSpan(8), Constants.FIRST_MAC_ADDRESS);
168146

169147
// The IV is generated by repeatedly hashing the IV with random data.
170148
// By using the MAC address and the current time, we add some initial entropy,

0 commit comments

Comments
 (0)