{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE MultiWayIf         #-}
module Codec.Crypto.RSA.Pure(
         RSAError(..)
       , HashInfo(..)
       -- * Keys and key generation
       , PrivateKey(..)
       , PublicKey(..)
       , generateKeyPair
       -- * High-level encryption and signature functions
       , encrypt
       , encryptOAEP
       , encryptPKCS
       , decrypt
       , decryptOAEP
       , decryptPKCS
       , sign
       , verify
       -- * Core routines for OAEP
       , MGF
       , generateMGF1
       , rsaes_oaep_encrypt
       , rsaes_oaep_decrypt
       -- * Core PSS routines
       -- $pss
       -- * Core PKCS1 (v1.5) Routines
       , rsaes_pkcs1_v1_5_encrypt
       , rsaes_pkcs1_v1_5_decrypt
       , rsassa_pkcs1_v1_5_sign
       , rsassa_pkcs1_v1_5_verify
       -- * Hashing algorithm declarations for use in RSA functions
       , hashSHA1
       , hashSHA224, hashSHA256, hashSHA384, hashSHA512
       -- * Other mathematical functions that are handy for implementing
       -- other RSA primitives.
       , largeRandomPrime
       , generatePQ
       , chunkify
       , os2ip, i2osp
       , rsa_dp, rsa_ep
       , rsa_vp1, rsa_sp1
       , modular_inverse
       , modular_exponentiation
       , randomBS, randomNZBS
       )
 where


import Control.Exception
import Control.Monad
import Crypto.Random
import Crypto.Types.PubKey.RSA
import Data.Binary
import Data.Binary.Get
import Data.Binary.Put
import Data.Bits
import Data.ByteString.Lazy(ByteString)
import qualified Data.ByteString.Lazy as BS
import Data.Digest.Pure.SHA
import Data.Int
import Data.Typeable

data RSAError = RSAError String
              | RSAKeySizeTooSmall
              | RSAIntegerTooLargeToPack
              | RSAMessageRepOutOfRange
              | RSACipherRepOutOfRange
              | RSAMessageTooShort
              | RSAMessageTooLong
              | RSAMaskTooLong
              | RSAIncorrectSigSize
              | RSAIncorrectMsgSize
              | RSADecryptionError
              | RSAGenError GenError
 deriving (RSAError -> RSAError -> Bool
(RSAError -> RSAError -> Bool)
-> (RSAError -> RSAError -> Bool) -> Eq RSAError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RSAError -> RSAError -> Bool
$c/= :: RSAError -> RSAError -> Bool
== :: RSAError -> RSAError -> Bool
$c== :: RSAError -> RSAError -> Bool
Eq, Int -> RSAError -> ShowS
[RSAError] -> ShowS
RSAError -> String
(Int -> RSAError -> ShowS)
-> (RSAError -> String) -> ([RSAError] -> ShowS) -> Show RSAError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RSAError] -> ShowS
$cshowList :: [RSAError] -> ShowS
show :: RSAError -> String
$cshow :: RSAError -> String
showsPrec :: Int -> RSAError -> ShowS
$cshowsPrec :: Int -> RSAError -> ShowS
Show, Typeable)

instance Exception RSAError

data HashInfo = HashInfo {
    HashInfo -> ByteString
algorithmIdent :: ByteString -- ^The ASN.1 DER encoding of the hash function
                                 -- identifier.
  , HashInfo -> ByteString -> ByteString
hashFunction   :: ByteString -> ByteString -- ^The hash function
  }

instance Show SystemRandom where
  show :: SystemRandom -> String
show _ = "SystemRandom"

class RSAKey a where
  genKeySize :: a -> Int

instance RSAKey PublicKey where
  genKeySize :: PublicKey -> Int
genKeySize = PublicKey -> Int
public_size

instance RSAKey PrivateKey where
  genKeySize :: PrivateKey -> Int
genKeySize = PrivateKey -> Int
private_size

instance Binary PublicKey where
  put :: PublicKey -> Put
put pk :: PublicKey
pk = do ByteString
sizeBS <- Either RSAError ByteString -> PutM ByteString
forall (m :: * -> *) a b. (Monad m, Show a) => Either a b -> m b
failOnError (Int -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp (PublicKey -> Int
public_size PublicKey
pk) 8)
              ByteString
nBS <- Either RSAError ByteString -> PutM ByteString
forall (m :: * -> *) a b. (Monad m, Show a) => Either a b -> m b
failOnError (Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp (PublicKey -> Integer
public_n PublicKey
pk) (PublicKey -> Int
public_size PublicKey
pk))
              ByteString -> Put
putLazyByteString ByteString
sizeBS
              ByteString -> Put
putLazyByteString ByteString
nBS
  get :: Get PublicKey
get    = do Int64
len <- (Integer -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Int64)
-> (ByteString -> Integer) -> ByteString -> Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Integer
os2ip) (ByteString -> Int64) -> Get ByteString -> Get Int64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int64 -> Get ByteString
getLazyByteString 8
              Integer
n   <- ByteString -> Integer
os2ip (ByteString -> Integer) -> Get ByteString -> Get Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int64 -> Get ByteString
getLazyByteString Int64
len
              PublicKey -> Get PublicKey
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Integer -> Integer -> PublicKey
PublicKey (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
len) Integer
n 65537)

instance Binary PrivateKey where
  put :: PrivateKey -> Put
put pk :: PrivateKey
pk = do PublicKey -> Put
forall t. Binary t => t -> Put
put (PrivateKey -> PublicKey
private_pub PrivateKey
pk)
              ByteString
dBS <- Either RSAError ByteString -> PutM ByteString
forall (m :: * -> *) a b. (Monad m, Show a) => Either a b -> m b
failOnError (Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp (PrivateKey -> Integer
private_d PrivateKey
pk) (PublicKey -> Int
public_size (PrivateKey -> PublicKey
private_pub PrivateKey
pk)))
              ByteString -> Put
putLazyByteString ByteString
dBS
  get :: Get PrivateKey
get    = do PublicKey
pub <- Get PublicKey
forall t. Binary t => Get t
get
              Integer
d   <- ByteString -> Integer
os2ip (ByteString -> Integer) -> Get ByteString -> Get Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int64 -> Get ByteString
getLazyByteString (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PublicKey -> Int
public_size PublicKey
pub))
              PrivateKey -> Get PrivateKey
forall (m :: * -> *) a. Monad m => a -> m a
return (PublicKey
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> PrivateKey
PrivateKey PublicKey
pub Integer
d 0 0 0 0 0)

failOnError :: (Monad m, Show a) => Either a b -> m b
failOnError :: Either a b -> m b
failOnError (Left e :: a
e)  = String -> m b
forall a. HasCallStack => String -> a
error (a -> String
forall a. Show a => a -> String
show a
e)
failOnError (Right b :: b
b) = b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b

-- ----------------------------------------------------------------------------

-- |Randomly generate a key pair of the given modules length (in bits) to use
-- in any of the following functions. Use of a good random number generator is
-- of considerable importance when using this function. The input
-- CryptoRandomGen should never be used again for any other purpose; either
-- use the output'd generator or throw it all away.
generateKeyPair :: CryptoRandomGen g =>
                   g -> Int ->
                   Either RSAError (PublicKey, PrivateKey, g)
generateKeyPair :: g -> Int -> Either RSAError (PublicKey, PrivateKey, g)
generateKeyPair g :: g
g sizeBits :: Int
sizeBits = do
  let keyLength :: Int
keyLength = Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
sizeBits Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 8)
  (p :: Integer
p, q :: Integer
q, g' :: g
g') <- g -> Int -> Either RSAError (Integer, Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, Integer, g)
generatePQ g
g Int
keyLength
  let n :: Integer
n          = Integer
p Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
q
      phi :: Integer
phi        = (Integer
p Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 1) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer
q Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 1)
      e :: Integer
e          = 65537
      d :: Integer
d          = Integer -> Integer -> Integer
modular_inverse Integer
e Integer
phi
  let publicKey :: PublicKey
publicKey  = Int -> Integer -> Integer -> PublicKey
PublicKey Int
keyLength Integer
n Integer
e
      privateKey :: PrivateKey
privateKey = PublicKey
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> PrivateKey
PrivateKey PublicKey
publicKey Integer
d Integer
p Integer
q 0 0 0
  (PublicKey, PrivateKey, g)
-> Either RSAError (PublicKey, PrivateKey, g)
forall (m :: * -> *) a. Monad m => a -> m a
return (PublicKey
publicKey, PrivateKey
privateKey, g
g')

-- ----------------------------------------------------------------------------

-- |Compute a signature for the given ByteString, using the SHA256 algorithm
-- in the computation. This is currently defined as rsassa_pkcs1_v1_5_sign
-- hashSHA256. If you want to use a different function, simply use the PKCS
-- function, below; it will accept arbitrarily-length messages.
sign :: PrivateKey -> ByteString -> Either RSAError ByteString
sign :: PrivateKey -> ByteString -> Either RSAError ByteString
sign = HashInfo -> PrivateKey -> ByteString -> Either RSAError ByteString
rsassa_pkcs1_v1_5_sign HashInfo
hashSHA256

-- |Verify a signature for the given ByteString, using the SHA25 algorithm in
-- the computation. Again, if you'd like to use a different algorithm, use the
-- rsassa_pkcs1_v1_5_verify function.
verify :: PublicKey {- ^The key of the signer -} ->
          ByteString {- ^The message -} ->
          ByteString {- ^The purported signature -} ->
          Either RSAError Bool
verify :: PublicKey -> ByteString -> ByteString -> Either RSAError Bool
verify = HashInfo
-> PublicKey -> ByteString -> ByteString -> Either RSAError Bool
rsassa_pkcs1_v1_5_verify HashInfo
hashSHA256

-- ----------------------------------------------------------------------------

-- |Encrypt an arbitrarily-sized message given the public key and reasonable
-- options. This is equivalent to calling encryptOAEP with SHA-256 as the
-- hash function, MGF1(SHA-256) as the mask generation function, and no label.
-- NOTE: This hash choice means that your key size must be 1024 bits or larger.
encrypt :: CryptoRandomGen g =>
           g -> PublicKey -> ByteString ->
           Either RSAError (ByteString, g)
encrypt :: g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
encrypt g :: g
g k :: PublicKey
k m :: ByteString
m = g
-> (ByteString -> ByteString)
-> MGF
-> ByteString
-> PublicKey
-> ByteString
-> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g
-> (ByteString -> ByteString)
-> MGF
-> ByteString
-> PublicKey
-> ByteString
-> Either RSAError (ByteString, g)
encryptOAEP g
g ByteString -> ByteString
sha256' ((ByteString -> ByteString) -> MGF
generateMGF1 ByteString -> ByteString
sha256') ByteString
BS.empty PublicKey
k ByteString
m
 where sha256' :: ByteString -> ByteString
sha256' = Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA256State -> ByteString)
-> (ByteString -> Digest SHA256State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256State
sha256

-- |Encrypt an arbitrarily-sized message using OAEP encoding. This is the
-- encouraged encoding for doing RSA encryption. Note that your key size
-- must be greater than (2 * hash length + 2) * 8. (For example, the
-- 'encrypt' convenience function uses a 256 bit / 32 byte hash function.
-- Thus, its key must be greater than (2 * 32 + 2) * 8 = 528 bits long,
-- and we suggest 1024 as a lower bound.)
encryptOAEP :: CryptoRandomGen g =>
               g ->
               (ByteString -> ByteString) {- ^The hash function to use -} ->
               MGF {- ^The mask generation function to use -} ->
               ByteString {- ^An optional label to include -} ->
               PublicKey {- ^The public key to encrypt with -} ->
               ByteString {- ^The message to encrypt -} ->
               Either RSAError (ByteString, g)
encryptOAEP :: g
-> (ByteString -> ByteString)
-> MGF
-> ByteString
-> PublicKey
-> ByteString
-> Either RSAError (ByteString, g)
encryptOAEP g :: g
g hash :: ByteString -> ByteString
hash mgf :: MGF
mgf l :: ByteString
l k :: PublicKey
k m :: ByteString
m =
  do Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Int
keySize Int -> Int -> Int
forall a. Num a => a -> a -> a
- (2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
hashLength) Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSAKeySizeTooSmall
     let chunks :: [ByteString]
chunks = PublicKey
-> (ByteString -> ByteString) -> ByteString -> [ByteString]
forall k.
RSAKey k =>
k -> (ByteString -> ByteString) -> ByteString -> [ByteString]
chunkBSForOAEP PublicKey
k ByteString -> ByteString
hash ByteString
m
     (chunks' :: [ByteString]
chunks', g' :: g
g') <- g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
forall g.
CryptoRandomGen g =>
g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
mapM' g
g [ByteString]
chunks (\ x :: g
x -> g
-> (ByteString -> ByteString)
-> MGF
-> PublicKey
-> ByteString
-> ByteString
-> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g
-> (ByteString -> ByteString)
-> MGF
-> PublicKey
-> ByteString
-> ByteString
-> Either RSAError (ByteString, g)
rsaes_oaep_encrypt g
x ByteString -> ByteString
hash MGF
mgf PublicKey
k ByteString
l)
     (ByteString, g) -> Either RSAError (ByteString, g)
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BS.concat [ByteString]
chunks', g
g')
 where
  keySize :: Int
keySize = PublicKey -> Int
public_size PublicKey
k
  hashLength :: Int
hashLength = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty))

-- |Encrypt an arbitrarily-sized message using PKCS1 v1.5 encoding. This
-- encoding is deprecated, and should only be used when interacting with
-- legacy software that cannot be modified.
encryptPKCS :: CryptoRandomGen g =>
               g -> PublicKey -> ByteString ->
               Either RSAError (ByteString, g)
encryptPKCS :: g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
encryptPKCS g :: g
g k :: PublicKey
k m :: ByteString
m =
  do let chunks :: [ByteString]
chunks = PublicKey -> ByteString -> [ByteString]
forall k. RSAKey k => k -> ByteString -> [ByteString]
chunkBSForPKCS PublicKey
k ByteString
m
     (chunks' :: [ByteString]
chunks', g' :: g
g') <- g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
forall g.
CryptoRandomGen g =>
g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
mapM' g
g [ByteString]
chunks (\ x :: g
x -> g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
rsaes_pkcs1_v1_5_encrypt g
x PublicKey
k)
     (ByteString, g) -> Either RSAError (ByteString, g)
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BS.concat [ByteString]
chunks', g
g')

-- this is just handy
mapM' :: CryptoRandomGen g =>
         g -> [ByteString] ->
         (g -> ByteString -> Either RSAError (ByteString, g)) ->
         Either RSAError ([ByteString], g)
mapM' :: g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
mapM' g :: g
g []       _ = ([ByteString], g) -> Either RSAError ([ByteString], g)
forall a b. b -> Either a b
Right ([], g
g)
mapM' g :: g
g (x :: ByteString
x:rest :: [ByteString]
rest) f :: g -> ByteString -> Either RSAError (ByteString, g)
f =
  do (x' :: ByteString
x', g' :: g
g')     <- g -> ByteString -> Either RSAError (ByteString, g)
f g
g ByteString
x
     (rest' :: [ByteString]
rest', g'' :: g
g'') <- g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
forall g.
CryptoRandomGen g =>
g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
mapM' g
g' [ByteString]
rest g -> ByteString -> Either RSAError (ByteString, g)
f
     ([ByteString], g) -> Either RSAError ([ByteString], g)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
x'ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
rest', g
g'')

-- ----------------------------------------------------------------------------

-- |Decrypt an arbitrarily-sized message given the public key and reasonable
-- options. This is equivalent to calling encryptOAEP with SHA-256 as the
-- hash function, MGF1(SHA-256) as the mask generation function, and no label.
decrypt :: PrivateKey -> ByteString -> Either RSAError ByteString
decrypt :: PrivateKey -> ByteString -> Either RSAError ByteString
decrypt k :: PrivateKey
k m :: ByteString
m = (ByteString -> ByteString)
-> MGF
-> ByteString
-> PrivateKey
-> ByteString
-> Either RSAError ByteString
decryptOAEP ByteString -> ByteString
sha256' ((ByteString -> ByteString) -> MGF
generateMGF1 ByteString -> ByteString
sha256') ByteString
BS.empty PrivateKey
k ByteString
m
 where sha256' :: ByteString -> ByteString
sha256' = Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA256State -> ByteString)
-> (ByteString -> Digest SHA256State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256State
sha256

-- |Decrypt an arbitrarily-sized message using OAEP encoding. This is the
-- encouraged encoding for doing RSA encryption.
decryptOAEP :: (ByteString -> ByteString) {- ^The hash function to use -} ->
               MGF {- ^The mask generation function to use -} ->
               ByteString {- ^An optional label to include -} ->
               PrivateKey {- ^The public key to encrypt with -} ->
               ByteString {- ^The message to decrypt -} ->
               Either RSAError ByteString
decryptOAEP :: (ByteString -> ByteString)
-> MGF
-> ByteString
-> PrivateKey
-> ByteString
-> Either RSAError ByteString
decryptOAEP hash :: ByteString -> ByteString
hash mgf :: MGF
mgf l :: ByteString
l k :: PrivateKey
k m :: ByteString
m =
  do let chunks :: [ByteString]
chunks = ByteString -> Int64 -> [ByteString]
chunkify ByteString
m (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PrivateKey -> Int
private_size PrivateKey
k))
     [ByteString]
chunks' <- [ByteString]
-> (ByteString -> Either RSAError ByteString)
-> Either RSAError [ByteString]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [ByteString]
chunks ((ByteString -> ByteString)
-> MGF
-> PrivateKey
-> ByteString
-> ByteString
-> Either RSAError ByteString
rsaes_oaep_decrypt ByteString -> ByteString
hash MGF
mgf PrivateKey
k ByteString
l)
     ByteString -> Either RSAError ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BS.concat [ByteString]
chunks')

-- |Decrypt an arbitrarily-sized message using PKCS1 v1.5 encoding. This
-- encoding is deprecated, and should only be used when interacting with
-- legacy software that cannot be modified.
decryptPKCS :: PrivateKey -> ByteString -> Either RSAError ByteString
decryptPKCS :: PrivateKey -> ByteString -> Either RSAError ByteString
decryptPKCS k :: PrivateKey
k m :: ByteString
m =
  do let chunks :: [ByteString]
chunks = ByteString -> Int64 -> [ByteString]
chunkify ByteString
m (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PrivateKey -> Int
private_size PrivateKey
k))
     [ByteString]
chunks' <- [ByteString]
-> (ByteString -> Either RSAError ByteString)
-> Either RSAError [ByteString]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [ByteString]
chunks (PrivateKey -> ByteString -> Either RSAError ByteString
rsaes_pkcs1_v1_5_decrypt PrivateKey
k)
     ByteString -> Either RSAError ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BS.concat [ByteString]
chunks')

-- ----------------------------------------------------------------------------

-- |Chunk an aribitrarily-sized message into a series of chunks that can be
-- encrypted by an OAEP encryption / decryption function.
chunkBSForOAEP :: RSAKey k =>
                  k {- ^The key being used -} ->
                  (ByteString -> ByteString) {- ^The hash function in use -} ->
                  ByteString {- ^The ByteString to chunk -} ->
                  [ByteString]
chunkBSForOAEP :: k -> (ByteString -> ByteString) -> ByteString -> [ByteString]
chunkBSForOAEP k :: k
k hash :: ByteString -> ByteString
hash bs :: ByteString
bs = ByteString -> Int64 -> [ByteString]
chunkify ByteString
bs Int64
chunkSize
 where
  chunkSize :: Int64
chunkSize = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (k -> Int
forall a. RSAKey a => a -> Int
genKeySize k
k) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- (2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
hashLen) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- 2
  hashLen :: Int64
hashLen   = ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty)

-- |Chunk an arbitrarily-sized message into a series of chunks that can be
-- encrypted by a PKCS1 1.5 encryption / decryption function.
chunkBSForPKCS :: RSAKey k => k -> ByteString -> [ByteString]
chunkBSForPKCS :: k -> ByteString -> [ByteString]
chunkBSForPKCS k :: k
k bstr :: ByteString
bstr = ByteString -> Int64 -> [ByteString]
chunkify ByteString
bstr (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (k -> Int
forall a. RSAKey a => a -> Int
genKeySize k
k) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- 11)

chunkify :: ByteString -> Int64 -> [ByteString]
chunkify :: ByteString -> Int64 -> [ByteString]
chunkify bs :: ByteString
bs size :: Int64
size
  | ByteString -> Int64
BS.length ByteString
bs Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = []
  | Bool
otherwise         = let (start :: ByteString
start, end :: ByteString
end) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt Int64
size ByteString
bs
                        in ByteString
start ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> Int64 -> [ByteString]
chunkify ByteString
end Int64
size

-- ----------------------------------------------------------------------------

-- |The generalized implementation of RSAES-OAEP-ENCRYPT. Using the default
-- instantiontion of this, provided by the 'encrypt' function, is a pretty
-- good plan if this makes no sense to you, as it is instantiated with
-- reasonable defaults.
--
-- The message to be encrypted may not be longer then (k - 2*hLen - 2),
-- where k is the length of the RSA modulus in bytes and hLen is the length
-- of a hash in bytes. Passing in a larger message will generate an error,
-- represented by the Left constructor. Note that this means that OAEP
-- encryption cannot be used with keys smaller than 512 bits.
--
-- I have not put in a check for the length of the label, because I don't
-- expect you to use more than 2^32 bytes. So don't make me regret that, eh?
--
rsaes_oaep_encrypt :: CryptoRandomGen g =>
                      g ->
                      (ByteString->ByteString) {-^The hash function to use-} ->
                      MGF {- ^An appropriate mask genereation function -} ->
                      PublicKey {- ^The recipient's public key -} ->
                      ByteString {- ^A label to associate with the message
                                    (feel free to use BS.empty) -} ->
                      ByteString {- ^The message to encrypt -} ->
                      Either RSAError (ByteString, g)
rsaes_oaep_encrypt :: g
-> (ByteString -> ByteString)
-> MGF
-> PublicKey
-> ByteString
-> ByteString
-> Either RSAError (ByteString, g)
rsaes_oaep_encrypt g :: g
g hash :: ByteString -> ByteString
hash mgf :: MGF
mgf k :: PublicKey
k l :: ByteString
l m :: ByteString
m =
  do let hashLength :: Int
hashLength = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty))
         keySize :: Int
keySize    = PublicKey -> Int
public_size PublicKey
k
         msgLength :: Int
msgLength  = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
m)
     -- WARNING: Step 1a is missing
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
msgLength Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> (Int
keySize Int -> Int -> Int
forall a. Num a => a -> a -> a
- (2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
hashLength) Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2)) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$            -- Step 1b
       RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSAMessageTooLong
     let lHash :: ByteString
lHash = ByteString -> ByteString
hash ByteString
l                                               -- Step 2a
     let zeros :: ByteString
zeros = Word8 -> ByteString
BS.repeat 0                                          -- Step 2b
         numZeros :: Int
numZeros = Int
keySize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
msgLength Int -> Int -> Int
forall a. Num a => a -> a -> a
- (2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
hashLength) Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2
         ps :: ByteString
ps = Int64 -> ByteString -> ByteString
BS.take (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
numZeros) ByteString
zeros
     let db :: ByteString
db = [ByteString] -> ByteString
BS.concat [ByteString
lHash, ByteString
ps, Word8 -> ByteString
BS.singleton 1, ByteString
m]                -- Step 2c
     (seed :: ByteString
seed, g' :: g
g') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
g Int
hashLength                              -- Step 2d
     ByteString
dbMask <- MGF
mgf ByteString
seed (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
keySize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hashLength Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1))     -- Step 2e
     let maskedDB :: ByteString
maskedDB = ByteString
db ByteString -> ByteString -> ByteString
`xorBS` ByteString
dbMask                                 -- Step 2f
     ByteString
seedMask <- MGF
mgf ByteString
maskedDB (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
hashLength)               -- Step 2g
     let maskedSeed :: ByteString
maskedSeed = ByteString
seed ByteString -> ByteString -> ByteString
`xorBS` ByteString
seedMask                           -- Step 2h
     let em :: ByteString
em = [ByteString] -> ByteString
BS.concat [Word8 -> ByteString
BS.singleton 0, ByteString
maskedSeed, ByteString
maskedDB]        -- Step 2i
     let m_i :: Integer
m_i = ByteString -> Integer
os2ip ByteString
em                                               -- Step 3a
     Integer
c_i <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_ep (PublicKey -> Integer
public_n PublicKey
k) (PublicKey -> Integer
public_e PublicKey
k) Integer
m_i                      -- Step 3b
     ByteString
c <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
c_i (PublicKey -> Int
public_size PublicKey
k)                                   -- Step 3c
     (ByteString, g) -> Either RSAError (ByteString, g)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
c, g
g')

-- |The generalized implementation of RSAES-OAEP-DECRYPT. Again, 'decrypt'
-- initializes this with a pretty good set of defaults if you don't understand
-- what all of the arguments involve.
--
-- The ciphertext message passed to this function must be k bytes long, where
-- k is the size of the modulus in bytes. If it is not, this function will
-- generate an error, represented by the Left constructor.
--
-- Futher, k (the length of the ciphertext in bytes) must be greater than or
-- equal to (2 * hLen + 2), where hLen is the length of the output of the
-- hash function in bytes. If this equation does not hold, a (different)
-- error will be generated.
--
-- Finally, there are any number of internal situations that may generate
-- an error indicating that decryption failed.
--
rsaes_oaep_decrypt :: (ByteString->ByteString) {-^The hash function to use-} ->
                      MGF {- ^A mask generation function -} ->
                      PrivateKey {- ^The private key to use -} ->
                      ByteString {- ^An optional label whose
                                     association with the message
                                     should be verified. -} ->
                      ByteString {- ^The ciphertext to decrypt -} ->
                      Either RSAError ByteString
rsaes_oaep_decrypt :: (ByteString -> ByteString)
-> MGF
-> PrivateKey
-> ByteString
-> ByteString
-> Either RSAError ByteString
rsaes_oaep_decrypt hash :: ByteString -> ByteString
hash mgf :: MGF
mgf k :: PrivateKey
k l :: ByteString
l c :: ByteString
c =
  do let hashLength :: Int64
hashLength = ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty)
         keySize :: Int
keySize    = PrivateKey -> Int
private_size PrivateKey
k
     -- WARNING: Step 1a is missing!
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Int64
BS.length ByteString
c Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
keySize) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$                -- Step 1b
       RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
keySize Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= ((2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
hashLength) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ 2)) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$     -- Step 1c
       RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     let c_ip :: Integer
c_ip = ByteString -> Integer
os2ip ByteString
c                                            -- Step 2a
     Integer
m_ip <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_dp (PrivateKey -> Integer
private_n PrivateKey
k) (PrivateKey -> Integer
private_d PrivateKey
k) Integer
c_ip               -- Step 2b
     ByteString
em <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
m_ip Int
keySize                                      -- Step 2c
     let lHash :: ByteString
lHash = ByteString -> ByteString
hash ByteString
l                                            -- Step 3a
     let (y :: ByteString
y, seed_db :: ByteString
seed_db) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt 1 ByteString
em                            -- Step 3b
         (maskedSeed :: ByteString
maskedSeed, maskedDB :: ByteString
maskedDB) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt (Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
hashLength) ByteString
seed_db
     ByteString
seedMask <- MGF
mgf ByteString
maskedDB Int64
hashLength                           -- Step 3c
     let seed :: ByteString
seed     = ByteString
maskedSeed ByteString -> ByteString -> ByteString
`xorBS` ByteString
seedMask                    -- Step 3d
     ByteString
dbMask <- MGF
mgf ByteString
seed (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
keySize Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
hashLength Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- 1)    -- Step 3e
     let db :: ByteString
db       = ByteString
maskedDB ByteString -> ByteString -> ByteString
`xorBS` ByteString
dbMask                        -- Step 3f
     let (lHash' :: ByteString
lHash', ps_o_m :: ByteString
ps_o_m) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt Int64
hashLength ByteString
db               -- Step 3g
         (ps :: ByteString
ps, o_m :: ByteString
o_m)        = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BS.span (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0) ByteString
ps_o_m
         (o :: ByteString
o, m :: ByteString
m)           = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt 1 ByteString
o_m
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> [Word8]
BS.unpack ByteString
o [Word8] -> [Word8] -> Bool
forall a. Eq a => a -> a -> Bool
== [1]) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString
lHash' ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
lHash)    (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> [Word8]
BS.unpack ByteString
y [Word8] -> [Word8] -> Bool
forall a. Eq a => a -> a -> Bool
== [0]) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Word8 -> Bool) -> ByteString -> Bool
BS.all (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0) ByteString
ps)   (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     ByteString -> Either RSAError ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
m

-- ----------------------------------------------------------------------------

-- |Implements RSAES-PKCS1-v1.5-Encrypt, for completeness and backward
-- compatibility. Also because I've already written everything else, so why not?
--
-- This encryption / padding mechanism has several known attacks, which are
-- described in the literature. So unless you absolutely need to use this
-- for some historical reason, you should avoid it.
--
-- The message to be encrypted must be less then or equal to (k - 11) bytes
-- long, where k is the length of the key modulus in bytes.
--
-- Because this function uses an unknown amount of randomly-generated data,
-- it takes an instance of RandomGen rather than taking a random number as
-- input, and returns the resultant generator as output. You should take care
-- that you (a) do not reuse the input generator, thus losing important
-- randomness, and (b) choose a decent instance of RandomGen for passing to
-- this function.
rsaes_pkcs1_v1_5_encrypt :: CryptoRandomGen g =>
                            g ->
                            PublicKey ->
                            ByteString ->
                            Either RSAError (ByteString, g)
rsaes_pkcs1_v1_5_encrypt :: g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
rsaes_pkcs1_v1_5_encrypt g :: g
g k :: PublicKey
k m :: ByteString
m =
  do Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
m) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= (PublicKey -> Int
public_size PublicKey
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- 11)) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ -- Step 1
       RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSAIncorrectMsgSize
     (ps :: ByteString
ps, g' :: g
g') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomNZBS g
g (PublicKey -> Int
public_size PublicKey
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
m) Int -> Int -> Int
forall a. Num a => a -> a -> a
- 3)
     let em :: ByteString
em = [ByteString] -> ByteString
BS.concat [Word8 -> ByteString
BS.singleton 0, Word8 -> ByteString
BS.singleton 2, ByteString
ps, Word8 -> ByteString
BS.singleton 0, ByteString
m]
     let m' :: Integer
m' = ByteString -> Integer
os2ip ByteString
em
     Integer
c_i <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_ep (PublicKey -> Integer
public_n PublicKey
k) (PublicKey -> Integer
public_e PublicKey
k) Integer
m'
     ByteString
res <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
c_i (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PublicKey -> Int
public_size PublicKey
k))
     (ByteString, g) -> Either RSAError (ByteString, g)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
res, g
g')

-- |Implements RSAES-PKCS1-v1.5-Decrypt, for completeness and possible backward
-- compatibility. Please see the notes for rsaes_pkcs_v1_5_encrypt regarding
-- use of this function in new applications without backwards compatibility
-- requirements.
--
-- The ciphertext message passed to this function must be of length k, where
-- k is the length of the key modulus in bytes.
rsaes_pkcs1_v1_5_decrypt :: PrivateKey -> ByteString ->
                            Either RSAError ByteString
rsaes_pkcs1_v1_5_decrypt :: PrivateKey -> ByteString -> Either RSAError ByteString
rsaes_pkcs1_v1_5_decrypt k :: PrivateKey
k c :: ByteString
c =
  do Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
c) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== PrivateKey -> Int
private_size PrivateKey
k) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$    -- Step 1
       RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSAIncorrectMsgSize
     let c_i :: Integer
c_i = ByteString -> Integer
os2ip ByteString
c                                          -- Step 2a
     Integer
m_i  <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_dp (PrivateKey -> Integer
private_n PrivateKey
k) (PrivateKey -> Integer
private_d PrivateKey
k) Integer
c_i             -- Step 2b
     ByteString
em   <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
m_i (PrivateKey -> Int
private_size PrivateKey
k)                         -- Step 2c
     let (zt :: ByteString
zt, ps_z_m :: ByteString
ps_z_m) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt 2 ByteString
em                         -- Step 3...
         (ps :: ByteString
ps, z_m :: ByteString
z_m)    = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BS.span (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= 0) ByteString
ps_z_m
         (z :: ByteString
z, m :: ByteString
m)       = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt 1 ByteString
z_m
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> [Word8]
BS.unpack ByteString
zt [Word8] -> [Word8] -> Bool
forall a. Eq a => a -> a -> Bool
/= [0,2]) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> [Word8]
BS.unpack ByteString
z  [Word8] -> [Word8] -> Bool
forall a. Eq a => a -> a -> Bool
/= [0])   (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int64
BS.length ByteString
ps Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<  8 )    (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
     ByteString -> Either RSAError ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
m

-- ----------------------------------------------------------------------------

-- $pss
-- |RSASSA-PSS-Sign, RSASSA-PSS-Verify, and the related functions are not
-- included because they are covered by U.S. Patent 7036014, and it's not clear
-- what the restrictions on implementation are. Sorry.

-- ----------------------------------------------------------------------------

-- |Generate a signature for the given message using the given private key,
-- using the RSASSA-PKCS1-v1.5-Sign algorithm. Note that in researching the
-- requirements for this project, several independent sources suggested not
-- using the same key across sign/validate and encrypt/decrypt contexts. You've
-- been warned.
--
-- The output of this function is the signature only, not the message and
-- the signature.
--
-- SIZE CONSTRAINT: The size of the public key (in bytes) must be greater
-- than or equal to the length of the hash identifier plus the length of
-- a hash plus 1. Thus, for example, you cannot use a 256 bit RSA key with
-- MD5: 32 (the size of a 256-bit RSA key in bytes) is less than 18 (the
-- size of MD5's identier) + 16 (the size of an MD5 hash in bytes) + 1,
-- or 35.
--
-- Thus,
--   * for SHA1 and SHA256, use 512+ bit keys
--   * for SHA384 and SHA512, use 1024+ bit keys
--
rsassa_pkcs1_v1_5_sign :: HashInfo {- ^The hash function to use -} ->
                          PrivateKey {- ^The private key to sign with -} ->
                          ByteString {- ^The message to sign -} ->
                          Either RSAError ByteString -- ^ The signature
rsassa_pkcs1_v1_5_sign :: HashInfo -> PrivateKey -> ByteString -> Either RSAError ByteString
rsassa_pkcs1_v1_5_sign hi :: HashInfo
hi k :: PrivateKey
k m :: ByteString
m =
  do ByteString
em  <- HashInfo -> ByteString -> Int -> Either RSAError ByteString
emsa_pkcs1_v1_5_encode HashInfo
hi ByteString
m (PrivateKey -> Int
private_size PrivateKey
k) -- Step 1
     let m_i :: Integer
m_i = ByteString -> Integer
os2ip ByteString
em                                  -- Step 2a
     Integer
s   <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_sp1 (PrivateKey -> Integer
private_n PrivateKey
k) (PrivateKey -> Integer
private_d PrivateKey
k) Integer
m_i      -- Step 2b
     ByteString
sig <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
s (PrivateKey -> Int
private_size PrivateKey
k)                     -- Step 2c
     ByteString -> Either RSAError ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
sig

-- |Validate a signature for the given message using the given public key. The
-- signature must be exactly k bytes long, where k is the size of the RSA
-- modulus IN BYTES.
rsassa_pkcs1_v1_5_verify :: HashInfo {- ^The hash function to use -} ->
                            PublicKey {-^The public key to validate against-} ->
                            ByteString {- ^The message that was signed -} ->
                            ByteString {- ^The purported signature -} ->
                            Either RSAError Bool
rsassa_pkcs1_v1_5_verify :: HashInfo
-> PublicKey -> ByteString -> ByteString -> Either RSAError Bool
rsassa_pkcs1_v1_5_verify hi :: HashInfo
hi k :: PublicKey
k m :: ByteString
m s :: ByteString
s
  | ByteString -> Int64
BS.length ByteString
s Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PublicKey -> Int
public_size PublicKey
k)  = RSAError -> Either RSAError Bool
forall a b. a -> Either a b
Left RSAError
RSAIncorrectSigSize
  | Bool
otherwise                                    =
      do let s_i :: Integer
s_i = ByteString -> Integer
os2ip ByteString
s                                  -- Step 2a
         Integer
m_i <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_vp1 (PublicKey -> Integer
public_n PublicKey
k) (PublicKey -> Integer
public_e PublicKey
k) Integer
s_i       -- Step 2b
         ByteString
em  <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
m_i (PublicKey -> Int
public_size PublicKey
k)                   -- Step 2c
         ByteString
em' <- HashInfo -> ByteString -> Int -> Either RSAError ByteString
emsa_pkcs1_v1_5_encode HashInfo
hi ByteString
m (PublicKey -> Int
public_size PublicKey
k) -- Step 3
         Bool -> Either RSAError Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
em ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
em')

-- ----------------------------------------------------------------------------

-- |A 'mask generation function'. The input is a bytestring, and the output
-- is a hash of the given length. Unless you know what you're doing, you
-- should probably use a MGF1 formulation created with generate_MGF1.
type MGF = ByteString -> Int64 -> Either RSAError ByteString

-- |Generate a mask generation function for the rsaes_oaep_*. As
-- suggested by the name, the generated function is an instance of the MGF1
-- function. The arguments are the underlying hash function to use and the
-- size of a hash in bytes.
--
-- The bytestring passed to the generated function cannot be longer than
-- 2^32 * hLen, where hLen is the passed length of the hash.
generateMGF1 :: (ByteString -> ByteString) -> MGF
generateMGF1 :: (ByteString -> ByteString) -> MGF
generateMGF1 hash :: ByteString -> ByteString
hash mgfSeed :: ByteString
mgfSeed maskLen :: Int64
maskLen
  | ByteString -> Int64
BS.length ByteString
mgfSeed Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> ((2 Int64 -> Integer -> Int64
forall a b. (Num a, Integral b) => a -> b -> a
^ (32::Integer)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
hLen) = RSAError -> Either RSAError ByteString
forall a b. a -> Either a b
Left RSAError
RSAMaskTooLong
  | Bool
otherwise                                        = MGF
loop ByteString
BS.empty 0
 where
  hLen :: Int64
hLen       = ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty)
  endCounter :: Int64
endCounter = (Int64
maskLen Int64 -> Int64 -> Int64
forall a. Integral a => a -> a -> a
`divCeil` Int64
hLen) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- 1
  loop :: MGF
loop t :: ByteString
t counter :: Int64
counter
    | Int64
counter Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64
endCounter = ByteString -> Either RSAError ByteString
forall a b. b -> Either a b
Right (Int64 -> ByteString -> ByteString
BS.take Int64
maskLen ByteString
t)
    | Bool
otherwise            = do ByteString
c <- Int64 -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Int64
counter 4
                                let bs :: ByteString
bs = ByteString
mgfSeed ByteString -> ByteString -> ByteString
`BS.append` ByteString
c
                                    t' :: ByteString
t' = ByteString
t ByteString -> ByteString -> ByteString
`BS.append` ByteString -> ByteString
hash ByteString
bs
                                MGF
loop ByteString
t' (Int64
counter Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ 1)

-- ----------------------------------------------------------------------------

-- "i2osp converts a nonnegative integer to an octet string of a specified
-- length" -- RFC 3447
i2osp :: Integral a => a -> Int -> Either RSAError ByteString
i2osp :: a -> Int -> Either RSAError ByteString
i2osp x :: a
x len :: Int
len | Bool
isTooLarge = RSAError -> Either RSAError ByteString
forall a b. a -> Either a b
Left RSAError
RSAIntegerTooLargeToPack
            | Bool
otherwise  = ByteString -> Either RSAError ByteString
forall a b. b -> Either a b
Right (ByteString
padding ByteString -> ByteString -> ByteString
`BS.append` ByteString
digits)
 where
  isTooLarge :: Bool
isTooLarge  = (a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
x :: Integer) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>=
                (256 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len :: Integer))
  padding :: ByteString
padding     = Int64 -> Word8 -> ByteString
BS.replicate (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- ByteString -> Int64
BS.length ByteString
digits) 0
  digits :: ByteString
digits      = ByteString -> ByteString
BS.reverse ((a -> Maybe (Word8, a)) -> a -> ByteString
forall a. (a -> Maybe (Word8, a)) -> a -> ByteString
BS.unfoldr a -> Maybe (Word8, a)
forall b a. (Num a, Integral b) => b -> Maybe (a, b)
digitize a
x)
  digitize :: b -> Maybe (a, b)
digitize 0  = Maybe (a, b)
forall a. Maybe a
Nothing
  digitize v :: b
v  = let (q :: b
q, r :: b
r) = b -> b -> (b, b)
forall a. Integral a => a -> a -> (a, a)
divMod b
v 256
                in (a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (b -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
r, b
q)

-- "os2ip converts an octet string to a nonnegative integer" - RFC 3447
os2ip :: ByteString -> Integer
os2ip :: ByteString -> Integer
os2ip = (Integer -> Word8 -> Integer) -> Integer -> ByteString -> Integer
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
BS.foldl (\ a :: Integer
a b :: Word8
b -> (256 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
a) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Word8 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b)) 0

-- the RSA encryption function
rsa_ep :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_ep :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_ep n :: Integer
n _ m :: Integer
m | (Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0) Bool -> Bool -> Bool
|| (Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
n) = RSAError -> Either RSAError Integer
forall a b. a -> Either a b
Left RSAError
RSAMessageRepOutOfRange
rsa_ep n :: Integer
n e :: Integer
e m :: Integer
m                       = Integer -> Either RSAError Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
m Integer
e Integer
n)

-- the RSA decryption function
rsa_dp :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_dp :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_dp n :: Integer
n _ c :: Integer
c | (Integer
c Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0) Bool -> Bool -> Bool
|| (Integer
c Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
n) = RSAError -> Either RSAError Integer
forall a b. a -> Either a b
Left RSAError
RSACipherRepOutOfRange
rsa_dp n :: Integer
n d :: Integer
d c :: Integer
c                       = Integer -> Either RSAError Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
c Integer
d Integer
n)

-- the RSA signature generation function
rsa_sp1 :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_sp1 :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_sp1 n :: Integer
n _ m :: Integer
m | (Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0) Bool -> Bool -> Bool
|| (Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
n) = RSAError -> Either RSAError Integer
forall a b. a -> Either a b
Left RSAError
RSAMessageRepOutOfRange
rsa_sp1 n :: Integer
n d :: Integer
d m :: Integer
m                       = Integer -> Either RSAError Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
m Integer
d Integer
n)

-- the RSA signature verification function
rsa_vp1 :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_vp1 :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_vp1 n :: Integer
n _ s :: Integer
s | (Integer
s Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0) Bool -> Bool -> Bool
|| (Integer
s Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
n) = RSAError -> Either RSAError Integer
forall a b. a -> Either a b
Left RSAError
RSACipherRepOutOfRange
rsa_vp1 n :: Integer
n e :: Integer
e s :: Integer
s                       = Integer -> Either RSAError Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
s Integer
e Integer
n)

-- EMSA PKCS1 1.5 encoding
emsa_pkcs1_v1_5_encode :: HashInfo -> ByteString -> Int ->
                          Either RSAError ByteString
emsa_pkcs1_v1_5_encode :: HashInfo -> ByteString -> Int -> Either RSAError ByteString
emsa_pkcs1_v1_5_encode (HashInfo ident :: ByteString
ident hash :: ByteString -> ByteString
hash) m :: ByteString
m emLen :: Int
emLen
  | Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
emLen Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< (Int64
tLen Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ 1) = RSAError -> Either RSAError ByteString
forall a b. a -> Either a b
Left RSAError
RSAMessageTooShort
  | Bool
otherwise                       = ByteString -> Either RSAError ByteString
forall a b. b -> Either a b
Right ByteString
em
 where
  h :: ByteString
h = ByteString -> ByteString
hash ByteString
m
  t :: ByteString
t = ByteString
ident ByteString -> ByteString -> ByteString
`BS.append` ByteString
h
  tLen :: Int64
tLen = ByteString -> Int64
BS.length ByteString
t
  ps :: ByteString
ps = Int64 -> Word8 -> ByteString
BS.replicate (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
emLen Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
tLen Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- 3) 0xFF
  em :: ByteString
em = [ByteString] -> ByteString
BS.concat [Word8 -> ByteString
BS.singleton 0x00,Word8 -> ByteString
BS.singleton 0x01,ByteString
ps,Word8 -> ByteString
BS.singleton 0x00,ByteString
t]

-- ----------------------------------------------------------------------------

-- Perform pair-wise xor of all the bytes in a bytestring
xorBS :: ByteString -> ByteString -> ByteString
xorBS :: ByteString -> ByteString -> ByteString
xorBS a :: ByteString
a b :: ByteString
b = [Word8] -> ByteString
BS.pack ((Word8 -> Word8 -> Word8) -> ByteString -> ByteString -> [Word8]
forall a. (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]
BS.zipWith Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
xor ByteString
a ByteString
b)

-- Divide a by b, rounding towards positive infinity
divCeil :: Integral a => a -> a -> a
divCeil :: a -> a -> a
divCeil a :: a
a b :: a
b = let (q :: a
q, r :: a
r) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
divMod a
a a
b
              in if a
r a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= 0 then (a
q a -> a -> a
forall a. Num a => a -> a -> a
+ 1) else a
q

-- Generate p and q. This is not necessarily the best way to do this, but it
-- appears to work.
generatePQ :: CryptoRandomGen g =>
              g ->
              Int ->
              Either RSAError (Integer, Integer, g)
generatePQ :: g -> Int -> Either RSAError (Integer, Integer, g)
generatePQ g :: g
g len :: Int
len
  | Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 2   = RSAError -> Either RSAError (Integer, Integer, g)
forall a b. a -> Either a b
Left RSAError
RSAKeySizeTooSmall
  | Bool
otherwise = do (baseP :: Integer
baseP, g' :: g
g')  <- g -> Int -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, g)
largeRandomPrime g
g  (Int
len Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2)
                   (baseQ :: Integer
baseQ, g'' :: g
g'') <- g -> Int -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, g)
largeRandomPrime g
g' (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
len Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2))
                   case () of
                     () | Integer
baseP Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
baseQ -> g -> Int -> Either RSAError (Integer, Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, Integer, g)
generatePQ g
g'' Int
len
                        | Integer
baseP Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<  Integer
baseQ -> (Integer, Integer, g) -> Either RSAError (Integer, Integer, g)
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer
baseQ, Integer
baseP, g
g'')
                        | Bool
otherwise      -> (Integer, Integer, g) -> Either RSAError (Integer, Integer, g)
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer
baseP, Integer
baseQ, g
g'')

-- |Generate a large random prime of a given length in bytes.
largeRandomPrime :: CryptoRandomGen g =>
                    g -> Int ->
                    Either RSAError (Integer, g)
largeRandomPrime :: g -> Int -> Either RSAError (Integer, g)
largeRandomPrime g :: g
g len :: Int
len =
  do (h_t :: ByteString
h_t, g' :: g
g')            <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
g 2
     let [startH :: Word8
startH, startT :: Word8
startT]  = ByteString -> [Word8]
BS.unpack ByteString
h_t
     (startMids :: ByteString
startMids, g'' :: g
g'')     <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
g' (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2)
     let bstr :: ByteString
bstr              = [ByteString] -> ByteString
BS.concat [Word8 -> ByteString
BS.singleton (Word8
startH Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 0xc0),
                                        ByteString
startMids, Word8 -> ByteString
BS.singleton (Word8
startT Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. 1)]
     g -> Integer -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g'' (ByteString -> Integer
os2ip ByteString
bstr)

-- |Generate a random ByteString of the given length
randomBS :: CryptoRandomGen g => g -> Int -> Either RSAError (ByteString, g)
randomBS :: g -> Int -> Either RSAError (ByteString, g)
randomBS g :: g
g n :: Int
n =
  case Int -> g -> Either GenError (ByteString, g)
forall g.
CryptoRandomGen g =>
Int -> g -> Either GenError (ByteString, g)
genBytes Int
n g
g of
    Left e :: GenError
e -> RSAError -> Either RSAError (ByteString, g)
forall a b. a -> Either a b
Left (GenError -> RSAError
RSAGenError GenError
e)
    Right (bs :: ByteString
bs, g' :: g
g') -> (ByteString, g) -> Either RSAError (ByteString, g)
forall a b. b -> Either a b
Right ([ByteString] -> ByteString
BS.fromChunks [ByteString
bs], g
g')

-- |Create a random bytestring of non-zero bytes of the given length.
randomNZBS :: CryptoRandomGen g => g -> Int -> Either RSAError (ByteString, g)
randomNZBS :: g -> Int -> Either RSAError (ByteString, g)
randomNZBS gen :: g
gen 0    = (ByteString, g) -> Either RSAError (ByteString, g)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
BS.empty, g
gen)
randomNZBS gen :: g
gen size :: Int
size =
  do (bstr :: ByteString
bstr, gen' :: g
gen') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
gen Int
size
     let nzbstr :: ByteString
nzbstr = (Word8 -> Bool) -> ByteString -> ByteString
BS.filter (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= 0) ByteString
bstr
     (rest :: ByteString
rest, gen'' :: g
gen'') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomNZBS g
gen' (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
nzbstr))
     (ByteString, g) -> Either RSAError (ByteString, g)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
nzbstr ByteString -> ByteString -> ByteString
`BS.append` ByteString
rest, g
gen'')

-- |Given a number, probabalistically find the first prime number that occurs
-- after it.
findNextPrime :: CryptoRandomGen g =>
                 g -> Integer ->
                 Either RSAError (Integer, g)
findNextPrime :: g -> Integer -> Either RSAError (Integer, g)
findNextPrime g :: g
g n :: Integer
n
  | Integer -> Bool
forall a. Integral a => a -> Bool
even Integer
n             = g -> Integer -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ 1)
  | Integer
n Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` 65537 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = g -> Integer -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ 2)
  | Bool
otherwise          = case g -> Integer -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Bool, g)
isProbablyPrime g
g Integer
n of
                           Left e :: RSAError
e            -> RSAError -> Either RSAError (Integer, g)
forall a b. a -> Either a b
Left RSAError
e
                           Right (True,  g' :: g
g') -> (Integer, g) -> Either RSAError (Integer, g)
forall a b. b -> Either a b
Right (Integer
n, g
g')
                           Right (False, g' :: g
g') -> g -> Integer -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g' (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ 2)

-- |Probabilistically test whether or not a given number is prime by first
-- checking some obvious factors and then defaulting to the Miller-Rabin
-- test. Should save time for numbers that are trivially composite.
isProbablyPrime :: CryptoRandomGen g =>
                   g {- ^a good random number generator -} ->
                   Integer {- ^the number to test -} ->
                   Either RSAError (Bool, g)
isProbablyPrime :: g -> Integer -> Either RSAError (Bool, g)
isProbablyPrime g :: g
g n :: Integer
n
  | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 541                                  = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Integer
n Integer -> [Integer] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Integer]
small_primes, g
g)
  | (Integer -> Bool) -> [Integer] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\ x :: Integer
x -> Integer
n Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== 0) [Integer]
small_primes = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
False, g
g)
  | Bool
otherwise                                = g -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Int -> Either RSAError (Bool, g)
millerRabin g
g Integer
n 100

-- the first 200 prime numbers
small_primes :: [Integer]
small_primes :: [Integer]
small_primes = [
      2,     3,     5,     7,    11,    13,    17,    19,    23,    29,
     31,    37,    41,    43,    47,    53,    59,    61,    67,    71,
     73,    79,    83,    89,    97,   101,   103,   107,   109,   113,
    127,   131,   137,   139,   149,   151,   157,   163,   167,   173,
    179,   181,   191,   193,   197,   199,   211,   223,   227,   229,
    233,   239,   241,   251,   257,   263,   269,   271,   277,   281,
    283,   293,   307,   311,   313,   317,   331,   337,   347,   349,
    353,   359,   367,   373,   379,   383,   389,   397,   401,   409,
    419,   421,   431,   433,   439,   443,   449,   457,   461,   463,
    467,   479,   487,   491,   499,   503,   509,   521,   523,   541,
    547,   557,   563,   569,   571,   577,   587,   593,   599,   601,
    607,   613,   617,   619,   631,   641,   643,   647,   653,   659,
    661,   673,   677,   683,   691,   701,   709,   719,   727,   733,
    739,   743,   751,   757,   761,   769,   773,   787,   797,   809,
    811,   821,   823,   827,   829,   839,   853,   857,   859,   863,
    877,   881,   883,   887,   907,   911,   919,   929,   937,   941,
    947,   953,   967,   971,   977,   983,   991,   997,  1009,  1013,
   1019,  1021,  1031,  1033,  1039,  1049,  1051,  1061,  1063,  1069,
   1087,  1091,  1093,  1097,  1103,  1109,  1117,  1123,  1129,  1151,
   1153,  1163,  1171,  1181,  1187,  1193,  1201,  1213,  1217,  1223
  ]

-- |Probabilistically test whether or not a given number is prime using
-- the Miller-Rabin test.
millerRabin :: CryptoRandomGen g =>
               g {- ^a good random number generator -} ->
               Integer {- ^the number to test -} ->
               Int {- ^the accuracy of the test -} ->
               Either RSAError (Bool, g)
millerRabin :: g -> Integer -> Int -> Either RSAError (Bool, g)
millerRabin g :: g
g n :: Integer
n k :: Int
k
  | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= 0    = RSAError -> Either RSAError (Bool, g)
forall a b. a -> Either a b
Left (String -> RSAError
RSAError "Primality test on negative number or 0.")
  | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== 1    = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
False, g
g)
  | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== 2    = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
True, g
g)
  | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== 3    = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
True, g
g)
  | Bool
otherwise =
     -- write (n-1) as 2^s*d with d odd by factoring powers of 2 from n-1
     let (s :: Integer
s, d :: Integer
d) = Integer -> Integer -> (Integer, Integer)
forall t t. (Bits t, Num t) => t -> t -> (t, t)
oddify 0 (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 1)
     in g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g Integer
s Integer
d Int
k
 where
  generateSize :: Int
generateSize = Integer -> Int -> Int
forall t. (Ord t, Bits t, Num t) => t -> Int -> Int
bitsize (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 2) 8 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 8
  -- k times, pick a random integer in [2, n-2] and see if you can find
  -- a witness suggesting that it's not prime.
  checkLoop :: CryptoRandomGen g =>
               g -> Integer -> Integer -> Int ->
               Either RSAError (Bool, g)
  checkLoop :: g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g' :: g
g' _ _ 0 = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
True, g
g')
  checkLoop g' :: g
g' s :: Integer
s d :: Integer
d c :: Int
c =
    case Int -> g -> Either GenError (ByteString, g)
forall g.
CryptoRandomGen g =>
Int -> g -> Either GenError (ByteString, g)
genBytes Int
generateSize g
g' of
      Left e :: GenError
e -> RSAError -> Either RSAError (Bool, g)
forall a b. a -> Either a b
Left (GenError -> RSAError
RSAGenError GenError
e)
      Right (bstr :: ByteString
bstr, g'' :: g
g'') ->
        let a :: Integer
a = ByteString -> Integer
os2ip (ByteString -> ByteString
BS.fromStrict ByteString
bstr)
            x :: Integer
x = Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
a Integer
d Integer
n
        in if | (Integer
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 2)       -> g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g'' Integer
s Integer
d Int
c
              | (Integer
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 2)) -> g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g'' Integer
s Integer
d Int
c
              | Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== 1        -> g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g'' Integer
s Integer
d (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1)
              | Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 1)  -> g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g'' Integer
s Integer
d (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1)
              | Bool
otherwise     -> g
-> Integer
-> Integer
-> Integer
-> Int
-> Integer
-> Either RSAError (Bool, g)
forall t t.
(Eq t, Num t, CryptoRandomGen t) =>
t
-> Integer
-> Integer
-> Integer
-> Int
-> t
-> Either RSAError (Bool, t)
checkWitnesses g
g'' Integer
s Integer
d Integer
x Int
c (Integer
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 1)
  -- s times, where n-1 = 2^s*d, check to see if the given number is a
  -- witness of something not being prime.
  checkWitnesses :: t
-> Integer
-> Integer
-> Integer
-> Int
-> t
-> Either RSAError (Bool, t)
checkWitnesses g'' :: t
g'' _ _ _ _  0  = (Bool, t) -> Either RSAError (Bool, t)
forall a b. b -> Either a b
Right (Bool
False, t
g'')
  checkWitnesses g'' :: t
g'' s :: Integer
s d :: Integer
d x :: Integer
x c1 :: Int
c1 c2 :: t
c2 =
    case (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
x) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
n of
       1                -> (Bool, t) -> Either RSAError (Bool, t)
forall a b. b -> Either a b
Right (Bool
False, t
g'')
       y :: Integer
y | Integer
y Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- 1) -> t -> Integer -> Integer -> Int -> Either RSAError (Bool, t)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop t
g'' Integer
s Integer
d (Int
c1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1)
       _                -> t
-> Integer
-> Integer
-> Integer
-> Int
-> t
-> Either RSAError (Bool, t)
checkWitnesses t
g'' Integer
s Integer
d Integer
x Int
c1 (t
c2 t -> t -> t
forall a. Num a => a -> a -> a
- 1)
  -- given n, compute s and d such that 2^s*d = n.
  oddify :: t -> t -> (t, t)
oddify s :: t
s x :: t
x | t -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit t
x 0 = (t
s, t
x)
             | Bool
otherwise   = t -> t -> (t, t)
oddify (t
s t -> t -> t
forall a. Num a => a -> a -> a
+ 1) (t
x t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftR` 1)
  -- given n, compute the number of bits required to hold it.
  bitsize :: t -> Int -> Int
bitsize v :: t
v x :: Int
x | (1 t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftL` Int
x) t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
v = Int
x
              | Bool
otherwise          = t -> Int -> Int
bitsize t
v (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 8)

-- |Computes a^b mod c using a moderately good algorithm.
modular_exponentiation :: Integer -> Integer -> Integer -> Integer
modular_exponentiation :: Integer -> Integer -> Integer -> Integer
modular_exponentiation x :: Integer
x y :: Integer
y m :: Integer
m = Integer -> Integer -> Integer -> Integer
forall t. (Num t, Bits t) => Integer -> t -> Integer -> Integer
m_e_loop Integer
x Integer
y 1
 where
  m_e_loop :: Integer -> t -> Integer -> Integer
m_e_loop _ 0 result :: Integer
result = Integer
result
  m_e_loop b :: Integer
b e :: t
e result :: Integer
result = Integer -> t -> Integer -> Integer
m_e_loop Integer
b' t
e' Integer
result'
   where
    b' :: Integer
b'      = (Integer
b Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
b) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m
    e' :: t
e'      = t
e t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftR` 1
    result' :: Integer
result' = if t -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit t
e 0 then (Integer
result Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
b) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m else Integer
result

-- |Compute the modular inverse (d = e^-1 mod phi) via the extended euclidean
-- algorithm.
modular_inverse :: Integer {- ^e -} ->
                   Integer  {- ^phi -} ->
                   Integer
modular_inverse :: Integer -> Integer -> Integer
modular_inverse e :: Integer
e phi :: Integer
phi = Integer
x Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
phi
 where (_, x :: Integer
x, _) = Integer -> Integer -> (Integer, Integer, Integer)
extended_euclidean Integer
e Integer
phi

-- Compute the extended euclidean algorithm
extended_euclidean :: Integer -> Integer -> (Integer, Integer, Integer)
extended_euclidean :: Integer -> Integer -> (Integer, Integer, Integer)
extended_euclidean a :: Integer
a b :: Integer
b | Integer
d Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0     = (-Integer
d, -Integer
x, -Integer
y)
                       | Bool
otherwise = (Integer
d, Integer
x, Integer
y)
 where
  (d :: Integer
d, x :: Integer
x, y :: Integer
y) = Integer -> Integer -> (Integer, Integer, Integer)
egcd Integer
a Integer
b

egcd :: Integer -> Integer -> (Integer, Integer, Integer)
egcd :: Integer -> Integer -> (Integer, Integer, Integer)
egcd 0 b :: Integer
b = (Integer
b, 0, 1)
egcd a :: Integer
a b :: Integer
b = let (g :: Integer
g, y :: Integer
y, x :: Integer
x) = Integer -> Integer -> (Integer, Integer, Integer)
egcd (Integer
b Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
a) Integer
a
           in (Integer
g, Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- ((Integer
b Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
a) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
y), Integer
y)

-- ----------------------------------------------------------------------------

hashSHA1 :: HashInfo
hashSHA1 :: HashInfo
hashSHA1 = HashInfo :: ByteString -> (ByteString -> ByteString) -> HashInfo
HashInfo {
   algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,
                             0x02,0x1a,0x05,0x00,0x04,0x14]
 , hashFunction :: ByteString -> ByteString
hashFunction   = Digest SHA1State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA1State -> ByteString)
-> (ByteString -> Digest SHA1State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA1State
sha1
 }

hashSHA224 :: HashInfo
hashSHA224 :: HashInfo
hashSHA224 = HashInfo :: ByteString -> (ByteString -> ByteString) -> HashInfo
HashInfo {
   algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [0x30,0x2d,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,
                             0x01,0x65,0x03,0x04,0x02,0x04,0x05,0x00,0x04,
                             0x1c]
 , hashFunction :: ByteString -> ByteString
hashFunction   = Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA256State -> ByteString)
-> (ByteString -> Digest SHA256State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256State
sha224
 }

hashSHA256 :: HashInfo
hashSHA256 :: HashInfo
hashSHA256 = HashInfo :: ByteString -> (ByteString -> ByteString) -> HashInfo
HashInfo {
   algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,
                             0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,
                             0x20]
 , hashFunction :: ByteString -> ByteString
hashFunction   = Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA256State -> ByteString)
-> (ByteString -> Digest SHA256State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256State
sha256
 }

hashSHA384 :: HashInfo
hashSHA384 :: HashInfo
hashSHA384 = HashInfo :: ByteString -> (ByteString -> ByteString) -> HashInfo
HashInfo {
   algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [0x30,0x41,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,
                             0x01,0x65,0x03,0x04,0x02,0x02,0x05,0x00,0x04,
                             0x30]
 , hashFunction :: ByteString -> ByteString
hashFunction   = Digest SHA512State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA512State -> ByteString)
-> (ByteString -> Digest SHA512State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA512State
sha384
 }

hashSHA512 :: HashInfo
hashSHA512 :: HashInfo
hashSHA512 = HashInfo :: ByteString -> (ByteString -> ByteString) -> HashInfo
HashInfo {
   algorithmIdent :: ByteString
algorithmIdent  = [Word8] -> ByteString
BS.pack [0x30,0x51,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,
                              0x01,0x65,0x03,0x04,0x02,0x03,0x05,0x00,0x04,
                              0x40]
 , hashFunction :: ByteString -> ByteString
hashFunction   = Digest SHA512State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA512State -> ByteString)
-> (ByteString -> Digest SHA512State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA512State
sha512
 }