packages feed

crypto-cipher-types 0.0.2 → 0.0.3

raw patch · 2 files changed

+31/−10 lines, 2 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

+ Crypto.Cipher.Types: KeyErrorInvalid :: String -> KeyError
+ Crypto.Cipher.Types: KeyErrorTooBig :: KeyError
+ Crypto.Cipher.Types: KeyErrorTooSmall :: KeyError
+ Crypto.Cipher.Types: KeySizeEnum :: [Int] -> KeySizeSpecifier
+ Crypto.Cipher.Types: KeySizeFixed :: Int -> KeySizeSpecifier
+ Crypto.Cipher.Types: KeySizeRange :: Int -> Int -> KeySizeSpecifier
+ Crypto.Cipher.Types: data KeyError
+ Crypto.Cipher.Types: data KeySizeSpecifier
+ Crypto.Cipher.Types: instance Eq KeyError
+ Crypto.Cipher.Types: instance Eq KeySizeSpecifier
+ Crypto.Cipher.Types: instance Show KeyError
+ Crypto.Cipher.Types: instance Show KeySizeSpecifier
- Crypto.Cipher.Types: cipherKeySize :: Cipher cipher => cipher -> Maybe Int
+ Crypto.Cipher.Types: cipherKeySize :: Cipher cipher => cipher -> KeySizeSpecifier
- Crypto.Cipher.Types: makeKey :: (ToSecureMem b, Cipher c) => b -> Maybe (Key c)
+ Crypto.Cipher.Types: makeKey :: (ToSecureMem b, Cipher c) => b -> Either KeyError (Key c)

Files

Crypto/Cipher/Types.hs view
@@ -17,6 +17,8 @@     , BlockCipher(..)     , StreamCipher(..)     , DataUnitOffset+    , KeySizeSpecifier(..)+    , KeyError(..)     , AEAD(..)     , AEADState(..)     , AEADMode(..)@@ -51,6 +53,20 @@ -- | Offset inside an XTS data unit, measured in block size. type DataUnitOffset = Word32 +-- | Possible Error that can be reported when initializating a key+data KeyError =+      KeyErrorTooSmall+    | KeyErrorTooBig+    | KeyErrorInvalid String+    deriving (Show,Eq)++-- | Different specifier for key size in bytes+data KeySizeSpecifier =+      KeySizeRange Int Int -- ^ in the range [min,max]+    | KeySizeEnum  [Int]   -- ^ one of the specified values+    | KeySizeFixed Int     -- ^ a specific size+    deriving (Show,Eq)+ -- | Symmetric cipher class. class Cipher cipher where     -- | Initialize a cipher context from a key@@ -59,7 +75,7 @@     cipherName    :: cipher -> String     -- | return the size of the key required for this cipher.     -- Some cipher accept any size for key-    cipherKeySize :: cipher -> Maybe Int+    cipherKeySize :: cipher -> KeySizeSpecifier  -- | Symmetric stream cipher class class Cipher cipher => StreamCipher cipher where@@ -240,14 +256,19 @@                            in (hi + (nw `shiftR` 8), fromIntegral nw)  -- | Create a Key for a specified cipher-makeKey :: (ToSecureMem b, Cipher c) => b -> Maybe (Key c)-makeKey b = toKey undefined (toSecureMem b)-  where toKey :: Cipher c => c -> SecureMem -> Maybe (Key c)-        toKey cipher sm =-            case cipherKeySize cipher of-                Nothing                           -> Just $ Key sm-                Just sz | sz == byteableLength sm -> Just $ Key sm-                        | otherwise               -> Nothing+makeKey :: (ToSecureMem b, Cipher c) => b -> Either KeyError (Key c)+makeKey b = toKey undefined+  where sm    = toSecureMem b+        smLen = byteableLength sm+        toKey :: Cipher c => c -> Either KeyError (Key c)+        toKey cipher = case cipherKeySize cipher of+            KeySizeRange mi ma | smLen < mi -> Left KeyErrorTooSmall+                               | smLen > ma -> Left KeyErrorTooBig+                               | otherwise  -> Right $ Key sm+            KeySizeEnum l | smLen `elem` l  -> Right $ Key sm+                          | otherwise       -> Left $ KeyErrorInvalid ("valid size: " ++ show l)+            KeySizeFixed v | smLen < v      -> Right $ Key sm+                           | otherwise      -> Left $ KeyErrorInvalid ("valid size: " ++ show v)  cbcEncryptGeneric :: BlockCipher cipher => cipher -> IV cipher -> ByteString -> ByteString cbcEncryptGeneric cipher (IV ivini) input = B.concat $ doEnc ivini $ chunk (blockSize cipher) input
crypto-cipher-types.cabal view
@@ -1,5 +1,5 @@ Name:                crypto-cipher-types-Version:             0.0.2+Version:             0.0.3 Synopsis:            Generic cryptography cipher types Description:         Generic cryptography cipher types License:             BSD3