-- 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.

-- | Password-based key derivation using PBKDF2-HMAC-SHA512. Inputs are raw bytes;
-- no text encoding or normalization is applied. This module deliberately exports
-- secret bytes: callers control their lifetime and must avoid logging them.
--
-- @
-- import qualified Cometa.Crypto.Pbkdf2 as Pbkdf2
-- -- Deterministic example data only; choose cost and salt for your application.
-- derive password salt = Pbkdf2.deriveBytes password salt 100000 32
-- @
module Cometa.Crypto.Pbkdf2 (deriveBytes) where

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

import Cometa.Error (Error (..))
import qualified Cometa.Internal.Pbkdf2Pure as Internal
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString

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

-- | Explicitly derive and export secret bytes from password, salt, iteration
-- count and output byte count, in that order. Password may be empty or contain
-- NUL; salt must be nonempty. Iterations must be @1..4294967295@. Output must be
-- @1..min(maxBound :: Int, 64 * (2^32 - 1))@ bytes, the PBKDF2 block limit.
-- Invalid inputs return 'EmptyPbkdf2Salt', 'InvalidPbkdf2Iterations' or
-- 'InvalidPbkdf2OutputLength' before derivation. Other failures return
-- 'NativeError'; allocation exceptions propagate. No default cost is chosen.
-- Large requests can require substantial memory and computation. Cancellation
-- waits for the ongoing derivation and cleanup. Input and returned Haskell
-- bytes are not erased by this operation.
deriveBytes :: ByteString -> ByteString -> Integer -> Integer -> Either Error ByteString
deriveBytes :: ByteString
-> ByteString -> Integer -> Integer -> Either Error ByteString
deriveBytes ByteString
password ByteString
salt Integer
iterations Integer
size
  | ByteString -> Bool
ByteString.null ByteString
salt = Error -> Either Error ByteString
forall a b. a -> Either a b
Left Error
EmptyPbkdf2Salt
  | Integer
iterations Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
1 Bool -> Bool -> Bool
|| Integer
iterations Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
4294967295 = Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Integer -> Error
InvalidPbkdf2Iterations Integer
iterations)
  | Integer
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
1 Bool -> Bool -> Bool
|| Integer
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
forall a. Bounded a => a
maxBound :: Int)) (Integer
64 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer
2 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
32 :: Int) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1)) = Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Integer -> Error
InvalidPbkdf2OutputLength Integer
size)
  | Bool
otherwise = ByteString
-> ByteString -> Word32 -> Int -> Either Error ByteString
Internal.deriveBytes ByteString
password ByteString
salt (Integer -> Word32
forall a. Num a => Integer -> a
fromInteger Integer
iterations) (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
size)