@@ -205,8 +205,7 @@ module Crypto =
207 Array.concat
207 Array.concat
208− [| [| Constants.blobVersion |]
209− ephPub
208+ [| ephPub
211 ciphertext
210 ciphertext
212 senderPubKey
211 senderPubKey
@@ -220,37 +219,24 @@ module Crypto =
220 if recipientPrivKey.Length <> 64 || blob.Length < minBlobLen then
219 if recipientPrivKey.Length <> 64 || blob.Length < minBlobLen then
223− let tryDecrypt offset decompress =
222+ try
224− if blob.Length < minBlobLen + offset then
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 ..]
229+ if not (verifyDetached signature ciphertext senderPub) then
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 ..]
234− if not (verifyDetached signature ciphertext senderPub) then
237− let recipientX25519Priv = ed25519SkToCurve25519 recipientPrivKey
232+ let recipientX25519Priv = ed25519SkToCurve25519 recipientPrivKey
238− let rawSecret = scalarMult recipientX25519Priv ephPub
233+ let rawSecret = scalarMult recipientX25519Priv ephPub
239− let recipientEd25519Pub = recipientPrivKey.[32..63]
234+ let recipientEd25519Pub = recipientPrivKey.[32..63]
240− let recipientX25519Pub = ed25519PkToCurve25519 recipientEd25519Pub
235+ let recipientX25519Pub = ed25519PkToCurve25519 recipientEd25519Pub
241− let key = deriveKey ephPub recipientX25519Pub rawSecret
236+ let key = deriveKey ephPub recipientX25519Pub rawSecret
242− let decrypted = aeadDecrypt ciphertext nonce key
237+ let decrypted = aeadDecrypt ciphertext nonce key
243− let plaintext = if decompress then LZ4Pickler.Unpickle(decrypted) else decrypted
238+ let plaintext = LZ4Pickler.Unpickle(decrypted)
244− Some(Encoding.UTF8.GetString(plaintext), toHex senderPub)
239+ Some(Encoding.UTF8.GetString(plaintext), toHex senderPub)
245− with ex ->
240+ with ex ->
246− log $"decrypt error: {ex.Message}"
241+ log $"decrypt error: {ex.Message}"
249− let version = blob.[0]
251− if version = 2uy then
253− elif version = 1uy then
254− tryDecrypt 1 false |> Option.orElseWith (fun () -> tryDecrypt 0 false)