-- 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-encrypted storage using EMIP-003: PBKDF2-HMAC-SHA512 with
-- 19162 iterations and a 32-byte key, then ChaCha20-Poly1305 with empty AAD.
-- The encoded envelope is salt (32 bytes), nonce (12), tag (16), ciphertext.
-- Passwords and plaintext are raw bytes with no normalization. Plaintext and
-- passwords supplied by the caller, and exported plaintext, are not erased.
--
-- @
-- import qualified Cometa.Crypto.Emip3 as Emip3
-- store password bytes = Emip3.encrypt password bytes
-- load password envelope = Emip3.decryptBytes password envelope
-- @
module Cometa.Crypto.Emip3 (encrypt, decryptBytes) where

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

import Cometa.Error (Error (..))
import Cometa.Internal.Error (fromNativeFailure)
import qualified Cometa.Internal.PasswordCrypto as Internal
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString

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

-- | Encrypt plaintext, including empty data, using a fresh random salt and
-- nonce. Password comes first and may be empty or contain NUL. Plaintext must
-- contain @0..min(maxBound - 60, 274877906880)@ bytes, where @maxBound :: Int@;
-- other lengths return 'InvalidEmip3PlaintextLength' before encryption.
-- Success is exactly 60 bytes longer than the plaintext. Random generation
-- and allocation occur in 'IO'. Other failures return 'NativeError'; allocation
-- exceptions propagate. Cancellation waits for the operation and cleanup.
encrypt :: ByteString -> ByteString -> IO (Either Error ByteString)
encrypt :: ByteString -> ByteString -> IO (Either Error ByteString)
encrypt ByteString
password ByteString
plaintext
  | Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
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 -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
60) Integer
274877906880 = Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Int -> Error
InvalidEmip3PlaintextLength Int
size))
  | Bool
otherwise = (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 -> IO (Either NativeFailure ByteString)
Internal.encrypt ByteString
password ByteString
plaintext
  where
    size :: Int
size = ByteString -> Int
ByteString.length ByteString
plaintext

-- | Authenticate and explicitly export plaintext from an encoded envelope.
-- Password comes first and may be empty or contain NUL. Envelopes must contain
-- 60 through 274877906940 bytes; other lengths return
-- 'InvalidEmip3EnvelopeLength'. A 60-byte envelope authenticates empty plaintext.
-- A wrong password or modified envelope
-- returns 'NativeError' with the native generic failure code and never returns
-- plaintext. Allocation exceptions propagate. Cancellation waits for native
-- completion and cleanup. Returned plaintext has application-managed lifetime;
-- this function cannot erase it or subsequent copies.
decryptBytes :: ByteString -> ByteString -> IO (Either Error ByteString)
decryptBytes :: ByteString -> ByteString -> IO (Either Error ByteString)
decryptBytes ByteString
password ByteString
envelope
  | ByteString -> Int
ByteString.length ByteString
envelope Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
60 Bool -> Bool -> Bool
|| Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
ByteString.length ByteString
envelope) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
274877906940 = Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Int -> Error
InvalidEmip3EnvelopeLength (ByteString -> Int
ByteString.length ByteString
envelope)))
  | Bool
otherwise = (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 -> IO (Either NativeFailure ByteString)
Internal.decryptBytes ByteString
password ByteString
envelope