more fixing up description and comments

This commit is contained in:
Vincent Hanquez 2015-05-20 06:22:00 +01:00
parent 92343f856a
commit 881d167cb5
6 changed files with 41 additions and 18 deletions

View File

@ -9,8 +9,7 @@
-- --
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Crypto.Hash.IO module Crypto.Hash.IO
( ( HashAlgorithm(..)
HashAlgorithm
, MutableContext , MutableContext
, hashMutableInit , hashMutableInit
, hashMutableInitWith , hashMutableInitWith

View File

@ -12,8 +12,7 @@ module Crypto.Hash.Types
( HashAlgorithm(..) ( HashAlgorithm(..)
, Context(..) , Context(..)
, Digest(..) , Digest(..)
) ) where
where
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
@ -22,23 +21,23 @@ import Foreign.Ptr (Ptr)
-- | Class representing hashing algorithms. -- | Class representing hashing algorithms.
-- --
-- The hash algorithm is built over 3 primitives: -- The interface presented here is update in place
-- -- and lowlevel. the Hash module takes care of
-- * init : create a new hashing context -- hidding the mutable interface properly.
--
-- * updates : update the hashing context with some strict bytestrings
-- and return the new context
--
-- * finalize : finalize the context into a digest
--
class HashAlgorithm a where class HashAlgorithm a where
-- | Get the block size of a hash algorithm
hashBlockSize :: a -> Int hashBlockSize :: a -> Int
-- | Get the digest size of a hash algorithm
hashDigestSize :: a -> Int hashDigestSize :: a -> Int
-- | Get the size of the context used for a hash algorithm
hashInternalContextSize :: a -> Int hashInternalContextSize :: a -> Int
--hashAlgorithmFromProxy :: Proxy a -> a --hashAlgorithmFromProxy :: Proxy a -> a
-- | Initialize a context pointer to the initial state of a hash algorithm
hashInternalInit :: Ptr (Context a) -> IO () hashInternalInit :: Ptr (Context a) -> IO ()
-- | Update the context with some raw data
hashInternalUpdate :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO () hashInternalUpdate :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
-- | Finalize the context and set the digest raw memory to the right value
hashInternalFinalize :: Ptr (Context a) -> Ptr (Digest a) -> IO () hashInternalFinalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()
{- {-

View File

@ -7,10 +7,13 @@
-- --
-- Simple and efficient byte array types -- Simple and efficient byte array types
-- --
{-# OPTIONS_HADDOCK hide #-}
module Crypto.Internal.ByteArray module Crypto.Internal.ByteArray
( module X ( module Data.ByteArray
, module Data.ByteArray.Mapping
, module Data.ByteArray.Encoding
) where ) where
import Data.ByteArray as X import Data.ByteArray
import Data.ByteArray.Mapping as X import Data.ByteArray.Mapping
import Data.ByteArray.Encoding as X import Data.ByteArray.Encoding

View File

@ -10,7 +10,8 @@
-- attacks. The 'm' parameter is implicitly derived from the irreducible -- attacks. The 'm' parameter is implicitly derived from the irreducible
-- polynomial where applicable. -- polynomial where applicable.
module Crypto.Number.F2m module Crypto.Number.F2m
( addF2m ( BinaryPolynomial
, addF2m
, mulF2m , mulF2m
, squareF2m , squareF2m
, modF2m , modF2m

View File

@ -12,6 +12,8 @@ module Crypto.PubKey.DSA
, Signature(..) , Signature(..)
, PublicKey(..) , PublicKey(..)
, PrivateKey(..) , PrivateKey(..)
, PublicNumber
, PrivateNumber
-- * generation -- * generation
, generatePrivate , generatePrivate
, calculatePublic , calculatePublic

View File

@ -17,6 +17,21 @@
module Crypto.PubKey.ECC.P256 module Crypto.PubKey.ECC.P256
( Scalar ( Scalar
, Point , Point
-- * point arithmetic
, pointAdd
, pointMul
, pointsMulVarTime
, pointIsValid
, toPoint
-- * scalar arithmetic
, scalarZero
, scalarAdd
, scalarSub
, scalarInv
, scalarInvVarTime
, scalarCmp
, scalarFromBinary
, scalarToBinary
) where ) where
import Data.Word import Data.Word
@ -92,6 +107,7 @@ pointIsValid p = unsafeDoIO $ withPoint p $ \px py -> do
-- Scalar methods -- Scalar methods
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | The scalar representing 0
scalarZero :: Scalar scalarZero :: Scalar
scalarZero = withNewScalarFreeze $ \d -> ccryptonite_p256_init d scalarZero = withNewScalarFreeze $ \d -> ccryptonite_p256_init d
@ -128,12 +144,14 @@ scalarInvVarTime a =
withNewScalarFreeze $ \b -> withScalar a $ \pa -> withNewScalarFreeze $ \b -> withScalar a $ \pa ->
ccryptonite_p256_modinv_vartime ccryptonite_SECP256r1_n pa b ccryptonite_p256_modinv_vartime ccryptonite_SECP256r1_n pa b
-- | Compare 2 Scalar
scalarCmp :: Scalar -> Scalar -> Ordering scalarCmp :: Scalar -> Scalar -> Ordering
scalarCmp a b = unsafeDoIO $ scalarCmp a b = unsafeDoIO $
withScalar a $ \pa -> withScalar b $ \pb -> do withScalar a $ \pa -> withScalar b $ \pb -> do
v <- ccryptonite_p256_cmp pa pb v <- ccryptonite_p256_cmp pa pb
return $ compare v 0 return $ compare v 0
-- | convert a scalar from binary
scalarFromBinary :: ByteArrayAccess ba => ba -> CryptoFailable Scalar scalarFromBinary :: ByteArrayAccess ba => ba -> CryptoFailable Scalar
scalarFromBinary ba scalarFromBinary ba
| B.length ba /= scalarSize = CryptoFailed $ CryptoError_SecretKeySizeInvalid | B.length ba /= scalarSize = CryptoFailed $ CryptoError_SecretKeySizeInvalid
@ -141,6 +159,7 @@ scalarFromBinary ba
CryptoPassed $ withNewScalarFreeze $ \p -> B.withByteArray ba $ \b -> CryptoPassed $ withNewScalarFreeze $ \p -> B.withByteArray ba $ \b ->
ccryptonite_p256_from_bin b p ccryptonite_p256_from_bin b p
-- | convert a scalar to binary
scalarToBinary :: ByteArray ba => Scalar -> ba scalarToBinary :: ByteArray ba => Scalar -> ba
scalarToBinary s = B.allocAndFreeze scalarSize $ \b -> withScalar s $ \p -> scalarToBinary s = B.allocAndFreeze scalarSize $ \b -> withScalar s $ \p ->
ccryptonite_p256_to_bin p b ccryptonite_p256_to_bin p b