[pubkey] remove bytestring from MaskGenFunction

This commit is contained in:
Vincent Hanquez 2015-05-21 11:09:48 +01:00
parent 22c1a1bb7f
commit c111dfeb8e
3 changed files with 50 additions and 35 deletions

View File

@ -5,28 +5,36 @@
-- Stability : experimental -- Stability : experimental
-- Portability : Good -- Portability : Good
-- --
{-# LANGUAGE BangPatterns #-}
module Crypto.PubKey.MaskGenFunction module Crypto.PubKey.MaskGenFunction
( MaskGenAlgorithm ( MaskGenAlgorithm
, mgf1 , mgf1
) where ) where
import Data.ByteString (ByteString) import Crypto.Number.Serialize (i2ospOf_)
import qualified Data.ByteString as B import Crypto.Hash
import Crypto.Number.Serialize (i2ospOf_) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray, Bytes)
import Crypto.Hash (hashWith, HashAlgorithm) import qualified Crypto.Internal.ByteArray as B
import qualified Crypto.Internal.ByteArray as B (convert)
-- | Represent a mask generation algorithm -- | Represent a mask generation algorithm
type MaskGenAlgorithm = type MaskGenAlgorithm seed output =
ByteString -- ^ seed seed -- ^ seed
-> Int -- ^ length to generate -> Int -- ^ length to generate
-> ByteString -> output
-- | Mask generation algorithm MGF1 -- | Mask generation algorithm MGF1
mgf1 :: HashAlgorithm hashAlg => hashAlg -> MaskGenAlgorithm mgf1 :: (ByteArrayAccess seed, ByteArray output, HashAlgorithm hashAlg)
mgf1 hashAlg seed len = loop B.empty 0 => hashAlg
where loop t counter -> seed
| B.length t >= len = B.take len t -> Int
| otherwise = let counterBS = i2ospOf_ 4 counter -> output
newT = t `B.append` B.convert (hashWith hashAlg (seed `B.append` counterBS)) mgf1 hashAlg seed len =
in loop newT (counter+1) let !seededCtx = hashUpdate (hashInitWith hashAlg) seed
in B.take len $ B.concat $ map (hashCounter seededCtx) [0..fromIntegral (maxCounter-1)]
where
digestLen = hashDigestSize hashAlg
(chunks,left) = len `divMod` digestLen
maxCounter = if left > 0 then chunks + 1 else chunks
hashCounter :: HashAlgorithm a => Context a -> Integer -> Digest a
hashCounter ctx counter = hashFinalize $ hashUpdate ctx (i2ospOf_ 4 counter :: Bytes)

View File

@ -31,17 +31,20 @@ import Data.ByteString (ByteString)
import qualified Data.ByteString as B import qualified Data.ByteString as B
import Data.Bits (xor) import Data.Bits (xor)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B (convert) import qualified Crypto.Internal.ByteArray as B (convert)
-- | Parameters for OAEP encryption/decryption -- | Parameters for OAEP encryption/decryption
data OAEPParams hash = OAEPParams data OAEPParams hash seed output = OAEPParams
{ oaepHash :: hash -- ^ Hash function to use. { oaepHash :: hash -- ^ Hash function to use.
, oaepMaskGenAlg :: MaskGenAlgorithm -- ^ Mask Gen algorithm to use. , oaepMaskGenAlg :: MaskGenAlgorithm seed output -- ^ Mask Gen algorithm to use.
, oaepLabel :: Maybe ByteString -- ^ Optional label prepended to message. , oaepLabel :: Maybe ByteString -- ^ Optional label prepended to message.
} }
-- | Default Params with a specified hash function -- | Default Params with a specified hash function
defaultOAEPParams :: HashAlgorithm hash => hash -> OAEPParams hash defaultOAEPParams :: (ByteArrayAccess seed, ByteArray output, HashAlgorithm hash)
=> hash
-> OAEPParams hash seed output
defaultOAEPParams hashAlg = defaultOAEPParams hashAlg =
OAEPParams { oaepHash = hashAlg OAEPParams { oaepHash = hashAlg
, oaepMaskGenAlg = mgf1 hashAlg , oaepMaskGenAlg = mgf1 hashAlg
@ -51,7 +54,7 @@ defaultOAEPParams hashAlg =
-- | Encrypt a message using OAEP with a predefined seed. -- | Encrypt a message using OAEP with a predefined seed.
encryptWithSeed :: HashAlgorithm hash encryptWithSeed :: HashAlgorithm hash
=> ByteString -- ^ Seed => ByteString -- ^ Seed
-> OAEPParams hash -- ^ OAEP params to use for encryption -> OAEPParams hash ByteString ByteString -- ^ OAEP params to use for encryption
-> PublicKey -- ^ Public key. -> PublicKey -- ^ Public key.
-> ByteString -- ^ Message to encrypt -> ByteString -- ^ Message to encrypt
-> Either Error ByteString -> Either Error ByteString
@ -78,7 +81,7 @@ encryptWithSeed seed oaep pk msg
-- | Encrypt a message using OAEP -- | Encrypt a message using OAEP
encrypt :: (HashAlgorithm hash, MonadRandom m) encrypt :: (HashAlgorithm hash, MonadRandom m)
=> OAEPParams hash -- ^ OAEP params to use for encryption. => OAEPParams hash ByteString ByteString -- ^ OAEP params to use for encryption.
-> PublicKey -- ^ Public key. -> PublicKey -- ^ Public key.
-> ByteString -- ^ Message to encrypt -> ByteString -- ^ Message to encrypt
-> m (Either Error ByteString) -> m (Either Error ByteString)
@ -92,7 +95,7 @@ encrypt oaep pk msg = do
-- --
-- It doesn't apply the RSA decryption primitive -- It doesn't apply the RSA decryption primitive
unpad :: HashAlgorithm hash unpad :: HashAlgorithm hash
=> OAEPParams hash -- ^ OAEP params to use => OAEPParams hash ByteString ByteString -- ^ OAEP params to use
-> Int -- ^ size of the key in bytes -> Int -- ^ size of the key in bytes
-> ByteString -- ^ encoded message (not encrypted) -> ByteString -- ^ encoded message (not encrypted)
-> Either Error ByteString -> Either Error ByteString
@ -128,7 +131,7 @@ unpad oaep k em
-- If unsure always set a blinder or use decryptSafer -- If unsure always set a blinder or use decryptSafer
decrypt :: HashAlgorithm hash decrypt :: HashAlgorithm hash
=> Maybe Blinder -- ^ Optional blinder => Maybe Blinder -- ^ Optional blinder
-> OAEPParams hash -- ^ OAEP params to use for decryption -> OAEPParams hash ByteString ByteString -- ^ OAEP params to use for decryption
-> PrivateKey -- ^ Private key -> PrivateKey -- ^ Private key
-> ByteString -- ^ Cipher text -> ByteString -- ^ Cipher text
-> Either Error ByteString -> Either Error ByteString
@ -142,7 +145,7 @@ decrypt blinder oaep pk cipher
-- | Decrypt a ciphertext using OAEP and by automatically generating a blinder. -- | Decrypt a ciphertext using OAEP and by automatically generating a blinder.
decryptSafer :: (HashAlgorithm hash, MonadRandom m) decryptSafer :: (HashAlgorithm hash, MonadRandom m)
=> OAEPParams hash -- ^ OAEP params to use for decryption => OAEPParams hash ByteString ByteString -- ^ OAEP params to use for decryption
-> PrivateKey -- ^ Private key -> PrivateKey -- ^ Private key
-> ByteString -- ^ Cipher text -> ByteString -- ^ Cipher text
-> m (Either Error ByteString) -> m (Either Error ByteString)

View File

@ -25,20 +25,23 @@ import Crypto.Hash
import Data.Bits (xor, shiftR, (.&.)) import Data.Bits (xor, shiftR, (.&.))
import Data.Word import Data.Word
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B (convert) import qualified Crypto.Internal.ByteArray as B (convert)
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import qualified Data.ByteString as B import qualified Data.ByteString as B
-- | Parameters for PSS signature/verification. -- | Parameters for PSS signature/verification.
data PSSParams hash = PSSParams data PSSParams hash seed output = PSSParams
{ pssHash :: hash -- ^ Hash function to use { pssHash :: hash -- ^ Hash function to use
, pssMaskGenAlg :: MaskGenAlgorithm -- ^ Mask Gen algorithm to use , pssMaskGenAlg :: MaskGenAlgorithm seed output -- ^ Mask Gen algorithm to use
, pssSaltLength :: Int -- ^ Length of salt. need to be <= to hLen. , pssSaltLength :: Int -- ^ Length of salt. need to be <= to hLen.
, pssTrailerField :: Word8 -- ^ Trailer field, usually 0xbc , pssTrailerField :: Word8 -- ^ Trailer field, usually 0xbc
} }
-- | Default Params with a specified hash function -- | Default Params with a specified hash function
defaultPSSParams :: HashAlgorithm hash => hash -> PSSParams hash defaultPSSParams :: (ByteArrayAccess seed, ByteArray output, HashAlgorithm hash)
=> hash
-> PSSParams hash seed output
defaultPSSParams hashAlg = defaultPSSParams hashAlg =
PSSParams { pssHash = hashAlg PSSParams { pssHash = hashAlg
, pssMaskGenAlg = mgf1 hashAlg , pssMaskGenAlg = mgf1 hashAlg
@ -47,7 +50,7 @@ defaultPSSParams hashAlg =
} }
-- | Default Params using SHA1 algorithm. -- | Default Params using SHA1 algorithm.
defaultPSSParamsSHA1 :: PSSParams SHA1 defaultPSSParamsSHA1 :: PSSParams SHA1 ByteString ByteString
defaultPSSParamsSHA1 = defaultPSSParams SHA1 defaultPSSParamsSHA1 = defaultPSSParams SHA1
-- | Sign using the PSS parameters and the salt explicitely passed as parameters. -- | Sign using the PSS parameters and the salt explicitely passed as parameters.
@ -56,7 +59,7 @@ defaultPSSParamsSHA1 = defaultPSSParams SHA1
signWithSalt :: HashAlgorithm hash signWithSalt :: HashAlgorithm hash
=> ByteString -- ^ Salt to use => ByteString -- ^ Salt to use
-> Maybe Blinder -- ^ optional blinder to use -> Maybe Blinder -- ^ optional blinder to use
-> PSSParams hash -- ^ PSS Parameters to use -> PSSParams hash ByteString ByteString -- ^ PSS Parameters to use
-> PrivateKey -- ^ RSA Private Key -> PrivateKey -- ^ RSA Private Key
-> ByteString -- ^ Message to sign -> ByteString -- ^ Message to sign
-> Either Error ByteString -> Either Error ByteString
@ -80,7 +83,7 @@ signWithSalt salt blinder params pk m
-- | Sign using the PSS Parameters -- | Sign using the PSS Parameters
sign :: (HashAlgorithm hash, MonadRandom m) sign :: (HashAlgorithm hash, MonadRandom m)
=> Maybe Blinder -- ^ optional blinder to use => Maybe Blinder -- ^ optional blinder to use
-> PSSParams hash -- ^ PSS Parameters to use -> PSSParams hash ByteString ByteString -- ^ PSS Parameters to use
-> PrivateKey -- ^ RSA Private Key -> PrivateKey -- ^ RSA Private Key
-> ByteString -- ^ Message to sign -> ByteString -- ^ Message to sign
-> m (Either Error ByteString) -> m (Either Error ByteString)
@ -90,7 +93,7 @@ sign blinder params pk m = do
-- | Sign using the PSS Parameters and an automatically generated blinder. -- | Sign using the PSS Parameters and an automatically generated blinder.
signSafer :: (HashAlgorithm hash, MonadRandom m) signSafer :: (HashAlgorithm hash, MonadRandom m)
=> PSSParams hash -- ^ PSS Parameters to use => PSSParams hash ByteString ByteString -- ^ PSS Parameters to use
-> PrivateKey -- ^ private key -> PrivateKey -- ^ private key
-> ByteString -- ^ message to sign -> ByteString -- ^ message to sign
-> m (Either Error ByteString) -> m (Either Error ByteString)
@ -100,8 +103,9 @@ signSafer params pk m = do
-- | Verify a signature using the PSS Parameters -- | Verify a signature using the PSS Parameters
verify :: HashAlgorithm hash verify :: HashAlgorithm hash
=> PSSParams hash -- ^ PSS Parameters to use to verify, => PSSParams hash ByteString ByteString
-- this need to be identical to the parameters when signing -- ^ PSS Parameters to use to verify,
-- this need to be identical to the parameters when signing
-> PublicKey -- ^ RSA Public Key -> PublicKey -- ^ RSA Public Key
-> ByteString -- ^ Message to verify -> ByteString -- ^ Message to verify
-> ByteString -- ^ Signature -> ByteString -- ^ Signature