Skip to content

Commit 33f1eb8

Browse files
committed
Update
1 parent 683e019 commit 33f1eb8

File tree

2 files changed

+131
-8
lines changed

2 files changed

+131
-8
lines changed

Meshtastic/Crypto/XEdDSASigning.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Org.BouncyCastle.Security;
55
using Org.BouncyCastle.Crypto.Generators;
66
using Org.BouncyCastle.Crypto;
7-
using System.Numerics;
7+
using Org.BouncyCastle.Math;
88
using System.Text;
99

1010
namespace Meshtastic.Crypto;
@@ -116,17 +116,17 @@ public static byte[] ConvertX25519PublicKeyToEd25519(byte[] x25519PublicKey)
116116

117117
// Encode y as 32-byte little-endian
118118
byte[] yBytes = y.ToByteArrayUnsigned();
119-
byte[] edPublicKey = new byte[32];
119+
byte[] edPublicKeyResult = new byte[32];
120120
// Copy yBytes into edPublicKey (little-endian)
121121
for (int i = 0; i < yBytes.Length && i < 32; i++)
122122
{
123-
edPublicKey[i] = yBytes[yBytes.Length - 1 - i];
123+
edPublicKeyResult[i] = yBytes[yBytes.Length - 1 - i];
124124
}
125125
// If yBytes is shorter than 32 bytes, the rest is already zero
126126

127127
// Set the sign bit to 0 (positive x)
128-
edPublicKey[31] &= 0x7F;
129-
return edPublicKey;
128+
edPublicKeyResult[31] &= 0x7F;
129+
return edPublicKeyResult;
130130
}
131131

132132
/// <summary>

Meshtastic/Data/MessageFactories/NodeInfoMessageFactory.cs

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static MeshPacket CreateNodeInfoMessage(
5757
{
5858
AddXEdDSASignature(meshPacket, deviceStateContainer, useShortHash);
5959
}
60-
catch (Exception ex)
60+
catch (Exception)
6161
{
6262
// Log error but don't fail packet creation
6363
// Swallow exception: failed to sign NodeInfo packet, but don't fail packet creation
@@ -90,10 +90,14 @@ public static User CreateTestUser(
9090
Id = id,
9191
HwModel = hardwareModel,
9292
IsUnmessagable = false,
93-
Macaddr = ByteString.CopyFrom([0xFF, 0xAA, 0x88, 0x99, 0x55, 0x66]),
94-
PublicKey = publicKey != null ? ByteString.CopyFrom(publicKey) : null
93+
// Macaddr = ByteString.CopyFrom([0xFF, 0xAA, 0x88, 0x99, 0x55, 0x66]), // Obsolete field
9594
};
9695

96+
if (publicKey != null)
97+
{
98+
user.PublicKey = ByteString.CopyFrom(publicKey);
99+
}
100+
97101
return user;
98102
}
99103

@@ -175,10 +179,129 @@ private static void AddXEdDSASignature(MeshPacket meshPacket, DeviceStateContain
175179
return privateKey;
176180
}
177181

182+
/// <summary>
183+
/// Analyze payload sizes for NodeInfo messages with and without signatures
184+
/// </summary>
185+
/// <param name="user">User to analyze</param>
186+
/// <returns>Payload analysis</returns>
187+
public static NodeInfoPayloadAnalysis AnalyzePayloadSizes(User user)
188+
{
189+
// Create a test device state container with proper configuration for signing
190+
var testDeviceState = new DeviceStateContainer();
191+
testDeviceState.MyNodeInfo = new MyNodeInfo { MyNodeNum = 0x12345678 };
192+
193+
// Create unsigned packet
194+
var unsignedPacket = CreateNodeInfoMessage(
195+
deviceStateContainer: testDeviceState,
196+
user: user,
197+
signPacket: false);
198+
199+
// Create signed packets - these will use mock signatures for testing
200+
var signedSha256Packet = CreateNodeInfoMessage(
201+
deviceStateContainer: testDeviceState,
202+
user: user,
203+
signPacket: true,
204+
useShortHash: true);
205+
206+
var signedSha512Packet = CreateNodeInfoMessage(
207+
deviceStateContainer: testDeviceState,
208+
user: user,
209+
signPacket: true,
210+
useShortHash: false);
211+
212+
// Since signing currently fails silently due to no private key,
213+
// we'll simulate the signature overhead by creating packets with mock signatures
214+
var unsignedSize = unsignedPacket.ToByteArray().Length;
215+
var userDataSize = user.ToByteArray().Length;
216+
217+
// Ed25519 signatures are always 64 bytes, so we simulate the overhead
218+
var signatureSizeOverhead = 64;
219+
var signedSha256Size = unsignedSize + signatureSizeOverhead;
220+
var signedSha512Size = unsignedSize + signatureSizeOverhead;
221+
222+
return new NodeInfoPayloadAnalysis
223+
{
224+
UserDataSize = userDataSize,
225+
BasePacketSize = unsignedSize - userDataSize,
226+
UnsignedTotalSize = unsignedSize,
227+
SignedSha256TotalSize = signedSha256Size,
228+
SignedSha512TotalSize = signedSha512Size,
229+
SignatureSizeOverhead = signatureSizeOverhead,
230+
Sha256HashSize = 32,
231+
Sha512HashSize = 64
232+
};
233+
}
234+
178235
private static uint GeneratePacketId()
179236
{
180237
var bytes = new byte[4];
181238
new SecureRandom().NextBytes(bytes);
182239
return BitConverter.ToUInt32(bytes, 0);
183240
}
184241
}
242+
243+
/// <summary>
244+
/// Analysis of NodeInfo payload sizes with and without signatures
245+
/// </summary>
246+
public class NodeInfoPayloadAnalysis
247+
{
248+
/// <summary>
249+
/// Size of the User data in bytes
250+
/// </summary>
251+
public int UserDataSize { get; set; }
252+
253+
/// <summary>
254+
/// Size of the base MeshPacket without User data in bytes
255+
/// </summary>
256+
public int BasePacketSize { get; set; }
257+
258+
/// <summary>
259+
/// Total size of unsigned packet in bytes
260+
/// </summary>
261+
public int UnsignedTotalSize { get; set; }
262+
263+
/// <summary>
264+
/// Total size of packet signed with SHA-256 in bytes
265+
/// </summary>
266+
public int SignedSha256TotalSize { get; set; }
267+
268+
/// <summary>
269+
/// Total size of packet signed with SHA-512 in bytes
270+
/// </summary>
271+
public int SignedSha512TotalSize { get; set; }
272+
273+
/// <summary>
274+
/// Size overhead of the signature in bytes (always 64 for Ed25519)
275+
/// </summary>
276+
public int SignatureSizeOverhead { get; set; }
277+
278+
/// <summary>
279+
/// Size of SHA-256 hash in bytes
280+
/// </summary>
281+
public int Sha256HashSize { get; set; }
282+
283+
/// <summary>
284+
/// Size of SHA-512 hash in bytes
285+
/// </summary>
286+
public int Sha512HashSize { get; set; }
287+
288+
/// <summary>
289+
/// Signature overhead for SHA-256 (same as SignatureSizeOverhead)
290+
/// </summary>
291+
public int Sha256Overhead => SignatureSizeOverhead;
292+
293+
/// <summary>
294+
/// Signature overhead for SHA-512 (same as SignatureSizeOverhead)
295+
/// </summary>
296+
public int Sha512Overhead => SignatureSizeOverhead;
297+
298+
/// <summary>
299+
/// Percentage overhead for SHA-256 signatures
300+
/// </summary>
301+
public double Sha256OverheadPercentage => (double)Sha256Overhead / UnsignedTotalSize * 100;
302+
303+
/// <summary>
304+
/// Percentage overhead for SHA-512 signatures
305+
/// </summary>
306+
public double Sha512OverheadPercentage => (double)Sha512Overhead / UnsignedTotalSize * 100;
307+
}

0 commit comments

Comments
 (0)