Skip to content

Commit

Permalink
Use stripPrefix to simplify decrypt.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed May 8, 2024
1 parent a0d7c88 commit 49f2040
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/crypto-primitives/src/Cryptography/Cipher/AES256CBC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,18 @@ decrypt mode key iv msg = do
when (mode == WithoutPadding && BS.length msg `mod` 16 /= 0) $
Left WrongPayloadSize
initedIV <- first FromCryptonite (createIV iv)
let (prefix,rest) = BS.splitAt 8 msg
let saltDetected = prefix == saltPrefix
if saltDetected then
second (, Just $ BS.take saltLengthBytes rest) $
bimap FromCryptonite
(\c -> cbcDecrypt c initedIV (BS.drop saltLengthBytes rest))
(initCipher key) >>=
unpad
else
second (, Nothing) $
bimap FromCryptonite
(\c -> cbcDecrypt c initedIV msg) (initCipher key) >>=
unpad
case BS.stripPrefix saltPrefix msg of
Just rest ->
second (, Just $ BS.take saltLengthBytes rest) $
bimap FromCryptonite
(\c -> cbcDecrypt c initedIV (BS.drop saltLengthBytes rest))
(initCipher key) >>=
unpad
Nothing ->
second (, Nothing) $
bimap FromCryptonite
(\c -> cbcDecrypt c initedIV msg) (initCipher key) >>=
unpad
where
unpad :: ByteString -> Either CipherError ByteString
unpad p = case mode of
Expand Down

0 comments on commit 49f2040

Please sign in to comment.