on master help sign in
Compress payload with LZ4 before encryption
dc3c18eiko
client/Constants.fs+1−1
@@ -15,4 +15,4 @@ module Constants =
15 let pollRetryBaseMs = 3000
15 let pollRetryBaseMs = 3000
16 let outboxRetryDelayMs = 3000
16 let outboxRetryDelayMs = 3000
17 let maxMessages = 200
17 let maxMessages = 200
18− let blobVersion = 1uy
18+ let blobVersion = 2uy
client/Crypto.fs+13−7
@@ -3,6 +3,7 @@ namespace Skrepka
3open System
3open System
4open System.Text
4open System.Text
5open System.Security.Cryptography
5open System.Security.Cryptography
6+open K4os.Compression.LZ4
6
7
7module Crypto =
8module Crypto =
8
9
@@ -198,7 +199,8 @@ module Crypto =
198 let key = deriveKey ephPub recipientX25519Pub rawSecret
199 let key = deriveKey ephPub recipientX25519Pub rawSecret
199 let nonce = getRandomBytes 24
200 let nonce = getRandomBytes 24
200 let ptBytes = Encoding.UTF8.GetBytes(plaintext)
201 let ptBytes = Encoding.UTF8.GetBytes(plaintext)
202+ let compressed = LZ4Pickler.Pickle(ptBytes)
201− let ciphertext = aeadEncrypt ptBytes nonce key
203+ let ciphertext = aeadEncrypt compressed nonce key
202 let signature = signDetached ciphertext senderPrivKey
204 let signature = signDetached ciphertext senderPrivKey
203
205
204 Some(
206 Some(
@@ -218,7 +220,7 @@ module Crypto =
218 if recipientPrivKey.Length <> 64 || blob.Length < minBlobLen then
220 if recipientPrivKey.Length <> 64 || blob.Length < minBlobLen then
219 None
221 None
220 else
222 else
221− let tryDecrypt offset =
223+ let tryDecrypt offset decompress =
222 if blob.Length < minBlobLen + offset then
224 if blob.Length < minBlobLen + offset then
223 None
225 None
224 else
226 else
@@ -237,14 +239,18 @@ module Crypto =
237 let recipientEd25519Pub = recipientPrivKey.[32..63]
239 let recipientEd25519Pub = recipientPrivKey.[32..63]
238 let recipientX25519Pub = ed25519PkToCurve25519 recipientEd25519Pub
240 let recipientX25519Pub = ed25519PkToCurve25519 recipientEd25519Pub
239 let key = deriveKey ephPub recipientX25519Pub rawSecret
241 let key = deriveKey ephPub recipientX25519Pub rawSecret
240− let plaintext = aeadDecrypt ciphertext nonce key
242+ let decrypted = aeadDecrypt ciphertext nonce key
243+ let plaintext = if decompress then LZ4Pickler.Unpickle(decrypted) else decrypted
241 Some(Encoding.UTF8.GetString(plaintext), toHex senderPub)
244 Some(Encoding.UTF8.GetString(plaintext), toHex senderPub)
242 with ex ->
245 with ex ->
243 log $"decrypt error: {ex.Message}"
246 log $"decrypt error: {ex.Message}"
244 None
247 None
245
248
246− // Try versioned format, fall back to unversioned for backwards compat
249+ let version = blob.[0]
247− if blob.[0] = Constants.blobVersion then
250+
251+ if version = 2uy then
252+ tryDecrypt 1 true
253+ elif version = 1uy then
248− tryDecrypt 1 |> Option.orElseWith (fun () -> tryDecrypt 0)
254+ tryDecrypt 1 false |> Option.orElseWith (fun () -> tryDecrypt 0 false)
249 else
255 else
250− tryDecrypt 0
256+ tryDecrypt 0 false
client/Skrepka.fsproj+1
@@ -128,6 +128,7 @@
128 <PackageReference Include="Sodium.Core" Version="1.4.0" />
128 <PackageReference Include="Sodium.Core" Version="1.4.0" />
129 <PackageReference Include="Donald" Version="10.0.2" />
129 <PackageReference Include="Donald" Version="10.0.2" />
130 <PackageReference Include="Microsoft.Data.Sqlite" Version="11.0.0-preview.2.26159.112" />
130 <PackageReference Include="Microsoft.Data.Sqlite" Version="11.0.0-preview.2.26159.112" />
131+ <PackageReference Include="K4os.Compression.LZ4" Version="1.3.8" />
131 <PackageReference Include="QRCoder" Version="1.6.0" />
132 <PackageReference Include="QRCoder" Version="1.6.0" />
132 </ItemGroup>
133 </ItemGroup>
133
134