{-# LINE 1 "OpenSSL/RSA.hsc" #-}
{-# LANGUAGE DeriveDataTypeable       #-}
{-# LANGUAGE EmptyDataDecls           #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI                  #-}
{-# OPTIONS_HADDOCK prune             #-}
-- |An interface to RSA public key generator.
module OpenSSL.RSA
    ( -- * Type
      RSAKey(..)
    , RSAPubKey
    , RSAKeyPair
    , RSA -- private

      -- * Generating keypair
    , RSAGenKeyCallback
    , generateRSAKey
    , generateRSAKey'

      -- * Exploring keypair
    , rsaD
    , rsaP
    , rsaQ
    , rsaDMP1
    , rsaDMQ1
    , rsaIQMP
    , rsaCopyPublic
    , rsaKeyPairFinalize -- private
    )
    where

import Control.Monad

{-# LINE 35 "OpenSSL/RSA.hsc" #-}
import Data.Typeable


{-# LINE 38 "OpenSSL/RSA.hsc" #-}
import Foreign.C.Types (CInt(..))

{-# LINE 42 "OpenSSL/RSA.hsc" #-}
import Foreign.ForeignPtr (ForeignPtr, finalizeForeignPtr, newForeignPtr, withForeignPtr)
import Foreign.Ptr (FunPtr, Ptr, freeHaskellFunPtr, nullFunPtr, nullPtr)
import Foreign.Storable (Storable(..))

{-# LINE 46 "OpenSSL/RSA.hsc" #-}
import Foreign.Marshal.Alloc (alloca)

{-# LINE 48 "OpenSSL/RSA.hsc" #-}
import OpenSSL.BN
import OpenSSL.Utils
import System.IO.Unsafe (unsafePerformIO)

-- |@'RSAPubKey'@ is an opaque object that represents RSA public key.
newtype RSAPubKey  = RSAPubKey (ForeignPtr RSA)
    deriving Typeable

-- |@'RSAKeyPair'@ is an opaque object that represents RSA keypair.
newtype RSAKeyPair = RSAKeyPair (ForeignPtr RSA)
    deriving Typeable

-- RSAPubKey and RSAKeyPair are in fact the same type at the OpenSSL
-- level, but we want to treat them differently for type-safety.
data {-# CTYPE "openssl/rsa.h" "RSA" #-} RSA

-- |@'RSAKey' a@ is either 'RSAPubKey' or 'RSAKeyPair'.
class RSAKey k where
    -- |@'rsaSize' key@ returns the length of key.
    rsaSize :: k -> Int
    rsaSize k
rsa
        = IO Int -> Int
forall a. IO a -> a
unsafePerformIO (IO Int -> Int) -> IO Int -> Int
forall a b. (a -> b) -> a -> b
$
          k -> (Ptr RSA -> IO Int) -> IO Int
forall a. k -> (Ptr RSA -> IO a) -> IO a
forall k a. RSAKey k => k -> (Ptr RSA -> IO a) -> IO a
withRSAPtr k
rsa ((Ptr RSA -> IO Int) -> IO Int) -> (Ptr RSA -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \ Ptr RSA
rsaPtr ->
              (CInt -> Int) -> IO CInt -> IO Int
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Ptr RSA -> IO CInt
_size Ptr RSA
rsaPtr)

    -- |@'rsaN' key@ returns the public modulus of the key.
    rsaN :: k -> Integer
    rsaN = (Ptr RSA -> IO (Ptr BIGNUM)) -> k -> Integer
forall a. RSAKey a => (Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Integer
peekI Ptr RSA -> IO (Ptr BIGNUM)
rsa_n

    -- |@'rsaE' key@ returns the public exponent of the key.
    rsaE :: k -> Integer
    rsaE = (Ptr RSA -> IO (Ptr BIGNUM)) -> k -> Integer
forall a. RSAKey a => (Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Integer
peekI Ptr RSA -> IO (Ptr BIGNUM)
rsa_e

    -- private
    withRSAPtr   :: k -> (Ptr RSA -> IO a) -> IO a
    peekRSAPtr   :: Ptr RSA -> IO (Maybe k)
    absorbRSAPtr :: Ptr RSA -> IO (Maybe k)


instance RSAKey RSAPubKey where
    withRSAPtr :: forall a. RSAPubKey -> (Ptr RSA -> IO a) -> IO a
withRSAPtr (RSAPubKey ForeignPtr RSA
fp) = ForeignPtr RSA -> (Ptr RSA -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr RSA
fp
    peekRSAPtr :: Ptr RSA -> IO (Maybe RSAPubKey)
peekRSAPtr Ptr RSA
rsaPtr         = Ptr RSA -> IO (Ptr RSA)
_pubDup Ptr RSA
rsaPtr IO (Ptr RSA)
-> (Ptr RSA -> IO (Maybe RSAPubKey)) -> IO (Maybe RSAPubKey)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr RSA -> IO (Maybe RSAPubKey)
forall k. RSAKey k => Ptr RSA -> IO (Maybe k)
absorbRSAPtr
    absorbRSAPtr :: Ptr RSA -> IO (Maybe RSAPubKey)
absorbRSAPtr Ptr RSA
rsaPtr       = (ForeignPtr RSA -> Maybe RSAPubKey)
-> IO (ForeignPtr RSA) -> IO (Maybe RSAPubKey)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (RSAPubKey -> Maybe RSAPubKey
forall a. a -> Maybe a
Just (RSAPubKey -> Maybe RSAPubKey)
-> (ForeignPtr RSA -> RSAPubKey)
-> ForeignPtr RSA
-> Maybe RSAPubKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ForeignPtr RSA -> RSAPubKey
RSAPubKey) (FinalizerPtr RSA -> Ptr RSA -> IO (ForeignPtr RSA)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr RSA
_free Ptr RSA
rsaPtr)


instance RSAKey RSAKeyPair where
    withRSAPtr :: forall a. RSAKeyPair -> (Ptr RSA -> IO a) -> IO a
withRSAPtr (RSAKeyPair ForeignPtr RSA
fp) = ForeignPtr RSA -> (Ptr RSA -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr RSA
fp
    peekRSAPtr :: Ptr RSA -> IO (Maybe RSAKeyPair)
peekRSAPtr Ptr RSA
rsaPtr
        = do Bool
hasP <- Ptr RSA -> IO Bool
hasRSAPrivateKey Ptr RSA
rsaPtr
             if Bool
hasP then
                 Ptr RSA -> IO (Ptr RSA)
_privDup Ptr RSA
rsaPtr IO (Ptr RSA)
-> (Ptr RSA -> IO (Maybe RSAKeyPair)) -> IO (Maybe RSAKeyPair)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr RSA -> IO (Maybe RSAKeyPair)
forall k. RSAKey k => Ptr RSA -> IO (Maybe k)
absorbRSAPtr
               else
                 Maybe RSAKeyPair -> IO (Maybe RSAKeyPair)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe RSAKeyPair
forall a. Maybe a
Nothing
    absorbRSAPtr :: Ptr RSA -> IO (Maybe RSAKeyPair)
absorbRSAPtr Ptr RSA
rsaPtr
        = do Bool
hasP <- Ptr RSA -> IO Bool
hasRSAPrivateKey Ptr RSA
rsaPtr
             if Bool
hasP then
                 (ForeignPtr RSA -> Maybe RSAKeyPair)
-> IO (ForeignPtr RSA) -> IO (Maybe RSAKeyPair)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (RSAKeyPair -> Maybe RSAKeyPair
forall a. a -> Maybe a
Just (RSAKeyPair -> Maybe RSAKeyPair)
-> (ForeignPtr RSA -> RSAKeyPair)
-> ForeignPtr RSA
-> Maybe RSAKeyPair
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ForeignPtr RSA -> RSAKeyPair
RSAKeyPair) (FinalizerPtr RSA -> Ptr RSA -> IO (ForeignPtr RSA)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr RSA
_free Ptr RSA
rsaPtr)
               else
                 Maybe RSAKeyPair -> IO (Maybe RSAKeyPair)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe RSAKeyPair
forall a. Maybe a
Nothing


hasRSAPrivateKey :: Ptr RSA -> IO Bool
hasRSAPrivateKey :: Ptr RSA -> IO Bool
hasRSAPrivateKey Ptr RSA
rsaPtr
    = do Ptr BIGNUM
d <- Ptr RSA -> IO (Ptr BIGNUM)
rsa_d Ptr RSA
rsaPtr
         Ptr BIGNUM
p <- Ptr RSA -> IO (Ptr BIGNUM)
rsa_p Ptr RSA
rsaPtr
         Ptr BIGNUM
q <- Ptr RSA -> IO (Ptr BIGNUM)
rsa_q Ptr RSA
rsaPtr
         Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr BIGNUM
d Ptr BIGNUM -> Ptr BIGNUM -> Bool
forall a. Eq a => a -> a -> Bool
/= Ptr BIGNUM
forall a. Ptr a
nullPtr Bool -> Bool -> Bool
&& Ptr BIGNUM
p Ptr BIGNUM -> Ptr BIGNUM -> Bool
forall a. Eq a => a -> a -> Bool
/= Ptr BIGNUM
forall a. Ptr a
nullPtr Bool -> Bool -> Bool
&& Ptr BIGNUM
q Ptr BIGNUM -> Ptr BIGNUM -> Bool
forall a. Eq a => a -> a -> Bool
/= Ptr BIGNUM
forall a. Ptr a
nullPtr)



foreign import capi unsafe "openssl/rsa.h &RSA_free"
        _free :: FunPtr (Ptr RSA -> IO ())

foreign import capi unsafe "openssl/rsa.h RSAPublicKey_dup"
        _pubDup :: Ptr RSA -> IO (Ptr RSA)

foreign import capi unsafe "openssl/rsa.h RSAPrivateKey_dup"
        _privDup :: Ptr RSA -> IO (Ptr RSA)

foreign import capi unsafe "openssl/rsa.h RSA_size"
        _size :: Ptr RSA -> IO CInt

-- | Make a copy of the public parameters of the given key.
rsaCopyPublic :: RSAKey key => key -> IO RSAPubKey
rsaCopyPublic :: forall key. RSAKey key => key -> IO RSAPubKey
rsaCopyPublic key
key = key -> (Ptr RSA -> IO RSAPubKey) -> IO RSAPubKey
forall a. key -> (Ptr RSA -> IO a) -> IO a
forall k a. RSAKey k => k -> (Ptr RSA -> IO a) -> IO a
withRSAPtr key
key ((ForeignPtr RSA -> RSAPubKey)
-> IO (ForeignPtr RSA) -> IO RSAPubKey
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForeignPtr RSA -> RSAPubKey
RSAPubKey (IO (ForeignPtr RSA) -> IO RSAPubKey)
-> (Ptr RSA -> IO (ForeignPtr RSA)) -> Ptr RSA -> IO RSAPubKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FinalizerPtr RSA -> Ptr RSA -> IO (ForeignPtr RSA)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr RSA
_free (Ptr RSA -> IO (ForeignPtr RSA))
-> IO (Ptr RSA) -> IO (ForeignPtr RSA)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (IO (Ptr RSA) -> IO (ForeignPtr RSA))
-> (Ptr RSA -> IO (Ptr RSA)) -> Ptr RSA -> IO (ForeignPtr RSA)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr RSA -> IO (Ptr RSA)
_pubDup)

-- private
rsaKeyPairFinalize :: RSAKeyPair -> IO ()
rsaKeyPairFinalize :: RSAKeyPair -> IO ()
rsaKeyPairFinalize (RSAKeyPair ForeignPtr RSA
fp) = ForeignPtr RSA -> IO ()
forall a. ForeignPtr a -> IO ()
finalizeForeignPtr ForeignPtr RSA
fp

{- generation --------------------------------------------------------------- -}

-- |@'RSAGenKeyCallback'@ represents a callback function to get
-- informed the progress of RSA key generation.
--
-- * @callback 0 i@ is called after generating the @i@-th potential
--   prime number.
--
-- * While the number is being tested for primality, @callback 1 j@ is
--   called after the @j@-th iteration (j = 0, 1, ...).
--
-- * When the @n@-th randomly generated prime is rejected as not
--   suitable for the key, @callback 2 n@ is called.
--
-- * When a random @p@ has been found with @p@-1 relatively prime to
--   @e@, it is called as @callback 3 0@.
--
-- * The process is then repeated for prime @q@ with @callback 3 1@.
type RSAGenKeyCallback = Int -> Int -> IO ()

type RSAGenKeyCallback' = Int -> Int -> Ptr () -> IO ()


foreign import ccall "wrapper"
        mkGenKeyCallback :: RSAGenKeyCallback' -> IO (FunPtr RSAGenKeyCallback')

foreign import capi safe "openssl/rsa.h RSA_generate_key"
        _generate_key :: CInt -> CInt -> FunPtr RSAGenKeyCallback' -> Ptr a -> IO (Ptr RSA)

-- |@'generateRSAKey'@ generates an RSA keypair.
generateRSAKey :: Int    -- ^ The number of bits of the public modulus
                         --   (i.e. key size). Key sizes with @n <
                         --   1024@ should be considered insecure.
               -> Int    -- ^ The public exponent. It is an odd
                         --   number, typically 3, 17 or 65537.
               -> Maybe RSAGenKeyCallback -- ^ A callback function.
               -> IO RSAKeyPair -- ^ The generated keypair.

generateRSAKey :: Int -> Int -> Maybe RSAGenKeyCallback -> IO RSAKeyPair
generateRSAKey Int
nbits Int
e Maybe RSAGenKeyCallback
Nothing
    = do Ptr RSA
ptr <- CInt
-> CInt -> FunPtr RSAGenKeyCallback' -> Ptr Any -> IO (Ptr RSA)
forall a.
CInt -> CInt -> FunPtr RSAGenKeyCallback' -> Ptr a -> IO (Ptr RSA)
_generate_key (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nbits) (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
e) FunPtr RSAGenKeyCallback'
forall a. FunPtr a
nullFunPtr Ptr Any
forall a. Ptr a
nullPtr
         Ptr RSA -> IO ()
forall a. Ptr a -> IO ()
failIfNull_ Ptr RSA
ptr
         (ForeignPtr RSA -> RSAKeyPair)
-> IO (ForeignPtr RSA) -> IO RSAKeyPair
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForeignPtr RSA -> RSAKeyPair
RSAKeyPair (FinalizerPtr RSA -> Ptr RSA -> IO (ForeignPtr RSA)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr RSA
_free Ptr RSA
ptr)

generateRSAKey Int
nbits Int
e (Just RSAGenKeyCallback
cb)
    = do FunPtr RSAGenKeyCallback'
cbPtr <- RSAGenKeyCallback' -> IO (FunPtr RSAGenKeyCallback')
mkGenKeyCallback
                  (RSAGenKeyCallback' -> IO (FunPtr RSAGenKeyCallback'))
-> RSAGenKeyCallback' -> IO (FunPtr RSAGenKeyCallback')
forall a b. (a -> b) -> a -> b
$ \ Int
arg1 Int
arg2 Ptr ()
_ -> RSAGenKeyCallback
cb Int
arg1 Int
arg2
         Ptr RSA
ptr   <- CInt
-> CInt -> FunPtr RSAGenKeyCallback' -> Ptr Any -> IO (Ptr RSA)
forall a.
CInt -> CInt -> FunPtr RSAGenKeyCallback' -> Ptr a -> IO (Ptr RSA)
_generate_key (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nbits) (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
e) FunPtr RSAGenKeyCallback'
cbPtr Ptr Any
forall a. Ptr a
nullPtr
         FunPtr RSAGenKeyCallback' -> IO ()
forall a. FunPtr a -> IO ()
freeHaskellFunPtr FunPtr RSAGenKeyCallback'
cbPtr
         Ptr RSA -> IO ()
forall a. Ptr a -> IO ()
failIfNull_ Ptr RSA
ptr
         (ForeignPtr RSA -> RSAKeyPair)
-> IO (ForeignPtr RSA) -> IO RSAKeyPair
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForeignPtr RSA -> RSAKeyPair
RSAKeyPair (FinalizerPtr RSA -> Ptr RSA -> IO (ForeignPtr RSA)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr RSA
_free Ptr RSA
ptr)

-- |A simplified alternative to 'generateRSAKey'
generateRSAKey' :: Int   -- ^ The number of bits of the public modulus
                         --   (i.e. key size). Key sizes with @n <
                         --   1024@ should be considered insecure.
                -> Int   -- ^ The public exponent. It is an odd
                         --   number, typically 3, 17 or 65537.
                -> IO RSAKeyPair -- ^ The generated keypair.
generateRSAKey' :: Int -> Int -> IO RSAKeyPair
generateRSAKey' Int
nbits Int
e
    = Int -> Int -> Maybe RSAGenKeyCallback -> IO RSAKeyPair
generateRSAKey Int
nbits Int
e Maybe RSAGenKeyCallback
forall a. Maybe a
Nothing


{- exploration -------------------------------------------------------------- -}

rsa_n, rsa_e, rsa_d, rsa_p, rsa_q :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_dmp1, rsa_dmq1, rsa_iqmp :: Ptr RSA -> IO (Ptr BIGNUM)


{-# LINE 206 "OpenSSL/RSA.hsc" #-}

foreign import capi unsafe "openssl/rsa.h RSA_get0_key"
        _get0_key :: Ptr RSA -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO ()

foreign import capi unsafe "openssl/rsa.h RSA_get0_factors"
        _get0_factors :: Ptr RSA -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO ()

foreign import capi unsafe "openssl/rsa.h RSA_get0_crt_params"
        _get0_crt_params :: Ptr RSA -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO ()

withNED :: (Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
        -> Ptr RSA -> IO b
withNED :: forall b.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
-> Ptr RSA -> IO b
withNED Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b
f Ptr RSA
rsa = (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr BIGNUM) -> IO b) -> IO b)
-> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
n -> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr BIGNUM) -> IO b) -> IO b)
-> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
e -> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr BIGNUM) -> IO b) -> IO b)
-> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
d -> do
    Ptr (Ptr BIGNUM) -> Ptr BIGNUM -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr BIGNUM)
n Ptr BIGNUM
forall a. Ptr a
nullPtr
    Ptr (Ptr BIGNUM) -> Ptr BIGNUM -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr BIGNUM)
e Ptr BIGNUM
forall a. Ptr a
nullPtr
    Ptr (Ptr BIGNUM) -> Ptr BIGNUM -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr BIGNUM)
d Ptr BIGNUM
forall a. Ptr a
nullPtr
    Ptr RSA
-> Ptr (Ptr BIGNUM)
-> Ptr (Ptr BIGNUM)
-> Ptr (Ptr BIGNUM)
-> IO ()
_get0_key Ptr RSA
rsa Ptr (Ptr BIGNUM)
n Ptr (Ptr BIGNUM)
e Ptr (Ptr BIGNUM)
d
    Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b
f Ptr (Ptr BIGNUM)
n Ptr (Ptr BIGNUM)
e Ptr (Ptr BIGNUM)
d

rsa_n :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_n = (Ptr (Ptr BIGNUM)
 -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA -> IO (Ptr BIGNUM)
forall b.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
-> Ptr RSA -> IO b
withNED ((Ptr (Ptr BIGNUM)
  -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
 -> Ptr RSA -> IO (Ptr BIGNUM))
-> (Ptr (Ptr BIGNUM)
    -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA
-> IO (Ptr BIGNUM)
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
n Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
_ -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr BIGNUM)
n
rsa_e :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_e = (Ptr (Ptr BIGNUM)
 -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA -> IO (Ptr BIGNUM)
forall b.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
-> Ptr RSA -> IO b
withNED ((Ptr (Ptr BIGNUM)
  -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
 -> Ptr RSA -> IO (Ptr BIGNUM))
-> (Ptr (Ptr BIGNUM)
    -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA
-> IO (Ptr BIGNUM)
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
e Ptr (Ptr BIGNUM)
_ -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr BIGNUM)
e
rsa_d :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_d = (Ptr (Ptr BIGNUM)
 -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA -> IO (Ptr BIGNUM)
forall b.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
-> Ptr RSA -> IO b
withNED ((Ptr (Ptr BIGNUM)
  -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
 -> Ptr RSA -> IO (Ptr BIGNUM))
-> (Ptr (Ptr BIGNUM)
    -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA
-> IO (Ptr BIGNUM)
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
d -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr BIGNUM)
d

withFactors
    :: (Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO a) -> Ptr RSA -> IO a
withFactors :: forall a.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO a) -> Ptr RSA -> IO a
withFactors Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO a
f Ptr RSA
rsa = (Ptr (Ptr BIGNUM) -> IO a) -> IO a
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr BIGNUM) -> IO a) -> IO a)
-> (Ptr (Ptr BIGNUM) -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
p -> (Ptr (Ptr BIGNUM) -> IO a) -> IO a
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr BIGNUM) -> IO a) -> IO a)
-> (Ptr (Ptr BIGNUM) -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
q -> do
    Ptr (Ptr BIGNUM) -> Ptr BIGNUM -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr BIGNUM)
p Ptr BIGNUM
forall a. Ptr a
nullPtr
    Ptr (Ptr BIGNUM) -> Ptr BIGNUM -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr BIGNUM)
q Ptr BIGNUM
forall a. Ptr a
nullPtr
    Ptr RSA -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO ()
_get0_factors Ptr RSA
rsa Ptr (Ptr BIGNUM)
p Ptr (Ptr BIGNUM)
q
    Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO a
f Ptr (Ptr BIGNUM)
p Ptr (Ptr BIGNUM)
q

rsa_p :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_p = (Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA -> IO (Ptr BIGNUM)
forall a.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO a) -> Ptr RSA -> IO a
withFactors ((Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
 -> Ptr RSA -> IO (Ptr BIGNUM))
-> (Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA
-> IO (Ptr BIGNUM)
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
p Ptr (Ptr BIGNUM)
_ -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr BIGNUM)
p
rsa_q :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_q = (Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA -> IO (Ptr BIGNUM)
forall a.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO a) -> Ptr RSA -> IO a
withFactors ((Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
 -> Ptr RSA -> IO (Ptr BIGNUM))
-> (Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA
-> IO (Ptr BIGNUM)
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
q -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr BIGNUM)
q

withCrtParams
    :: (Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
    -> Ptr RSA -> IO b
withCrtParams :: forall b.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
-> Ptr RSA -> IO b
withCrtParams Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b
f Ptr RSA
rsa = (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr BIGNUM) -> IO b) -> IO b)
-> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
dmp1 -> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr BIGNUM) -> IO b) -> IO b)
-> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
dmq1 -> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr BIGNUM) -> IO b) -> IO b)
-> (Ptr (Ptr BIGNUM) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
iqmp -> do
    Ptr (Ptr BIGNUM) -> Ptr BIGNUM -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr BIGNUM)
dmp1 Ptr BIGNUM
forall a. Ptr a
nullPtr
    Ptr (Ptr BIGNUM) -> Ptr BIGNUM -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr BIGNUM)
dmq1 Ptr BIGNUM
forall a. Ptr a
nullPtr
    Ptr (Ptr BIGNUM) -> Ptr BIGNUM -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr BIGNUM)
iqmp Ptr BIGNUM
forall a. Ptr a
nullPtr
    Ptr RSA
-> Ptr (Ptr BIGNUM)
-> Ptr (Ptr BIGNUM)
-> Ptr (Ptr BIGNUM)
-> IO ()
_get0_crt_params Ptr RSA
rsa Ptr (Ptr BIGNUM)
dmp1 Ptr (Ptr BIGNUM)
dmq1 Ptr (Ptr BIGNUM)
iqmp
    Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b
f Ptr (Ptr BIGNUM)
dmp1 Ptr (Ptr BIGNUM)
dmq1 Ptr (Ptr BIGNUM)
iqmp

rsa_dmp1 :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_dmp1 = (Ptr (Ptr BIGNUM)
 -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA -> IO (Ptr BIGNUM)
forall b.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
-> Ptr RSA -> IO b
withCrtParams ((Ptr (Ptr BIGNUM)
  -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
 -> Ptr RSA -> IO (Ptr BIGNUM))
-> (Ptr (Ptr BIGNUM)
    -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA
-> IO (Ptr BIGNUM)
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
dmp1 Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
_ -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr BIGNUM)
dmp1
rsa_dmq1 :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_dmq1 = (Ptr (Ptr BIGNUM)
 -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA -> IO (Ptr BIGNUM)
forall b.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
-> Ptr RSA -> IO b
withCrtParams ((Ptr (Ptr BIGNUM)
  -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
 -> Ptr RSA -> IO (Ptr BIGNUM))
-> (Ptr (Ptr BIGNUM)
    -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA
-> IO (Ptr BIGNUM)
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
dmq1 Ptr (Ptr BIGNUM)
_ -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr BIGNUM)
dmq1
rsa_iqmp :: Ptr RSA -> IO (Ptr BIGNUM)
rsa_iqmp = (Ptr (Ptr BIGNUM)
 -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA -> IO (Ptr BIGNUM)
forall b.
(Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO b)
-> Ptr RSA -> IO b
withCrtParams ((Ptr (Ptr BIGNUM)
  -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
 -> Ptr RSA -> IO (Ptr BIGNUM))
-> (Ptr (Ptr BIGNUM)
    -> Ptr (Ptr BIGNUM) -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM))
-> Ptr RSA
-> IO (Ptr BIGNUM)
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
_ Ptr (Ptr BIGNUM)
iqmp -> Ptr (Ptr BIGNUM) -> IO (Ptr BIGNUM)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr BIGNUM)
iqmp


{-# LINE 266 "OpenSSL/RSA.hsc" #-}

peekI :: RSAKey a => (Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Integer
peekI :: forall a. RSAKey a => (Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Integer
peekI Ptr RSA -> IO (Ptr BIGNUM)
peeker a
rsa
    = IO Integer -> Integer
forall a. IO a -> a
unsafePerformIO (IO Integer -> Integer) -> IO Integer -> Integer
forall a b. (a -> b) -> a -> b
$
      a -> (Ptr RSA -> IO Integer) -> IO Integer
forall a. a -> (Ptr RSA -> IO a) -> IO a
forall k a. RSAKey k => k -> (Ptr RSA -> IO a) -> IO a
withRSAPtr a
rsa ((Ptr RSA -> IO Integer) -> IO Integer)
-> (Ptr RSA -> IO Integer) -> IO Integer
forall a b. (a -> b) -> a -> b
$ \ Ptr RSA
rsaPtr ->
      do Ptr BIGNUM
bn <- Ptr RSA -> IO (Ptr BIGNUM)
peeker Ptr RSA
rsaPtr
         Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Ptr BIGNUM
bn Ptr BIGNUM -> Ptr BIGNUM -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr BIGNUM
forall a. Ptr a
nullPtr) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"peekI: got a nullPtr"
         BigNum -> IO Integer
peekBN (Ptr BIGNUM -> BigNum
wrapBN Ptr BIGNUM
bn)

peekMI :: RSAKey a => (Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Maybe Integer
peekMI :: forall a.
RSAKey a =>
(Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Maybe Integer
peekMI Ptr RSA -> IO (Ptr BIGNUM)
peeker a
rsa
    = IO (Maybe Integer) -> Maybe Integer
forall a. IO a -> a
unsafePerformIO (IO (Maybe Integer) -> Maybe Integer)
-> IO (Maybe Integer) -> Maybe Integer
forall a b. (a -> b) -> a -> b
$
      a -> (Ptr RSA -> IO (Maybe Integer)) -> IO (Maybe Integer)
forall a. a -> (Ptr RSA -> IO a) -> IO a
forall k a. RSAKey k => k -> (Ptr RSA -> IO a) -> IO a
withRSAPtr a
rsa ((Ptr RSA -> IO (Maybe Integer)) -> IO (Maybe Integer))
-> (Ptr RSA -> IO (Maybe Integer)) -> IO (Maybe Integer)
forall a b. (a -> b) -> a -> b
$ \ Ptr RSA
rsaPtr ->
      do Ptr BIGNUM
bn <- Ptr RSA -> IO (Ptr BIGNUM)
peeker Ptr RSA
rsaPtr
         if Ptr BIGNUM
bn Ptr BIGNUM -> Ptr BIGNUM -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr BIGNUM
forall a. Ptr a
nullPtr then
             Maybe Integer -> IO (Maybe Integer)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Integer
forall a. Maybe a
Nothing
           else
             (Integer -> Maybe Integer) -> IO Integer -> IO (Maybe Integer)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Maybe Integer
forall a. a -> Maybe a
Just (BigNum -> IO Integer
peekBN (Ptr BIGNUM -> BigNum
wrapBN Ptr BIGNUM
bn))

-- |@'rsaD' privKey@ returns the private exponent of the key.
rsaD :: RSAKeyPair -> Integer
rsaD :: RSAKeyPair -> Integer
rsaD = (Ptr RSA -> IO (Ptr BIGNUM)) -> RSAKeyPair -> Integer
forall a. RSAKey a => (Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Integer
peekI Ptr RSA -> IO (Ptr BIGNUM)
rsa_d

-- |@'rsaP' privkey@ returns the secret prime factor @p@ of the key.
rsaP :: RSAKeyPair -> Integer
rsaP :: RSAKeyPair -> Integer
rsaP = (Ptr RSA -> IO (Ptr BIGNUM)) -> RSAKeyPair -> Integer
forall a. RSAKey a => (Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Integer
peekI Ptr RSA -> IO (Ptr BIGNUM)
rsa_p

-- |@'rsaQ' privkey@ returns the secret prime factor @q@ of the key.
rsaQ :: RSAKeyPair -> Integer
rsaQ :: RSAKeyPair -> Integer
rsaQ = (Ptr RSA -> IO (Ptr BIGNUM)) -> RSAKeyPair -> Integer
forall a. RSAKey a => (Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Integer
peekI Ptr RSA -> IO (Ptr BIGNUM)
rsa_q

-- |@'rsaDMP1' privkey@ returns @d mod (p-1)@ of the key.
rsaDMP1 :: RSAKeyPair -> Maybe Integer
rsaDMP1 :: RSAKeyPair -> Maybe Integer
rsaDMP1 = (Ptr RSA -> IO (Ptr BIGNUM)) -> RSAKeyPair -> Maybe Integer
forall a.
RSAKey a =>
(Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Maybe Integer
peekMI Ptr RSA -> IO (Ptr BIGNUM)
rsa_dmp1

-- |@'rsaDMQ1' privkey@ returns @d mod (q-1)@ of the key.
rsaDMQ1 :: RSAKeyPair -> Maybe Integer
rsaDMQ1 :: RSAKeyPair -> Maybe Integer
rsaDMQ1 = (Ptr RSA -> IO (Ptr BIGNUM)) -> RSAKeyPair -> Maybe Integer
forall a.
RSAKey a =>
(Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Maybe Integer
peekMI Ptr RSA -> IO (Ptr BIGNUM)
rsa_dmq1

-- |@'rsaIQMP' privkey@ returns @q^-1 mod p@ of the key.
rsaIQMP :: RSAKeyPair -> Maybe Integer
rsaIQMP :: RSAKeyPair -> Maybe Integer
rsaIQMP = (Ptr RSA -> IO (Ptr BIGNUM)) -> RSAKeyPair -> Maybe Integer
forall a.
RSAKey a =>
(Ptr RSA -> IO (Ptr BIGNUM)) -> a -> Maybe Integer
peekMI Ptr RSA -> IO (Ptr BIGNUM)
rsa_iqmp

{- instances ---------------------------------------------------------------- -}

instance Eq RSAPubKey where
    RSAPubKey
a == :: RSAPubKey -> RSAPubKey -> Bool
== RSAPubKey
b
        = RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAPubKey
a Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAPubKey
b Bool -> Bool -> Bool
&&
          RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAPubKey
a Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAPubKey
b

instance Eq RSAKeyPair where
    RSAKeyPair
a == :: RSAKeyPair -> RSAKeyPair -> Bool
== RSAKeyPair
b
        = RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAKeyPair
a Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAKeyPair
b Bool -> Bool -> Bool
&&
          RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAKeyPair
a Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAKeyPair
b Bool -> Bool -> Bool
&&
          RSAKeyPair -> Integer
rsaD RSAKeyPair
a Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== RSAKeyPair -> Integer
rsaD RSAKeyPair
b Bool -> Bool -> Bool
&&
          RSAKeyPair -> Integer
rsaP RSAKeyPair
a Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== RSAKeyPair -> Integer
rsaP RSAKeyPair
b Bool -> Bool -> Bool
&&
          RSAKeyPair -> Integer
rsaQ RSAKeyPair
a Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== RSAKeyPair -> Integer
rsaQ RSAKeyPair
b

instance Ord RSAPubKey where
    RSAPubKey
a compare :: RSAPubKey -> RSAPubKey -> Ordering
`compare` RSAPubKey
b
        | RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAPubKey
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAPubKey
b = Ordering
LT
        | RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAPubKey
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAPubKey
b = Ordering
GT
        | RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAPubKey
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAPubKey
b = Ordering
LT
        | RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAPubKey
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAPubKey
b = Ordering
GT
        | Bool
otherwise       = Ordering
EQ

instance Ord RSAKeyPair where
    RSAKeyPair
a compare :: RSAKeyPair -> RSAKeyPair -> Ordering
`compare` RSAKeyPair
b
        | RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAKeyPair
b = Ordering
LT
        | RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAKeyPair
b = Ordering
GT
        | RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAKeyPair
b = Ordering
LT
        | RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAKeyPair
b = Ordering
GT
        | RSAKeyPair -> Integer
rsaD RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< RSAKeyPair -> Integer
rsaD RSAKeyPair
b = Ordering
LT
        | RSAKeyPair -> Integer
rsaD RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> RSAKeyPair -> Integer
rsaD RSAKeyPair
b = Ordering
GT
        | RSAKeyPair -> Integer
rsaP RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< RSAKeyPair -> Integer
rsaP RSAKeyPair
b = Ordering
LT
        | RSAKeyPair -> Integer
rsaP RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> RSAKeyPair -> Integer
rsaP RSAKeyPair
b = Ordering
GT
        | RSAKeyPair -> Integer
rsaQ RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< RSAKeyPair -> Integer
rsaQ RSAKeyPair
b = Ordering
LT
        | RSAKeyPair -> Integer
rsaQ RSAKeyPair
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> RSAKeyPair -> Integer
rsaQ RSAKeyPair
b = Ordering
GT
        | Bool
otherwise       = Ordering
EQ

instance Show RSAPubKey where
    show :: RSAPubKey -> String
show RSAPubKey
a
        = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ String
"RSAPubKey {"
                 , String
"rsaN = ", Integer -> String
forall a. Show a => a -> String
show (RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAPubKey
a), String
", "
                 , String
"rsaE = ", Integer -> String
forall a. Show a => a -> String
show (RSAPubKey -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAPubKey
a)
                 , String
"}"
                 ]

instance Show RSAKeyPair where
    show :: RSAKeyPair -> String
show RSAKeyPair
a
        = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ String
"RSAKeyPair {"
                 , String
"rsaN = ", Integer -> String
forall a. Show a => a -> String
show (RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaN RSAKeyPair
a), String
", "
                 , String
"rsaE = ", Integer -> String
forall a. Show a => a -> String
show (RSAKeyPair -> Integer
forall k. RSAKey k => k -> Integer
rsaE RSAKeyPair
a), String
", "
                 , String
"rsaD = ", Integer -> String
forall a. Show a => a -> String
show (RSAKeyPair -> Integer
rsaD RSAKeyPair
a), String
", "
                 , String
"rsaP = ", Integer -> String
forall a. Show a => a -> String
show (RSAKeyPair -> Integer
rsaP RSAKeyPair
a), String
", "
                 , String
"rsaQ = ", Integer -> String
forall a. Show a => a -> String
show (RSAKeyPair -> Integer
rsaQ RSAKeyPair
a)
                 , String
"}"
                 ]