packages feed

RSA 2.0.0 → 2.1.0

raw patch · 4 files changed

+56/−103 lines, 4 filesdep −RSAdep −monadcryptorandomdep ~DRBGdep ~SHAdep ~basePVP ok

version bump matches the API change (PVP)

Dependencies removed: RSA, monadcryptorandom

Dependency ranges changed: DRBG, SHA, base, binary, bytestring, crypto-api, crypto-pubkey-types, pureMD5, tagged, test-framework, test-framework-quickcheck2

API changes (from Hackage documentation)

+ Codec.Crypto.RSA.Exceptions: PrivateKey :: PublicKey -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> PrivateKey
+ Codec.Crypto.RSA.Exceptions: PublicKey :: Int -> Integer -> Integer -> PublicKey
+ Codec.Crypto.RSA.Exceptions: private_d :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Exceptions: private_dP :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Exceptions: private_dQ :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Exceptions: private_p :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Exceptions: private_pub :: PrivateKey -> PublicKey
+ Codec.Crypto.RSA.Exceptions: private_q :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Exceptions: private_qinv :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Exceptions: public_e :: PublicKey -> Integer
+ Codec.Crypto.RSA.Exceptions: public_n :: PublicKey -> Integer
+ Codec.Crypto.RSA.Exceptions: public_size :: PublicKey -> Int
+ Codec.Crypto.RSA.Pure: PrivateKey :: PublicKey -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> PrivateKey
+ Codec.Crypto.RSA.Pure: PublicKey :: Int -> Integer -> Integer -> PublicKey
+ Codec.Crypto.RSA.Pure: private_d :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Pure: private_dP :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Pure: private_dQ :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Pure: private_p :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Pure: private_pub :: PrivateKey -> PublicKey
+ Codec.Crypto.RSA.Pure: private_q :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Pure: private_qinv :: PrivateKey -> Integer
+ Codec.Crypto.RSA.Pure: public_e :: PublicKey -> Integer
+ Codec.Crypto.RSA.Pure: public_n :: PublicKey -> Integer
+ Codec.Crypto.RSA.Pure: public_size :: PublicKey -> Int

Files

RSA.cabal view
@@ -1,6 +1,6 @@ name:       RSA category:   Cryptography, Codec-version:    2.0.0+version:    2.1.0 license:    BSD3 license-file: LICENSE author:     Adam Wick <awick@galois.com>@@ -17,91 +17,39 @@              included are based of RFC 3447, or the Public-Key Cryptography              Standard for RSA, version 2.1 (a.k.a, PKCS#1 v2.1).    -Flag OldBase-  Description: Whether or not to use base 3 (default: no)-  Default: False--Flag BuildTestExecutable-  Description: Whether to build the test code as a separate exeutable,-               instead of just utilizing the cabal test framework.- Library- hs-source-dirs: src- build-depends: binary > 0.0 && < 10000,-                bytestring > 0.8 && < 10000,-                crypto-api >= 0.10 && < 10000,-                crypto-pubkey-types >= 0.2 && < 10000,-                monadcryptorandom > 0 && < 10000-- if flag(OldBase)-   build-depends: base >= 3 && < 4,-                  pureMD5 < 1.1,-                  SHA < 1.4.1- else-   build-depends: base >= 4 && < 7,-                  pureMD5 > 0 && < 10000,-                  SHA > 0 && < 10000-- exposed-modules: Codec.Crypto.RSA,-                  Codec.Crypto.RSA.Exceptions,-                  Codec.Crypto.RSA.Pure-- GHC-Options: -Wall -fno-warn-orphans- extensions: BangPatterns, DeriveDataTypeable--executable rsa_test-  Main-Is: Test.hs-  if flag(OldBase)-    build-depends: base >= 3 && < 4,-                   pureMD5 < 1.1,-                   SHA < 1.4.1-  else-    build-depends: base >= 4 && < 7,-                   pureMD5 > 0 && < 10000,-                   SHA > 0 && < 10000--  build-depends: binary > 0.0 && < 10000,-                 bytestring > 0.8 && < 10000,-                 crypto-api >= 0.10 && < 10000,-                 crypto-pubkey-types >= 0.2 && < 10000,-                 DRBG >= 0.5.2 && < 10000,-                 QuickCheck >= 2.5 && < 3,-                 RSA >= 2 && < 10000,-                 tagged >= 0.2 && < 10000,-                 test-framework >= 0.8.0.3 && < 10000,-                 test-framework-quickcheck2 >= 0.3.0.2 && < 10000-  if flag(BuildTestExecutable)-    buildable: True-  else-    buildable: False-  GHC-Options: -Wall -fno-warn-orphans -  extensions: ScopedTypeVariables--Test-Suite test-RSA-  type:       exitcode-stdio-1.0-  Main-Is: Test.hs--  if flag(OldBase)-    build-depends: base >= 3 && < 4,-                   pureMD5 < 1.1,-                   SHA < 1.4.1-  else-    build-depends: base >= 4 && < 7,-                   pureMD5 > 0 && < 10000,-                   SHA > 0 && < 10000+  hs-source-dirs:  src+  build-depends:   base                >= 4.0     && < 7.0,+                   binary              >  0.7     && < 1.0,+                   bytestring          >  0.8     && < 0.12,+                   crypto-api          >= 0.10    && < 0.14,+                   crypto-pubkey-types >= 0.2     && < 0.6,+                   pureMD5             >= 2.1     && < 2.3,+                   SHA                 >= 1.6.4.1 && < 2.0+  exposed-modules: Codec.Crypto.RSA,+                   Codec.Crypto.RSA.Exceptions,+                   Codec.Crypto.RSA.Pure+  GHC-Options:     -Wall -fno-warn-orphans+  extensions:      DeriveDataTypeable, MultiWayIf -  build-depends: binary > 0.0 && < 10000,-                 bytestring > 0.8 && < 10000,-                 crypto-api >= 0.10 && < 10000,-                 crypto-pubkey-types >= 0.2 && < 10000,-                 DRBG >= 0.5.2 && < 10000,-                 QuickCheck >= 2.5 && < 3,-                 RSA >= 2 && < 10000,-                 tagged >= 0.2 && < 10000,-                 test-framework >= 0.8.0.3 && < 10000,-                 test-framework-quickcheck2 >= 0.3.0.2 && < 10000-  GHC-Options: -Wall -fno-warn-orphans -  extensions: ScopedTypeVariables+test-suite test-rsa+  type:           exitcode-stdio-1.0+  Main-Is:        Test.hs+  hs-source-dirs: src,.+  build-depends:  base                       >= 4.0     && < 7.0,+                  binary                     >  0.7     && < 1.0,+                  bytestring                 >  0.8     && < 0.12,+                  crypto-api                 >= 0.10    && < 0.14,+                  crypto-pubkey-types        >= 0.4     && < 0.6,+                  DRBG                       >= 0.5.2   && < 0.7,+                  pureMD5                    >= 2.1     && < 2.3,+                  QuickCheck                 >= 2.5     && < 3,+                  tagged                     >= 0.2     && < 0.9,+                  test-framework             >= 0.8.0.3 && < 0.10,+                  test-framework-quickcheck2 >= 0.3.0.2 && < 0.5,+                  SHA                        >= 1.6.4.1 && < 2.0+  GHC-Options:    -Wall -fno-warn-orphans -fhpc+  extensions:     DeriveDataTypeable, MultiWayIf, ScopedTypeVariables  source-repository head   type: git
Test.hs view
@@ -9,7 +9,6 @@ import Test.QuickCheck import Crypto.Random import Crypto.Random.DRBG-import Crypto.Types.PubKey.RSA hiding (KeyPair)  import Test.Framework (defaultMain, testGroup) import Test.Framework.Providers.QuickCheck2 (testProperty)
src/Codec/Crypto/RSA/Exceptions.hs view
@@ -2,8 +2,8 @@          RSAError(..)        , HashInfo(..)        -- * Keys and key generation-       , PrivateKey-       , PublicKey+       , PrivateKey(..)+       , PublicKey(..)        , generateKeyPair        -- * High-level encryption and signature functions        , encrypt
src/Codec/Crypto/RSA/Pure.hs view
@@ -1,11 +1,11 @@-{-# LANGUAGE BangPatterns #-} {-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE MultiWayIf         #-} module Codec.Crypto.RSA.Pure(          RSAError(..)        , HashInfo(..)        -- * Keys and key generation-       , PrivateKey-       , PublicKey+       , PrivateKey(..)+       , PublicKey(..)        , generateKeyPair        -- * High-level encryption and signature functions        , encrypt@@ -48,7 +48,6 @@  import Control.Exception import Control.Monad-import Control.Monad.CryptoRandom import Crypto.Random import Crypto.Types.PubKey.RSA import Data.Binary@@ -135,7 +134,7 @@       e          = 65537       d          = modular_inverse e phi   let publicKey  = PublicKey keyLength n e-      privateKey = PrivateKey publicKey d 0 0 0 0 0+      privateKey = PrivateKey publicKey d p q 0 0 0   return (publicKey, privateKey, g')  -- ----------------------------------------------------------------------------@@ -683,6 +682,7 @@      let (s, d) = oddify 0 (n - 1)      in checkLoop g s d k  where+  generateSize = bitsize (n - 2) 8 `div` 8   -- k times, pick a random integer in [2, n-2] and see if you can find   -- a witness suggesting that it's not prime.   checkLoop :: CryptoRandomGen g =>@@ -690,13 +690,16 @@                Either RSAError (Bool, g)   checkLoop g' _ _ 0 = Right (True, g')   checkLoop g' s d c =-    case crandomR (2, n - 2) g' of+    case genBytes generateSize g' of       Left e -> Left (RSAGenError e)-      Right (a, g'') ->-        case modular_exponentiation a d n of-          1                -> checkLoop g'' s d (c - 1)-          x | x == (n - 1) -> checkLoop g'' s d (c - 1)-          x                -> checkWitnesses g'' s d x c (s - 1)+      Right (bstr, g'') ->+        let a = os2ip (BS.fromStrict bstr)+            x = modular_exponentiation a d n+        in if | (a < 2)       -> checkLoop g'' s d c+              | (a > (n - 2)) -> checkLoop g'' s d c+              | x == 1        -> checkLoop g'' s d (c - 1)+              | x == (n - 1)  -> checkLoop g'' s d (c - 1)+              | otherwise     -> checkWitnesses g'' s d x c (s - 1)   -- s times, where n-1 = 2^s*d, check to see if the given number is a   -- witness of something not being prime.   checkWitnesses g'' _ _ _ _  0  = Right (False, g'')@@ -708,17 +711,20 @@   -- given n, compute s and d such that 2^s*d = n.   oddify s x | testBit x 0 = (s, x)              | otherwise   = oddify (s + 1) (x `shiftR` 1)+  -- given n, compute the number of bits required to hold it.+  bitsize v x | (1 `shiftL` x) > v = x+              | otherwise          = bitsize v (x + 8)  -- |Computes a^b mod c using a moderately good algorithm. modular_exponentiation :: Integer -> Integer -> Integer -> Integer modular_exponentiation x y m = m_e_loop x y 1  where-  m_e_loop _   0 !result = result-  m_e_loop !b !e !result = m_e_loop b' e' result'+  m_e_loop _ 0 result = result+  m_e_loop b e result = m_e_loop b' e' result'    where-    !b'      = (b * b) `mod` m-    !e'      = e `shiftR` 1-    !result' = if testBit e 0 then (result * b) `mod` m else result+    b'      = (b * b) `mod` m+    e'      = e `shiftR` 1+    result' = if testBit e 0 then (result * b) `mod` m else result  -- |Compute the modular inverse (d = e^-1 mod phi) via the extended euclidean -- algorithm.