Debian Codegraph @

Crypto/Classes.hs-boot

raw
1 {-|
2 Maintainer: [email protected]
3 Stability: beta
4 Portability: portable
5 Authors: Thomas DuBuisson
6
7 This is the heart of the crypto-api package. By making (or having) an instance
8 of Hash, AsymCipher, BlockCipher or StreamCipher you provide (or obtain) access
9 to any infrastructure built on these primitives include block cipher modes of
10 operation, hashing, hmac, signing, etc. These classes allow users to build
11 routines that are agnostic to the algorithm used so changing algorithms is as
12 simple as changing a type signature.
13 -}
14 module Crypto.Classes where
15
16 import Data.ByteString as B
17 import Data.ByteString.Lazy as L
18 import Crypto.Types
19 import Data.Serialize
20 import Data.Tagged
21
22 class ( Serialize k) => BlockCipher k where
23 blockSize :: Tagged k BitLength -- ^ The size of a single block; the smallest unit on which the cipher operates.
24 encryptBlock :: k -> B.ByteString -> B.ByteString -- ^ encrypt data of size @n*blockSize@ where @n `elem` [0..]@ (ecb encryption)
25 decryptBlock :: k -> B.ByteString -> B.ByteString -- ^ decrypt data of size @n*blockSize@ where @n `elem` [0..]@ (ecb decryption)
26 buildKey :: B.ByteString -> Maybe k -- ^ smart constructor for keys from a bytestring.
27 keyLength :: Tagged k BitLength -- ^ length of the cryptographic key
28
29 -- * Modes of operation over strict bytestrings
30 -- | Electronic Cookbook (encryption)
31 ecb :: k -> B.ByteString -> B.ByteString
32 ecb = modeEcb'
33 -- | Electronic Cookbook (decryption)
34 unEcb :: k -> B.ByteString -> B.ByteString
35 unEcb = modeUnEcb'
36 -- | Cipherblock Chaining (encryption)
37 cbc :: k -> IV k -> B.ByteString -> (B.ByteString, IV k)
38 cbc = modeCbc'
39 -- | Cipherblock Chaining (decryption)
40 unCbc :: k -> IV k -> B.ByteString -> (B.ByteString, IV k)
41 unCbc = modeUnCbc'
42
43 -- | Counter (encryption)
44 ctr :: k -> IV k -> B.ByteString -> (B.ByteString, IV k)
45 ctr = modeCtr' incIV
46
47 -- | Counter (decryption)
48 unCtr :: k -> IV k -> B.ByteString -> (B.ByteString, IV k)
49 unCtr = modeUnCtr' incIV
50
51 -- | Counter (encryption)
52 ctrLazy :: k -> IV k -> L.ByteString -> (L.ByteString, IV k)
53 ctrLazy = modeCtr incIV
54
55 -- | Counter (decryption)
56 unCtrLazy :: k -> IV k -> L.ByteString -> (L.ByteString, IV k)
57 unCtrLazy = modeUnCtr incIV
58
59 -- | Ciphertext feedback (encryption)
60 cfb :: k -> IV k -> B.ByteString -> (B.ByteString, IV k)
61 cfb = modeCfb'
62 -- | Ciphertext feedback (decryption)
63 unCfb :: k -> IV k -> B.ByteString -> (B.ByteString, IV k)
64 unCfb = modeUnCfb'
65 -- | Output feedback (encryption)
66 ofb :: k -> IV k -> B.ByteString -> (B.ByteString, IV k)
67 ofb = modeOfb'
68
69 -- | Output feedback (decryption)
70 unOfb :: k -> IV k -> B.ByteString -> (B.ByteString, IV k)
71 unOfb = modeUnOfb'
72
73 -- |Cipher block chaining encryption for lazy bytestrings
74 cbcLazy :: k -> IV k -> L.ByteString -> (L.ByteString, IV k)
75 cbcLazy = modeCbc
76
77 -- |Cipher block chaining decryption for lazy bytestrings
78 unCbcLazy :: k -> IV k -> L.ByteString -> (L.ByteString, IV k)
79 unCbcLazy = modeUnCbc
80
81 -- |SIV (Synthetic IV) mode for lazy bytestrings. The third argument is
82 -- the optional list of bytestrings to be authenticated but not
83 -- encrypted As required by the specification this algorithm may
84 -- return nothing when certain constraints aren't met.
85 sivLazy :: k -> k -> [L.ByteString] -> L.ByteString -> Maybe L.ByteString
86 sivLazy = modeSiv
87
88 -- |SIV (Synthetic IV) for lazy bytestrings. The third argument is the
89 -- optional list of bytestrings to be authenticated but not encrypted.
90 -- As required by the specification this algorithm may return nothing
91 -- when authentication fails.
92 unSivLazy :: k -> k -> [L.ByteString] -> L.ByteString -> Maybe L.ByteString
93 unSivLazy = modeUnSiv
94
95 -- |SIV (Synthetic IV) mode for strict bytestrings. First argument is
96 -- the optional list of bytestrings to be authenticated but not
97 -- encrypted. As required by the specification this algorithm may
98 -- return nothing when certain constraints aren't met.
99 siv :: k -> k -> [B.ByteString] -> B.ByteString -> Maybe B.ByteString
100 siv = modeSiv'
101
102 -- |SIV (Synthetic IV) for strict bytestrings First argument is the
103 -- optional list of bytestrings to be authenticated but not encrypted
104 -- As required by the specification this algorithm may return nothing
105 -- when authentication fails.
106 unSiv :: k -> k -> [B.ByteString] -> B.ByteString -> Maybe B.ByteString
107 unSiv = modeUnSiv'
108
109 -- |Cook book mode - not really a mode at all. If you don't know what you're doing, don't use this mode^H^H^H^H library.
110 ecbLazy :: k -> L.ByteString -> L.ByteString
111 ecbLazy = modeEcb
112
113 -- |ECB decrypt, complementary to `ecb`.
114 unEcbLazy :: k -> L.ByteString -> L.ByteString
115 unEcbLazy = modeUnEcb
116
117 -- |Ciphertext feed-back encryption mode for lazy bytestrings (with s
118 -- == blockSize)
119 cfbLazy :: k -> IV k -> L.ByteString -> (L.ByteString, IV k)
120 cfbLazy = modeCfb
121
122 -- |Ciphertext feed-back decryption mode for lazy bytestrings (with s
123 -- == blockSize)
124 unCfbLazy :: k -> IV k -> L.ByteString -> (L.ByteString, IV k)
125 unCfbLazy = modeUnCfb
126
127 -- |Output feedback mode for lazy bytestrings
128 ofbLazy :: k -> IV k -> L.ByteString -> (L.ByteString, IV k)
129 ofbLazy = modeOfb
130
131 -- |Output feedback mode for lazy bytestrings
132 unOfbLazy :: k -> IV k -> L.ByteString -> (L.ByteString, IV k)
133 unOfbLazy = modeUnOfb