{-|
Module      : Botan.Low.RSA
Description : Algorithm specific key operations: RSA
Copyright   : (c) 2023-2024, Apotheca Labs
              (c) 2024-2025, Haskell Foundation
License     : BSD-3-Clause
Maintainer  : joris@well-typed.com, leo@apotheca.io
Stability   : experimental
Portability : POSIX
-}

module Botan.Low.PubKey.RSA (
    privKeyLoadRSA
  , privKeyLoadRSA_PKCS1
  , privKeyRSAGetPrivKey
  , pubKeyLoadRSA
  ) where

import           Botan.Bindings.PubKey.RSA
import           Botan.Low.MPI
import           Botan.Low.PubKey
import           Botan.Low.Remake
import           Data.ByteString (ByteString)
import           Data.Word

-- /*
-- * Algorithm specific key operations: RSA
-- */

privKeyLoadRSA
    :: MP           -- ^ __p__
    -> MP           -- ^ __q__
    -> MP           -- ^ __e__
    -> IO PrivKey   -- ^ __key__
privKeyLoadRSA :: MP -> MP -> MP -> IO PrivKey
privKeyLoadRSA = (Ptr BotanPrivKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
-> MP -> MP -> MP -> IO PrivKey
mkPrivKeyLoad3 Ptr BotanPrivKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt
botan_privkey_load_rsa

privKeyLoadRSA_PKCS1
    :: ByteString   -- ^ __bits__
    -> IO PrivKey   -- ^ __key__
privKeyLoadRSA_PKCS1 :: ByteString -> IO PrivKey
privKeyLoadRSA_PKCS1 = ((Ptr BotanPrivKey -> IO CInt) -> IO PrivKey)
-> (Ptr BotanPrivKey -> ConstPtr Word8 -> CSize -> IO CInt)
-> ByteString
-> IO PrivKey
forall botan object.
((Ptr botan -> IO CInt) -> IO object)
-> (Ptr botan -> ConstPtr Word8 -> CSize -> IO CInt)
-> ByteString
-> IO object
mkCreateObjectCBytesLen (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
createPrivKey Ptr BotanPrivKey -> ConstPtr Word8 -> CSize -> IO CInt
botan_privkey_load_rsa_pkcs1

privKeyRSAGetPrivKey
    :: PrivKey          -- ^ __rsa_key__
    -> Word32           -- ^ __flags__
    -> IO ByteString    -- ^ __out__
-- WRONG: privKeyRSAGetPrivKey = mkCreateObjectCBytesLen1 botan_privkey_rsa_get_privkey
privKeyRSAGetPrivKey :: PrivKey -> Word32 -> IO ByteString
privKeyRSAGetPrivKey = (forall b. PrivKey -> (BotanPrivKey -> IO b) -> IO b)
-> (BotanPrivKey -> Word32 -> Ptr Word8 -> Ptr CSize -> IO CInt)
-> PrivKey
-> Word32
-> IO ByteString
forall object botan a.
(forall b. object -> (botan -> IO b) -> IO b)
-> (botan -> a -> Ptr Word8 -> Ptr CSize -> IO CInt)
-> object
-> a
-> IO ByteString
mkWithObjectGetterCBytesLen1 PrivKey -> (BotanPrivKey -> IO b) -> IO b
forall b. PrivKey -> (BotanPrivKey -> IO b) -> IO b
withPrivKey
    ((BotanPrivKey -> Word32 -> Ptr Word8 -> Ptr CSize -> IO CInt)
 -> PrivKey -> Word32 -> IO ByteString)
-> (BotanPrivKey -> Word32 -> Ptr Word8 -> Ptr CSize -> IO CInt)
-> PrivKey
-> Word32
-> IO ByteString
forall a b. (a -> b) -> a -> b
$ \ BotanPrivKey
rsa_key Word32
flags Ptr Word8
out Ptr CSize
out_len -> BotanPrivKey -> Ptr Word8 -> Ptr CSize -> Word32 -> IO CInt
botan_privkey_rsa_get_privkey BotanPrivKey
rsa_key Ptr Word8
out Ptr CSize
out_len Word32
flags

pubKeyLoadRSA
    :: MP           -- ^ __n__
    -> MP           -- ^ __e__
    -> IO PubKey    -- ^ __key__
pubKeyLoadRSA :: MP -> MP -> IO PubKey
pubKeyLoadRSA = (Ptr BotanPubKey -> BotanMP -> BotanMP -> IO CInt)
-> MP -> MP -> IO PubKey
mkPubKeyLoad2 Ptr BotanPubKey -> BotanMP -> BotanMP -> IO CInt
botan_pubkey_load_rsa