Wycheproof.hs (1633B)
1 {-# LANGUAGE OverloadedStrings #-} 2 3 module Wycheproof ( 4 Wycheproof(..) 5 , HkdfTestGroup(..) 6 , HkdfTest(..) 7 ) where 8 9 import Data.Aeson ((.:)) 10 import qualified Data.Aeson as A 11 import qualified Data.ByteString as BS 12 import qualified Data.ByteString.Base16 as B16 13 import qualified Data.Text as T 14 import qualified Data.Text.Encoding as TE 15 import Data.Word (Word64) 16 17 data Wycheproof = Wycheproof { 18 wp_numberOfTests :: !Int 19 , wp_testGroups :: ![HkdfTestGroup] 20 } deriving Show 21 22 instance A.FromJSON Wycheproof where 23 parseJSON = A.withObject "Wycheproof" $ \m -> Wycheproof 24 <$> m .: "numberOfTests" 25 <*> m .: "testGroups" 26 27 data HkdfTestGroup = HkdfTestGroup { 28 htg_keySize :: !Int 29 , htg_type :: !T.Text 30 , htg_tests :: ![HkdfTest] 31 } deriving Show 32 33 instance A.FromJSON HkdfTestGroup where 34 parseJSON = A.withObject "HkdfTestGroup" $ \m -> HkdfTestGroup 35 <$> m .: "keySize" 36 <*> m .: "type" 37 <*> m .: "tests" 38 39 data HkdfTest = HkdfTest { 40 ht_tcId :: !Int 41 , ht_comment :: !T.Text 42 , ht_ikm :: !BS.ByteString 43 , ht_salt :: !BS.ByteString 44 , ht_info :: !BS.ByteString 45 , ht_size :: !Word64 46 , ht_okm :: !BS.ByteString 47 , ht_result :: !T.Text 48 } deriving Show 49 50 decodehex :: T.Text -> BS.ByteString 51 decodehex = B16.decodeLenient . TE.encodeUtf8 52 53 instance A.FromJSON HkdfTest where 54 parseJSON = A.withObject "HkdfTest" $ \m -> HkdfTest 55 <$> m .: "tcId" 56 <*> m .: "comment" 57 <*> fmap decodehex (m .: "ikm") 58 <*> fmap decodehex (m .: "salt") 59 <*> fmap decodehex (m .: "info") 60 <*> m .: "size" 61 <*> fmap decodehex (m .: "okm") 62 <*> m .: "result" 63 64