File: CryptoCipherTypes.hs

package info (click to toggle)
haskell-hopenpgp 2.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,028 kB
  • sloc: haskell: 6,478; sh: 21; makefile: 6
file content (59 lines) | stat: -rw-r--r-- 2,105 bytes parent folder | download
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
-- CryptoCipherTypes.hs: shim for crypto-cipher-types stuff (current nettle)
-- Copyright © 2016-2024  Clint Adams
-- This software is released under the terms of the Expat license.
-- (See the LICENSE file).
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE UndecidableInstances #-}

module Codec.Encryption.OpenPGP.Internal.CryptoCipherTypes
  ( HOWrappedOldCCT(..)
  ) where

import Control.Error.Util (note)
import qualified "crypto-cipher-types" Crypto.Cipher.Types as OldCCT
import qualified "crypton" Crypto.Cipher.Types as CCT
import qualified Data.ByteString as B

import Codec.Encryption.OpenPGP.Internal.HOBlockCipher

newtype HOWrappedOldCCT a =
  HWOCCT a

instance OldCCT.BlockCipher cipher =>
         HOBlockCipher (HOWrappedOldCCT cipher) where
  cipherInit =
    fmap HWOCCT .
    either (const (Left "nettle invalid key")) (Right . OldCCT.cipherInit) .
    OldCCT.makeKey
  cipherName (HWOCCT c) = OldCCT.cipherName c
  cipherKeySize (HWOCCT c) = convertKSS . OldCCT.cipherKeySize $ c
  blockSize (HWOCCT c) = OldCCT.blockSize c
  cfbEncrypt (HWOCCT c) iv bs =
    hammerIV iv >>= \i -> return (OldCCT.cfbEncrypt c i bs)
  cfbDecrypt (HWOCCT c) iv bs =
    hammerIV iv >>= \i -> return (OldCCT.cfbDecrypt c i bs)
  paddedCfbEncrypt _ _ _ =
    Left "padding for nettle-encryption not implemented yet"
  paddedCfbDecrypt (HWOCCT cipher) iv ciphertext =
    hammerIV iv >>= \i ->
      return (B.take (B.length ciphertext) (OldCCT.cfbDecrypt cipher i padded))
    where
      padded =
        ciphertext `B.append`
        B.pack
          (replicate
             (OldCCT.blockSize cipher -
              (B.length ciphertext `mod` OldCCT.blockSize cipher))
             0)

convertKSS :: OldCCT.KeySizeSpecifier -> CCT.KeySizeSpecifier
convertKSS (OldCCT.KeySizeRange a b) = CCT.KeySizeRange a b
convertKSS (OldCCT.KeySizeEnum as) = CCT.KeySizeEnum as
convertKSS (OldCCT.KeySizeFixed a) = CCT.KeySizeFixed a

hammerIV ::
     OldCCT.BlockCipher cipher
  => B.ByteString
  -> Either String (OldCCT.IV cipher)
hammerIV = note "nettle bad IV" . OldCCT.makeIV