packages feed

crypton-2.3.1 / Crypto / KDF / HKDF.hs

{-# LANGUAGE BangPatterns #-}{-# LANGUAGE ScopedTypeVariables #-}-- |-- Module      : Crypto.KDF.HKDF-- License     : BSD-style-- Maintainer  : Vincent Hanquez <vincent@snarc.org>-- Stability   : experimental-- Portability : unknown---- Key Derivation Function based on HMAC---- See RFC5869module Crypto.KDF.HKDF (    PRK,    extract,    extractSkip,    expand,    tryExpand,    toPRK,) whereimport Crypto.Errorimport Crypto.Hashimport Crypto.Internal.ByteArray (    ByteArray,    ByteArrayAccess,    ScrubbedBytes, )import qualified Crypto.Internal.ByteArray as Bimport Crypto.MAC.HMACimport Data.Word-- | Pseudo Random Keydata PRK a = PRK (HMAC a) | PRK_NoExpand ScrubbedBytes    deriving (Eq)instance ByteArrayAccess (PRK a) where    length (PRK hm) = B.length hm    length (PRK_NoExpand sb) = B.length sb    withByteArray (PRK hm) = B.withByteArray hm    withByteArray (PRK_NoExpand sb) = B.withByteArray sb-- | Extract a Pseudo Random Key using the parameter and the underlying hash mechanismextract    :: (HashAlgorithm a, ByteArrayAccess salt, ByteArrayAccess ikm)    => salt    -- ^ Salt    -> ikm    -- ^ Input Keying Material    -> PRK a    -- ^ Pseudo random keyextract salt ikm = PRK $ hmac salt ikm-- | Create a PRK directly from the input key material.---- Only use when guaranteed to have a good quality and random data to use directly as key.-- This effectively skip a HMAC with key=salt and data=key.extractSkip    :: ByteArrayAccess ikm    => ikm    -> PRK aextractSkip ikm = PRK_NoExpand $ B.convert ikm-- | Expand key material of specific length out of the parameters---- Requests exceeding the RFC 5869 limit of @255 * HashLen@ raise-- 'CryptoError_OutputLengthTooBig'; 'tryExpand' reports the same condition as-- 'CryptoFailed'.expand    :: forall a info out     . (HashAlgorithm a, ByteArrayAccess info, ByteArray out)    => PRK a    -- ^ Pseudo Random Key    -> info    -- ^ Optional context and application specific information    -> Int    -- ^ Output length in bytes    -> out    -- ^ Output dataexpand prkAt infoAt outputLength =    throwCryptoError (tryExpand prkAt infoAt outputLength)-- | Expand key material of specific length out of the parameters, reporting a-- length the RFC refuses rather than raising.tryExpand    :: forall a info out     . (HashAlgorithm a, ByteArrayAccess info, ByteArray out)    => PRK a    -- ^ Pseudo Random Key    -> info    -- ^ Optional context and application specific information    -> Int    -- ^ Output length in bytes    -> CryptoFailable out    -- ^ Output datatryExpand prkAt infoAt outputLength    | outputLength > 255 * hashDigestSize (undefined :: a) =        CryptoFailed CryptoError_OutputLengthTooBig    | otherwise =        let hF = hFGet prkAt         in CryptoPassed $ B.concat $ loop hF B.empty outputLength 1  where    hFGet :: (HashAlgorithm a, ByteArrayAccess b) => PRK a -> (b -> HMAC a)    hFGet prk = case prk of        PRK hmacKey -> hmac hmacKey        PRK_NoExpand ikm -> hmac ikm    info :: ScrubbedBytes    info = B.convert infoAt    loop        :: HashAlgorithm a        => (ScrubbedBytes -> HMAC a)        -> ScrubbedBytes        -> Int        -> Word8        -> [ScrubbedBytes]    loop hF tim1 n i        | n <= 0 = []        | otherwise =            let input = B.concat [tim1, info, B.singleton i] :: ScrubbedBytes                ti = B.convert $ hF input                hashLen = B.length ti                r = n - hashLen             in (if n >= hashLen then ti else B.take n ti)                    : loop hF ti r (i + 1)-- | Take bytes, such as a PRK kept from an earlier 'extract', as a PRK.-- 'Nothing' unless they are exactly the digest size of the hash.toPRK :: (HashAlgorithm a, ByteArrayAccess ba) => ba -> Maybe (PRK a)toPRK bs = case digestFromByteString bs of    Nothing -> Nothing    Just digest -> Just $ PRK $ HMAC digest