add optional support for deepseq
This commit is contained in:
parent
c78e9472b9
commit
8eaaa06e1e
@ -5,6 +5,7 @@
|
|||||||
-- Stability : stable
|
-- Stability : stable
|
||||||
-- Portability : good
|
-- Portability : good
|
||||||
{-# LANGUAGE CPP #-}
|
{-# LANGUAGE CPP #-}
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Crypto.Cipher.AES
|
module Crypto.Cipher.AES
|
||||||
( AES128
|
( AES128
|
||||||
, AES192
|
, AES192
|
||||||
@ -15,15 +16,19 @@ import Crypto.Error
|
|||||||
import Crypto.Cipher.Types
|
import Crypto.Cipher.Types
|
||||||
import Crypto.Cipher.Types.Block
|
import Crypto.Cipher.Types.Block
|
||||||
import Crypto.Cipher.AES.Primitive
|
import Crypto.Cipher.AES.Primitive
|
||||||
|
import Crypto.Internal.Imports
|
||||||
|
|
||||||
-- | AES with 128 bit key
|
-- | AES with 128 bit key
|
||||||
newtype AES128 = AES128 AES
|
newtype AES128 = AES128 AES
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | AES with 192 bit key
|
-- | AES with 192 bit key
|
||||||
newtype AES192 = AES192 AES
|
newtype AES192 = AES192 AES
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | AES with 256 bit key
|
-- | AES with 256 bit key
|
||||||
newtype AES256 = AES256 AES
|
newtype AES256 = AES256 AES
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
instance Cipher AES128 where
|
instance Cipher AES128 where
|
||||||
cipherName _ = "AES128"
|
cipherName _ = "AES128"
|
||||||
|
|||||||
@ -67,6 +67,7 @@ import Crypto.Error
|
|||||||
import Crypto.Cipher.Types
|
import Crypto.Cipher.Types
|
||||||
import Crypto.Cipher.Types.Block (IV(..))
|
import Crypto.Cipher.Types.Block (IV(..))
|
||||||
import Crypto.Internal.Compat
|
import Crypto.Internal.Compat
|
||||||
|
import Crypto.Internal.Imports
|
||||||
import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes, withByteArray)
|
import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes, withByteArray)
|
||||||
import qualified Crypto.Internal.ByteArray as B
|
import qualified Crypto.Internal.ByteArray as B
|
||||||
|
|
||||||
@ -110,12 +111,15 @@ ocbMode aes = AEADModeImpl
|
|||||||
|
|
||||||
-- | AES Context (pre-processed key)
|
-- | AES Context (pre-processed key)
|
||||||
newtype AES = AES ScrubbedBytes
|
newtype AES = AES ScrubbedBytes
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | AESGCM State
|
-- | AESGCM State
|
||||||
newtype AESGCM = AESGCM ScrubbedBytes
|
newtype AESGCM = AESGCM ScrubbedBytes
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | AESOCB State
|
-- | AESOCB State
|
||||||
newtype AESOCB = AESOCB ScrubbedBytes
|
newtype AESOCB = AESOCB ScrubbedBytes
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
sizeGCM :: Int
|
sizeGCM :: Int
|
||||||
sizeGCM = 80
|
sizeGCM = 80
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
{-# LANGUAGE CPP #-}
|
|
||||||
-- |
|
-- |
|
||||||
-- Module : Crypto.Cipher.Blowfish
|
-- Module : Crypto.Cipher.Blowfish
|
||||||
-- License : BSD-style
|
-- License : BSD-style
|
||||||
@ -6,6 +5,8 @@
|
|||||||
-- Stability : stable
|
-- Stability : stable
|
||||||
-- Portability : good
|
-- Portability : good
|
||||||
--
|
--
|
||||||
|
{-# LANGUAGE CPP #-}
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Crypto.Cipher.Blowfish
|
module Crypto.Cipher.Blowfish
|
||||||
( Blowfish
|
( Blowfish
|
||||||
, Blowfish64
|
, Blowfish64
|
||||||
@ -14,23 +15,29 @@ module Crypto.Cipher.Blowfish
|
|||||||
, Blowfish448
|
, Blowfish448
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
import Crypto.Internal.Imports
|
||||||
import Crypto.Cipher.Types
|
import Crypto.Cipher.Types
|
||||||
import Crypto.Cipher.Blowfish.Primitive
|
import Crypto.Cipher.Blowfish.Primitive
|
||||||
|
|
||||||
-- | variable keyed blowfish state
|
-- | variable keyed blowfish state
|
||||||
newtype Blowfish = Blowfish Context
|
newtype Blowfish = Blowfish Context
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | 64 bit keyed blowfish state
|
-- | 64 bit keyed blowfish state
|
||||||
newtype Blowfish64 = Blowfish64 Context
|
newtype Blowfish64 = Blowfish64 Context
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | 128 bit keyed blowfish state
|
-- | 128 bit keyed blowfish state
|
||||||
newtype Blowfish128 = Blowfish128 Context
|
newtype Blowfish128 = Blowfish128 Context
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | 256 bit keyed blowfish state
|
-- | 256 bit keyed blowfish state
|
||||||
newtype Blowfish256 = Blowfish256 Context
|
newtype Blowfish256 = Blowfish256 Context
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | 448 bit keyed blowfish state
|
-- | 448 bit keyed blowfish state
|
||||||
newtype Blowfish448 = Blowfish448 Context
|
newtype Blowfish448 = Blowfish448 Context
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
instance Cipher Blowfish where
|
instance Cipher Blowfish where
|
||||||
cipherName _ = "blowfish"
|
cipherName _ = "blowfish"
|
||||||
|
|||||||
@ -18,12 +18,13 @@ module Crypto.Cipher.Blowfish.Primitive
|
|||||||
, decrypt
|
, decrypt
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad (forM_, when)
|
import Control.Monad (when)
|
||||||
import Data.Bits
|
import Data.Bits
|
||||||
import Data.Word
|
import Data.Word
|
||||||
|
|
||||||
import Crypto.Error
|
import Crypto.Error
|
||||||
import Crypto.Internal.Compat
|
import Crypto.Internal.Compat
|
||||||
|
import Crypto.Internal.Imports
|
||||||
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
|
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
|
||||||
import qualified Crypto.Internal.ByteArray as B
|
import qualified Crypto.Internal.ByteArray as B
|
||||||
import Crypto.Internal.Words
|
import Crypto.Internal.Words
|
||||||
@ -37,6 +38,9 @@ data Context = BF (Int -> Word32) -- p
|
|||||||
(Int -> Word32) -- sbox2
|
(Int -> Word32) -- sbox2
|
||||||
(Int -> Word32) -- sbox2
|
(Int -> Word32) -- sbox2
|
||||||
|
|
||||||
|
instance NFData Context where
|
||||||
|
rnf (BF p a b c d) = p `seq` a `seq` b `seq` c `seq` d `seq` ()
|
||||||
|
|
||||||
-- | Encrypt blocks
|
-- | Encrypt blocks
|
||||||
--
|
--
|
||||||
-- Input need to be a multiple of 8 bytes
|
-- Input need to be a multiple of 8 bytes
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
-- Portability : good
|
-- Portability : good
|
||||||
--
|
--
|
||||||
{-# LANGUAGE ForeignFunctionInterface #-}
|
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Crypto.Cipher.ChaCha
|
module Crypto.Cipher.ChaCha
|
||||||
( initialize
|
( initialize
|
||||||
, combine
|
, combine
|
||||||
@ -26,9 +27,11 @@ import Foreign.C.Types
|
|||||||
|
|
||||||
-- | ChaCha context
|
-- | ChaCha context
|
||||||
newtype State = State ScrubbedBytes
|
newtype State = State ScrubbedBytes
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | ChaCha context for DRG purpose (see Crypto.Random.ChaChaDRG)
|
-- | ChaCha context for DRG purpose (see Crypto.Random.ChaChaDRG)
|
||||||
newtype StateSimple = StateSimple ScrubbedBytes -- just ChaCha's state
|
newtype StateSimple = StateSimple ScrubbedBytes -- just ChaCha's state
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | Initialize a new ChaCha context with the number of rounds,
|
-- | Initialize a new ChaCha context with the number of rounds,
|
||||||
-- the key and the nonce associated.
|
-- the key and the nonce associated.
|
||||||
|
|||||||
@ -26,11 +26,12 @@ import Foreign.Ptr
|
|||||||
import Crypto.Internal.ByteArray (ScrubbedBytes, ByteArray, ByteArrayAccess)
|
import Crypto.Internal.ByteArray (ScrubbedBytes, ByteArray, ByteArrayAccess)
|
||||||
import qualified Crypto.Internal.ByteArray as B
|
import qualified Crypto.Internal.ByteArray as B
|
||||||
|
|
||||||
import Crypto.Internal.Compat
|
import Crypto.Internal.Compat
|
||||||
|
import Crypto.Internal.Imports
|
||||||
|
|
||||||
-- | The encryption state for RC4
|
-- | The encryption state for RC4
|
||||||
newtype State = State ScrubbedBytes
|
newtype State = State ScrubbedBytes
|
||||||
deriving (ByteArrayAccess)
|
deriving (ByteArrayAccess,NFData)
|
||||||
|
|
||||||
-- | C Call for initializing the encryptor
|
-- | C Call for initializing the encryptor
|
||||||
foreign import ccall unsafe "cryptonite_rc4.h cryptonite_rc4_init"
|
foreign import ccall unsafe "cryptonite_rc4.h cryptonite_rc4_init"
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
-- Portability : good
|
-- Portability : good
|
||||||
--
|
--
|
||||||
{-# LANGUAGE ForeignFunctionInterface #-}
|
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Crypto.Cipher.Salsa
|
module Crypto.Cipher.Salsa
|
||||||
( initialize
|
( initialize
|
||||||
, combine
|
, combine
|
||||||
@ -22,6 +23,7 @@ import Foreign.C.Types
|
|||||||
|
|
||||||
-- | Salsa context
|
-- | Salsa context
|
||||||
newtype State = State ScrubbedBytes
|
newtype State = State ScrubbedBytes
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | Initialize a new Salsa context with the number of rounds,
|
-- | Initialize a new Salsa context with the number of rounds,
|
||||||
-- the key and the nonce associated.
|
-- the key and the nonce associated.
|
||||||
|
|||||||
@ -14,6 +14,7 @@ module Crypto.Hash.Types
|
|||||||
, Digest(..)
|
, Digest(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
import Crypto.Internal.Imports
|
||||||
import Crypto.Internal.ByteArray (ByteArrayAccess, Bytes)
|
import Crypto.Internal.ByteArray (ByteArrayAccess, Bytes)
|
||||||
import qualified Crypto.Internal.ByteArray as B
|
import qualified Crypto.Internal.ByteArray as B
|
||||||
import Data.Word
|
import Data.Word
|
||||||
@ -47,11 +48,11 @@ hashContextGetAlgorithm = undefined
|
|||||||
|
|
||||||
-- | Represent a context for a given hash algorithm.
|
-- | Represent a context for a given hash algorithm.
|
||||||
newtype Context a = Context Bytes
|
newtype Context a = Context Bytes
|
||||||
deriving (ByteArrayAccess)
|
deriving (ByteArrayAccess,NFData)
|
||||||
|
|
||||||
-- | Represent a digest for a given hash algorithm.
|
-- | Represent a digest for a given hash algorithm.
|
||||||
newtype Digest a = Digest Bytes
|
newtype Digest a = Digest Bytes
|
||||||
deriving (Eq,ByteArrayAccess)
|
deriving (Eq,ByteArrayAccess,NFData)
|
||||||
|
|
||||||
instance Show (Digest a) where
|
instance Show (Digest a) where
|
||||||
show (Digest bs) = show (B.convertToBase B.Base16 bs :: Bytes)
|
show (Digest bs) = show (B.convertToBase B.Base16 bs :: Bytes)
|
||||||
|
|||||||
33
Crypto/Internal/DeepSeq.hs
Normal file
33
Crypto/Internal/DeepSeq.hs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
-- |
|
||||||
|
-- Module : Crypto.Internal.DeepSeq
|
||||||
|
-- License : BSD-style
|
||||||
|
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
|
||||||
|
-- Stability : experimental
|
||||||
|
-- Portability : unknown
|
||||||
|
--
|
||||||
|
-- Simple abstraction module to allow compilation without deepseq
|
||||||
|
-- by defining our own NFData class if not compiling with deepseq
|
||||||
|
-- support.
|
||||||
|
--
|
||||||
|
{-# LANGUAGE CPP #-}
|
||||||
|
module Crypto.Internal.DeepSeq
|
||||||
|
( NFData(..)
|
||||||
|
) where
|
||||||
|
|
||||||
|
#ifdef WITH_DEEPSEQ_SUPPORT
|
||||||
|
import Control.DeepSeq
|
||||||
|
#else
|
||||||
|
import Data.Word
|
||||||
|
import Data.ByteArray
|
||||||
|
|
||||||
|
class NFData a where rnf :: a -> ()
|
||||||
|
|
||||||
|
instance NFData Word8 where rnf w = w `seq` ()
|
||||||
|
instance NFData Word16 where rnf w = w `seq` ()
|
||||||
|
instance NFData Word32 where rnf w = w `seq` ()
|
||||||
|
instance NFData Word64 where rnf w = w `seq` ()
|
||||||
|
|
||||||
|
instance NFData Bytes where rnf b = b `seq` ()
|
||||||
|
instance NFData ScrubbedBytes where rnf b = b `seq` ()
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -5,12 +5,12 @@
|
|||||||
-- Stability : experimental
|
-- Stability : experimental
|
||||||
-- Portability : unknown
|
-- Portability : unknown
|
||||||
--
|
--
|
||||||
{-# LANGUAGE BangPatterns #-}
|
|
||||||
module Crypto.Internal.Imports
|
module Crypto.Internal.Imports
|
||||||
( module X
|
( module X
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Word as X
|
import Data.Word as X
|
||||||
import Control.Applicative as X
|
import Control.Applicative as X
|
||||||
import Control.Monad as X (forM, forM_, void)
|
import Control.Monad as X (forM, forM_, void)
|
||||||
import Control.Arrow as X (first, second)
|
import Control.Arrow as X (first, second)
|
||||||
|
import Crypto.Internal.DeepSeq as X
|
||||||
|
|||||||
@ -33,16 +33,16 @@ import qualified Crypto.Internal.ByteArray as B
|
|||||||
|
|
||||||
-- | A Curve25519 Secret key
|
-- | A Curve25519 Secret key
|
||||||
newtype SecretKey = SecretKey ScrubbedBytes
|
newtype SecretKey = SecretKey ScrubbedBytes
|
||||||
deriving (Show,Eq,ByteArrayAccess)
|
deriving (Show,Eq,ByteArrayAccess,NFData)
|
||||||
|
|
||||||
-- | A Curve25519 public key
|
-- | A Curve25519 public key
|
||||||
newtype PublicKey = PublicKey Bytes
|
newtype PublicKey = PublicKey Bytes
|
||||||
deriving (Show,Eq,ByteArrayAccess)
|
deriving (Show,Eq,ByteArrayAccess,NFData)
|
||||||
|
|
||||||
-- | A Curve25519 Diffie Hellman secret related to a
|
-- | A Curve25519 Diffie Hellman secret related to a
|
||||||
-- public key and a secret key.
|
-- public key and a secret key.
|
||||||
newtype DhSecret = DhSecret ScrubbedBytes
|
newtype DhSecret = DhSecret ScrubbedBytes
|
||||||
deriving (Show,Eq,ByteArrayAccess)
|
deriving (Show,Eq,ByteArrayAccess,NFData)
|
||||||
|
|
||||||
-- | Try to build a public key from a bytearray
|
-- | Try to build a public key from a bytearray
|
||||||
publicKey :: ByteArrayAccess bs => bs -> Either String PublicKey
|
publicKey :: ByteArrayAccess bs => bs -> Either String PublicKey
|
||||||
|
|||||||
@ -35,6 +35,7 @@ import Crypto.Number.ModArithmetic (expFast, expSafe, inverse)
|
|||||||
import Crypto.Number.Serialize
|
import Crypto.Number.Serialize
|
||||||
import Crypto.Number.Generate
|
import Crypto.Number.Generate
|
||||||
import Crypto.Internal.ByteArray (ByteArrayAccess)
|
import Crypto.Internal.ByteArray (ByteArrayAccess)
|
||||||
|
import Crypto.Internal.Imports
|
||||||
import Crypto.Hash
|
import Crypto.Hash
|
||||||
|
|
||||||
-- | DSA Public Number, usually embedded in DSA Public Key
|
-- | DSA Public Number, usually embedded in DSA Public Key
|
||||||
@ -50,18 +51,27 @@ data Params = Params
|
|||||||
, params_q :: Integer -- ^ DSA q
|
, params_q :: Integer -- ^ DSA q
|
||||||
} deriving (Show,Read,Eq,Data,Typeable)
|
} deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData Params where
|
||||||
|
rnf (Params p g q) = p `seq` g `seq` q `seq` ()
|
||||||
|
|
||||||
-- | Represent a DSA signature namely R and S.
|
-- | Represent a DSA signature namely R and S.
|
||||||
data Signature = Signature
|
data Signature = Signature
|
||||||
{ sign_r :: Integer -- ^ DSA r
|
{ sign_r :: Integer -- ^ DSA r
|
||||||
, sign_s :: Integer -- ^ DSA s
|
, sign_s :: Integer -- ^ DSA s
|
||||||
} deriving (Show,Read,Eq,Data,Typeable)
|
} deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData Signature where
|
||||||
|
rnf (Signature r s) = r `seq` s `seq` ()
|
||||||
|
|
||||||
-- | Represent a DSA public key.
|
-- | Represent a DSA public key.
|
||||||
data PublicKey = PublicKey
|
data PublicKey = PublicKey
|
||||||
{ public_params :: Params -- ^ DSA parameters
|
{ public_params :: Params -- ^ DSA parameters
|
||||||
, public_y :: PublicNumber -- ^ DSA public Y
|
, public_y :: PublicNumber -- ^ DSA public Y
|
||||||
} deriving (Show,Read,Eq,Data,Typeable)
|
} deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData PublicKey where
|
||||||
|
rnf (PublicKey params y) = y `seq` params `seq` ()
|
||||||
|
|
||||||
-- | Represent a DSA private key.
|
-- | Represent a DSA private key.
|
||||||
--
|
--
|
||||||
-- Only x need to be secret.
|
-- Only x need to be secret.
|
||||||
@ -71,10 +81,16 @@ data PrivateKey = PrivateKey
|
|||||||
, private_x :: PrivateNumber -- ^ DSA private X
|
, private_x :: PrivateNumber -- ^ DSA private X
|
||||||
} deriving (Show,Read,Eq,Data,Typeable)
|
} deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData PrivateKey where
|
||||||
|
rnf (PrivateKey params x) = x `seq` params `seq` ()
|
||||||
|
|
||||||
-- | Represent a DSA key pair
|
-- | Represent a DSA key pair
|
||||||
data KeyPair = KeyPair Params PublicNumber PrivateNumber
|
data KeyPair = KeyPair Params PublicNumber PrivateNumber
|
||||||
deriving (Show,Read,Eq,Data,Typeable)
|
deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData KeyPair where
|
||||||
|
rnf (KeyPair params y x) = x `seq` y `seq` params `seq` ()
|
||||||
|
|
||||||
-- | Public key of a DSA Key pair
|
-- | Public key of a DSA Key pair
|
||||||
toPublicKey :: KeyPair -> PublicKey
|
toPublicKey :: KeyPair -> PublicKey
|
||||||
toPublicKey (KeyPair params pub _) = PublicKey params pub
|
toPublicKey (KeyPair params pub _) = PublicKey params pub
|
||||||
|
|||||||
@ -25,7 +25,8 @@ module Crypto.PubKey.ECC.Types
|
|||||||
, getCurveByName
|
, getCurveByName
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Data
|
import Data.Data
|
||||||
|
import Crypto.Internal.Imports
|
||||||
|
|
||||||
-- | Define either a binary curve or a prime curve.
|
-- | Define either a binary curve or a prime curve.
|
||||||
data Curve = CurveF2m CurveBinary -- ^ 𝔽(2^m)
|
data Curve = CurveF2m CurveBinary -- ^ 𝔽(2^m)
|
||||||
@ -43,11 +44,18 @@ data Point = Point Integer Integer
|
|||||||
| PointO -- ^ Point at Infinity
|
| PointO -- ^ Point at Infinity
|
||||||
deriving (Show,Read,Eq,Data,Typeable)
|
deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData Point where
|
||||||
|
rnf (Point x y) = x `seq` y `seq` ()
|
||||||
|
rnf PointO = ()
|
||||||
|
|
||||||
-- | Define an elliptic curve in 𝔽(2^m).
|
-- | Define an elliptic curve in 𝔽(2^m).
|
||||||
-- The firt parameter is the Integer representatioin of the irreducible polynomial f(x).
|
-- The firt parameter is the Integer representatioin of the irreducible polynomial f(x).
|
||||||
data CurveBinary = CurveBinary Integer CurveCommon
|
data CurveBinary = CurveBinary Integer CurveCommon
|
||||||
deriving (Show,Read,Eq,Data,Typeable)
|
deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData CurveBinary where
|
||||||
|
rnf (CurveBinary i cc) = i `seq` cc `seq` ()
|
||||||
|
|
||||||
-- | Define an elliptic curve in 𝔽p.
|
-- | Define an elliptic curve in 𝔽p.
|
||||||
-- The first parameter is the Prime Number.
|
-- The first parameter is the Prime Number.
|
||||||
data CurvePrime = CurvePrime Integer CurveCommon
|
data CurvePrime = CurvePrime Integer CurveCommon
|
||||||
|
|||||||
@ -35,15 +35,15 @@ import Crypto.Error
|
|||||||
|
|
||||||
-- | An Ed25519 Secret key
|
-- | An Ed25519 Secret key
|
||||||
newtype SecretKey = SecretKey ScrubbedBytes
|
newtype SecretKey = SecretKey ScrubbedBytes
|
||||||
deriving (Eq,ByteArrayAccess)
|
deriving (Eq,ByteArrayAccess,NFData)
|
||||||
|
|
||||||
-- | An Ed25519 public key
|
-- | An Ed25519 public key
|
||||||
newtype PublicKey = PublicKey Bytes
|
newtype PublicKey = PublicKey Bytes
|
||||||
deriving (Show,Eq,ByteArrayAccess)
|
deriving (Show,Eq,ByteArrayAccess,NFData)
|
||||||
|
|
||||||
-- | An Ed25519 signature
|
-- | An Ed25519 signature
|
||||||
newtype Signature = Signature Bytes
|
newtype Signature = Signature Bytes
|
||||||
deriving (Show,Eq,ByteArrayAccess)
|
deriving (Show,Eq,ByteArrayAccess,NFData)
|
||||||
|
|
||||||
-- | Try to build a public key from a bytearray
|
-- | Try to build a public key from a bytearray
|
||||||
publicKey :: ByteArrayAccess ba => ba -> CryptoFailable PublicKey
|
publicKey :: ByteArrayAccess ba => ba -> CryptoFailable PublicKey
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
-- TODO: provide a mapping between integer and ciphertext
|
-- TODO: provide a mapping between integer and ciphertext
|
||||||
-- generate numbers correctly
|
-- generate numbers correctly
|
||||||
--
|
--
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Crypto.PubKey.ElGamal
|
module Crypto.PubKey.ElGamal
|
||||||
( Params
|
( Params
|
||||||
, PublicNumber
|
, PublicNumber
|
||||||
@ -48,6 +49,7 @@ data Signature = Signature (Integer, Integer)
|
|||||||
|
|
||||||
-- | ElGamal Ephemeral key. also called Temporary key.
|
-- | ElGamal Ephemeral key. also called Temporary key.
|
||||||
newtype EphemeralKey = EphemeralKey Integer
|
newtype EphemeralKey = EphemeralKey Integer
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | generate a private number with no specific property
|
-- | generate a private number with no specific property
|
||||||
-- this number is usually called a and need to be between
|
-- this number is usually called a and need to be between
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
-- Portability : Good
|
-- Portability : Good
|
||||||
--
|
--
|
||||||
{-# LANGUAGE DeriveDataTypeable #-}
|
{-# LANGUAGE DeriveDataTypeable #-}
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Crypto.PubKey.RSA.Types
|
module Crypto.PubKey.RSA.Types
|
||||||
( Error(..)
|
( Error(..)
|
||||||
, Blinder(..)
|
, Blinder(..)
|
||||||
@ -20,6 +21,7 @@ module Crypto.PubKey.RSA.Types
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Data
|
import Data.Data
|
||||||
|
import Crypto.Internal.Imports
|
||||||
|
|
||||||
-- | Blinder which is used to obfuscate the timing
|
-- | Blinder which is used to obfuscate the timing
|
||||||
-- of the decryption primitive (used by decryption and signing).
|
-- of the decryption primitive (used by decryption and signing).
|
||||||
@ -42,6 +44,9 @@ data PublicKey = PublicKey
|
|||||||
, public_e :: Integer -- ^ public exponant e
|
, public_e :: Integer -- ^ public exponant e
|
||||||
} deriving (Show,Read,Eq,Data,Typeable)
|
} deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData PublicKey where
|
||||||
|
rnf (PublicKey sz n e) = rnf n `seq` rnf e `seq` sz `seq` ()
|
||||||
|
|
||||||
-- | Represent a RSA private key.
|
-- | Represent a RSA private key.
|
||||||
--
|
--
|
||||||
-- Only the pub, d fields are mandatory to fill.
|
-- Only the pub, d fields are mandatory to fill.
|
||||||
@ -62,6 +67,10 @@ data PrivateKey = PrivateKey
|
|||||||
, private_qinv :: Integer -- ^ q^(-1) mod p
|
, private_qinv :: Integer -- ^ q^(-1) mod p
|
||||||
} deriving (Show,Read,Eq,Data,Typeable)
|
} deriving (Show,Read,Eq,Data,Typeable)
|
||||||
|
|
||||||
|
instance NFData PrivateKey where
|
||||||
|
rnf (PrivateKey pub d p q dp dq qinv) =
|
||||||
|
rnf pub `seq` rnf d `seq` rnf p `seq` rnf q `seq` rnf dp `seq` rnf dq `seq` qinv `seq` ()
|
||||||
|
|
||||||
-- | get the size in bytes from a private key
|
-- | get the size in bytes from a private key
|
||||||
private_size :: PrivateKey -> Int
|
private_size :: PrivateKey -> Int
|
||||||
private_size = public_size . private_pub
|
private_size = public_size . private_pub
|
||||||
@ -78,7 +87,7 @@ private_e = public_e . private_pub
|
|||||||
--
|
--
|
||||||
-- note the RSA private key contains already an instance of public key for efficiency
|
-- note the RSA private key contains already an instance of public key for efficiency
|
||||||
newtype KeyPair = KeyPair PrivateKey
|
newtype KeyPair = KeyPair PrivateKey
|
||||||
deriving (Show,Read,Eq,Data,Typeable)
|
deriving (Show,Read,Eq,Data,Typeable,NFData)
|
||||||
|
|
||||||
-- | Public key of a RSA KeyPair
|
-- | Public key of a RSA KeyPair
|
||||||
toPublicKey :: KeyPair -> PublicKey
|
toPublicKey :: KeyPair -> PublicKey
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
-- Stability : stable
|
-- Stability : stable
|
||||||
-- Portability : good
|
-- Portability : good
|
||||||
--
|
--
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Crypto.Random.ChaChaDRG
|
module Crypto.Random.ChaChaDRG
|
||||||
( ChaChaDRG
|
( ChaChaDRG
|
||||||
, initialize
|
, initialize
|
||||||
@ -12,9 +13,9 @@ module Crypto.Random.ChaChaDRG
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Crypto.Random.Types
|
import Crypto.Random.Types
|
||||||
|
import Crypto.Internal.Imports
|
||||||
import Crypto.Internal.ByteArray (ByteArray, ScrubbedBytes)
|
import Crypto.Internal.ByteArray (ByteArray, ScrubbedBytes)
|
||||||
import qualified Crypto.Internal.ByteArray as B
|
import qualified Crypto.Internal.ByteArray as B
|
||||||
import Data.Word
|
|
||||||
import Foreign.Storable (pokeElemOff)
|
import Foreign.Storable (pokeElemOff)
|
||||||
|
|
||||||
import qualified Crypto.Cipher.ChaCha as C
|
import qualified Crypto.Cipher.ChaCha as C
|
||||||
@ -24,6 +25,7 @@ instance DRG ChaChaDRG where
|
|||||||
|
|
||||||
-- | ChaCha Deterministic Random Generator
|
-- | ChaCha Deterministic Random Generator
|
||||||
newtype ChaChaDRG = ChaChaDRG C.StateSimple
|
newtype ChaChaDRG = ChaChaDRG C.StateSimple
|
||||||
|
deriving (NFData)
|
||||||
|
|
||||||
-- | Initialize a new ChaCha context with the number of rounds,
|
-- | Initialize a new ChaCha context with the number of rounds,
|
||||||
-- the key and the nonce associated.
|
-- the key and the nonce associated.
|
||||||
|
|||||||
@ -61,6 +61,11 @@ Flag integer-gmp
|
|||||||
Default: True
|
Default: True
|
||||||
Manual: True
|
Manual: True
|
||||||
|
|
||||||
|
Flag support_deepseq
|
||||||
|
Description: add deepseq instances for cryptographic types
|
||||||
|
Default: True
|
||||||
|
Manual: True
|
||||||
|
|
||||||
Library
|
Library
|
||||||
Exposed-modules: Crypto.Cipher.AES
|
Exposed-modules: Crypto.Cipher.AES
|
||||||
Crypto.Cipher.Blowfish
|
Crypto.Cipher.Blowfish
|
||||||
@ -147,6 +152,7 @@ Library
|
|||||||
Crypto.Internal.ByteArray
|
Crypto.Internal.ByteArray
|
||||||
Crypto.Internal.Compat
|
Crypto.Internal.Compat
|
||||||
Crypto.Internal.CompatPrim
|
Crypto.Internal.CompatPrim
|
||||||
|
Crypto.Internal.DeepSeq
|
||||||
Crypto.Internal.Imports
|
Crypto.Internal.Imports
|
||||||
Crypto.Internal.Words
|
Crypto.Internal.Words
|
||||||
Crypto.Internal.WordArray
|
Crypto.Internal.WordArray
|
||||||
@ -216,6 +222,10 @@ Library
|
|||||||
if impl(ghc) && flag(integer-gmp)
|
if impl(ghc) && flag(integer-gmp)
|
||||||
Build-depends: integer-gmp
|
Build-depends: integer-gmp
|
||||||
|
|
||||||
|
if flag(support_deepseq)
|
||||||
|
CPP-options: -DWITH_DEEPSEQ_SUPPORT
|
||||||
|
Build-depends: deepseq
|
||||||
|
|
||||||
Test-Suite test-cryptonite
|
Test-Suite test-cryptonite
|
||||||
type: exitcode-stdio-1.0
|
type: exitcode-stdio-1.0
|
||||||
hs-source-dirs: tests
|
hs-source-dirs: tests
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user