-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# OPTIONS_GHC -fno-cse -fno-full-laziness #-}
{-# OPTIONS_HADDOCK hide #-}

-- | Concrete purity exception for the separately audited pinned PBKDF2 path.
-- Caller-owned scratch is the only mutable output. Neither native configured
-- allocation, RNG, global state, callbacks nor logging is reached. See
-- docs/development/PBKDF2_PURITY.md. Never generalize to an arbitrary IO runner.
module Cometa.Internal.Pbkdf2Pure (deriveBytes) where

-- IMPORTS --------------------------------------------------------------------

import Cometa.Error (Error (..))
import qualified Cometa.Error.Code as Code
import Cometa.Internal.Error (fromNativeFailure)
import qualified Cometa.Internal.PasswordCrypto as Internal
import Control.Exception (evaluate)
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.Text as Text
import Data.Word (Word32)
import System.IO.Unsafe (unsafePerformIO)

-- DEFINITIONS ----------------------------------------------------------------

-- | Validated inputs only. Force the strict owned result and any diagnostic
-- before returning from this boundary; repeated evaluation is equivalent.
{-# NOINLINE deriveBytes #-}
deriveBytes :: ByteString -> ByteString -> Word32 -> Int -> Either Error ByteString
deriveBytes :: ByteString
-> ByteString -> Word32 -> Int -> Either Error ByteString
deriveBytes ByteString
password ByteString
salt Word32
iterations Int
size = IO (Either Error ByteString) -> Either Error ByteString
forall a. IO a -> a
unsafePerformIO (IO (Either Error ByteString) -> Either Error ByteString)
-> IO (Either Error ByteString) -> Either Error ByteString
forall a b. (a -> b) -> a -> b
$ do
  Either Error ByteString
result <- (NativeFailure -> Either Error ByteString)
-> (ByteString -> Either Error ByteString)
-> Either NativeFailure ByteString
-> Either Error ByteString
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Error -> Either Error ByteString)
-> (NativeFailure -> Error)
-> NativeFailure
-> Either Error ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NativeFailure -> Error
fromNativeFailure) ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (Either NativeFailure ByteString -> Either Error ByteString)
-> IO (Either NativeFailure ByteString)
-> IO (Either Error ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> ByteString
-> Word32
-> Int
-> IO (Either NativeFailure ByteString)
Internal.deriveBytes ByteString
password ByteString
salt Word32
iterations Int
size
  () -> IO ()
forall a. a -> IO a
evaluate (Either Error ByteString -> ()
forceResult Either Error ByteString
result)
  Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either Error ByteString
result

forceResult :: Either Error ByteString -> ()
forceResult :: Either Error ByteString -> ()
forceResult (Right ByteString
bytes) = ByteString -> Int
ByteString.length ByteString
bytes Int -> () -> ()
forall a b. a -> b -> b
`seq` ()
forceResult (Left (NativeError Text
operation ErrorCode
code Text
diagnostic)) =
  Text -> Int
Text.length Text
operation Int -> () -> ()
forall a b. a -> b -> b
`seq` ErrorCode -> Integer
Code.toNumber ErrorCode
code Integer -> () -> ()
forall a b. a -> b -> b
`seq` Text -> Int
Text.length Text
diagnostic Int -> () -> ()
forall a b. a -> b -> b
`seq` ()
forceResult (Left (NativeContractViolation Text
operation)) = Text -> Int
Text.length Text
operation Int -> () -> ()
forall a b. a -> b -> b
`seq` ()
forceResult (Left Error
err) = Error
err Error -> () -> ()
forall a b. a -> b -> b
`seq` ()