packages feed

cryptocipher 0.2.14 → 0.3.0

raw patch · 5 files changed

+24/−61 lines, 5 filesdep +crypto-pubkey-typesPVP ok

version bump matches the API change (PVP)

Dependencies added: crypto-pubkey-types

API changes (from Hackage documentation)

- Crypto.Cipher.DH: instance Enum PrivateNumber
- Crypto.Cipher.DH: instance Enum PublicNumber
- Crypto.Cipher.DH: instance Enum SharedKey
- Crypto.Cipher.DH: instance Eq PrivateNumber
- Crypto.Cipher.DH: instance Eq PublicNumber
- Crypto.Cipher.DH: instance Eq SharedKey
- Crypto.Cipher.DH: instance Num PrivateNumber
- Crypto.Cipher.DH: instance Num PublicNumber
- Crypto.Cipher.DH: instance Num SharedKey
- Crypto.Cipher.DH: instance Ord PrivateNumber
- Crypto.Cipher.DH: instance Ord PublicNumber
- Crypto.Cipher.DH: instance Ord SharedKey
- Crypto.Cipher.DH: instance Read PrivateNumber
- Crypto.Cipher.DH: instance Read PublicNumber
- Crypto.Cipher.DH: instance Read SharedKey
- Crypto.Cipher.DH: instance Real PrivateNumber
- Crypto.Cipher.DH: instance Real PublicNumber
- Crypto.Cipher.DH: instance Real SharedKey
- Crypto.Cipher.DH: instance Show PrivateNumber
- Crypto.Cipher.DH: instance Show PublicNumber
- Crypto.Cipher.DH: instance Show SharedKey
- Crypto.Cipher.DSA: instance Show PrivateKey
- Crypto.Cipher.DSA: instance Show PublicKey
- Crypto.Cipher.RSA: instance Show PrivateKey
- Crypto.Cipher.RSA: instance Show PublicKey
- Crypto.Cipher.RSA: private_sz :: PrivateKey -> Int
- Crypto.Cipher.RSA: public_sz :: PublicKey -> Int
+ Crypto.Cipher.RSA: private_size :: PrivateKey -> Int
+ Crypto.Cipher.RSA: public_size :: PublicKey -> Int
- Crypto.Cipher.DH: data PrivateNumber
+ Crypto.Cipher.DH: data PrivateNumber :: *
- Crypto.Cipher.DH: data PublicNumber
+ Crypto.Cipher.DH: data PublicNumber :: *
- Crypto.Cipher.DH: data SharedKey
+ Crypto.Cipher.DH: data SharedKey :: *
- Crypto.Cipher.DSA: data PrivateKey
+ Crypto.Cipher.DSA: data PrivateKey :: *
- Crypto.Cipher.DSA: data PublicKey
+ Crypto.Cipher.DSA: data PublicKey :: *
- Crypto.Cipher.RSA: data PrivateKey
+ Crypto.Cipher.RSA: data PrivateKey :: *
- Crypto.Cipher.RSA: data PublicKey
+ Crypto.Cipher.RSA: data PublicKey :: *

Files

Crypto/Cipher/DH.hs view
@@ -21,19 +21,9 @@ import Number.ModArithmetic (exponantiation_rtl_binary) import Number.Prime (generateSafePrime) import Number.Generate (generateOfSize)+import Crypto.Types.PubKey.DH import Crypto.Random import Control.Arrow (first)--type Params = (Integer,Integer) {- P prime, G generator -}--newtype PublicNumber = PublicNumber Integer {- Y -}-	deriving (Show,Read,Eq,Enum,Real,Num,Ord)--newtype PrivateNumber = PrivateNumber Integer {- X -}-	deriving (Show,Read,Eq,Enum,Real,Num,Ord)--newtype SharedKey = SharedKey Integer {- S -}-	deriving (Show,Read,Eq,Enum,Real,Num,Ord)  -- | generate params from a specific generator (2 or 5 are common values) -- we generate a safe prime (a prime number of the form 2p+1 where p is also prime)
Crypto/Cipher/DSA.hs view
@@ -22,24 +22,12 @@ import Number.ModArithmetic (exponantiation_rtl_binary, inverse) import Number.Serialize import Number.Generate+import Crypto.Types.PubKey.DSA  data Error =  	  InvalidSignature          -- ^ signature is not valid r or s is not between the bound 0..q 	| RandomGenFailure GenError -- ^ the random generator returns an error. give the opportunity to reseed for example. 	deriving (Show,Eq)--type Params = (Integer,Integer,Integer) {- P, G, Q -}-type Signature = (Integer,Integer) {- R,S -}--data PublicKey = PublicKey-	{ public_params :: Params {- P, G, Q -}-	, public_y      :: Integer-	} deriving (Show)--data PrivateKey = PrivateKey-	{ private_params :: Params {- P, G, Q -}-	, private_x      :: Integer-	} deriving (Show)  {-| sign message using the private key. -} sign :: CryptoRandomGen g => g -> (ByteString -> ByteString) -> PrivateKey -> ByteString -> Either GenError (Signature, g)
Crypto/Cipher/RSA.hs view
@@ -22,6 +22,7 @@  import Control.Arrow (first) import Crypto.Random+import Crypto.Types.PubKey.RSA import Data.ByteString (ByteString) import qualified Data.ByteString as B import Number.ModArithmetic (exponantiation_rtl_binary, inverse)@@ -38,23 +39,6 @@ 	| KeyInternalError          -- ^ the whole key is probably not valid, since the message is bigger than the key size 	deriving (Show,Eq) -data PublicKey = PublicKey-	{ public_sz :: Int      -- ^ size of key in bytes-	, public_n  :: Integer  -- ^ public p*q-	, public_e  :: Integer  -- ^ public exponant e-	} deriving (Show)--data PrivateKey = PrivateKey-	{ private_sz   :: Int     -- ^ size of key in bytes-	, private_n    :: Integer -- ^ private p*q-	, private_d    :: Integer -- ^ private exponant d-	, private_p    :: Integer -- ^ p prime number-	, private_q    :: Integer -- ^ q prime number-	, private_dP   :: Integer -- ^ d mod (p-1)-	, private_dQ   :: Integer -- ^ d mod (q-1)-	, private_qinv :: Integer -- ^ q^(-1) mod p-	} deriving (Show)- type HashF = ByteString -> ByteString type HashASN1 = ByteString @@ -83,13 +67,13 @@ {- dpSlow computes the decrypted message not using any precomputed cache value.    only n and d need to valid. -} dpSlow :: PrivateKey -> ByteString -> Either Error ByteString-dpSlow pk c = i2ospOf (private_sz pk) $ expmod (os2ip c) (private_d pk) (private_n pk)+dpSlow pk c = i2ospOf (private_size pk) $ expmod (os2ip c) (private_d pk) (private_n pk)  {- dpFast computes the decrypted message more efficiently if the    precomputed private values are available. mod p and mod q are faster    to compute than mod pq -} dpFast :: PrivateKey -> ByteString -> Either Error ByteString-dpFast pk c = i2ospOf (private_sz pk) (m2 + h * (private_q pk))+dpFast pk c = i2ospOf (private_size pk) (m2 + h * (private_q pk)) 	where 		iC = os2ip c 		m1 = expmod iC (private_dP pk) (private_p pk)@@ -99,8 +83,8 @@ {-| decrypt message using the private key. -} decrypt :: PrivateKey -> ByteString -> Either Error ByteString decrypt pk c-	| B.length c /= (private_sz pk) = Left MessageSizeIncorrect-	| otherwise                     = dp pk c >>= unpadPKCS1+	| B.length c /= (private_size pk) = Left MessageSizeIncorrect+	| otherwise                       = dp pk c >>= unpadPKCS1 		where dp = if private_p pk /= 0 && private_q pk /= 0 then dpFast else dpSlow  {- | encrypt a bytestring using the public key and a CryptoRandomGen random generator.@@ -108,22 +92,22 @@  -} encrypt :: CryptoRandomGen g => g -> PublicKey -> ByteString -> Either Error (ByteString, g) encrypt rng pk m-	| B.length m > public_sz pk - 11 = Left MessageTooLong-	| otherwise                      = do-		(em, rng') <- padPKCS1 rng (public_sz pk) m-		c          <- i2ospOf (public_sz pk) $ expmod (os2ip em) (public_e pk) (public_n pk)+	| B.length m > public_size pk - 11 = Left MessageTooLong+	| otherwise                        = do+		(em, rng') <- padPKCS1 rng (public_size pk) m+		c          <- i2ospOf (public_size pk) $ expmod (os2ip em) (public_e pk) (public_n pk) 		return (c, rng')  {-| sign message using private key, a hash and its ASN1 description -} sign :: HashF -> HashASN1 -> PrivateKey -> ByteString -> Either Error ByteString-sign hash hashdesc pk m = makeSignature hash hashdesc (private_sz pk) m >>= d pk+sign hash hashdesc pk m = makeSignature hash hashdesc (private_size pk) m >>= d pk 	where d = if private_p pk /= 0 && private_q pk /= 0 then dpFast else dpSlow  {-| verify message with the signed message -} verify :: HashF -> HashASN1 -> PublicKey -> ByteString -> ByteString -> Either Error Bool verify hash hashdesc pk m sm = do-	s  <- makeSignature hash hashdesc (public_sz pk) m-	em <- i2ospOf (public_sz pk) $ expmod (os2ip sm) (public_e pk) (public_n pk)+	s  <- makeSignature hash hashdesc (public_size pk) m+	em <- i2ospOf (public_size pk) $ expmod (os2ip sm) (public_e pk) (public_n pk) 	Right (s == em)  -- | generate a pair of (private, public) key of size in bytes.@@ -136,7 +120,7 @@ 		Nothing -> generate rng' size e 		Just d  -> do 			let priv = PrivateKey-				{ private_sz   = size+				{ private_size = size 				, private_n    = n 				, private_d    = d 				, private_p    = p@@ -146,9 +130,9 @@ 				, private_qinv = fromJust $ inverse q p -- q and p are coprime, so fromJust is safe. 				} 			let pub = PublicKey-				{ public_sz = size-				, public_n  = n-				, public_e  = e+				{ public_size = size+				, public_n    = n+				, public_e    = e 				} 			Right ((pub, priv), rng') 	where
Tests.hs view
@@ -348,7 +348,7 @@ prop_rsa_sign_slow_valid = prop_rsa_sign_valid False  rsaPrivatekey = RSA.PrivateKey-	{ RSA.private_sz   = 128+	{ RSA.private_size = 128 	, RSA.private_n    = 140203425894164333410594309212077886844966070748523642084363106504571537866632850620326769291612455847330220940078873180639537021888802572151020701352955762744921926221566899281852945861389488419179600933178716009889963150132778947506523961974222282461654256451508762805133855866018054403911588630700228345151 	, RSA.private_d    = 133764127300370985476360382258931504810339098611363623122953018301285450176037234703101635770582297431466449863745848961134143024057267778947569638425565153896020107107895924597628599677345887446144410702679470631826418774397895304952287674790343620803686034122942606764275835668353720152078674967983573326257 	, RSA.private_p    = 12909745499610419492560645699977670082358944785082915010582495768046269235061708286800087976003942261296869875915181420265794156699308840835123749375331319@@ -359,9 +359,9 @@ 	}  rsaPublickey = RSA.PublicKey-	{ RSA.public_sz = 128-	, RSA.public_n  = 140203425894164333410594309212077886844966070748523642084363106504571537866632850620326769291612455847330220940078873180639537021888802572151020701352955762744921926221566899281852945861389488419179600933178716009889963150132778947506523961974222282461654256451508762805133855866018054403911588630700228345151-	, RSA.public_e  = 65537+	{ RSA.public_size = 128+	, RSA.public_n    = 140203425894164333410594309212077886844966070748523642084363106504571537866632850620326769291612455847330220940078873180639537021888802572151020701352955762744921926221566899281852945861389488419179600933178716009889963150132778947506523961974222282461654256451508762805133855866018054403911588630700228345151+	, RSA.public_e    = 65537 	}  {-----------------------------------------------------------------------------------------------}
cryptocipher.cabal view
@@ -1,5 +1,5 @@ Name:                cryptocipher-Version:             0.2.14+Version:             0.3.0 Description:         Symmetrical Block, Stream and PubKey Ciphers License:             BSD3 License-file:        LICENSE@@ -27,6 +27,7 @@                    , ghc-prim                    , primitive                    , crypto-api >= 0.5+                   , crypto-pubkey-types                    , tagged                    , cereal   Exposed-modules:   Crypto.Cipher.RC4