Add support for HKDF (RFC 5869)
This commit is contained in:
parent
c2d791f2ec
commit
43890b1175
77
Crypto/KDF/HKDF.hs
Normal file
77
Crypto/KDF/HKDF.hs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
-- |
|
||||||
|
-- Module : Crypto.KDF.HKDF
|
||||||
|
-- License : BSD-style
|
||||||
|
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
|
||||||
|
-- Stability : experimental
|
||||||
|
-- Portability : unknown
|
||||||
|
--
|
||||||
|
-- Key Derivation Function based on HMAC
|
||||||
|
--
|
||||||
|
-- See rfc5869
|
||||||
|
--
|
||||||
|
{-# LANGUAGE BangPatterns #-}
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
|
module Crypto.KDF.HKDF
|
||||||
|
( PRK
|
||||||
|
, extract
|
||||||
|
, extractSkip
|
||||||
|
, expand
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Data.Word
|
||||||
|
import Crypto.Hash
|
||||||
|
import Crypto.MAC.HMAC
|
||||||
|
import Crypto.Internal.ByteArray (ScrubbedBytes, Bytes, ByteArray, ByteArrayAccess)
|
||||||
|
import qualified Crypto.Internal.ByteArray as B
|
||||||
|
|
||||||
|
-- | Pseudo Random Key
|
||||||
|
data PRK a = PRK (HMAC a) | PRK_NoExpand ScrubbedBytes
|
||||||
|
deriving (Eq)
|
||||||
|
|
||||||
|
-- | Extract a Pseudo Random Key using the parameter and the underlaying hash mechanism
|
||||||
|
extract :: (HashAlgorithm a, ByteArrayAccess salt, ByteArrayAccess ikm)
|
||||||
|
=> salt -- ^ Salt
|
||||||
|
-> ikm -- ^ Input Keying Material
|
||||||
|
-> PRK a -- ^ Pseudo random key
|
||||||
|
extract salt ikm = PRK $ hmac salt ikm
|
||||||
|
|
||||||
|
-- | Create a PRK directly from the input key material, skipping any hmacing
|
||||||
|
extractSkip :: (HashAlgorithm a, ByteArrayAccess ikm)
|
||||||
|
=> ikm
|
||||||
|
-> PRK a
|
||||||
|
extractSkip ikm = PRK_NoExpand $ B.convert ikm
|
||||||
|
|
||||||
|
-- | Expand key material of specific length out of the parameters
|
||||||
|
expand :: (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 data
|
||||||
|
expand prkAt infoAt outputLength =
|
||||||
|
let hF = hFGet prkAt
|
||||||
|
in 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)
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ instance Eq (HMAC a) where
|
|||||||
(HMAC b1) == (HMAC b2) = B.constEq b1 b2
|
(HMAC b1) == (HMAC b2) = B.constEq b1 b2
|
||||||
|
|
||||||
-- | compute a MAC using the supplied hashing function
|
-- | compute a MAC using the supplied hashing function
|
||||||
hmac :: (ByteArrayAccess key, ByteArray message, HashAlgorithm a)
|
hmac :: (ByteArrayAccess key, ByteArrayAccess message, HashAlgorithm a)
|
||||||
=> key -- ^ Secret key
|
=> key -- ^ Secret key
|
||||||
-> message -- ^ Message to MAC
|
-> message -- ^ Message to MAC
|
||||||
-> HMAC a
|
-> HMAC a
|
||||||
|
|||||||
@ -12,7 +12,7 @@ Description:
|
|||||||
.
|
.
|
||||||
* Assymmetric crypto: DSA, RSA, DH, ECDH, ECDSA, ECC, Curve25519, Ed25519
|
* Assymmetric crypto: DSA, RSA, DH, ECDH, ECDSA, ECC, Curve25519, Ed25519
|
||||||
.
|
.
|
||||||
* Key Derivation Function: PBKDF2, Scrypt
|
* Key Derivation Function: PBKDF2, Scrypt, HKDF
|
||||||
.
|
.
|
||||||
* Cryptographic Random generation: System Entropy, Deterministic Random Generator
|
* Cryptographic Random generation: System Entropy, Deterministic Random Generator
|
||||||
.
|
.
|
||||||
@ -106,6 +106,7 @@ Library
|
|||||||
Crypto.KDF.PBKDF2
|
Crypto.KDF.PBKDF2
|
||||||
Crypto.KDF.Scrypt
|
Crypto.KDF.Scrypt
|
||||||
Crypto.KDF.BCrypt
|
Crypto.KDF.BCrypt
|
||||||
|
Crypto.KDF.HKDF
|
||||||
Crypto.Hash
|
Crypto.Hash
|
||||||
Crypto.Hash.IO
|
Crypto.Hash.IO
|
||||||
Crypto.Hash.Algorithms
|
Crypto.Hash.Algorithms
|
||||||
|
|||||||
54
tests/KAT_HKDF.hs
Normal file
54
tests/KAT_HKDF.hs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
module KAT_HKDF (tests) where
|
||||||
|
|
||||||
|
import qualified Crypto.KDF.HKDF as HKDF
|
||||||
|
import Crypto.Hash (MD5(..), SHA1(..), SHA256(..)
|
||||||
|
, Keccak_224(..), Keccak_256(..), Keccak_384(..), Keccak_512(..)
|
||||||
|
, SHA3_224(..), SHA3_256(..), SHA3_384(..), SHA3_512(..)
|
||||||
|
, HashAlgorithm, digestFromByteString)
|
||||||
|
import qualified Data.ByteString as B
|
||||||
|
|
||||||
|
import Imports
|
||||||
|
|
||||||
|
|
||||||
|
data KDFVector hash = KDFVector
|
||||||
|
{ kdfIKM :: ByteString
|
||||||
|
, kdfSalt :: ByteString
|
||||||
|
, kdfInfo :: ByteString
|
||||||
|
, kdfResult :: ByteString
|
||||||
|
}
|
||||||
|
|
||||||
|
sha256KDFVectors :: [KDFVector SHA256]
|
||||||
|
sha256KDFVectors =
|
||||||
|
[ KDFVector
|
||||||
|
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
|
||||||
|
(B.pack [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c])
|
||||||
|
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9"
|
||||||
|
"\x3c\xb2\x5f\x25\xfa\xac\xd5\x7a\x90\x43\x4f\x64\xd0\x36\x2f\x2a\x2d\x2d\x0a\x90\xcf\x1a\x5a\x4c\x5d\xb0\x2d\x56\xec\xc4\xc5\xbf\x34\x00\x72\x08\xd5\xb8\x87\x18\x58\x65"
|
||||||
|
, KDFVector
|
||||||
|
(B.pack [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f])
|
||||||
|
(B.pack [0x60,0x61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x36,0x77,0x93,0xa9,0xac,0xa3,0xdb,0x71,0xcc,0x30,0xc5,0x81,0x79,0xec,0x3e,0x87,0xc1,0x4c,0x01,0xd5,0xc1,0xf3,0x43,0x4f,0x1d,0x87])
|
||||||
|
]
|
||||||
|
|
||||||
|
kdfTests :: [TestTree]
|
||||||
|
kdfTests =
|
||||||
|
[ testGroup "sha256" $ concatMap toKDFTest $ zip is sha256KDFVectors
|
||||||
|
]
|
||||||
|
where toKDFTest (i, kdfVector) =
|
||||||
|
[ testCase (show i) (t HKDF.extract kdfVector)
|
||||||
|
]
|
||||||
|
|
||||||
|
t :: HashAlgorithm a => (ByteString -> ByteString -> HKDF.PRK a) -> KDFVector a -> Assertion
|
||||||
|
t ext v =
|
||||||
|
let prk = ext (kdfSalt v) (kdfIKM v)
|
||||||
|
in kdfResult v @=? HKDF.expand prk (kdfInfo v) (B.length $ kdfResult v)
|
||||||
|
|
||||||
|
is :: [Int]
|
||||||
|
is = [1..]
|
||||||
|
|
||||||
|
|
||||||
|
tests = testGroup "HKDF"
|
||||||
|
[ testGroup "KATs" kdfTests
|
||||||
|
]
|
||||||
@ -11,6 +11,7 @@ import qualified Salsa
|
|||||||
import qualified ChaCha
|
import qualified ChaCha
|
||||||
import qualified ChaChaPoly1305
|
import qualified ChaChaPoly1305
|
||||||
import qualified KAT_HMAC
|
import qualified KAT_HMAC
|
||||||
|
import qualified KAT_HKDF
|
||||||
import qualified KAT_PBKDF2
|
import qualified KAT_PBKDF2
|
||||||
import qualified KAT_Curve25519
|
import qualified KAT_Curve25519
|
||||||
import qualified KAT_Ed25519
|
import qualified KAT_Ed25519
|
||||||
@ -42,6 +43,7 @@ tests = testGroup "cryptonite"
|
|||||||
[ KAT_PBKDF2.tests
|
[ KAT_PBKDF2.tests
|
||||||
, KAT_Scrypt.tests
|
, KAT_Scrypt.tests
|
||||||
, BCrypt.tests
|
, BCrypt.tests
|
||||||
|
, KAT_HKDF.tests
|
||||||
]
|
]
|
||||||
, testGroup "block-cipher"
|
, testGroup "block-cipher"
|
||||||
[ KAT_AES.tests
|
[ KAT_AES.tests
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user