Skip to content

Commit cd34b3a

Browse files
committed
Fixed memory stream issues with Security serialization
1 parent 03a5bf8 commit cd34b3a

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<RepositoryType>git</RepositoryType>
99
<PackageTags>eocron</PackageTags>
1010
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
11-
<Version>3.0.0</Version>
11+
<Version>3.0.1</Version>
1212
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
1313
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1414
<LangVersion>latest</LangVersion>

Eocron.Serialization.Security/SymmetricEncryptionSerializationConverter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ protected override void SerializeTo(Type type, object obj, BinaryWriter writer)
6565
using var ms = new MemoryStream();
6666
_inner.SerializeTo(type, obj, ms);
6767
using var nonce = PasswordDerivationHelper.CreateRandomBytes(_pool, NonceByteSize);
68-
using var encrypted = _pool.RentExact((int)ms.Position + MacByteSize);
68+
var cipher = CreateAeadCipher(nonce, true);
69+
using var encrypted = _pool.RentExact(cipher.GetOutputSize((int)ms.Position));
6970
using var body = new RentedAesGcmData(nonce, encrypted);
70-
var cipher = CreateAeadCipher(body.Nonce, true);
71+
7172
var len = cipher.ProcessBytes(
72-
ms.GetBuffer(),
73+
new Span<byte>(ms.GetBuffer(), 0, (int)ms.Position),
7374
body.EncryptedPayload.Data);
7475
cipher.DoFinal(body.EncryptedPayload.Data.Slice(len));
7576

Eocron.Serialization.Tests/EncryptionSerializationTestsBase.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using Eocron.Serialization.Tests.Models.Json;
4+
using Eocron.Serialization.Tests.Models.Protobuf;
45
using FluentAssertions;
56
using NUnit.Framework;
67

@@ -14,9 +15,9 @@ public abstract class EncryptionSerializationTestsBase
1415
public void EncryptThenDecrypt()
1516
{
1617
var converter = GetConverter();
17-
var model = new JsonTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string"};
18+
var model = new ProtobufTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string"};
1819
var data = converter.SerializeToBytes(model);
19-
var decryptedModel = converter.Deserialize<JsonTestModel>(data);
20+
var decryptedModel = converter.Deserialize<ProtobufTestModel>(data);
2021
decryptedModel.FooBarString.Should().Be("some_string");
2122
decryptedModel.Should().BeEquivalentTo(model);
2223
}
@@ -26,9 +27,9 @@ public void EncryptThenDecryptWithAnotherConverterInstance()
2627
{
2728
var converter1 = GetConverter();
2829
var converter2 = GetConverter();
29-
var model = new JsonTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string"};
30+
var model = new ProtobufTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string"};
3031
var data = converter1.SerializeToBytes(model);
31-
var decryptedModel = converter2.Deserialize<JsonTestModel>(data);
32+
var decryptedModel = converter2.Deserialize<ProtobufTestModel>(data);
3233
decryptedModel.FooBarString.Should().Be("some_string");
3334
decryptedModel.Should().BeEquivalentTo(model);
3435
}
@@ -37,17 +38,17 @@ public void EncryptThenDecryptWithAnotherConverterInstance()
3738
public void EncryptMultipleThenDecrypt()
3839
{
3940
var converter = GetConverter();
40-
var model1 = new JsonTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string"};
41-
var model2 = new JsonTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string_2"};
41+
var model1 = new ProtobufTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string"};
42+
var model2 = new ProtobufTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string_2"};
4243
var ms = new MemoryStream();
4344
converter.SerializeTo(model1, ms);
4445
converter.SerializeTo(model2, ms);
4546
ms.Seek(0, SeekOrigin.Begin);
4647

47-
var decryptedModel1 = converter.DeserializeFrom<JsonTestModel>(ms);
48+
var decryptedModel1 = converter.DeserializeFrom<ProtobufTestModel>(ms);
4849
decryptedModel1.FooBarString.Should().Be("some_string");
4950
decryptedModel1.Should().BeEquivalentTo(model1);
50-
var decryptedModel2 = converter.DeserializeFrom<JsonTestModel>(ms);
51+
var decryptedModel2 = converter.DeserializeFrom<ProtobufTestModel>(ms);
5152
decryptedModel2.FooBarString.Should().Be("some_string_2");
5253
decryptedModel2.Should().BeEquivalentTo(model2);
5354
}
@@ -56,7 +57,7 @@ public void EncryptMultipleThenDecrypt()
5657
public void EncryptionDoesNotRepeat()
5758
{
5859
var converter = GetConverter();
59-
var model = new JsonTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string"};
60+
var model = new ProtobufTestModel() { Guid = Guid.NewGuid(), FooBarString = "some_string"};
6061
var data1 = converter.SerializeToBytes(model);
6162
var data2 = converter.SerializeToBytes(model);
6263
data1.Should().NotBeEquivalentTo(data2);

Eocron.Serialization.Tests/SymmetricEncryptionSerializationTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Eocron.Serialization.Json;
2+
using Eocron.Serialization.Protobuf;
23
using Eocron.Serialization.Security;
34
using NUnit.Framework;
45

@@ -9,7 +10,7 @@ public class SymmetricEncryptionSerializationTests : EncryptionSerializationTest
910
{
1011
public override ISerializationConverter GetConverter()
1112
{
12-
return new SymmetricEncryptionSerializationConverter(SerializationConverterJson.Json, "foobar");
13+
return new SymmetricEncryptionSerializationConverter(SerializationConverterProtobuf.Protobuf, "foobar");
1314
}
1415
}
1516
}

0 commit comments

Comments
 (0)