-- |-- Module : Crypto.PubKey.RSA.PKCS15-- License : BSD-style-- Maintainer : Vincent Hanquez <vincent@snarc.org>-- Stability : experimental-- Portability : Goodmodule Crypto.PubKey.RSA.PKCS15 ( -- * Padding and unpadding pad, padSignature, unpad, -- * Private key operations decrypt, decryptSafer, sign, signSafer, signDigest, signSaferDigest, signDigestInfo, signSaferDigestInfo, -- * Private key operations with a kept blinding decryptBlinded, signDigestBlinded, signDigestInfoBlinded, -- * Public key operations encrypt, verify, verifyDigest, verifyDigestInfo, -- * Hash ASN1 description HashAlgorithmASN1,) whereimport Crypto.Hashimport Crypto.Number.Serialize (os2ip)import Crypto.PubKey.Internal (and')import Crypto.PubKey.RSA.Blinded.Internalimport Crypto.PubKey.RSA.Primimport Crypto.PubKey.RSA.Typesimport Crypto.Random.Typesimport Data.Bits (complement, shiftR, (.&.), (.|.))import Data.ByteString (ByteString)import Data.Wordimport Crypto.Internal.ByteArray (ByteArray, Bytes)import qualified Crypto.Internal.ByteArray as Bimport qualified Data.List as L-- | A specialized class for hash algorithm that can product-- a ASN1 wrapped description the algorithm plus the content-- of the digest.class HashAlgorithm hashAlg => HashAlgorithmASN1 hashAlg where -- | Convert a Digest into an ASN1 wrapped descriptive ByteArray hashDigestASN1 :: ByteArray out => Digest hashAlg -> out-- http://uk.emc.com/emc-plus/rsa-labs/pkcs/files/h11300-wp-pkcs-1v2-2-rsa-cryptography-standard.pdf-- EMSA-PKCS1-v1_5instance HashAlgorithmASN1 MD2 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x20 , 0x30 , 0x0c , 0x06 , 0x08 , 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x0d , 0x02 , 0x02 , 0x05 , 0x00 , 0x04 , 0x10 ]instance HashAlgorithmASN1 MD5 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x20 , 0x30 , 0x0c , 0x06 , 0x08 , 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x0d , 0x02 , 0x05 , 0x05 , 0x00 , 0x04 , 0x10 ]instance HashAlgorithmASN1 SHA1 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x21 , 0x30 , 0x09 , 0x06 , 0x05 , 0x2b , 0x0e , 0x03 , 0x02 , 0x1a , 0x05 , 0x00 , 0x04 , 0x14 ]instance HashAlgorithmASN1 SHA224 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x2d , 0x30 , 0x0d , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x04 , 0x05 , 0x00 , 0x04 , 0x1c ]instance HashAlgorithmASN1 SHA256 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x31 , 0x30 , 0x0d , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x01 , 0x05 , 0x00 , 0x04 , 0x20 ]instance HashAlgorithmASN1 SHA384 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x41 , 0x30 , 0x0d , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x02 , 0x05 , 0x00 , 0x04 , 0x30 ]instance HashAlgorithmASN1 SHA512 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x51 , 0x30 , 0x0d , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x03 , 0x05 , 0x00 , 0x04 , 0x40 ]instance HashAlgorithmASN1 SHA512t_224 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x2d , 0x30 , 0x0d , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x05 , 0x05 , 0x00 , 0x04 , 0x1c ]instance HashAlgorithmASN1 SHA512t_256 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x31 , 0x30 , 0x0d , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x06 , 0x05 , 0x00 , 0x04 , 0x20 ]instance HashAlgorithmASN1 SHA3_224 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x2b , 0x30 , 0x0b , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x07 , 0x04 , 0x1c ]instance HashAlgorithmASN1 SHA3_256 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x2f , 0x30 , 0x0b , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x08 , 0x04 , 0x20 ]instance HashAlgorithmASN1 SHA3_384 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x3f , 0x30 , 0x0b , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x09 , 0x04 , 0x30 ]instance HashAlgorithmASN1 SHA3_512 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x4f , 0x30 , 0x0b , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x0a , 0x04 , 0x40 ]instance HashAlgorithmASN1 RIPEMD160 where hashDigestASN1 = addDigestPrefix [ 0x30 , 0x21 , 0x30 , 0x09 , 0x06 , 0x05 , 0x2b , 0x24 , 0x03 , 0x02 , 0x01 , 0x05 , 0x00 , 0x04 , 0x14 ]---- ** Hack **---- this happens to not need a real ASN1 encoder, because-- thanks to the digest being a specific size AND-- that the digest data is the last bytes in the encoding,-- this allows to just prepend the right prefix to the-- computed digest, to make it in the expected and valid shape.---- Otherwise the expected structure is in the following form:---- Start Sequence-- ,Start Sequence-- ,OID oid-- ,optional parameters (Null for SHA-2, absent for SHA-3)-- ,End Sequence-- ,OctetString digest-- ,End SequenceaddDigestPrefix :: ByteArray out => [Word8] -> Digest hashAlg -> outaddDigestPrefix prefix digest = B.pack prefix `B.append` B.convert digest-- | This produce a standard PKCS1.5 padding for encryptionpad :: (MonadRandom m, ByteArray message) => Int -> message -> m (Either Error message)pad len m | B.length m > len - 11 = return (Left MessageTooLong) | otherwise = do padding <- getNonNullRandom (len - B.length m - 3) return $ Right $ B.concat [B.pack [0, 2], padding, B.pack [0], m] where -- get random non-null bytes getNonNullRandom :: (ByteArray bytearray, MonadRandom m) => Int -> m bytearray getNonNullRandom n = do bs0 <- getRandomBytes n let bytes = B.pack $ filter (/= 0) $ B.unpack (bs0 :: Bytes) left = n - B.length bytes if left == 0 then return bytes else do bend <- getNonNullRandom left return (bytes `B.append` bend)-- | Produce a standard PKCS1.5 padding for signaturepadSignature :: ByteArray signature => Int -> signature -> Either Error signaturepadSignature klen signature | klen < siglen + 11 = Left SignatureTooLong | otherwise = Right (B.concat [B.pack [0, 1], B.replicate (klen - siglen - 3) 0xff, B.pack [0], signature]) where siglen = B.length signature-- | Try to remove a standard PKCS1.5 encryption padding.---- The block is scanned in full rather than up to the octet ending the padding-- string, so how long that string is does not show up in how long this takes.---- What remains visible is the result itself: whether the padding was well-- formed, and the length of the message when it was. That is inherent to the-- scheme, and it is the signal Bleichenbacher's attack needs, so a caller that-- decrypts attacker-supplied ciphertext must not pass the distinction on ---- TLS, for instance, continues with a random premaster secret and reports-- nothing.unpad :: ByteArray bytearray => bytearray -> Either Error bytearrayunpad packed | paddingSuccess = Right m | otherwise = Left MessageNotRecognized where len = B.length packed (zt, ps0m) = B.splitAt 2 packed -- index of the first zero octet in ps0m, counted from the start of packed, -- or len when there is none; every octet is looked at either way zeroIndex = fst $ L.foldl' step (fromIntegral len :: Word32, 1 :: Word32) indexed indexed = zip [2 ..] (B.unpack ps0m) step (idx, unseen) (i, b) = (select found i idx, unseen .&. complement found) where w = fromIntegral b :: Word32 -- 0 when b is zero, 1 otherwise nonZero = (w .|. negate w) `shiftR` 31 -- all ones at the first zero octet only found = negate (unseen .&. complement nonZero) select mask a b = (a .&. mask) .|. (b .&. complement mask) psLength = fromIntegral zeroIndex - 2 :: Int m = B.drop (fromIntegral zeroIndex + 1) packed paddingSuccess = and' [ zt `B.constEq` (B.pack [0, 2] :: Bytes) , fromIntegral zeroIndex < len , psLength >= 8 ]-- | decrypt message using the private key.---- The blinder is optional and 'Nothing' is accepted, but see t'Blinder' for-- what it covers and when leaving it out is a decision rather than a default.-- 'decryptSafer' generates one for you.---- The message is returned un-padded.---- Following RFC 8017, the ciphertext is rejected unless it is exactly as long-- as the modulus (section 7.2.2, step 1) and its integer representative is-- below the modulus (RSADP, section 5.1.2, step 1). The decryption primitive-- normalises any multiple of the modulus away, so without the second check-- @c + n@ would decrypt to the same message as @c@, and a ciphertext would not-- be unique to its plaintext. Both checks are made on the ciphertext alone,-- which is public, and report 'MessageSizeIncorrect'.decrypt :: ByteArray ba => Maybe Blinder -- ^ optional blinder -> PrivateKey -- ^ RSA private key -> ByteString -- ^ cipher text -> Either Error badecrypt blinder pk c | B.length c /= (private_size pk) = Left MessageSizeIncorrect | os2ip c >= private_n pk = Left MessageSizeIncorrect -- "convert" must be apply to "c". | otherwise = unpad $ dp blinder pk $ B.convert c-- | decrypt message using the private key, blinded with the pair kept for-- this key; see "Crypto.PubKey.RSA.Blinded".decryptSafer :: (MonadRandom m, ByteArray ba) => PrivateKey -- ^ RSA private key -> ByteString -- ^ cipher text -> m (Either Error ba)decryptSafer pk b | B.length b /= private_size pk = return (Left MessageSizeIncorrect) | os2ip b >= private_n pk = return (Left MessageSizeIncorrect) | otherwise = do d <- drawFor pk return $ withBlindingFor pk d $ \pair -> unpad (dpWithPair pair pk (B.convert b))-- | 'decrypt' with the blinding a t'BlindedKey' keeps, and the key for the-- next operation.decryptBlinded :: (MonadRandom m, ByteArray ba) => BlindedKey -> ByteString -- ^ cipher text -> m (Either Error ba, BlindedKey)decryptBlinded bk c | B.length c /= private_size pk = return (Left MessageSizeIncorrect, bk) | os2ip c >= private_n pk = return (Left MessageSizeIncorrect, bk) | otherwise = do (m, bk') <- dpBlinded bk (B.convert c) return (unpad m, bk') where pk = blindedPrivateKey bk-- | encrypt a bytestring using the public key.---- The message needs to be smaller than the key size - 11.-- The message should not be padded.encrypt :: (MonadRandom m, ByteArray ba) => PublicKey -> ba -> m (Either Error ByteString)encrypt pk m = do r <- pad (public_size pk) m case r of Left err -> return $ Left err Right em -> return $ Right (B.convert $ ep pk em)-- | sign message using private key, a hash and its ASN1 description---- __Deprecated.__ Use 'signDigest', or 'signDigestInfo' where this would-- have been given 'Nothing'. The @Maybe hashAlg@ argument says both \"hash-- it with this\" and \"do not hash it\", and down the first path the value-- inside the 'Just' is never read: it is there to fix the type, which-- leaves a caller that has only a type with nothing to pass.---- The blinder is optional and 'Nothing' is accepted, but see t'Blinder' for-- what it covers and when leaving it out is a decision rather than a default.-- 'signSafer' generates one for you.sign :: HashAlgorithmASN1 hashAlg => Maybe Blinder -- ^ optional blinder -> Maybe hashAlg -- ^ hash algorithm -> PrivateKey -- ^ private key -> ByteString -- ^ message to sign -> Either Error ByteStringsign blinder hashDescr pk m = dp blinder pk `fmap` makeSignature hashDescr (private_size pk) m{-# DEPRECATED sign "Use signDigest, or signDigestInfo when the message is already a DigestInfo" #-}-- | sign message using the private key, blinded with the pair kept for this-- key; see "Crypto.PubKey.RSA.Blinded".---- __Deprecated.__ Use 'signSaferDigest', or 'signSaferDigestInfo'.signSafer :: (HashAlgorithmASN1 hashAlg, MonadRandom m) => Maybe hashAlg -- ^ Hash algorithm -> PrivateKey -- ^ private key -> ByteString -- ^ message to sign -> m (Either Error ByteString)signSafer hashAlg pk m = do d <- drawFor pk return $ signWithBlindingFor pk d (makeSignature hashAlg (private_size pk) m){-# DEPRECATED signSafer "Use signSaferDigest, or signSaferDigestInfo when the message is already a DigestInfo" #-}-- | verify message with the signed message---- __Deprecated.__ Use 'verifyDigest', or 'verifyDigestInfo' where this-- would have been given 'Nothing'.---- Following RFC 8017, the signature is rejected unless it is exactly as long-- as the modulus (section 8.2.2, step 1) and its integer representative is-- below the modulus (section 5.2.2, step 1). Verification works by-- re-encoding the expected signature and comparing it with the result of the-- public-key operation, and that operation normalises away both the length of-- the encoding and any multiple of the modulus; without these checks a-- zero-padded signature, or @s + n@, would verify just as well as @s@.verify :: HashAlgorithmASN1 hashAlg => Maybe hashAlg -> PublicKey -> ByteString -- ^ Message -> ByteString -- ^ Signature -> Boolverify hashAlg pk m sm = verifyEncoded pk (makeSignature hashAlg (public_size pk) m) sm{-# DEPRECATED verify "Use verifyDigest, or verifyDigestInfo when the message is already a DigestInfo" #-}-- | The two checks of RFC 8017 and the comparison, shared by the three-- verification entry points. The expected encoding is a thunk and is-- forced only once the checks have passed, as it was when this was written-- out inside 'verify'.verifyEncoded :: PublicKey -> Either Error ByteString -- ^ the encoding a signature of this message would have -> ByteString -- ^ signature -> BoolverifyEncoded pk expected sm | B.length sm /= public_size pk = False | os2ip sm >= public_n pk = False | otherwise = case expected of Left _ -> False Right s -> s == ep pk sm-- | Sign a digest.---- The digest's type says which algorithm made it, so this needs nothing-- else to name one: the ASN.1 DigestInfo prefix comes from the-- 'HashAlgorithmASN1' instance that type selects. 'sign' takes @Maybe-- hashAlg@ and never reads the value inside the @Just@ -- it is there only-- to fix the type -- which is no burden when the caller has a value and-- leaves nothing to pass when the caller is itself polymorphic in the-- algorithm.---- > signDigest blinder key (hashWith SHA256 message)---- The blinder is optional and 'Nothing' is accepted, but see t'Blinder' for-- what it covers and when leaving it out is a decision rather than a-- default. 'signSaferDigest' generates one for you.signDigest :: HashAlgorithmASN1 hashAlg => Maybe Blinder -- ^ optional blinder -> PrivateKey -- ^ private key -> Digest hashAlg -- ^ digest of the message to sign -> Either Error ByteStringsignDigest blinder pk digest = dp blinder pk `fmap` padSignature (private_size pk) (hashDigestASN1 digest)-- | 'signDigest' blinded with the pair kept for this key, as 'signSafer' is-- to 'sign'; see "Crypto.PubKey.RSA.Blinded".signSaferDigest :: (HashAlgorithmASN1 hashAlg, MonadRandom m) => PrivateKey -- ^ private key -> Digest hashAlg -- ^ digest of the message to sign -> m (Either Error ByteString)signSaferDigest pk digest = do d <- drawFor pk return $ signWithBlindingFor pk d (padSignature (private_size pk) (hashDigestASN1 digest))-- | 'signDigest' with the blinding a t'BlindedKey' keeps, and the key for the-- next operation.signDigestBlinded :: (HashAlgorithmASN1 hashAlg, MonadRandom m) => BlindedKey -> Digest hashAlg -- ^ digest of the message to sign -> m (Either Error ByteString, BlindedKey)signDigestBlinded bk digest = signBlindedEncoded bk $ padSignature (private_size (blindedPrivateKey bk)) (hashDigestASN1 digest)-- | Sign something that is already a DigestInfo, the ASN.1 structure-- naming a hash algorithm and carrying a digest under it.---- This is what @'sign' blinder 'Nothing'@ does. It needs no-- 'HashAlgorithmASN1' constraint, because nothing here hashes or encodes:-- the caller has done both. @'sign' blinder 'Nothing'@ carries the-- constraint anyway, and since the type variable then appears nowhere else-- the caller has to name an algorithm that is never used ---- @'sign' blinder ('Nothing' :: 'Maybe' 'Crypto.Hash.SHA256')@ -- to say-- which one it is not using.signDigestInfo :: Maybe Blinder -- ^ optional blinder -> PrivateKey -- ^ private key -> ByteString -- ^ a DigestInfo, encoded -> Either Error ByteStringsignDigestInfo blinder pk di = dp blinder pk `fmap` padSignature (private_size pk) di-- | 'signDigestInfo' blinded with the pair kept for this key.signSaferDigestInfo :: MonadRandom m => PrivateKey -- ^ private key -> ByteString -- ^ a DigestInfo, encoded -> m (Either Error ByteString)signSaferDigestInfo pk di = do d <- drawFor pk return $ signWithBlindingFor pk d (padSignature (private_size pk) di)-- | 'signDigestInfo' with the blinding a t'BlindedKey' keeps, and the key for-- the next operation.signDigestInfoBlinded :: MonadRandom m => BlindedKey -> ByteString -- ^ a DigestInfo, encoded -> m (Either Error ByteString, BlindedKey)signDigestInfoBlinded bk di = signBlindedEncoded bk $ padSignature (private_size (blindedPrivateKey bk)) di-- | The private-key operation on an encoded signature, if there is one; the-- key is used up only if there is.signBlindedEncoded :: MonadRandom m => BlindedKey -> Either Error ByteString -> m (Either Error ByteString, BlindedKey)signBlindedEncoded bk (Left err) = return (Left err, bk)signBlindedEncoded bk (Right em) = do (s, bk') <- dpBlinded bk em return (Right s, bk')-- | The same, with the pair the @Safer@ functions keep for this key.signWithBlindingFor :: PrivateKey -> Draw -> Either Error ByteString -> Either Error ByteStringsignWithBlindingFor _ _ (Left err) = Left errsignWithBlindingFor pk d (Right em) = Right (withBlindingFor pk d (\pair -> dpWithPair pair pk em))-- | Verify a signature over a digest. The checks are 'verify's.verifyDigest :: HashAlgorithmASN1 hashAlg => PublicKey -> Digest hashAlg -- ^ digest of the message -> ByteString -- ^ signature -> BoolverifyDigest pk digest sm = verifyEncoded pk (padSignature (public_size pk) (hashDigestASN1 digest)) sm-- | Verify a signature over something that is already a DigestInfo, which-- is what @'verify' 'Nothing'@ does.verifyDigestInfo :: PublicKey -> ByteString -- ^ a DigestInfo, encoded -> ByteString -- ^ signature -> BoolverifyDigestInfo pk di sm = verifyEncoded pk (padSignature (public_size pk) di) sm-- | make signature digest, used in 'sign' and 'verify'makeSignature :: HashAlgorithmASN1 hashAlg => Maybe hashAlg -- ^ optional hashing algorithm -> Int -> ByteString -> Either Error ByteStringmakeSignature Nothing klen m = padSignature klen mmakeSignature (Just hashAlg) klen m = padSignature klen (hashDigestASN1 $ hashWith hashAlg m)