[RSA] properly indent modules imports

This commit is contained in:
Vincent Hanquez 2015-05-21 06:35:19 +01:00
parent 94d0da9676
commit dc34ce8289
5 changed files with 50 additions and 45 deletions

View File

@ -25,25 +25,29 @@ module Crypto.PubKey.RSA.PKCS15
import Crypto.Random.Types import Crypto.Random.Types
import Crypto.PubKey.Internal (and') import Crypto.PubKey.Internal (and')
import Crypto.PubKey.RSA.Types import Crypto.PubKey.RSA.Types
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Crypto.PubKey.RSA.Prim import Crypto.PubKey.RSA.Prim
import Crypto.PubKey.RSA (generateBlinder) import Crypto.PubKey.RSA (generateBlinder)
import Crypto.PubKey.HashDescr import Crypto.PubKey.HashDescr
import Data.ByteString (ByteString)
import Crypto.Internal.ByteArray (ByteArray, Bytes)
import qualified Crypto.Internal.ByteArray as B
-- | This produce a standard PKCS1.5 padding for encryption -- | This produce a standard PKCS1.5 padding for encryption
pad :: MonadRandom m => Int -> ByteString -> m (Either Error ByteString) pad :: (MonadRandom m, ByteArray message) => Int -> message -> m (Either Error message)
pad len m pad len m
| B.length m > len - 11 = return (Left MessageTooLong) | B.length m > len - 11 = return (Left MessageTooLong)
| otherwise = do | otherwise = do
padding <- getNonNullRandom (len - B.length m - 3) padding <- getNonNullRandom (len - B.length m - 3)
return $ Right $ B.concat [ B.singleton 0, B.singleton 2, padding, B.singleton 0, m ] return $ Right $ B.concat [ B.pack [0,2], padding, B.pack [0], m ]
where {- get random non-null bytes -} where
getNonNullRandom :: MonadRandom m => Int -> m ByteString -- get random non-null bytes
getNonNullRandom :: (ByteArray bytearray, MonadRandom m) => Int -> m bytearray
getNonNullRandom n = do getNonNullRandom n = do
bs0 <- getRandomBytes n bs0 <- getRandomBytes n
let bytes = B.pack $ filter (/= 0) $ B.unpack $ bs0 let bytes = B.pack $ filter (/= 0) $ B.unpack (bs0 :: Bytes)
left = n - B.length bytes left = n - B.length bytes
if left == 0 if left == 0
then return bytes then return bytes
@ -51,16 +55,16 @@ pad len m
return (bytes `B.append` bend) return (bytes `B.append` bend)
-- | Produce a standard PKCS1.5 padding for signature -- | Produce a standard PKCS1.5 padding for signature
padSignature :: Int -> ByteString -> Either Error ByteString padSignature :: ByteArray signature => Int -> signature -> Either Error signature
padSignature klen signature padSignature klen signature
| klen < siglen+1 = Left SignatureTooLong | klen < siglen+1 = Left SignatureTooLong
| otherwise = Right $ B.concat [B.singleton 0,B.singleton 1,padding,B.singleton 0,signature] | otherwise = Right (B.pack padding `B.append` signature)
where where
siglen = B.length signature siglen = B.length signature
padding = B.replicate (klen - siglen - 3) 0xff padding = 0 : 1 : (replicate (klen - siglen - 3) 0xff ++ [0])
-- | Try to remove a standard PKCS1.5 encryption padding. -- | Try to remove a standard PKCS1.5 encryption padding.
unpad :: ByteString -> Either Error ByteString unpad :: ByteArray bytearray => bytearray -> Either Error bytearray
unpad packed unpad packed
| paddingSuccess = Right m | paddingSuccess = Right m
| otherwise = Left MessageNotRecognized | otherwise = Left MessageNotRecognized
@ -68,8 +72,8 @@ unpad packed
(zt, ps0m) = B.splitAt 2 packed (zt, ps0m) = B.splitAt 2 packed
(ps, zm) = B.span (/= 0) ps0m (ps, zm) = B.span (/= 0) ps0m
(z, m) = B.splitAt 1 zm (z, m) = B.splitAt 1 zm
paddingSuccess = and' [ zt == "\x00\x02" paddingSuccess = and' [ zt `B.constEq` (B.pack [0,2] :: Bytes)
, z == "\x00" , z == B.zero 1
, B.length ps >= 8 , B.length ps >= 8
] ]

View File

@ -18,15 +18,16 @@ module Crypto.PubKey.RSA.PSS
import Crypto.Random.Types import Crypto.Random.Types
import Crypto.PubKey.RSA.Types import Crypto.PubKey.RSA.Types
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Crypto.PubKey.RSA.Prim import Crypto.PubKey.RSA.Prim
import Crypto.PubKey.RSA (generateBlinder) import Crypto.PubKey.RSA (generateBlinder)
import Crypto.PubKey.MaskGenFunction import Crypto.PubKey.MaskGenFunction
import Crypto.Hash import Crypto.Hash
import Data.Bits (xor, shiftR, (.&.)) import Data.Bits (xor, shiftR, (.&.))
import Data.Word import Data.Word
import qualified Crypto.Internal.ByteArray as B (convert) import qualified Crypto.Internal.ByteArray as B (convert)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
-- | Parameters for PSS signature/verification. -- | Parameters for PSS signature/verification.
data PSSParams hash = PSSParams data PSSParams hash = PSSParams