aead

Pure Haskell AEAD-ChaCha20-Poly1305 (docs.ppad.tech/aead).
git clone git://git.ppad.tech/aead.git
Log | Files | Refs | README | LICENSE

Wycheproof.hs (1942B)


      1 {-# LANGUAGE OverloadedStrings #-}
      2 
      3 module Wycheproof (
      4     Wycheproof(..)
      5   , AEADTestGroup(..)
      6   , AEADTest(..)
      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 
     16 data Wycheproof = Wycheproof {
     17     wp_numberOfTests :: !Int
     18   , wp_testGroups    :: ![AEADTestGroup]
     19   } deriving Show
     20 
     21 instance A.FromJSON Wycheproof where
     22   parseJSON = A.withObject "Wycheproof" $ \m -> Wycheproof
     23     <$> m .: "numberOfTests"
     24     <*> m .: "testGroups"
     25 
     26 data AEADTestGroup = AEADTestGroup {
     27     aeadtg_type    :: !T.Text
     28   , aeadtg_ivSize  :: !Int
     29   , aeadtg_keySize :: !Int
     30   , aeadtg_tagSize :: !Int
     31   , aeadtg_tests   :: ![AEADTest]
     32   } deriving Show
     33 
     34 instance A.FromJSON AEADTestGroup where
     35   parseJSON = A.withObject "AEADTestGroup" $ \m -> AEADTestGroup
     36     <$> m .: "type"
     37     <*> m .: "ivSize"
     38     <*> m .: "keySize"
     39     <*> m .: "tagSize"
     40     <*> m .: "tests"
     41 
     42 data AEADTest = AEADTest {
     43     aeadt_tcId           :: !Int
     44   , aeadt_comment        :: !T.Text
     45   , aeadt_key            :: !BS.ByteString
     46   , aeadt_iv             :: !BS.ByteString
     47   , aeadt_aad            :: !BS.ByteString
     48   , aeadt_msg            :: !BS.ByteString
     49   , aeadt_ct             :: !BS.ByteString
     50   , aeadt_tag            :: !BS.ByteString
     51   , aeadt_result         :: !T.Text
     52   } deriving Show
     53 
     54 decodehex :: T.Text -> BS.ByteString
     55 decodehex t = case B16.decode (TE.encodeUtf8 t) of
     56   Nothing -> error "bang"
     57   Just bs -> bs
     58 
     59 instance A.FromJSON AEADTest where
     60   parseJSON = A.withObject "AEADTest" $ \m -> AEADTest
     61     <$> m .: "tcId"
     62     <*> m .: "comment"
     63     <*> fmap decodehex (m .: "key")
     64     <*> fmap decodehex (m .: "iv")
     65     <*> fmap decodehex (m .: "aad")
     66     <*> fmap decodehex (m .: "msg")
     67     <*> fmap decodehex (m .: "ct")
     68     <*> fmap decodehex (m .: "tag")
     69     <*> m .: "result"
     70