@@ -205,8 +205,7 @@ module Crypto =
205205
206206 Some(
207207 Array.concat
208− [| [| Constants.blobVersion |]
209− ephPub
208+ [| ephPub
210209 nonce
211210 ciphertext
212211 senderPubKey
@@ -220,37 +219,24 @@ module Crypto =
220219 if recipientPrivKey.Length <> 64 || blob.Length < minBlobLen then
221220 None
222221 else
223− let tryDecrypt offset decompress =
224− if blob.Length < minBlobLen + offset then
222+ try
223+ let ephPub = blob.[.. ephPubLen - 1]
224+ let nonce = blob.[ephPubLen .. headerLen - 1]
225+ let ciphertext = blob.[headerLen .. blob.Length - trailerLen - 1]
226+ let senderPub = blob.[blob.Length - trailerLen .. blob.Length - sigLen - 1]
227+ let signature = blob.[blob.Length - sigLen ..]
228+
229+ if not (verifyDetached signature ciphertext senderPub) then
225230 None
226231 else
227− try
228− let ephPub = blob.[offset .. offset + ephPubLen - 1]
229− let nonce = blob.[offset + ephPubLen .. offset + headerLen - 1]
230− let ciphertext = blob.[offset + headerLen .. blob.Length - trailerLen - 1]
231− let senderPub = blob.[blob.Length - trailerLen .. blob.Length - sigLen - 1]
232− let signature = blob.[blob.Length - sigLen ..]
233−
234− if not (verifyDetached signature ciphertext senderPub) then
235− None
236− else
237− let recipientX25519Priv = ed25519SkToCurve25519 recipientPrivKey
238− let rawSecret = scalarMult recipientX25519Priv ephPub
239− let recipientEd25519Pub = recipientPrivKey.[32..63]
240− let recipientX25519Pub = ed25519PkToCurve25519 recipientEd25519Pub
241− let key = deriveKey ephPub recipientX25519Pub rawSecret
242− let decrypted = aeadDecrypt ciphertext nonce key
243− let plaintext = if decompress then LZ4Pickler.Unpickle(decrypted) else decrypted
244− Some(Encoding.UTF8.GetString(plaintext), toHex senderPub)
245− with ex ->
246− log $"decrypt error: {ex.Message}"
247− None
248−
249− let version = blob.[0]
250−
251− if version = 2uy then
252− tryDecrypt 1 true
253− elif version = 1uy then
254− tryDecrypt 1 false |> Option.orElseWith (fun () -> tryDecrypt 0 false)
255− else
256− tryDecrypt 0 false
232+ let recipientX25519Priv = ed25519SkToCurve25519 recipientPrivKey
233+ let rawSecret = scalarMult recipientX25519Priv ephPub
234+ let recipientEd25519Pub = recipientPrivKey.[32..63]
235+ let recipientX25519Pub = ed25519PkToCurve25519 recipientEd25519Pub
236+ let key = deriveKey ephPub recipientX25519Pub rawSecret
237+ let decrypted = aeadDecrypt ciphertext nonce key
238+ let plaintext = LZ4Pickler.Unpickle(decrypted)
239+ Some(Encoding.UTF8.GetString(plaintext), toHex senderPub)
240+ with ex ->
241+ log $"decrypt error: {ex.Message}"
242+ None