From 76ba39fc954c873ddbd34207c1b54cb604d995cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Ch=C3=A9ron?= Date: Sun, 19 May 2019 09:07:38 +0200 Subject: [PATCH] Add benchmark with AES GCM and CCM --- Crypto/Cipher/Types/Base.hs | 3 ++- benchs/Bench.hs | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Crypto/Cipher/Types/Base.hs b/Crypto/Cipher/Types/Base.hs index 03a20f8..37d9028 100644 --- a/Crypto/Cipher/Types/Base.hs +++ b/Crypto/Cipher/Types/Base.hs @@ -22,6 +22,7 @@ module Crypto.Cipher.Types.Base import Data.Word import Crypto.Internal.ByteArray (Bytes, ByteArrayAccess, ByteArray) import qualified Crypto.Internal.ByteArray as B +import Crypto.Internal.DeepSeq import Crypto.Error -- | Different specifier for key size in bytes @@ -36,7 +37,7 @@ type DataUnitOffset = Word32 -- | Authentication Tag for AE cipher mode newtype AuthTag = AuthTag { unAuthTag :: Bytes } - deriving (Show, ByteArrayAccess) + deriving (Show, ByteArrayAccess, NFData) instance Eq AuthTag where (AuthTag a) == (AuthTag b) = B.constEq a b diff --git a/benchs/Bench.hs b/benchs/Bench.hs index 180bafa..92e7e62 100644 --- a/benchs/Bench.hs +++ b/benchs/Bench.hs @@ -162,14 +162,27 @@ benchBlockCipher = iv16 = maybe (error "iv size 16") id $ makeIV key16 benchAE = - [ bench "ChaChaPoly1305" $ nf (run key32) (input64, input1024) + [ bench "ChaChaPoly1305" $ nf (cp key32) (input64, input1024) + , bench "AES-GCM" $ nf (gcm key32) (input64, input1024) + , bench "AES-CCM" $ nf (ccm key32) (input64, input1024) ] - where run k (ini, plain) = + where cp k (ini, plain) = let iniState = throwCryptoError $ CP.initialize k (throwCryptoError $ CP.nonce12 nonce12) afterAAD = CP.finalizeAAD (CP.appendAAD ini iniState) (out, afterEncrypt) = CP.encrypt plain afterAAD outtag = CP.finalize afterEncrypt - in (out, outtag) + in (outtag, out) + + gcm k (ini, plain) = + let ctx = throwCryptoError (cipherInit k) :: AES256 + state = throwCryptoError $ aeadInit AEAD_GCM ctx nonce12 + in aeadSimpleEncrypt state ini plain 16 + + ccm k (ini, plain) = + let ctx = throwCryptoError (cipherInit k) :: AES256 + mode = AEAD_CCM 1024 CCM_M16 CCM_L3 + state = throwCryptoError $ aeadInit mode ctx nonce12 + in aeadSimpleEncrypt state ini plain 16 input64 = B.replicate 64 0 input1024 = B.replicate 1024 0