2011-03-15 21:47:29 +00:00
|
|
|
{- git-annex Key data type
|
|
|
|
-
|
2019-01-11 17:55:00 +00:00
|
|
|
- Copyright 2011-2019 Joey Hess <id@joeyh.name>
|
2011-03-15 21:47:29 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-03-15 21:47:29 +00:00
|
|
|
-}
|
|
|
|
|
2018-03-15 15:16:00 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2019-01-11 17:55:00 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-03-15 15:16:00 +00:00
|
|
|
|
2017-02-24 17:42:30 +00:00
|
|
|
module Types.Key where
|
2011-03-15 21:47:29 +00:00
|
|
|
|
2019-01-11 17:55:00 +00:00
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import qualified Data.ByteString.Char8 as S8
|
2011-03-16 01:34:13 +00:00
|
|
|
import System.Posix.Types
|
2019-01-18 17:59:29 +00:00
|
|
|
import Data.Monoid
|
|
|
|
import Prelude
|
2011-10-04 02:24:57 +00:00
|
|
|
|
2013-07-04 06:36:02 +00:00
|
|
|
{- A Key has a unique name, which is derived from a particular backend,
|
2011-03-16 01:34:13 +00:00
|
|
|
- and may contain other optional metadata. -}
|
2013-07-04 06:45:46 +00:00
|
|
|
data Key = Key
|
2019-01-11 17:55:00 +00:00
|
|
|
{ keyName :: S.ByteString
|
2017-02-24 19:16:56 +00:00
|
|
|
, keyVariety :: KeyVariety
|
2013-07-04 06:45:46 +00:00
|
|
|
, keySize :: Maybe Integer
|
|
|
|
, keyMtime :: Maybe EpochTime
|
2014-07-24 17:36:23 +00:00
|
|
|
, keyChunkSize :: Maybe Integer
|
|
|
|
, keyChunkNum :: Maybe Integer
|
2013-07-04 06:45:46 +00:00
|
|
|
} deriving (Eq, Ord, Read, Show)
|
2011-03-15 21:47:29 +00:00
|
|
|
|
2013-07-04 06:36:02 +00:00
|
|
|
{- A filename may be associated with a Key. -}
|
2017-03-10 17:12:24 +00:00
|
|
|
newtype AssociatedFile = AssociatedFile (Maybe FilePath)
|
|
|
|
deriving (Show, Eq, Ord)
|
2017-02-24 19:16:56 +00:00
|
|
|
|
2019-01-16 20:33:08 +00:00
|
|
|
{- There are several different varieties of keys. -}
|
2017-02-24 19:16:56 +00:00
|
|
|
data KeyVariety
|
2019-01-16 20:33:08 +00:00
|
|
|
= SHA2Key HashSize HasExt
|
|
|
|
| SHA3Key HashSize HasExt
|
|
|
|
| SKEINKey HashSize HasExt
|
|
|
|
| Blake2bKey HashSize HasExt
|
|
|
|
| Blake2sKey HashSize HasExt
|
|
|
|
| Blake2spKey HashSize HasExt
|
|
|
|
| SHA1Key HasExt
|
|
|
|
| MD5Key HasExt
|
2017-02-24 19:16:56 +00:00
|
|
|
| WORMKey
|
|
|
|
| URLKey
|
|
|
|
-- Some repositories may contain keys of other varieties,
|
|
|
|
-- which can still be processed to some extent.
|
2019-01-11 17:55:00 +00:00
|
|
|
| OtherKey S.ByteString
|
2017-02-24 19:16:56 +00:00
|
|
|
deriving (Eq, Ord, Read, Show)
|
|
|
|
|
|
|
|
{- Some varieties of keys may contain an extension at the end of the
|
|
|
|
- keyName -}
|
|
|
|
newtype HasExt = HasExt Bool
|
|
|
|
deriving (Eq, Ord, Read, Show)
|
|
|
|
|
|
|
|
newtype HashSize = HashSize Int
|
|
|
|
deriving (Eq, Ord, Read, Show)
|
|
|
|
|
|
|
|
hasExt :: KeyVariety -> Bool
|
2019-01-16 20:33:08 +00:00
|
|
|
hasExt (SHA2Key _ (HasExt b)) = b
|
|
|
|
hasExt (SHA3Key _ (HasExt b)) = b
|
|
|
|
hasExt (SKEINKey _ (HasExt b)) = b
|
|
|
|
hasExt (Blake2bKey _ (HasExt b)) = b
|
|
|
|
hasExt (Blake2sKey _ (HasExt b)) = b
|
|
|
|
hasExt (Blake2spKey _ (HasExt b)) = b
|
|
|
|
hasExt (SHA1Key (HasExt b)) = b
|
|
|
|
hasExt (MD5Key (HasExt b)) = b
|
2017-02-24 19:16:56 +00:00
|
|
|
hasExt WORMKey = False
|
|
|
|
hasExt URLKey = False
|
2019-01-11 17:55:00 +00:00
|
|
|
hasExt (OtherKey s) = (snd <$> S8.unsnoc s) == Just 'E'
|
2017-02-24 19:16:56 +00:00
|
|
|
|
|
|
|
sameExceptExt :: KeyVariety -> KeyVariety -> Bool
|
2019-01-16 20:33:08 +00:00
|
|
|
sameExceptExt (SHA2Key sz1 _) (SHA2Key sz2 _) = sz1 == sz2
|
|
|
|
sameExceptExt (SHA3Key sz1 _) (SHA3Key sz2 _) = sz1 == sz2
|
|
|
|
sameExceptExt (SKEINKey sz1 _) (SKEINKey sz2 _) = sz1 == sz2
|
|
|
|
sameExceptExt (Blake2bKey sz1 _) (Blake2bKey sz2 _) = sz1 == sz2
|
|
|
|
sameExceptExt (Blake2sKey sz1 _) (Blake2sKey sz2 _) = sz1 == sz2
|
|
|
|
sameExceptExt (Blake2spKey sz1 _) (Blake2spKey sz2 _) = sz1 == sz2
|
|
|
|
sameExceptExt (SHA1Key _) (SHA1Key _) = True
|
|
|
|
sameExceptExt (MD5Key _) (MD5Key _) = True
|
2017-02-24 19:16:56 +00:00
|
|
|
sameExceptExt _ _ = False
|
|
|
|
|
2017-02-27 16:54:06 +00:00
|
|
|
{- Is the Key variety cryptographically secure, such that no two differing
|
|
|
|
- file contents can be mapped to the same Key? -}
|
|
|
|
cryptographicallySecure :: KeyVariety -> Bool
|
2019-01-16 20:33:08 +00:00
|
|
|
cryptographicallySecure (SHA2Key _ _) = True
|
|
|
|
cryptographicallySecure (SHA3Key _ _) = True
|
|
|
|
cryptographicallySecure (SKEINKey _ _) = True
|
|
|
|
cryptographicallySecure (Blake2bKey _ _) = True
|
|
|
|
cryptographicallySecure (Blake2sKey _ _) = True
|
|
|
|
cryptographicallySecure (Blake2spKey _ _) = True
|
2017-02-27 16:54:06 +00:00
|
|
|
cryptographicallySecure _ = False
|
|
|
|
|
2018-06-21 15:35:27 +00:00
|
|
|
{- Is the Key variety backed by a hash, which allows verifying content?
|
|
|
|
- It does not have to be cryptographically secure against eg birthday
|
|
|
|
- attacks.
|
|
|
|
-}
|
|
|
|
isVerifiable :: KeyVariety -> Bool
|
2019-01-16 20:33:08 +00:00
|
|
|
isVerifiable (SHA2Key _ _) = True
|
|
|
|
isVerifiable (SHA3Key _ _) = True
|
|
|
|
isVerifiable (SKEINKey _ _) = True
|
|
|
|
isVerifiable (Blake2bKey _ _) = True
|
|
|
|
isVerifiable (Blake2sKey _ _) = True
|
|
|
|
isVerifiable (Blake2spKey _ _) = True
|
|
|
|
isVerifiable (SHA1Key _) = True
|
|
|
|
isVerifiable (MD5Key _) = True
|
2018-06-21 15:35:27 +00:00
|
|
|
isVerifiable WORMKey = False
|
|
|
|
isVerifiable URLKey = False
|
2019-01-11 17:55:00 +00:00
|
|
|
isVerifiable (OtherKey _) = False
|
2018-06-21 15:35:27 +00:00
|
|
|
|
2019-01-11 17:55:00 +00:00
|
|
|
formatKeyVariety :: KeyVariety -> S.ByteString
|
2017-02-24 19:16:56 +00:00
|
|
|
formatKeyVariety v = case v of
|
2019-01-16 20:33:08 +00:00
|
|
|
SHA2Key sz e -> adde e (addsz sz "SHA")
|
|
|
|
SHA3Key sz e -> adde e (addsz sz "SHA3_")
|
|
|
|
SKEINKey sz e -> adde e (addsz sz "SKEIN")
|
|
|
|
Blake2bKey sz e -> adde e (addsz sz "BLAKE2B")
|
|
|
|
Blake2sKey sz e -> adde e (addsz sz "BLAKE2S")
|
|
|
|
Blake2spKey sz e -> adde e (addsz sz "BLAKE2SP")
|
|
|
|
SHA1Key e -> adde e "SHA1"
|
|
|
|
MD5Key e -> adde e "MD5"
|
2017-02-24 19:16:56 +00:00
|
|
|
WORMKey -> "WORM"
|
|
|
|
URLKey -> "URL"
|
|
|
|
OtherKey s -> s
|
|
|
|
where
|
|
|
|
adde (HasExt False) s = s
|
2019-01-11 17:55:00 +00:00
|
|
|
adde (HasExt True) s = s <> "E"
|
2019-01-16 20:33:08 +00:00
|
|
|
addsz (HashSize n) s = s <> case n of
|
|
|
|
256 -> "256"
|
|
|
|
512 -> "512"
|
|
|
|
224 -> "224"
|
|
|
|
384 -> "384"
|
|
|
|
160 -> "160"
|
|
|
|
-- This is relatively slow, which is why the common hash
|
|
|
|
-- sizes are hardcoded above.
|
|
|
|
_ -> S8.pack (show n)
|
2017-02-24 19:16:56 +00:00
|
|
|
|
2019-01-11 17:55:00 +00:00
|
|
|
parseKeyVariety :: S.ByteString -> KeyVariety
|
2019-01-16 20:33:08 +00:00
|
|
|
parseKeyVariety "SHA256" = SHA2Key (HashSize 256) (HasExt False)
|
|
|
|
parseKeyVariety "SHA256E" = SHA2Key (HashSize 256) (HasExt True)
|
|
|
|
parseKeyVariety "SHA512" = SHA2Key (HashSize 512) (HasExt False)
|
|
|
|
parseKeyVariety "SHA512E" = SHA2Key (HashSize 512) (HasExt True)
|
|
|
|
parseKeyVariety "SHA224" = SHA2Key (HashSize 224) (HasExt False)
|
|
|
|
parseKeyVariety "SHA224E" = SHA2Key (HashSize 224) (HasExt True)
|
|
|
|
parseKeyVariety "SHA384" = SHA2Key (HashSize 384) (HasExt False)
|
|
|
|
parseKeyVariety "SHA384E" = SHA2Key (HashSize 384) (HasExt True)
|
|
|
|
parseKeyVariety "SHA3_512" = SHA3Key (HashSize 512) (HasExt False)
|
|
|
|
parseKeyVariety "SHA3_512E" = SHA3Key (HashSize 512) (HasExt True)
|
|
|
|
parseKeyVariety "SHA3_384" = SHA3Key (HashSize 384) (HasExt False)
|
|
|
|
parseKeyVariety "SHA3_384E" = SHA3Key (HashSize 384) (HasExt True)
|
|
|
|
parseKeyVariety "SHA3_256" = SHA3Key (HashSize 256) (HasExt False)
|
|
|
|
parseKeyVariety "SHA3_256E" = SHA3Key (HashSize 256) (HasExt True)
|
|
|
|
parseKeyVariety "SHA3_224" = SHA3Key (HashSize 224) (HasExt False)
|
|
|
|
parseKeyVariety "SHA3_224E" = SHA3Key (HashSize 224) (HasExt True)
|
|
|
|
parseKeyVariety "SKEIN512" = SKEINKey (HashSize 512) (HasExt False)
|
|
|
|
parseKeyVariety "SKEIN512E" = SKEINKey (HashSize 512) (HasExt True)
|
|
|
|
parseKeyVariety "SKEIN256" = SKEINKey (HashSize 256) (HasExt False)
|
|
|
|
parseKeyVariety "SKEIN256E" = SKEINKey (HashSize 256) (HasExt True)
|
2018-03-15 15:16:00 +00:00
|
|
|
#if MIN_VERSION_cryptonite(0,23,0)
|
2019-01-16 20:33:08 +00:00
|
|
|
parseKeyVariety "BLAKE2B160" = Blake2bKey (HashSize 160) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2B160E" = Blake2bKey (HashSize 160) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2B224" = Blake2bKey (HashSize 224) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2B224E" = Blake2bKey (HashSize 224) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2B256" = Blake2bKey (HashSize 256) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2B256E" = Blake2bKey (HashSize 256) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2B384" = Blake2bKey (HashSize 384) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2B384E" = Blake2bKey (HashSize 384) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2B512" = Blake2bKey (HashSize 512) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2B512E" = Blake2bKey (HashSize 512) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2S160" = Blake2sKey (HashSize 160) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2S160E" = Blake2sKey (HashSize 160) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2S224" = Blake2sKey (HashSize 224) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2S224E" = Blake2sKey (HashSize 224) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2S256" = Blake2sKey (HashSize 256) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2S256E" = Blake2sKey (HashSize 256) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2SP224" = Blake2spKey (HashSize 224) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2SP224E" = Blake2spKey (HashSize 224) (HasExt True)
|
|
|
|
parseKeyVariety "BLAKE2SP256" = Blake2spKey (HashSize 256) (HasExt False)
|
|
|
|
parseKeyVariety "BLAKE2SP256E" = Blake2spKey (HashSize 256) (HasExt True)
|
2018-03-15 15:16:00 +00:00
|
|
|
#endif
|
2019-01-16 20:33:08 +00:00
|
|
|
parseKeyVariety "SHA1" = SHA1Key (HasExt False)
|
|
|
|
parseKeyVariety "SHA1E" = SHA1Key (HasExt True)
|
|
|
|
parseKeyVariety "MD5" = MD5Key (HasExt False)
|
|
|
|
parseKeyVariety "MD5E" = MD5Key (HasExt True)
|
|
|
|
parseKeyVariety "WORM" = WORMKey
|
|
|
|
parseKeyVariety "URL" = URLKey
|
|
|
|
parseKeyVariety b = OtherKey b
|