From c25a6d68964a0db52012392adae92e93825a211d Mon Sep 17 00:00:00 2001 From: Efim Poberezkin Date: Mon, 15 Feb 2021 19:51:02 +0400 Subject: [PATCH] crypto: clean up magic numbers (#40) --- src/Simplex/Messaging/Crypto.hs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index ff437b146..d4f387942 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -1,6 +1,5 @@ {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE DeriveAnyClass #-} -{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -102,6 +101,12 @@ data CryptoError pubExpRange :: Integer pubExpRange = 2 ^ (1024 :: Int) +aeKeySize :: Int +aeKeySize = 256 `div` 8 + +aeTagSize :: Int +aeTagSize = 128 `div` 8 + generateKeyPair :: Int -> IO KeyPair generateKeyPair size = loop where @@ -118,7 +123,7 @@ generateKeyPair size = loop encrypt :: PublicKey -> ByteString -> ExceptT CryptoError IO ByteString encrypt k msg = do - aesKey <- randomBytes 32 + aesKey <- randomBytes aeKeySize ivBytes <- randomIVBytes @AES256 aead <- initAEAD @AES256 (aesKey, ivBytes) let (authTag, msg') = encryptAES aead msg @@ -128,13 +133,13 @@ encrypt k msg = do decrypt :: PrivateKey -> ByteString -> ExceptT CryptoError IO ByteString decrypt pk msg'' = do let (encKeyIv, msg') = B.splitAt (private_size pk) msg'' - (authTag, msg) = B.splitAt 16 msg' - keyIv <- B.splitAt 32 <$> decryptOAEP pk encKeyIv + (authTag, msg) = B.splitAt aeTagSize msg' + keyIv <- B.splitAt aeKeySize <$> decryptOAEP pk encKeyIv aead <- initAEAD @AES256 keyIv decryptAES aead msg (bsToAuthTag authTag) encryptAES :: AES.AEAD AES256 -> ByteString -> (AES.AuthTag, ByteString) -encryptAES aead plaintext = AES.aeadSimpleEncrypt aead B.empty plaintext 16 +encryptAES aead plaintext = AES.aeadSimpleEncrypt aead B.empty plaintext aeTagSize decryptAES :: AES.AEAD AES256 -> ByteString -> AES.AuthTag -> ExceptT CryptoError IO ByteString decryptAES aead ciphertext authTag =