{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE MultiWayIf #-}
module Codec.Crypto.RSA.Pure(
RSAError(..)
, HashInfo(..)
, PrivateKey(..)
, PublicKey(..)
, generateKeyPair
, encrypt
, encryptOAEP
, encryptPKCS
, decrypt
, decryptOAEP
, decryptPKCS
, sign
, verify
, MGF
, generateMGF1
, rsaes_oaep_encrypt
, rsaes_oaep_decrypt
, rsaes_pkcs1_v1_5_encrypt
, rsaes_pkcs1_v1_5_decrypt
, rsassa_pkcs1_v1_5_sign
, rsassa_pkcs1_v1_5_verify
, hashSHA1
, hashSHA224, hashSHA256, hashSHA384, hashSHA512
, largeRandomPrime
, generatePQ
, chunkify
, os2ip, i2osp
, rsa_dp, rsa_ep
, rsa_vp1, rsa_sp1
, modular_inverse
, modular_exponentiation
, randomBS, randomNZBS
)
where
import Control.Exception
import Control.Monad
import Crypto.Random
import Crypto.Types.PubKey.RSA
import Data.Binary
import Data.Binary.Get
import Data.Binary.Put
import Data.Bits
import Data.ByteString.Lazy(ByteString)
import qualified Data.ByteString.Lazy as BS
import Data.Digest.Pure.SHA
import Data.Int
import Data.Typeable
data RSAError = RSAError String
| RSAKeySizeTooSmall
| RSAIntegerTooLargeToPack
| RSAMessageRepOutOfRange
| RSACipherRepOutOfRange
| RSAMessageTooShort
| RSAMessageTooLong
| RSAMaskTooLong
| RSAIncorrectSigSize
| RSAIncorrectMsgSize
| RSADecryptionError
| RSAGenError GenError
deriving (RSAError -> RSAError -> Bool
(RSAError -> RSAError -> Bool)
-> (RSAError -> RSAError -> Bool) -> Eq RSAError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RSAError -> RSAError -> Bool
== :: RSAError -> RSAError -> Bool
$c/= :: RSAError -> RSAError -> Bool
/= :: RSAError -> RSAError -> Bool
Eq, Int -> RSAError -> ShowS
[RSAError] -> ShowS
RSAError -> String
(Int -> RSAError -> ShowS)
-> (RSAError -> String) -> ([RSAError] -> ShowS) -> Show RSAError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RSAError -> ShowS
showsPrec :: Int -> RSAError -> ShowS
$cshow :: RSAError -> String
show :: RSAError -> String
$cshowList :: [RSAError] -> ShowS
showList :: [RSAError] -> ShowS
Show, Typeable)
instance Exception RSAError
data HashInfo = HashInfo {
HashInfo -> ByteString
algorithmIdent :: ByteString
, HashInfo -> ByteString -> ByteString
hashFunction :: ByteString -> ByteString
}
instance Show SystemRandom where
show :: SystemRandom -> String
show SystemRandom
_ = String
"SystemRandom"
class RSAKey a where
genKeySize :: a -> Int
instance RSAKey PublicKey where
genKeySize :: PublicKey -> Int
genKeySize = PublicKey -> Int
public_size
instance RSAKey PrivateKey where
genKeySize :: PrivateKey -> Int
genKeySize = PrivateKey -> Int
private_size
instance Binary PublicKey where
put :: PublicKey -> Put
put PublicKey
pk = do ByteString
sizeBS <- Either RSAError ByteString -> PutM ByteString
forall (m :: * -> *) a b. (Monad m, Show a) => Either a b -> m b
failOnError (Int -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp (PublicKey -> Int
public_size PublicKey
pk) Int
8)
ByteString
nBS <- Either RSAError ByteString -> PutM ByteString
forall (m :: * -> *) a b. (Monad m, Show a) => Either a b -> m b
failOnError (Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp (PublicKey -> Integer
public_n PublicKey
pk) (PublicKey -> Int
public_size PublicKey
pk))
ByteString -> Put
putLazyByteString ByteString
sizeBS
ByteString -> Put
putLazyByteString ByteString
nBS
get :: Get PublicKey
get = do Int64
len <- (Integer -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Int64)
-> (ByteString -> Integer) -> ByteString -> Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Integer
os2ip) (ByteString -> Int64) -> Get ByteString -> Get Int64
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int64 -> Get ByteString
getLazyByteString Int64
8
Integer
n <- ByteString -> Integer
os2ip (ByteString -> Integer) -> Get ByteString -> Get Integer
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int64 -> Get ByteString
getLazyByteString Int64
len
PublicKey -> Get PublicKey
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Integer -> Integer -> PublicKey
PublicKey (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
len) Integer
n Integer
65537)
instance Binary PrivateKey where
put :: PrivateKey -> Put
put PrivateKey
pk = do PublicKey -> Put
forall t. Binary t => t -> Put
put (PrivateKey -> PublicKey
private_pub PrivateKey
pk)
ByteString
dBS <- Either RSAError ByteString -> PutM ByteString
forall (m :: * -> *) a b. (Monad m, Show a) => Either a b -> m b
failOnError (Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp (PrivateKey -> Integer
private_d PrivateKey
pk) (PublicKey -> Int
public_size (PrivateKey -> PublicKey
private_pub PrivateKey
pk)))
ByteString -> Put
putLazyByteString ByteString
dBS
get :: Get PrivateKey
get = do PublicKey
pub <- Get PublicKey
forall t. Binary t => Get t
get
Integer
d <- ByteString -> Integer
os2ip (ByteString -> Integer) -> Get ByteString -> Get Integer
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int64 -> Get ByteString
getLazyByteString (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PublicKey -> Int
public_size PublicKey
pub))
PrivateKey -> Get PrivateKey
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (PublicKey
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> PrivateKey
PrivateKey PublicKey
pub Integer
d Integer
0 Integer
0 Integer
0 Integer
0 Integer
0)
failOnError :: (Monad m, Show a) => Either a b -> m b
failOnError :: forall (m :: * -> *) a b. (Monad m, Show a) => Either a b -> m b
failOnError (Left a
e) = String -> m b
forall a. HasCallStack => String -> a
error (a -> String
forall a. Show a => a -> String
show a
e)
failOnError (Right b
b) = b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
generateKeyPair :: CryptoRandomGen g =>
g -> Int ->
Either RSAError (PublicKey, PrivateKey, g)
generateKeyPair :: forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (PublicKey, PrivateKey, g)
generateKeyPair g
g Int
sizeBits = do
let keyLength :: Int
keyLength = Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
sizeBits Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8)
(Integer
p, Integer
q, g
g') <- g -> Int -> Either RSAError (Integer, Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, Integer, g)
generatePQ g
g Int
keyLength
let n :: Integer
n = Integer
p Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
q
phi :: Integer
phi = (Integer
p Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer
q Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1)
e :: Integer
e = Integer
65537
d :: Integer
d = Integer -> Integer -> Integer
modular_inverse Integer
e Integer
phi
let publicKey :: PublicKey
publicKey = Int -> Integer -> Integer -> PublicKey
PublicKey Int
keyLength Integer
n Integer
e
privateKey :: PrivateKey
privateKey = PublicKey
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> PrivateKey
PrivateKey PublicKey
publicKey Integer
d Integer
p Integer
q Integer
0 Integer
0 Integer
0
(PublicKey, PrivateKey, g)
-> Either RSAError (PublicKey, PrivateKey, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (PublicKey
publicKey, PrivateKey
privateKey, g
g')
sign :: PrivateKey -> ByteString -> Either RSAError ByteString
sign :: PrivateKey -> ByteString -> Either RSAError ByteString
sign = HashInfo -> PrivateKey -> ByteString -> Either RSAError ByteString
rsassa_pkcs1_v1_5_sign HashInfo
hashSHA256
verify :: PublicKey ->
ByteString ->
ByteString ->
Either RSAError Bool
verify :: PublicKey -> ByteString -> ByteString -> Either RSAError Bool
verify = HashInfo
-> PublicKey -> ByteString -> ByteString -> Either RSAError Bool
rsassa_pkcs1_v1_5_verify HashInfo
hashSHA256
encrypt :: CryptoRandomGen g =>
g -> PublicKey -> ByteString ->
Either RSAError (ByteString, g)
encrypt :: forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
encrypt g
g PublicKey
k ByteString
m = g
-> (ByteString -> ByteString)
-> MGF
-> ByteString
-> PublicKey
-> ByteString
-> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g
-> (ByteString -> ByteString)
-> MGF
-> ByteString
-> PublicKey
-> ByteString
-> Either RSAError (ByteString, g)
encryptOAEP g
g ByteString -> ByteString
sha256' ((ByteString -> ByteString) -> MGF
generateMGF1 ByteString -> ByteString
sha256') ByteString
BS.empty PublicKey
k ByteString
m
where sha256' :: ByteString -> ByteString
sha256' = Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA256State -> ByteString)
-> (ByteString -> Digest SHA256State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256State
sha256
encryptOAEP :: CryptoRandomGen g =>
g ->
(ByteString -> ByteString) ->
MGF ->
ByteString ->
PublicKey ->
ByteString ->
Either RSAError (ByteString, g)
encryptOAEP :: forall g.
CryptoRandomGen g =>
g
-> (ByteString -> ByteString)
-> MGF
-> ByteString
-> PublicKey
-> ByteString
-> Either RSAError (ByteString, g)
encryptOAEP g
g ByteString -> ByteString
hash MGF
mgf ByteString
l PublicKey
k ByteString
m =
do Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Int
keySize Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
hashLength) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSAKeySizeTooSmall
let chunks :: [ByteString]
chunks = PublicKey
-> (ByteString -> ByteString) -> ByteString -> [ByteString]
forall k.
RSAKey k =>
k -> (ByteString -> ByteString) -> ByteString -> [ByteString]
chunkBSForOAEP PublicKey
k ByteString -> ByteString
hash ByteString
m
([ByteString]
chunks', g
g') <- g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
forall g.
CryptoRandomGen g =>
g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
mapM' g
g [ByteString]
chunks (\ g
x -> g
-> (ByteString -> ByteString)
-> MGF
-> PublicKey
-> ByteString
-> ByteString
-> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g
-> (ByteString -> ByteString)
-> MGF
-> PublicKey
-> ByteString
-> ByteString
-> Either RSAError (ByteString, g)
rsaes_oaep_encrypt g
x ByteString -> ByteString
hash MGF
mgf PublicKey
k ByteString
l)
(ByteString, g) -> Either RSAError (ByteString, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BS.concat [ByteString]
chunks', g
g')
where
keySize :: Int
keySize = PublicKey -> Int
public_size PublicKey
k
hashLength :: Int
hashLength = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty))
encryptPKCS :: CryptoRandomGen g =>
g -> PublicKey -> ByteString ->
Either RSAError (ByteString, g)
encryptPKCS :: forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
encryptPKCS g
g PublicKey
k ByteString
m =
do let chunks :: [ByteString]
chunks = PublicKey -> ByteString -> [ByteString]
forall k. RSAKey k => k -> ByteString -> [ByteString]
chunkBSForPKCS PublicKey
k ByteString
m
([ByteString]
chunks', g
g') <- g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
forall g.
CryptoRandomGen g =>
g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
mapM' g
g [ByteString]
chunks (\ g
x -> g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
rsaes_pkcs1_v1_5_encrypt g
x PublicKey
k)
(ByteString, g) -> Either RSAError (ByteString, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BS.concat [ByteString]
chunks', g
g')
mapM' :: CryptoRandomGen g =>
g -> [ByteString] ->
(g -> ByteString -> Either RSAError (ByteString, g)) ->
Either RSAError ([ByteString], g)
mapM' :: forall g.
CryptoRandomGen g =>
g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
mapM' g
g [] g -> ByteString -> Either RSAError (ByteString, g)
_ = ([ByteString], g) -> Either RSAError ([ByteString], g)
forall a b. b -> Either a b
Right ([], g
g)
mapM' g
g (ByteString
x:[ByteString]
rest) g -> ByteString -> Either RSAError (ByteString, g)
f =
do (ByteString
x', g
g') <- g -> ByteString -> Either RSAError (ByteString, g)
f g
g ByteString
x
([ByteString]
rest', g
g'') <- g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
forall g.
CryptoRandomGen g =>
g
-> [ByteString]
-> (g -> ByteString -> Either RSAError (ByteString, g))
-> Either RSAError ([ByteString], g)
mapM' g
g' [ByteString]
rest g -> ByteString -> Either RSAError (ByteString, g)
f
([ByteString], g) -> Either RSAError ([ByteString], g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
x'ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
rest', g
g'')
decrypt :: PrivateKey -> ByteString -> Either RSAError ByteString
decrypt :: PrivateKey -> ByteString -> Either RSAError ByteString
decrypt PrivateKey
k ByteString
m = (ByteString -> ByteString)
-> MGF
-> ByteString
-> PrivateKey
-> ByteString
-> Either RSAError ByteString
decryptOAEP ByteString -> ByteString
sha256' ((ByteString -> ByteString) -> MGF
generateMGF1 ByteString -> ByteString
sha256') ByteString
BS.empty PrivateKey
k ByteString
m
where sha256' :: ByteString -> ByteString
sha256' = Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA256State -> ByteString)
-> (ByteString -> Digest SHA256State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256State
sha256
decryptOAEP :: (ByteString -> ByteString) ->
MGF ->
ByteString ->
PrivateKey ->
ByteString ->
Either RSAError ByteString
decryptOAEP :: (ByteString -> ByteString)
-> MGF
-> ByteString
-> PrivateKey
-> ByteString
-> Either RSAError ByteString
decryptOAEP ByteString -> ByteString
hash MGF
mgf ByteString
l PrivateKey
k ByteString
m =
do let chunks :: [ByteString]
chunks = ByteString -> Int64 -> [ByteString]
chunkify ByteString
m (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PrivateKey -> Int
private_size PrivateKey
k))
[ByteString]
chunks' <- [ByteString]
-> (ByteString -> Either RSAError ByteString)
-> Either RSAError [ByteString]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [ByteString]
chunks ((ByteString -> ByteString)
-> MGF
-> PrivateKey
-> ByteString
-> ByteString
-> Either RSAError ByteString
rsaes_oaep_decrypt ByteString -> ByteString
hash MGF
mgf PrivateKey
k ByteString
l)
ByteString -> Either RSAError ByteString
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BS.concat [ByteString]
chunks')
decryptPKCS :: PrivateKey -> ByteString -> Either RSAError ByteString
decryptPKCS :: PrivateKey -> ByteString -> Either RSAError ByteString
decryptPKCS PrivateKey
k ByteString
m =
do let chunks :: [ByteString]
chunks = ByteString -> Int64 -> [ByteString]
chunkify ByteString
m (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PrivateKey -> Int
private_size PrivateKey
k))
[ByteString]
chunks' <- [ByteString]
-> (ByteString -> Either RSAError ByteString)
-> Either RSAError [ByteString]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [ByteString]
chunks (PrivateKey -> ByteString -> Either RSAError ByteString
rsaes_pkcs1_v1_5_decrypt PrivateKey
k)
ByteString -> Either RSAError ByteString
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BS.concat [ByteString]
chunks')
chunkBSForOAEP :: RSAKey k =>
k ->
(ByteString -> ByteString) ->
ByteString ->
[ByteString]
chunkBSForOAEP :: forall k.
RSAKey k =>
k -> (ByteString -> ByteString) -> ByteString -> [ByteString]
chunkBSForOAEP k
k ByteString -> ByteString
hash ByteString
bs = ByteString -> Int64 -> [ByteString]
chunkify ByteString
bs Int64
chunkSize
where
chunkSize :: Int64
chunkSize = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (k -> Int
forall a. RSAKey a => a -> Int
genKeySize k
k) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- (Int64
2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
hashLen) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
2
hashLen :: Int64
hashLen = ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty)
chunkBSForPKCS :: RSAKey k => k -> ByteString -> [ByteString]
chunkBSForPKCS :: forall k. RSAKey k => k -> ByteString -> [ByteString]
chunkBSForPKCS k
k ByteString
bstr = ByteString -> Int64 -> [ByteString]
chunkify ByteString
bstr (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (k -> Int
forall a. RSAKey a => a -> Int
genKeySize k
k) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
11)
chunkify :: ByteString -> Int64 -> [ByteString]
chunkify :: ByteString -> Int64 -> [ByteString]
chunkify ByteString
bs Int64
size
| ByteString -> Int64
BS.length ByteString
bs Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int64
0 = []
| Bool
otherwise = let (ByteString
start, ByteString
end) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt Int64
size ByteString
bs
in ByteString
start ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> Int64 -> [ByteString]
chunkify ByteString
end Int64
size
rsaes_oaep_encrypt :: CryptoRandomGen g =>
g ->
(ByteString->ByteString) ->
MGF ->
PublicKey ->
ByteString ->
ByteString ->
Either RSAError (ByteString, g)
rsaes_oaep_encrypt :: forall g.
CryptoRandomGen g =>
g
-> (ByteString -> ByteString)
-> MGF
-> PublicKey
-> ByteString
-> ByteString
-> Either RSAError (ByteString, g)
rsaes_oaep_encrypt g
g ByteString -> ByteString
hash MGF
mgf PublicKey
k ByteString
l ByteString
m =
do let hashLength :: Int
hashLength = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty))
keySize :: Int
keySize = PublicKey -> Int
public_size PublicKey
k
msgLength :: Int
msgLength = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
m)
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
msgLength Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> (Int
keySize Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
hashLength) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$
RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSAMessageTooLong
let lHash :: ByteString
lHash = ByteString -> ByteString
hash ByteString
l
let zeros :: ByteString
zeros = Word8 -> ByteString
BS.repeat Word8
0
numZeros :: Int
numZeros = Int
keySize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
msgLength Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
hashLength) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2
ps :: ByteString
ps = Int64 -> ByteString -> ByteString
BS.take (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
numZeros) ByteString
zeros
let db :: ByteString
db = [ByteString] -> ByteString
BS.concat [ByteString
lHash, ByteString
ps, Word8 -> ByteString
BS.singleton Word8
1, ByteString
m]
(ByteString
seed, g
g') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
g Int
hashLength
ByteString
dbMask <- MGF
mgf ByteString
seed (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
keySize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hashLength Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
let maskedDB :: ByteString
maskedDB = ByteString
db ByteString -> ByteString -> ByteString
`xorBS` ByteString
dbMask
ByteString
seedMask <- MGF
mgf ByteString
maskedDB (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
hashLength)
let maskedSeed :: ByteString
maskedSeed = ByteString
seed ByteString -> ByteString -> ByteString
`xorBS` ByteString
seedMask
let em :: ByteString
em = [ByteString] -> ByteString
BS.concat [Word8 -> ByteString
BS.singleton Word8
0, ByteString
maskedSeed, ByteString
maskedDB]
let m_i :: Integer
m_i = ByteString -> Integer
os2ip ByteString
em
Integer
c_i <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_ep (PublicKey -> Integer
public_n PublicKey
k) (PublicKey -> Integer
public_e PublicKey
k) Integer
m_i
ByteString
c <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
c_i (PublicKey -> Int
public_size PublicKey
k)
(ByteString, g) -> Either RSAError (ByteString, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
c, g
g')
rsaes_oaep_decrypt :: (ByteString->ByteString) ->
MGF ->
PrivateKey ->
ByteString ->
ByteString ->
Either RSAError ByteString
rsaes_oaep_decrypt :: (ByteString -> ByteString)
-> MGF
-> PrivateKey
-> ByteString
-> ByteString
-> Either RSAError ByteString
rsaes_oaep_decrypt ByteString -> ByteString
hash MGF
mgf PrivateKey
k ByteString
l ByteString
c =
do let hashLength :: Int64
hashLength = ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty)
keySize :: Int
keySize = PrivateKey -> Int
private_size PrivateKey
k
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Int64
BS.length ByteString
c Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
keySize) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$
RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
keySize Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= ((Int64
2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
hashLength) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
2)) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$
RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
let c_ip :: Integer
c_ip = ByteString -> Integer
os2ip ByteString
c
Integer
m_ip <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_dp (PrivateKey -> Integer
private_n PrivateKey
k) (PrivateKey -> Integer
private_d PrivateKey
k) Integer
c_ip
ByteString
em <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
m_ip Int
keySize
let lHash :: ByteString
lHash = ByteString -> ByteString
hash ByteString
l
let (ByteString
y, ByteString
seed_db) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt Int64
1 ByteString
em
(ByteString
maskedSeed, ByteString
maskedDB) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt (Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
hashLength) ByteString
seed_db
ByteString
seedMask <- MGF
mgf ByteString
maskedDB Int64
hashLength
let seed :: ByteString
seed = ByteString
maskedSeed ByteString -> ByteString -> ByteString
`xorBS` ByteString
seedMask
ByteString
dbMask <- MGF
mgf ByteString
seed (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
keySize Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
hashLength Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
1)
let db :: ByteString
db = ByteString
maskedDB ByteString -> ByteString -> ByteString
`xorBS` ByteString
dbMask
let (ByteString
lHash', ByteString
ps_o_m) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt Int64
hashLength ByteString
db
(ByteString
ps, ByteString
o_m) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BS.span (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0) ByteString
ps_o_m
(ByteString
o, ByteString
m) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt Int64
1 ByteString
o_m
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> [Word8]
BS.unpack ByteString
o [Word8] -> [Word8] -> Bool
forall a. Eq a => a -> a -> Bool
== [Word8
1]) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString
lHash' ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
lHash) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> [Word8]
BS.unpack ByteString
y [Word8] -> [Word8] -> Bool
forall a. Eq a => a -> a -> Bool
== [Word8
0]) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Word8 -> Bool) -> ByteString -> Bool
BS.all (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0) ByteString
ps) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
ByteString -> Either RSAError ByteString
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
m
rsaes_pkcs1_v1_5_encrypt :: CryptoRandomGen g =>
g ->
PublicKey ->
ByteString ->
Either RSAError (ByteString, g)
rsaes_pkcs1_v1_5_encrypt :: forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
rsaes_pkcs1_v1_5_encrypt g
g PublicKey
k ByteString
m =
do Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
m) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= (PublicKey -> Int
public_size PublicKey
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
11)) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$
RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSAIncorrectMsgSize
(ByteString
ps, g
g') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomNZBS g
g (PublicKey -> Int
public_size PublicKey
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
m) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
3)
let em :: ByteString
em = [ByteString] -> ByteString
BS.concat [Word8 -> ByteString
BS.singleton Word8
0, Word8 -> ByteString
BS.singleton Word8
2, ByteString
ps, Word8 -> ByteString
BS.singleton Word8
0, ByteString
m]
let m' :: Integer
m' = ByteString -> Integer
os2ip ByteString
em
Integer
c_i <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_ep (PublicKey -> Integer
public_n PublicKey
k) (PublicKey -> Integer
public_e PublicKey
k) Integer
m'
ByteString
res <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
c_i (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PublicKey -> Int
public_size PublicKey
k))
(ByteString, g) -> Either RSAError (ByteString, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
res, g
g')
rsaes_pkcs1_v1_5_decrypt :: PrivateKey -> ByteString ->
Either RSAError ByteString
rsaes_pkcs1_v1_5_decrypt :: PrivateKey -> ByteString -> Either RSAError ByteString
rsaes_pkcs1_v1_5_decrypt PrivateKey
k ByteString
c =
do Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
c) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== PrivateKey -> Int
private_size PrivateKey
k) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$
RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSAIncorrectMsgSize
let c_i :: Integer
c_i = ByteString -> Integer
os2ip ByteString
c
Integer
m_i <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_dp (PrivateKey -> Integer
private_n PrivateKey
k) (PrivateKey -> Integer
private_d PrivateKey
k) Integer
c_i
ByteString
em <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
m_i (PrivateKey -> Int
private_size PrivateKey
k)
let (ByteString
zt, ByteString
ps_z_m) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt Int64
2 ByteString
em
(ByteString
ps, ByteString
z_m) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BS.span (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0) ByteString
ps_z_m
(ByteString
z, ByteString
m) = Int64 -> ByteString -> (ByteString, ByteString)
BS.splitAt Int64
1 ByteString
z_m
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> [Word8]
BS.unpack ByteString
zt [Word8] -> [Word8] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Word8
0,Word8
2]) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> [Word8]
BS.unpack ByteString
z [Word8] -> [Word8] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Word8
0]) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
Bool -> Either RSAError () -> Either RSAError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int64
BS.length ByteString
ps Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Int64
8 ) (Either RSAError () -> Either RSAError ())
-> Either RSAError () -> Either RSAError ()
forall a b. (a -> b) -> a -> b
$ RSAError -> Either RSAError ()
forall a b. a -> Either a b
Left RSAError
RSADecryptionError
ByteString -> Either RSAError ByteString
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
m
rsassa_pkcs1_v1_5_sign :: HashInfo ->
PrivateKey ->
ByteString ->
Either RSAError ByteString
rsassa_pkcs1_v1_5_sign :: HashInfo -> PrivateKey -> ByteString -> Either RSAError ByteString
rsassa_pkcs1_v1_5_sign HashInfo
hi PrivateKey
k ByteString
m =
do ByteString
em <- HashInfo -> ByteString -> Int -> Either RSAError ByteString
emsa_pkcs1_v1_5_encode HashInfo
hi ByteString
m (PrivateKey -> Int
private_size PrivateKey
k)
let m_i :: Integer
m_i = ByteString -> Integer
os2ip ByteString
em
Integer
s <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_sp1 (PrivateKey -> Integer
private_n PrivateKey
k) (PrivateKey -> Integer
private_d PrivateKey
k) Integer
m_i
ByteString
sig <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
s (PrivateKey -> Int
private_size PrivateKey
k)
ByteString -> Either RSAError ByteString
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
sig
rsassa_pkcs1_v1_5_verify :: HashInfo ->
PublicKey ->
ByteString ->
ByteString ->
Either RSAError Bool
rsassa_pkcs1_v1_5_verify :: HashInfo
-> PublicKey -> ByteString -> ByteString -> Either RSAError Bool
rsassa_pkcs1_v1_5_verify HashInfo
hi PublicKey
k ByteString
m ByteString
s
| ByteString -> Int64
BS.length ByteString
s Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PublicKey -> Int
public_size PublicKey
k) = RSAError -> Either RSAError Bool
forall a b. a -> Either a b
Left RSAError
RSAIncorrectSigSize
| Bool
otherwise =
do let s_i :: Integer
s_i = ByteString -> Integer
os2ip ByteString
s
Integer
m_i <- Integer -> Integer -> Integer -> Either RSAError Integer
rsa_vp1 (PublicKey -> Integer
public_n PublicKey
k) (PublicKey -> Integer
public_e PublicKey
k) Integer
s_i
ByteString
em <- Integer -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Integer
m_i (PublicKey -> Int
public_size PublicKey
k)
ByteString
em' <- HashInfo -> ByteString -> Int -> Either RSAError ByteString
emsa_pkcs1_v1_5_encode HashInfo
hi ByteString
m (PublicKey -> Int
public_size PublicKey
k)
Bool -> Either RSAError Bool
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
em ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
em')
type MGF = ByteString -> Int64 -> Either RSAError ByteString
generateMGF1 :: (ByteString -> ByteString) -> MGF
generateMGF1 :: (ByteString -> ByteString) -> MGF
generateMGF1 ByteString -> ByteString
hash ByteString
mgfSeed Int64
maskLen
| ByteString -> Int64
BS.length ByteString
mgfSeed Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> ((Int64
2 Int64 -> Integer -> Int64
forall a b. (Num a, Integral b) => a -> b -> a
^ (Integer
32::Integer)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
hLen) = RSAError -> Either RSAError ByteString
forall a b. a -> Either a b
Left RSAError
RSAMaskTooLong
| Bool
otherwise = MGF
loop ByteString
BS.empty Int64
0
where
hLen :: Int64
hLen = ByteString -> Int64
BS.length (ByteString -> ByteString
hash ByteString
BS.empty)
endCounter :: Int64
endCounter = (Int64
maskLen Int64 -> Int64 -> Int64
forall a. Integral a => a -> a -> a
`divCeil` Int64
hLen) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
1
loop :: MGF
loop ByteString
t Int64
counter
| Int64
counter Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64
endCounter = ByteString -> Either RSAError ByteString
forall a b. b -> Either a b
Right (Int64 -> ByteString -> ByteString
BS.take Int64
maskLen ByteString
t)
| Bool
otherwise = do ByteString
c <- Int64 -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp Int64
counter Int
4
let bs :: ByteString
bs = ByteString
mgfSeed ByteString -> ByteString -> ByteString
`BS.append` ByteString
c
t' :: ByteString
t' = ByteString
t ByteString -> ByteString -> ByteString
`BS.append` ByteString -> ByteString
hash ByteString
bs
MGF
loop ByteString
t' (Int64
counter Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
1)
i2osp :: Integral a => a -> Int -> Either RSAError ByteString
i2osp :: forall a. Integral a => a -> Int -> Either RSAError ByteString
i2osp a
x Int
len | Bool
isTooLarge = RSAError -> Either RSAError ByteString
forall a b. a -> Either a b
Left RSAError
RSAIntegerTooLargeToPack
| Bool
otherwise = ByteString -> Either RSAError ByteString
forall a b. b -> Either a b
Right (ByteString
padding ByteString -> ByteString -> ByteString
`BS.append` ByteString
digits)
where
isTooLarge :: Bool
isTooLarge = (a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
x :: Integer) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>=
(Integer
256 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len :: Integer))
padding :: ByteString
padding = Int64 -> Word8 -> ByteString
BS.replicate (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- ByteString -> Int64
BS.length ByteString
digits) Word8
0
digits :: ByteString
digits = ByteString -> ByteString
BS.reverse ((a -> Maybe (Word8, a)) -> a -> ByteString
forall a. (a -> Maybe (Word8, a)) -> a -> ByteString
BS.unfoldr a -> Maybe (Word8, a)
forall {b} {a}. (Integral b, Num a) => b -> Maybe (a, b)
digitize a
x)
digitize :: b -> Maybe (a, b)
digitize b
0 = Maybe (a, b)
forall a. Maybe a
Nothing
digitize b
v = let (b
q, b
r) = b -> b -> (b, b)
forall a. Integral a => a -> a -> (a, a)
divMod b
v b
256
in (a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (b -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
r, b
q)
os2ip :: ByteString -> Integer
os2ip :: ByteString -> Integer
os2ip = (Integer -> Word8 -> Integer) -> Integer -> ByteString -> Integer
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
BS.foldl (\ Integer
a Word8
b -> (Integer
256 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
a) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Word8 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b)) Integer
0
rsa_ep :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_ep :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_ep Integer
n Integer
_ Integer
m | (Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0) Bool -> Bool -> Bool
|| (Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
n) = RSAError -> Either RSAError Integer
forall a b. a -> Either a b
Left RSAError
RSAMessageRepOutOfRange
rsa_ep Integer
n Integer
e Integer
m = Integer -> Either RSAError Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
m Integer
e Integer
n)
rsa_dp :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_dp :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_dp Integer
n Integer
_ Integer
c | (Integer
c Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0) Bool -> Bool -> Bool
|| (Integer
c Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
n) = RSAError -> Either RSAError Integer
forall a b. a -> Either a b
Left RSAError
RSACipherRepOutOfRange
rsa_dp Integer
n Integer
d Integer
c = Integer -> Either RSAError Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
c Integer
d Integer
n)
rsa_sp1 :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_sp1 :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_sp1 Integer
n Integer
_ Integer
m | (Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0) Bool -> Bool -> Bool
|| (Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
n) = RSAError -> Either RSAError Integer
forall a b. a -> Either a b
Left RSAError
RSAMessageRepOutOfRange
rsa_sp1 Integer
n Integer
d Integer
m = Integer -> Either RSAError Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
m Integer
d Integer
n)
rsa_vp1 :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_vp1 :: Integer -> Integer -> Integer -> Either RSAError Integer
rsa_vp1 Integer
n Integer
_ Integer
s | (Integer
s Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0) Bool -> Bool -> Bool
|| (Integer
s Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
n) = RSAError -> Either RSAError Integer
forall a b. a -> Either a b
Left RSAError
RSACipherRepOutOfRange
rsa_vp1 Integer
n Integer
e Integer
s = Integer -> Either RSAError Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
s Integer
e Integer
n)
emsa_pkcs1_v1_5_encode :: HashInfo -> ByteString -> Int ->
Either RSAError ByteString
emsa_pkcs1_v1_5_encode :: HashInfo -> ByteString -> Int -> Either RSAError ByteString
emsa_pkcs1_v1_5_encode (HashInfo ByteString
ident ByteString -> ByteString
hash) ByteString
m Int
emLen
| Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
emLen Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< (Int64
tLen Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
1) = RSAError -> Either RSAError ByteString
forall a b. a -> Either a b
Left RSAError
RSAMessageTooShort
| Bool
otherwise = ByteString -> Either RSAError ByteString
forall a b. b -> Either a b
Right ByteString
em
where
h :: ByteString
h = ByteString -> ByteString
hash ByteString
m
t :: ByteString
t = ByteString
ident ByteString -> ByteString -> ByteString
`BS.append` ByteString
h
tLen :: Int64
tLen = ByteString -> Int64
BS.length ByteString
t
ps :: ByteString
ps = Int64 -> Word8 -> ByteString
BS.replicate (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
emLen Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
tLen Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
3) Word8
0xFF
em :: ByteString
em = [ByteString] -> ByteString
BS.concat [Word8 -> ByteString
BS.singleton Word8
0x00,Word8 -> ByteString
BS.singleton Word8
0x01,ByteString
ps,Word8 -> ByteString
BS.singleton Word8
0x00,ByteString
t]
xorBS :: ByteString -> ByteString -> ByteString
xorBS :: ByteString -> ByteString -> ByteString
xorBS ByteString
a ByteString
b = [Word8] -> ByteString
BS.pack ((Word8 -> Word8 -> Word8) -> ByteString -> ByteString -> [Word8]
forall a. (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]
BS.zipWith Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
xor ByteString
a ByteString
b)
divCeil :: Integral a => a -> a -> a
divCeil :: forall a. Integral a => a -> a -> a
divCeil a
a a
b = let (a
q, a
r) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
divMod a
a a
b
in if a
r a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0 then (a
q a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) else a
q
generatePQ :: CryptoRandomGen g =>
g ->
Int ->
Either RSAError (Integer, Integer, g)
generatePQ :: forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, Integer, g)
generatePQ g
g Int
len
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2 = RSAError -> Either RSAError (Integer, Integer, g)
forall a b. a -> Either a b
Left RSAError
RSAKeySizeTooSmall
| Bool
otherwise = do (Integer
baseP, g
g') <- g -> Int -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, g)
largeRandomPrime g
g (Int
len Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2)
(Integer
baseQ, g
g'') <- g -> Int -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, g)
largeRandomPrime g
g' (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
len Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2))
case () of
() | Integer
baseP Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
baseQ -> g -> Int -> Either RSAError (Integer, Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, Integer, g)
generatePQ g
g'' Int
len
| Integer
baseP Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
baseQ -> (Integer, Integer, g) -> Either RSAError (Integer, Integer, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer
baseQ, Integer
baseP, g
g'')
| Bool
otherwise -> (Integer, Integer, g) -> Either RSAError (Integer, Integer, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer
baseP, Integer
baseQ, g
g'')
largeRandomPrime :: CryptoRandomGen g =>
g -> Int ->
Either RSAError (Integer, g)
largeRandomPrime :: forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, g)
largeRandomPrime g
g Int
len =
do (ByteString
h_t, g
g') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
g Int
2
let [Word8
startH, Word8
startT] = ByteString -> [Word8]
BS.unpack ByteString
h_t
(ByteString
startMids, g
g'') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
g' (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)
let bstr :: ByteString
bstr = [ByteString] -> ByteString
BS.concat [Word8 -> ByteString
BS.singleton (Word8
startH Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0xc0),
ByteString
startMids, Word8 -> ByteString
BS.singleton (Word8
startT Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
1)]
g -> Integer -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g'' (ByteString -> Integer
os2ip ByteString
bstr)
randomBS :: CryptoRandomGen g => g -> Int -> Either RSAError (ByteString, g)
randomBS :: forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
g Int
n =
case Int -> g -> Either GenError (ByteString, g)
forall g.
CryptoRandomGen g =>
Int -> g -> Either GenError (ByteString, g)
genBytes Int
n g
g of
Left GenError
e -> RSAError -> Either RSAError (ByteString, g)
forall a b. a -> Either a b
Left (GenError -> RSAError
RSAGenError GenError
e)
Right (ByteString
bs, g
g') -> (ByteString, g) -> Either RSAError (ByteString, g)
forall a b. b -> Either a b
Right ([ByteString] -> ByteString
BS.fromChunks [ByteString
bs], g
g')
randomNZBS :: CryptoRandomGen g => g -> Int -> Either RSAError (ByteString, g)
randomNZBS :: forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomNZBS g
gen Int
0 = (ByteString, g) -> Either RSAError (ByteString, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
BS.empty, g
gen)
randomNZBS g
gen Int
size =
do (ByteString
bstr, g
gen') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomBS g
gen Int
size
let nzbstr :: ByteString
nzbstr = (Word8 -> Bool) -> ByteString -> ByteString
BS.filter (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0) ByteString
bstr
(ByteString
rest, g
gen'') <- g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
randomNZBS g
gen' (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BS.length ByteString
nzbstr))
(ByteString, g) -> Either RSAError (ByteString, g)
forall a. a -> Either RSAError a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
nzbstr ByteString -> ByteString -> ByteString
`BS.append` ByteString
rest, g
gen'')
findNextPrime :: CryptoRandomGen g =>
g -> Integer ->
Either RSAError (Integer, g)
findNextPrime :: forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g Integer
n
| Integer -> Bool
forall a. Integral a => a -> Bool
even Integer
n = g -> Integer -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
| Integer
n Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
65537 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 = g -> Integer -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
2)
| Bool
otherwise = case g -> Integer -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Bool, g)
isProbablyPrime g
g Integer
n of
Left RSAError
e -> RSAError -> Either RSAError (Integer, g)
forall a b. a -> Either a b
Left RSAError
e
Right (Bool
True, g
g') -> (Integer, g) -> Either RSAError (Integer, g)
forall a b. b -> Either a b
Right (Integer
n, g
g')
Right (Bool
False, g
g') -> g -> Integer -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Integer, g)
findNextPrime g
g' (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
2)
isProbablyPrime :: CryptoRandomGen g =>
g ->
Integer ->
Either RSAError (Bool, g)
isProbablyPrime :: forall g.
CryptoRandomGen g =>
g -> Integer -> Either RSAError (Bool, g)
isProbablyPrime g
g Integer
n
| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
541 = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Integer
n Integer -> [Integer] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Integer]
small_primes, g
g)
| (Integer -> Bool) -> [Integer] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\ Integer
x -> Integer
n Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0) [Integer]
small_primes = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
False, g
g)
| Bool
otherwise = g -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Int -> Either RSAError (Bool, g)
millerRabin g
g Integer
n Int
100
small_primes :: [Integer]
small_primes :: [Integer]
small_primes = [
Integer
2, Integer
3, Integer
5, Integer
7, Integer
11, Integer
13, Integer
17, Integer
19, Integer
23, Integer
29,
Integer
31, Integer
37, Integer
41, Integer
43, Integer
47, Integer
53, Integer
59, Integer
61, Integer
67, Integer
71,
Integer
73, Integer
79, Integer
83, Integer
89, Integer
97, Integer
101, Integer
103, Integer
107, Integer
109, Integer
113,
Integer
127, Integer
131, Integer
137, Integer
139, Integer
149, Integer
151, Integer
157, Integer
163, Integer
167, Integer
173,
Integer
179, Integer
181, Integer
191, Integer
193, Integer
197, Integer
199, Integer
211, Integer
223, Integer
227, Integer
229,
Integer
233, Integer
239, Integer
241, Integer
251, Integer
257, Integer
263, Integer
269, Integer
271, Integer
277, Integer
281,
Integer
283, Integer
293, Integer
307, Integer
311, Integer
313, Integer
317, Integer
331, Integer
337, Integer
347, Integer
349,
Integer
353, Integer
359, Integer
367, Integer
373, Integer
379, Integer
383, Integer
389, Integer
397, Integer
401, Integer
409,
Integer
419, Integer
421, Integer
431, Integer
433, Integer
439, Integer
443, Integer
449, Integer
457, Integer
461, Integer
463,
Integer
467, Integer
479, Integer
487, Integer
491, Integer
499, Integer
503, Integer
509, Integer
521, Integer
523, Integer
541,
Integer
547, Integer
557, Integer
563, Integer
569, Integer
571, Integer
577, Integer
587, Integer
593, Integer
599, Integer
601,
Integer
607, Integer
613, Integer
617, Integer
619, Integer
631, Integer
641, Integer
643, Integer
647, Integer
653, Integer
659,
Integer
661, Integer
673, Integer
677, Integer
683, Integer
691, Integer
701, Integer
709, Integer
719, Integer
727, Integer
733,
Integer
739, Integer
743, Integer
751, Integer
757, Integer
761, Integer
769, Integer
773, Integer
787, Integer
797, Integer
809,
Integer
811, Integer
821, Integer
823, Integer
827, Integer
829, Integer
839, Integer
853, Integer
857, Integer
859, Integer
863,
Integer
877, Integer
881, Integer
883, Integer
887, Integer
907, Integer
911, Integer
919, Integer
929, Integer
937, Integer
941,
Integer
947, Integer
953, Integer
967, Integer
971, Integer
977, Integer
983, Integer
991, Integer
997, Integer
1009, Integer
1013,
Integer
1019, Integer
1021, Integer
1031, Integer
1033, Integer
1039, Integer
1049, Integer
1051, Integer
1061, Integer
1063, Integer
1069,
Integer
1087, Integer
1091, Integer
1093, Integer
1097, Integer
1103, Integer
1109, Integer
1117, Integer
1123, Integer
1129, Integer
1151,
Integer
1153, Integer
1163, Integer
1171, Integer
1181, Integer
1187, Integer
1193, Integer
1201, Integer
1213, Integer
1217, Integer
1223
]
millerRabin :: CryptoRandomGen g =>
g ->
Integer ->
Int ->
Either RSAError (Bool, g)
millerRabin :: forall g.
CryptoRandomGen g =>
g -> Integer -> Int -> Either RSAError (Bool, g)
millerRabin g
g Integer
n Int
k
| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
0 = RSAError -> Either RSAError (Bool, g)
forall a b. a -> Either a b
Left (String -> RSAError
RSAError String
"Primality test on negative number or 0.")
| Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
False, g
g)
| Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
2 = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
True, g
g)
| Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
3 = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
True, g
g)
| Bool
otherwise =
let (Integer
s, Integer
d) = Integer -> Integer -> (Integer, Integer)
forall {t} {t}. (Bits t, Num t) => t -> t -> (t, t)
oddify Integer
0 (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1)
in g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g Integer
s Integer
d Int
k
where
generateSize :: Int
generateSize = Integer -> Int -> Int
forall {t}. (Ord t, Bits t, Num t) => t -> Int -> Int
bitsize (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
2) Int
8 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8
checkLoop :: CryptoRandomGen g =>
g -> Integer -> Integer -> Int ->
Either RSAError (Bool, g)
checkLoop :: forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g' Integer
_ Integer
_ Int
0 = (Bool, g) -> Either RSAError (Bool, g)
forall a b. b -> Either a b
Right (Bool
True, g
g')
checkLoop g
g' Integer
s Integer
d Int
c =
case Int -> g -> Either GenError (ByteString, g)
forall g.
CryptoRandomGen g =>
Int -> g -> Either GenError (ByteString, g)
genBytes Int
generateSize g
g' of
Left GenError
e -> RSAError -> Either RSAError (Bool, g)
forall a b. a -> Either a b
Left (GenError -> RSAError
RSAGenError GenError
e)
Right (ByteString
bstr, g
g'') ->
let a :: Integer
a = ByteString -> Integer
os2ip (ByteString -> ByteString
BS.fromStrict ByteString
bstr)
x :: Integer
x = Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
a Integer
d Integer
n
in if | (Integer
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
2) -> g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g'' Integer
s Integer
d Int
c
| (Integer
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
2)) -> g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g'' Integer
s Integer
d Int
c
| Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 -> g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g'' Integer
s Integer
d (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
| Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1) -> g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop g
g'' Integer
s Integer
d (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
| Bool
otherwise -> g
-> Integer
-> Integer
-> Integer
-> Int
-> Integer
-> Either RSAError (Bool, g)
forall {t} {t}.
(Eq t, Num t, CryptoRandomGen t) =>
t
-> Integer
-> Integer
-> Integer
-> Int
-> t
-> Either RSAError (Bool, t)
checkWitnesses g
g'' Integer
s Integer
d Integer
x Int
c (Integer
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1)
checkWitnesses :: t
-> Integer
-> Integer
-> Integer
-> Int
-> t
-> Either RSAError (Bool, t)
checkWitnesses t
g'' Integer
_ Integer
_ Integer
_ Int
_ t
0 = (Bool, t) -> Either RSAError (Bool, t)
forall a b. b -> Either a b
Right (Bool
False, t
g'')
checkWitnesses t
g'' Integer
s Integer
d Integer
x Int
c1 t
c2 =
case (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
x) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
n of
Integer
1 -> (Bool, t) -> Either RSAError (Bool, t)
forall a b. b -> Either a b
Right (Bool
False, t
g'')
Integer
y | Integer
y Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1) -> t -> Integer -> Integer -> Int -> Either RSAError (Bool, t)
forall g.
CryptoRandomGen g =>
g -> Integer -> Integer -> Int -> Either RSAError (Bool, g)
checkLoop t
g'' Integer
s Integer
d (Int
c1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
Integer
_ -> t
-> Integer
-> Integer
-> Integer
-> Int
-> t
-> Either RSAError (Bool, t)
checkWitnesses t
g'' Integer
s Integer
d Integer
x Int
c1 (t
c2 t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
oddify :: t -> t -> (t, t)
oddify t
s t
x | t -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit t
x Int
0 = (t
s, t
x)
| Bool
otherwise = t -> t -> (t, t)
oddify (t
s t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) (t
x t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftR` Int
1)
bitsize :: t -> Int -> Int
bitsize t
v Int
x | (t
1 t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftL` Int
x) t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
v = Int
x
| Bool
otherwise = t -> Int -> Int
bitsize t
v (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8)
modular_exponentiation :: Integer -> Integer -> Integer -> Integer
modular_exponentiation :: Integer -> Integer -> Integer -> Integer
modular_exponentiation Integer
x Integer
y Integer
m = Integer -> Integer -> Integer -> Integer
forall {t}. (Num t, Bits t) => Integer -> t -> Integer -> Integer
m_e_loop Integer
x Integer
y Integer
1
where
m_e_loop :: Integer -> t -> Integer -> Integer
m_e_loop Integer
_ t
0 Integer
result = Integer
result
m_e_loop Integer
b t
e Integer
result = Integer -> t -> Integer -> Integer
m_e_loop Integer
b' t
e' Integer
result'
where
b' :: Integer
b' = (Integer
b Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
b) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m
e' :: t
e' = t
e t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftR` Int
1
result' :: Integer
result' = if t -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit t
e Int
0 then (Integer
result Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
b) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m else Integer
result
modular_inverse :: Integer ->
Integer ->
Integer
modular_inverse :: Integer -> Integer -> Integer
modular_inverse Integer
e Integer
phi = Integer
x Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
phi
where (Integer
_, Integer
x, Integer
_) = Integer -> Integer -> (Integer, Integer, Integer)
extended_euclidean Integer
e Integer
phi
extended_euclidean :: Integer -> Integer -> (Integer, Integer, Integer)
extended_euclidean :: Integer -> Integer -> (Integer, Integer, Integer)
extended_euclidean Integer
a Integer
b | Integer
d Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = (-Integer
d, -Integer
x, -Integer
y)
| Bool
otherwise = (Integer
d, Integer
x, Integer
y)
where
(Integer
d, Integer
x, Integer
y) = Integer -> Integer -> (Integer, Integer, Integer)
egcd Integer
a Integer
b
egcd :: Integer -> Integer -> (Integer, Integer, Integer)
egcd :: Integer -> Integer -> (Integer, Integer, Integer)
egcd Integer
0 Integer
b = (Integer
b, Integer
0, Integer
1)
egcd Integer
a Integer
b = let (Integer
g, Integer
y, Integer
x) = Integer -> Integer -> (Integer, Integer, Integer)
egcd (Integer
b Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
a) Integer
a
in (Integer
g, Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- ((Integer
b Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
a) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
y), Integer
y)
hashSHA1 :: HashInfo
hashSHA1 :: HashInfo
hashSHA1 = HashInfo {
algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [Word8
0x30,Word8
0x21,Word8
0x30,Word8
0x09,Word8
0x06,Word8
0x05,Word8
0x2b,Word8
0x0e,Word8
0x03,
Word8
0x02,Word8
0x1a,Word8
0x05,Word8
0x00,Word8
0x04,Word8
0x14]
, hashFunction :: ByteString -> ByteString
hashFunction = Digest SHA1State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA1State -> ByteString)
-> (ByteString -> Digest SHA1State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA1State
sha1
}
hashSHA224 :: HashInfo
hashSHA224 :: HashInfo
hashSHA224 = HashInfo {
algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [Word8
0x30,Word8
0x2d,Word8
0x30,Word8
0x0d,Word8
0x06,Word8
0x09,Word8
0x60,Word8
0x86,Word8
0x48,
Word8
0x01,Word8
0x65,Word8
0x03,Word8
0x04,Word8
0x02,Word8
0x04,Word8
0x05,Word8
0x00,Word8
0x04,
Word8
0x1c]
, hashFunction :: ByteString -> ByteString
hashFunction = Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA256State -> ByteString)
-> (ByteString -> Digest SHA256State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256State
sha224
}
hashSHA256 :: HashInfo
hashSHA256 :: HashInfo
hashSHA256 = HashInfo {
algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [Word8
0x30,Word8
0x31,Word8
0x30,Word8
0x0d,Word8
0x06,Word8
0x09,Word8
0x60,Word8
0x86,Word8
0x48,
Word8
0x01,Word8
0x65,Word8
0x03,Word8
0x04,Word8
0x02,Word8
0x01,Word8
0x05,Word8
0x00,Word8
0x04,
Word8
0x20]
, hashFunction :: ByteString -> ByteString
hashFunction = Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA256State -> ByteString)
-> (ByteString -> Digest SHA256State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256State
sha256
}
hashSHA384 :: HashInfo
hashSHA384 :: HashInfo
hashSHA384 = HashInfo {
algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [Word8
0x30,Word8
0x41,Word8
0x30,Word8
0x0d,Word8
0x06,Word8
0x09,Word8
0x60,Word8
0x86,Word8
0x48,
Word8
0x01,Word8
0x65,Word8
0x03,Word8
0x04,Word8
0x02,Word8
0x02,Word8
0x05,Word8
0x00,Word8
0x04,
Word8
0x30]
, hashFunction :: ByteString -> ByteString
hashFunction = Digest SHA512State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA512State -> ByteString)
-> (ByteString -> Digest SHA512State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA512State
sha384
}
hashSHA512 :: HashInfo
hashSHA512 :: HashInfo
hashSHA512 = HashInfo {
algorithmIdent :: ByteString
algorithmIdent = [Word8] -> ByteString
BS.pack [Word8
0x30,Word8
0x51,Word8
0x30,Word8
0x0d,Word8
0x06,Word8
0x09,Word8
0x60,Word8
0x86,Word8
0x48,
Word8
0x01,Word8
0x65,Word8
0x03,Word8
0x04,Word8
0x02,Word8
0x03,Word8
0x05,Word8
0x00,Word8
0x04,
Word8
0x40]
, hashFunction :: ByteString -> ByteString
hashFunction = Digest SHA512State -> ByteString
forall t. Digest t -> ByteString
bytestringDigest (Digest SHA512State -> ByteString)
-> (ByteString -> Digest SHA512State) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA512State
sha512
}