/
Conduit.hs
470 lines (387 loc) · 15.9 KB
/
Conduit.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
{-# LANGUAGE BangPatterns, CPP, RankNTypes #-}
-- | This module contains wrappers for cryptographic functions
-- using the @conduit@ package. Currently there is support for
-- hashes, HMACs and many modes of block ciphers (but not
-- everything @crypto-api@ supports has a counterpart here).
-- All functions on this package work in constant memory.
module Crypto.Conduit
( -- * Cryptographic hash functions
sinkHash
, hashFile
-- * Hash-based message authentication code (HMAC)
, sinkHmac
-- * Block ciphers
-- ** Electronic codebook mode (ECB)
, conduitEncryptEcb
, conduitDecryptEcb
-- ** Cipher-block chaining mode (CBC)
, conduitEncryptCbc
, conduitDecryptCbc
-- ** Cipher feedback mode (CFB)
, conduitEncryptCfb
, conduitDecryptCfb
-- ** Output feedback mode (OFB)
, conduitEncryptOfb
, conduitDecryptOfb
-- ** Counter mode (CTR)
, conduitEncryptCtr
, conduitDecryptCtr
, sourceCtr
-- ** Cipher-block chaining message authentication code (CBC-MAC)
, sinkCbcMac
-- * Internal helpers
, blocked
, BlockMode(..)
, Block(..)
) where
-- from base
import Control.Arrow (first)
import Data.Bits (xor)
-- from bytestring
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
-- from cereal
import qualified Data.Serialize as S
-- from crypto-api
import Crypto.Classes ((.::.))
import qualified Crypto.Classes as C
import qualified Crypto.HMAC as C
import qualified Crypto.Modes as C
--import qualified Crypto.Padding as C
--import qualified Crypto.Random as C
import qualified Crypto.Types as C
-- from conduit
import Data.Conduit hiding (Source, Sink, Conduit, Pipe)
import Data.Conduit.Binary (sourceFile)
-- from transformers
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Trans.Class (lift)
-- | Helper to get our return type.
getType :: Monad m => sink input m output -> output
getType = undefined
----------------------------------------------------------------------
-- | A 'Sink' that hashes a stream of 'B.ByteString'@s@ and
-- creates a digest @d@.
sinkHash :: (Monad m, C.Hash ctx d) => GLSink B.ByteString m d
sinkHash =
self
where
self = sink C.initialCtx
sink ctx = do
x <- getBlock AnyMultiple blockSize
case x of
Full bs ->
let !ctx' = C.updateCtx ctx bs
in sink ctx'
LastOne bs -> return $! C.finalize ctx bs
blockSize = (C.blockLength .::. getType self) `div` 8
getBlock :: Monad m => BlockMode -> C.ByteLength -> GLSink B.ByteString m Block
getBlock blockMode blockSize =
go id
where
go front = await >>= maybe (close front) (push front)
push front bs' =
case compare (B.length bs) blockSize of
LT -> go $ B.append bs
EQ -> return $ Full bs
GT -> leftover y >> return (Full x)
where
bs = front bs'
(x, y) = B.splitAt splitter bs
splitter =
case blockMode of
StrictBlockSize -> blockSize
AnyMultiple -> B.length bs - (B.length bs `mod` blockSize)
close front = return $ LastOne $ front B.empty
-- | Hashes the whole contents of the given file in constant
-- memory. This function is just a convenient wrapper around
-- 'sinkHash' defined as:
--
-- @
-- hashFile fp = 'liftIO' $ 'runResourceT' ('sourceFile' fp '$$' 'sinkHash')
-- @
hashFile :: (MonadIO m, C.Hash ctx d) => FilePath -> m d
hashFile fp = liftIO $ runResourceT (sourceFile fp $$ sinkHash)
----------------------------------------------------------------------
-- | A 'Sink' that computes the HMAC of a stream of
-- 'B.ByteString'@s@ and creates a digest @d@.
sinkHmac :: (Monad m, C.Hash ctx d) =>
#if OLD_CRYPTO_API
C.MacKey
#else
C.MacKey ctx d
#endif
-> GLSink B.ByteString m d
sinkHmac (C.MacKey key) =
sink
where
--------- Taken and modified from Crypto.HMAC:
key' =
case B.length key `compare` blockSize of
GT -> B.append
(S.encode $ C.hashFunc' d key)
(B.replicate (blockSize - outputSize) 0x00)
EQ -> key
LT -> B.append key (B.replicate (blockSize - B.length key) 0x00)
ko = B.map (`xor` 0x5c) key'
ki = B.map (`xor` 0x36) key'
---------
sink = go $ C.updateCtx C.initialCtx ki
go ctx = do
x <- getBlock AnyMultiple blockSize
case x of
Full bs ->
let !ctx' = C.updateCtx ctx bs
in go ctx'
LastOne bs ->
let !inner = C.finalize ctx bs `asTypeOf` d
!outer = C.hash $ L.fromChunks [ko, S.encode inner]
in return outer
d = getType sink
blockSize = (C.blockLength .::. d) `div` 8
outputSize = (C.outputLength .::. d) `div` 8
----------------------------------------------------------------------
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using ECB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise. (Note that
-- ECB has many undesirable cryptographic properties, please
-- avoid it if you don't know what you're doing.)
conduitEncryptEcb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> GLInfConduit B.ByteString m B.ByteString
conduitEncryptEcb k =
blockCipherConduit k
AnyMultiple
()
(\_ input -> ((), C.encryptBlock k input))
(\_ _ -> fail "conduitEncryptEcb: input has an incomplete final block.")
-- | A 'Conduit' that decrypts a stream of 'B.ByteString'@s@
-- using ECB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
conduitDecryptEcb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> GLInfConduit B.ByteString m B.ByteString
conduitDecryptEcb k =
blockCipherConduit k
AnyMultiple
()
(\_ input -> ((), C.decryptBlock k input))
(\_ _ -> fail "conduitDecryptEcb: input has an incomplete final block.")
----------------------------------------------------------------------
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using CBC mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
conduitEncryptCbc :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> GLInfConduit B.ByteString m B.ByteString
conduitEncryptCbc k iv =
blockCipherConduit k
StrictBlockSize
(S.encode iv)
(\iv' input -> let output = C.encryptBlock k (iv' `zwp` input)
in (output, output))
(\_ _ -> fail "conduitEncryptCbc: input has an incomplete final block.")
-- | A 'Conduit' that decrypts a stream of 'B.ByteString'@s@
-- using CBC mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
conduitDecryptCbc :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> GLInfConduit B.ByteString m B.ByteString
conduitDecryptCbc k iv =
blockCipherConduit k
StrictBlockSize
(S.encode iv)
(\iv' input -> let output = C.decryptBlock k input `zwp` iv'
in (input, output))
(\_ _ -> fail "conduitDecryptCbc: input has an incomplete final block.")
----------------------------------------------------------------------
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using CFB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
conduitEncryptCfb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> GLInfConduit B.ByteString m B.ByteString
conduitEncryptCfb k iv =
blockCipherConduit k
StrictBlockSize
(S.encode iv)
(\iv' input -> let output = C.encryptBlock k iv' `zwp` input
in (output, output))
(\_ _ -> fail "conduitEncryptCfb: input has an incomplete final block.")
-- | A 'Conduit' that decrypts a stream of 'B.ByteString'@s@
-- using CFB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
conduitDecryptCfb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> GLInfConduit B.ByteString m B.ByteString
conduitDecryptCfb k iv =
blockCipherConduit k
StrictBlockSize
(S.encode iv)
(\iv' input -> let output = C.encryptBlock k iv' `zwp` input
in (input, output))
(\_ _ -> fail "conduitDecryptCfb: input has an incomplete final block.")
----------------------------------------------------------------------
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using OFB mode. Expects the input length to be a multiple of
-- the block size of the cipher and fails otherwise.
conduitEncryptOfb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> GLInfConduit B.ByteString m B.ByteString
conduitEncryptOfb k iv =
blockCipherConduit k
StrictBlockSize
(S.encode iv)
(\iv' input -> let inter = C.encryptBlock k iv'
in (inter, inter `zwp` input))
(\_ _ -> fail "conduitEncryptOfb: input has an incomplete final block.")
-- | Synonym for 'conduitEncryptOfb', since for OFB mode both
-- encryption and decryption are the same.
conduitDecryptOfb :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> GLInfConduit B.ByteString m B.ByteString
conduitDecryptOfb = conduitEncryptOfb
----------------------------------------------------------------------
-- | A 'Conduit' that encrypts a stream of 'B.ByteString'@s@
-- using CTR mode. The input may have any length, even
-- non-multiples of the block size.
conduitEncryptCtr :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> (C.IV k -> C.IV k) -- ^ Increment counter ('C.incIV' is recommended)
-> GLInfConduit B.ByteString m B.ByteString
conduitEncryptCtr k iv incIV =
blockCipherConduit k
StrictBlockSize
iv
(\iv' input -> let !iv'' = incIV iv'
output = C.encryptBlock k (S.encode iv') `zwp` input
in (iv'', output))
(\iv' input -> let output = C.encryptBlock k (S.encode iv') `zwp` input
in return output)
-- | Synonym for 'conduitEncryptCtr', since for CTR mode both
-- encryption and decryption are the same.
conduitDecryptCtr :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> (C.IV k -> C.IV k) -- ^ Increment counter ('C.incIV' is recommended)
-> GLInfConduit B.ByteString m B.ByteString
conduitDecryptCtr = conduitEncryptCtr
-- | An infinite stream of bytes generated by a block cipher on
-- CTR mode.
sourceCtr :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> C.IV k -- ^ Initialization vector.
-> GSource m B.ByteString
sourceCtr k =
loop
where
loop iv =
yield block >> loop iv'
where
!iv' = C.incIV iv
block = C.encryptBlock k $ S.encode iv
----------------------------------------------------------------------
-- | A 'Sink' that computes the CBC-MAC of a stream of
-- 'B.ByteString'@s@ and creates a digest (already encoded in a
-- 'B.ByteString', since we're using a block cipher). Expects
-- the input length to be a multiple of the block size of the
-- cipher and fails otherwise. (Note that CBC-MAC is not secure
-- for variable-length messages.)
sinkCbcMac :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key.
-> GLSink B.ByteString m B.ByteString
sinkCbcMac k =
go $ B.replicate blockSize 0
where
go iv = do
x <- getBlock StrictBlockSize blockSize
case x of
Full input ->
let !iv' = C.encryptBlock k (iv `zwp` input)
in go iv'
LastOne input
| B.null input -> return iv
| otherwise -> lift $ fail "sinkCbcMac: input has an incomplete final block."
blockSize = (C.blockSize .::. k) `div` 8
----------------------------------------------------------------------
-- | A 'Conduit' that takes arbitrary 'B.ByteString'@s@ and
-- outputs 'Block'@s@. Each 'Full' block will have a length that
-- is multiple of the given block size (either exactly the block
-- size or a multiple of at least 1x the block size, depending on
-- the 'BlockMode'). All 'Block'@s@ beside the last one will be
-- 'Full'. The last block will always be 'LastOne' with less
-- bytes than the block size, possibly zero.
blocked :: Monad m =>
BlockMode
-> C.ByteLength -- ^ Block size
-> GInfConduit B.ByteString m Block
blocked mode blockSize = go B.empty
where
go x = awaitE >>= either (close x) (push x)
block = case mode of
StrictBlockSize -> blockStrict id
AnyMultiple -> blockAny
where
blockStrict front bs
| B.length bs < blockSize = (front [], bs)
| otherwise = blockStrict (front . (Full this :)) rest
where (this, rest) = B.splitAt blockSize bs
blockAny bs
| n >= 1 = first ((:[]) . Full) $ B.splitAt (n * blockSize) bs
| otherwise = ([], bs)
where n = B.length bs `div` blockSize
append bs1 bs2
| B.null bs1 = bs2
| otherwise = B.append bs1 bs2
push acc x = mapM_ yield blks >> go rest
where
(blks, rest) = block bs
bs = append acc x
close acc r = yield (LastOne acc) >> return r
-- | How 'Block's should be returned, either with strictly the
-- block size or with a multiple of at least 1x the block size.
data BlockMode = StrictBlockSize | AnyMultiple
deriving (Eq, Ord, Show, Enum)
-- | A block returned by 'blocked'.
data Block = Full B.ByteString | LastOne B.ByteString
deriving (Eq, Ord, Show)
-- | Constructs a 'Conduit' for a 'BlockCipher'.
blockCipherConduit :: (Monad m, C.BlockCipher k) =>
k -- ^ Cipher key (not used, just for getting block size).
-> BlockMode
-> s -- ^ Initial state.
-> (s -> B.ByteString -> (s, B.ByteString)) -- ^ Encrypt block.
-> (s -> B.ByteString -> m B.ByteString) -- ^ Final encryption.
-> GLInfConduit B.ByteString m B.ByteString
blockCipherConduit key mode initialState apply final =
go initialState
where
blockSize = (C.blockSize .::. key) `div` 8
go state = do
x <- getBlock mode blockSize
case x of
Full input -> do
let (!state', !output) = apply state input
yield output
go state'
LastOne input
| B.null input -> return () >> finish
| otherwise -> lift (final state input) >>= yield >> finish
finish = awaitE >>= either return (const finish)
-- | zipWith xor + pack
--
-- As a result of rewrite rules, this should automatically be
-- optimized (at compile time) to use the bytestring libraries
-- 'zipWith'' function.
--
-- Taken from crypto-api.
zwp :: B.ByteString -> B.ByteString -> B.ByteString
zwp a = B.pack . B.zipWith xor a
{-# INLINEABLE zwp #-}