{-|
Module      : Botan.Low.Ed25519
Description : Algorithm specific key operations: Ed25519
Copyright   : (c) Leo D, 2023
License     : BSD-3-Clause
Maintainer  : leo@apotheca.io
Stability   : experimental
Portability : POSIX
-}

module Botan.Low.PubKey.Ed25519 where

import qualified Data.ByteString as ByteString

import Botan.Bindings.PubKey
import Botan.Bindings.PubKey.Ed25519

import Botan.Low.Error
import Botan.Low.Make
import Botan.Low.Prelude
import Botan.Low.PubKey
import Botan.Low.Remake

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

-- NOTE: Input must be exactly 32 bytes long
privKeyLoadEd25519
    :: ByteString       -- ^ __privkey[32]__
    -> IO PrivKey       -- ^ __key__
privKeyLoadEd25519 :: ByteString -> IO PrivKey
privKeyLoadEd25519 = ((Ptr BotanPrivKey -> IO CInt) -> IO PrivKey)
-> (Ptr BotanPrivKey -> ConstPtr Word8 -> IO CInt)
-> ByteString
-> IO PrivKey
forall botan object.
((Ptr botan -> IO CInt) -> IO object)
-> (Ptr botan -> ConstPtr Word8 -> IO CInt)
-> ByteString
-> IO object
mkCreateObjectCBytes (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
createPrivKey Ptr BotanPrivKey -> ConstPtr Word8 -> IO CInt
botan_privkey_load_ed25519

-- NOTE: Input must be exactly 32 bytes long
pubKeyLoadEd25519
    :: ByteString       -- ^ __pubkey[32]__
    -> IO PubKey        -- ^ __key__
pubKeyLoadEd25519 :: ByteString -> IO PubKey
pubKeyLoadEd25519 = ((Ptr BotanPubKey -> IO CInt) -> IO PubKey)
-> (Ptr BotanPubKey -> ConstPtr Word8 -> IO CInt)
-> ByteString
-> IO PubKey
forall botan object.
((Ptr botan -> IO CInt) -> IO object)
-> (Ptr botan -> ConstPtr Word8 -> IO CInt)
-> ByteString
-> IO object
mkCreateObjectCBytes (Ptr BotanPubKey -> IO CInt) -> IO PubKey
createPubKey Ptr BotanPubKey -> ConstPtr Word8 -> IO CInt
botan_pubkey_load_ed25519

privKeyEd25519GetPrivKey
    :: PrivKey          -- ^ __output[64]__
    -> IO ByteString    -- ^ __key__
privKeyEd25519GetPrivKey :: PrivKey -> IO ByteString
privKeyEd25519GetPrivKey PrivKey
sk = PrivKey -> (BotanPrivKey -> IO ByteString) -> IO ByteString
forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey PrivKey
sk ((BotanPrivKey -> IO ByteString) -> IO ByteString)
-> (BotanPrivKey -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \ BotanPrivKey
skPtr -> do
    Int -> (Ptr Word8 -> IO ()) -> IO ByteString
forall byte. Int -> (Ptr byte -> IO ()) -> IO ByteString
allocBytes Int
64 ((Ptr Word8 -> IO ()) -> IO ByteString)
-> (Ptr Word8 -> IO ()) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
outPtr -> do
        HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanPrivKey -> Ptr Word8 -> IO CInt
botan_privkey_ed25519_get_privkey BotanPrivKey
skPtr Ptr Word8
outPtr

pubKeyEd25519GetPubKey
    :: PubKey           -- ^ __pubkey[32]__
    -> IO ByteString    -- ^ __key__
pubKeyEd25519GetPubKey :: PubKey -> IO ByteString
pubKeyEd25519GetPubKey PubKey
pk = PubKey -> (BotanPubKey -> IO ByteString) -> IO ByteString
forall a. PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey PubKey
pk ((BotanPubKey -> IO ByteString) -> IO ByteString)
-> (BotanPubKey -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \ BotanPubKey
pkPtr -> do
    Int -> (Ptr Word8 -> IO ()) -> IO ByteString
forall byte. Int -> (Ptr byte -> IO ()) -> IO ByteString
allocBytes Int
32 ((Ptr Word8 -> IO ()) -> IO ByteString)
-> (Ptr Word8 -> IO ()) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
outPtr -> do
        HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanPubKey -> Ptr Word8 -> IO CInt
botan_pubkey_ed25519_get_pubkey BotanPubKey
pkPtr Ptr Word8
outPtr