Skip to content

Commit

Permalink
Fixed #7 and #3
Browse files Browse the repository at this point in the history
  • Loading branch information
adinapoli committed Dec 14, 2014
1 parent 1134f92 commit 13f9a8e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/Crypto/RNCryptor/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Crypto.RNCryptor.Types
( RNCryptorHeader(..)
, RNCryptorContext(ctxHeader, ctxCipher)
, UserInput(..)
, newRNCryptorContext
, newRNCryptorHeader
, renderRNCryptorHeader
Expand All @@ -17,6 +18,7 @@ import Control.Applicative
import Control.Monad
import Crypto.Cipher.AES
import Crypto.PBKDF.ByteString
import Test.QuickCheck


data RNCryptorHeader = RNCryptorHeader {
Expand All @@ -36,6 +38,25 @@ data RNCryptorHeader = RNCryptorHeader {
-- as the HMAC is at the end of the file.
}

instance Show RNCryptorHeader where
show = C8.unpack . renderRNCryptorHeader

instance Arbitrary RNCryptorHeader where
arbitrary = do
let version = toEnum 3
let options = toEnum 1
eSalt <- C8.pack <$> vector saltSize
iv <- C8.pack <$> vector blockSize
hmacSalt <- C8.pack <$> vector saltSize
return RNCryptorHeader {
rncVersion = version
, rncOptions = options
, rncEncryptionSalt = eSalt
, rncHMACSalt = hmacSalt
, rncIV = iv
, rncHMAC = \uKey -> sha1PBKDF2 uKey hmacSalt 10000 32
}

--------------------------------------------------------------------------------
saltSize :: Int
saltSize = 8
Expand Down Expand Up @@ -82,6 +103,11 @@ data RNCryptorContext = RNCryptorContext {
, ctxCipher :: AES
}

newtype UserInput = UI { unInput :: ByteString } deriving Show

instance Arbitrary UserInput where
arbitrary = UI . C8.pack <$> arbitrary

--------------------------------------------------------------------------------
newRNCryptorContext :: ByteString -> RNCryptorHeader -> RNCryptorContext
newRNCryptorContext userKey hdr =
Expand Down
2 changes: 2 additions & 0 deletions src/Crypto/RNCryptor/V3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
module Crypto.RNCryptor.V3 (
module Crypto.RNCryptor.V3.Encrypt
, module Crypto.RNCryptor.V3.Decrypt
, module Crypto.RNCryptor.Types
) where

import Crypto.RNCryptor.V3.Encrypt
import Crypto.RNCryptor.V3.Decrypt
import Crypto.RNCryptor.Types
7 changes: 3 additions & 4 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where

import System.Environment
import Data.Monoid
import Tests
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck

----------------------------------------------------------------------
Expand All @@ -18,5 +15,7 @@ main :: IO ()
main = do
defaultMainWithIngredients defaultIngredients $
testGroup "RNCryptor tests" $ [
testGroup "RNCryptor properties" []
withQuickCheckDepth "RNCryptor properties" 100 [
testProperty "encrypt/decrypt roundtrip" testEncryptDecryptRoundtrip
]
]
22 changes: 21 additions & 1 deletion test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Tests where

import Test.Tasty.HUnit
import Test.Tasty.QuickCheck
import Crypto.RNCryptor.V3
import Control.Applicative
import qualified Data.ByteString as B


newtype TestVector = TV (UserInput, UserInput, RNCryptorHeader) deriving Show

instance Arbitrary TestVector where
arbitrary = TV <$> ((,,) <$> arbitrary <*> arbitrary <*> arbitrary)


testEncryptDecryptRoundtrip :: Property
testEncryptDecryptRoundtrip =
forAll arbitrary $ \(TV (input,pwd,hdr)) ->
B.length (unInput input) > 0 &&
B.length (unInput pwd) > 0 ==>
let ctx = newRNCryptorContext (unInput pwd) hdr
encrypted = encrypt ctx (unInput input)
in decrypt encrypted (unInput pwd) == unInput input
Empty file added 
Empty file.

0 comments on commit 13f9a8e

Please sign in to comment.