{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}{-|
Maintainer: Thomas.DuBuisson@gmail.com
Stability: beta
Portability: portable
This is the heart of the crypto-api package. By making (or having)
an instance of Hash, AsymCipher, BlockCipher or StreamCipher you provide (or obtain)
access to any infrastructure built on these primitives include block cipher modes
of operation, hashing, hmac, signing, etc. These classes allow users to build
routines that are agnostic to the algorithm used so changing algorithms is as simple
as changing a type signature.
-}moduleCrypto.Classes(Hash(..),BlockCipher(..),blockSizeBytes,StreamCipher(..),AsymCipher(..),Signing(..),for,(.::.),hash,hash',hashFunc,hashFunc')whereimportData.SerializeimportqualifiedData.ByteString.LazyasLimportqualifiedData.ByteStringasBimportqualifiedData.ByteString.InternalasIimportData.List(foldl')importData.TaggedimportCrypto.TypesimportCrypto.Random-- |The Hash class is intended as the generic interface-- targeted by maintainers of Haskell digest implementations.-- Using this generic interface, higher level functions-- such as 'hash' and 'hash'' provide a useful API-- for comsumers of hash implementations.---- Any instantiated implementation must handle unaligned dataclass(Serialized,Eqd,Ordd)=>Hashctxd|d->ctx,ctx->dwhereoutputLength::TaggeddBitLength-- ^ The size of the digest when encodedblockLength::TaggeddBitLength-- ^ The amount of data operated on in each round of the digest computationinitialCtx::ctx-- ^ An initial context, provided with the first call to 'updateCtx'updateCtx::ctx->B.ByteString->ctx-- ^ Used to update a context, repeatedly called until all data is exhausted-- must operate correctly for imputs of @n*blockLength@ bytes for @n `elem` [0..]@finalize::ctx->B.ByteString->d-- ^ Finializing a context, plus any message data less than the block size, into a digest-- |Hash a lazy ByteString, creating a digesthash::(Hashctxd)=>L.ByteString->dhashmsg=reswhereres=finalizectxendctx=foldl'updateCtxinitialCtxblks(blks,end)=makeBlocksmsgblockLenblockLen=(blockLength.::.res)`div`8-- |Hash a strict ByteString, creating a digesthash'::(Hashctxd)=>B.ByteString->dhash'msg=reswhereres=finalize(updateCtxinitialCtxtop)end(top,end)=B.splitAtremlenmsgremlen=B.lengthmsg-(B.lengthmsg`rem`bLen)bLen=blockLength`for`res`div`8-- |Obtain a lazy hash function from a digesthashFunc::Hashcd=>d->(L.ByteString->d)hashFuncd=fwheref=hasha=fundefined`asTypeOf`d-- |Obtain a strict hash function from a digesthashFunc'::Hashcd=>d->(B.ByteString->d)hashFunc'd=fwheref=hash'a=fundefined`asTypeOf`d{-# INLINE makeBlocks #-}makeBlocks::L.ByteString->ByteLength->([B.ByteString],B.ByteString)makeBlocksmsglen=go(L.toChunksmsg)wherego[]=([],B.empty)go(x:xs)|B.lengthx>=len=letl=B.lengthx-B.lengthx`rem`len(top,end)=B.splitAtlx(rest,trueEnd)=go(end:xs)in(top:rest,trueEnd)|otherwise=casexsof[]->([],x)(a:as)->go(B.appendxa:as)-- |Obtain a tagged value for a given typefor::Taggedab->a->bfort_=unTaggedt-- |Infix `for` operator(.::.)::Taggedab->a->b(.::.)=for-- |The BlockCipher class is intended as the generic interface-- targeted by maintainers of Haskell cipher implementations.-- Using this generic interface higher level functions-- such as 'cbc', and other functions from Data.Crypto.Modes, provide a useful API-- for comsumers of cipher implementations.---- Instances must handle unaligned dataclass(Serializek)=>BlockCipherkwhereblockSize::TaggedkBitLength-- ^ The size of a single block; the smallest unit on which the cipher operates.encryptBlock::k->B.ByteString->B.ByteString-- ^ encrypt data of size @n*blockSize@ where @n `elem` [0..]@ (ecb encryption)decryptBlock::k->B.ByteString->B.ByteString-- ^ decrypt data of size @n*blockSize@ where @n `elem` [0..]@ (ecb decryption)buildKey::B.ByteString->Maybek-- ^ smart constructor for keys from a bytestring.keyLength::k->BitLength-- ^ keyLength may inspect its argument to return the lengthblockSizeBytes::(BlockCipherk)=>TaggedkByteLengthblockSizeBytes=fmap(`div`8)blockSize-- |Asymetric ciphers (common ones being RSA or EC based)class(Serializep)=>AsymCipherpwherebuildKeyPair::CryptoRandomGeng=>g->BitLength->Maybe((p,p),g)-- ^ build a public/private key pair using the provided generatorencryptAsym::p->B.ByteString->B.ByteString-- ^ Asymetric encryptiondecryptAsym::p->B.ByteString->B.ByteString-- ^ Asymetric decryptionasymKeyLength::p->BitLength-- | `signUsing d k msg` Returns a signature (not a message + signature) for `msg`-- by hashing into a digest asTypeOf `d` and encrypting using the asymetric key `k`.---- Expect a "Signature" class to appear in a future crypto-api-- (this function might become depricated pending discussion)signUsing::(Hashcd,AsymCipherp)=>d->p->L.ByteString->B.ByteStringsignUsingdp=encryptAsymp.Data.Serialize.encode.hashFuncd-- | Like `signUsing` but for strict ByteStrings.signUsing'::(Hashcd,AsymCipherp)=>d->p->B.ByteString->B.ByteStringsignUsing'dp=encryptAsymp.Data.Serialize.encode.hashFunc'd-- | A stream cipher class. Instance are expected to work on messages as small as one byte-- The length of the resulting cipher text should be equal-- to the length of the input message.class(Serializek)=>StreamCipherkiv|k->ivwherebuildStreamKey::B.ByteString->MaybekencryptStream::k->iv->B.ByteString->(B.ByteString,iv)decryptStream::k->iv->B.ByteString->(B.ByteString,iv)streamKeyLength::k->BitLength-- | A class for signing operations which inherently can not be as generic-- as asymetric ciphers (ex: DSA).class(Serializep,Serializev)=>Signingpv|p->v,v->pwheresign::v->L.ByteString->B.ByteStringverify::p->L.ByteString->B.ByteString->BoolbuildSigningPair::CryptoRandomGeng=>g->BitLength->Maybe((p,v),g)signingKeyLength::v->BitLengthverifyingKeyLength::p->BitLength