aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@google.com>2018-08-02 16:43:43 -0400
committerGravatar Benjamin Barenblat <bbaren@google.com>2018-08-02 16:43:43 -0400
commita70d7fa4a478753e0ab765fb05559e1769760232 (patch)
tree9e1385446eb1914a5c1eed5f0a434a0e5549365e
parentc6466a719d784054a82578c619e7dfff613e777b (diff)
Codec.Crypto.HKDF: Add test 7 from RFC 5869
-rw-r--r--src/Codec/Crypto/HKDF.hs4
-rw-r--r--src/Types.hs6
-rw-r--r--tests/Codec/Crypto/HKDFTests.hs7
3 files changed, 13 insertions, 4 deletions
diff --git a/src/Codec/Crypto/HKDF.hs b/src/Codec/Crypto/HKDF.hs
index bb29ca6..8e81fd1 100644
--- a/src/Codec/Crypto/HKDF.hs
+++ b/src/Codec/Crypto/HKDF.hs
@@ -13,7 +13,7 @@
-- the License.
module Codec.Crypto.HKDF
- ( Salt(Salt), SecretKey(SecretKey)
+ ( Salt(Salt), SecretKey(SecretKey), noSalt
, extract
) where
@@ -27,7 +27,7 @@ import Unsafe.Coerce (unsafeCoerce)
import Data.Digest.Internal (Algorithm(Algorithm))
import Internal.Digest (evpMaxMDSize)
import Internal.HKDF
-import Types (Salt(Salt), SecretKey(SecretKey))
+import Types (Salt(Salt), SecretKey(SecretKey), noSalt)
-- | Computes an HKDF pseudorandom key (PRK) as specified by RFC 5869.
extract :: Algorithm -> Salt -> SecretKey -> SecretKey
diff --git a/src/Types.hs b/src/Types.hs
index a625c3e..6b430c2 100644
--- a/src/Types.hs
+++ b/src/Types.hs
@@ -13,16 +13,20 @@
-- the License.
module Types
- ( Salt(Salt)
+ ( Salt(Salt), noSalt
, SecretKey(SecretKey)
) where
import Data.ByteString (ByteString)
+import qualified Data.ByteString as ByteString
-- | A salt. Equality comparisons on this type are variable-time.
newtype Salt = Salt ByteString
deriving (Eq, Ord, Show)
+noSalt :: Salt
+noSalt = Salt ByteString.empty
+
-- | A secret key used as input to a cipher or HMAC. Equality comparisons on
-- this type are variable-time.
newtype SecretKey = SecretKey ByteString
diff --git a/tests/Codec/Crypto/HKDFTests.hs b/tests/Codec/Crypto/HKDFTests.hs
index 5daffdb..995518c 100644
--- a/tests/Codec/Crypto/HKDFTests.hs
+++ b/tests/Codec/Crypto/HKDFTests.hs
@@ -23,7 +23,7 @@ import qualified Data.ByteString.Char8 as ByteString.Char8
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit ((@?=), testCase)
-import Codec.Crypto.HKDF (Salt(Salt), SecretKey(SecretKey))
+import Codec.Crypto.HKDF (Salt(Salt), SecretKey(SecretKey), noSalt)
import qualified Codec.Crypto.HKDF as HKDF
import Data.Digest (sha1, sha256)
@@ -62,6 +62,11 @@ testRFC5869 = testGroup "RFC 5869 examples"
(SecretKey $ ByteString.replicate 22 0x0b)
(Salt "")
(SecretKey $ hex "da8c8a73c7fa77288ec6f5e7c297786aa0d32d01")
+ , t "test case 7"
+ sha1
+ (SecretKey $ ByteString.replicate 22 0x0c)
+ noSalt
+ (SecretKey $ hex "2adccada18779e7c2077ad2eb19d3f3e731385dd")
]
where
t name hash ikm salt prk =