Marcos Dumay de Medeiros 8 years ago
parent
commit
6693573d4d
3 changed files with 12 additions and 13 deletions
  1. 2 2
      src/Crypto/Chunked.hs
  2. 8 9
      src/Crypto/ChunkedAlgorithms.hs
  3. 2 2
      test/RoundTrips.hs

+ 2 - 2
src/Crypto/Chunked.hs

@@ -37,8 +37,8 @@ type Nonce = ByteString
 
 -- | A set of encryption programs
 data ChunkedCrypto = ChunkedCrypto {
-  encryptGen :: (Nonce -> ByteString -> CryptoFailable ByteString),
-  decryptGen :: (Nonce -> ByteString -> CryptoFailable ByteString),
+  encryptGen :: Nonce -> ByteString -> CryptoFailable ByteString,
+  decryptGen :: Nonce -> ByteString -> CryptoFailable ByteString,
   plainSize :: Int64,
   encryptedSize :: Int64
   }

+ 8 - 9
src/Crypto/ChunkedAlgorithms.hs

@@ -24,22 +24,21 @@ chunkedChaChaPoly1305 ::
 chunkedChaChaPoly1305 s k = ChunkedCrypto enc dec (fromIntegral s) (fromIntegral s + auth_size)
   where
     auth_size = 16
-    enc = \nonce dt -> do
-      n <- ChaP.nonce12 nonce
-      st0 <- ChaP.initialize k n
-      let st1 = ChaP.finalizeAAD st0
+    enc nonce dt = do
+      st1 <- initChaP nonce
       let (out, st2) = ChaP.encrypt dt st1
       let auth = BA.convert $ ChaP.finalize st2
       return $ BS.append auth out
-    dec = \nonce dt' -> do
+    dec nonce dt' = do
       let (auth, dt) = BS.splitAt 16 dt'
-      n <- ChaP.nonce12 nonce
-      st0 <- ChaP.initialize k n
-      let st1 = ChaP.finalizeAAD st0
+      st1 <- initChaP nonce
       let (out, st2) = ChaP.decrypt dt st1
       let auth' = BA.convert $ ChaP.finalize st2
       if auth == auth'
         then return out
         else CryptoFailed CryptoError_MacKeyInvalid
+    initChaP nonce = do
+      n <- ChaP.nonce12 nonce
+      st0 <- ChaP.initialize k n
+      return $ ChaP.finalizeAAD st0
 
-      

+ 2 - 2
test/RoundTrips.hs

@@ -70,7 +70,7 @@ goodRoundTrip = do
   let algo = chunkedChaChaPoly1305 32 testKey
       (enc, fails1) = encrypt algo testNonce 0 testString
       (dec, fails2) = decrypt algo testNonce 0 enc
-  case sequence fails1 >> sequence fails2 of
+  case sequence_ fails1 >> sequence fails2 of
     CryptoPassed _ -> if dec == testString
                       then return . Finished $ Pass
                       else return . Finished . Fail $ "Text differs: " ++ C8.unpack dec
@@ -85,7 +85,7 @@ authError = do
     Just (t, tt) -> do
       let enc' = LBS.cons (t+1) tt
           (dec, fails2) = decrypt algo testNonce 0 enc'
-      case sequence fails1 >> sequence fails2 of
+      case sequence_ fails1 >> sequence fails2 of
         CryptoFailed CryptoError_MacKeyInvalid -> return . Finished $ Pass
         CryptoFailed e -> return . Finished . Fail $ "Crypto failed: " ++ show e
         CryptoPassed _ -> return . Finished . Fail $ "Got decrypted text: " ++ C8.unpack dec