module Codec.Crypto.RSA.Exceptions(
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 qualified Codec.Crypto.RSA.Pure as Pure
import Codec.Crypto.RSA.Pure(HashInfo,RSAError)
import Crypto.Random
import Crypto.Types.PubKey.RSA
import Data.ByteString.Lazy(ByteString)
import Data.Int
generateKeyPair :: CryptoRandomGen g =>
g -> Int ->
(PublicKey, PrivateKey, g)
generateKeyPair :: forall g.
CryptoRandomGen g =>
g -> Int -> (PublicKey, PrivateKey, g)
generateKeyPair g
g Int
sizeBits = Either RSAError (PublicKey, PrivateKey, g)
-> (PublicKey, PrivateKey, g)
forall e a. Exception e => Either e a -> a
throwLeft (g -> Int -> Either RSAError (PublicKey, PrivateKey, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (PublicKey, PrivateKey, g)
Pure.generateKeyPair g
g Int
sizeBits)
sign :: PrivateKey -> ByteString -> ByteString
sign :: PrivateKey -> ByteString -> ByteString
sign PrivateKey
pk ByteString
bs = Either RSAError ByteString -> ByteString
forall e a. Exception e => Either e a -> a
throwLeft (PrivateKey -> ByteString -> Either RSAError ByteString
Pure.sign PrivateKey
pk ByteString
bs)
verify :: PublicKey ->
ByteString ->
ByteString ->
Bool
verify :: PublicKey -> ByteString -> ByteString -> Bool
verify PublicKey
pk ByteString
m ByteString
s = Either RSAError Bool -> Bool
forall e a. Exception e => Either e a -> a
throwLeft (PublicKey -> ByteString -> ByteString -> Either RSAError Bool
Pure.verify PublicKey
pk ByteString
m ByteString
s)
encrypt :: CryptoRandomGen g =>
g -> PublicKey -> ByteString ->
(ByteString, g)
encrypt :: forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> (ByteString, g)
encrypt g
g PublicKey
k ByteString
m = Either RSAError (ByteString, g) -> (ByteString, g)
forall e a. Exception e => Either e a -> a
throwLeft (g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
Pure.encrypt g
g PublicKey
k ByteString
m)
encryptOAEP :: CryptoRandomGen g =>
g ->
(ByteString -> ByteString) ->
MGF ->
ByteString ->
PublicKey ->
ByteString ->
(ByteString, g)
encryptOAEP :: forall g.
CryptoRandomGen g =>
g
-> (ByteString -> ByteString)
-> MGF
-> ByteString
-> PublicKey
-> ByteString
-> (ByteString, g)
encryptOAEP g
g ByteString -> ByteString
hash MGF
mgf ByteString
l PublicKey
k ByteString
m = Either RSAError (ByteString, g) -> (ByteString, g)
forall e a. Exception e => Either e a -> a
throwLeft (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)
Pure.encryptOAEP g
g ByteString -> ByteString
hash MGF
mgf ByteString
l PublicKey
k ByteString
m)
encryptPKCS :: CryptoRandomGen g =>
g -> PublicKey -> ByteString ->
(ByteString, g)
encryptPKCS :: forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> (ByteString, g)
encryptPKCS g
g PublicKey
k ByteString
m = Either RSAError (ByteString, g) -> (ByteString, g)
forall e a. Exception e => Either e a -> a
throwLeft (g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
Pure.encryptPKCS g
g PublicKey
k ByteString
m)
decrypt :: PrivateKey -> ByteString -> ByteString
decrypt :: PrivateKey -> ByteString -> ByteString
decrypt PrivateKey
k ByteString
m = Either RSAError ByteString -> ByteString
forall e a. Exception e => Either e a -> a
throwLeft (PrivateKey -> ByteString -> Either RSAError ByteString
Pure.decrypt PrivateKey
k ByteString
m)
decryptOAEP :: (ByteString -> ByteString) ->
MGF ->
ByteString ->
PrivateKey ->
ByteString ->
ByteString
decryptOAEP :: (ByteString -> ByteString)
-> MGF -> ByteString -> PrivateKey -> ByteString -> ByteString
decryptOAEP ByteString -> ByteString
hash MGF
mgf ByteString
l PrivateKey
k ByteString
m = Either RSAError ByteString -> ByteString
forall e a. Exception e => Either e a -> a
throwLeft ((ByteString -> ByteString)
-> MGF
-> ByteString
-> PrivateKey
-> ByteString
-> Either RSAError ByteString
Pure.decryptOAEP ByteString -> ByteString
hash MGF
mgf ByteString
l PrivateKey
k ByteString
m)
decryptPKCS :: PrivateKey -> ByteString -> ByteString
decryptPKCS :: PrivateKey -> ByteString -> ByteString
decryptPKCS PrivateKey
k ByteString
m = Either RSAError ByteString -> ByteString
forall e a. Exception e => Either e a -> a
throwLeft (PrivateKey -> ByteString -> Either RSAError ByteString
Pure.decryptPKCS PrivateKey
k ByteString
m)
chunkify :: ByteString -> Int64 -> [ByteString]
chunkify :: ByteString -> Int64 -> [ByteString]
chunkify = ByteString -> Int64 -> [ByteString]
Pure.chunkify
rsaes_oaep_encrypt :: CryptoRandomGen g =>
g ->
(ByteString->ByteString) ->
MGF ->
PublicKey ->
ByteString ->
ByteString ->
(ByteString, g)
rsaes_oaep_encrypt :: forall g.
CryptoRandomGen g =>
g
-> (ByteString -> ByteString)
-> MGF
-> PublicKey
-> ByteString
-> ByteString
-> (ByteString, g)
rsaes_oaep_encrypt g
g ByteString -> ByteString
hash MGF
mgf PublicKey
k ByteString
l ByteString
m =
Either RSAError (ByteString, g) -> (ByteString, g)
forall e a. Exception e => Either e a -> a
throwLeft (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)
Pure.rsaes_oaep_encrypt g
g ByteString -> ByteString
hash MGF
mgf PublicKey
k ByteString
l ByteString
m)
rsaes_oaep_decrypt :: (ByteString->ByteString) ->
MGF ->
PrivateKey ->
ByteString ->
ByteString ->
ByteString
rsaes_oaep_decrypt :: (ByteString -> ByteString)
-> MGF -> PrivateKey -> ByteString -> ByteString -> ByteString
rsaes_oaep_decrypt ByteString -> ByteString
hash MGF
mgf PrivateKey
k ByteString
l ByteString
c =
Either RSAError ByteString -> ByteString
forall e a. Exception e => Either e a -> a
throwLeft ((ByteString -> ByteString)
-> MGF
-> PrivateKey
-> ByteString
-> ByteString
-> Either RSAError ByteString
Pure.rsaes_oaep_decrypt ByteString -> ByteString
hash MGF
mgf PrivateKey
k ByteString
l ByteString
c)
rsaes_pkcs1_v1_5_encrypt :: CryptoRandomGen g =>
g ->
PublicKey ->
ByteString ->
(ByteString, g)
rsaes_pkcs1_v1_5_encrypt :: forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> (ByteString, g)
rsaes_pkcs1_v1_5_encrypt g
g PublicKey
k ByteString
m =
Either RSAError (ByteString, g) -> (ByteString, g)
forall e a. Exception e => Either e a -> a
throwLeft (g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> PublicKey -> ByteString -> Either RSAError (ByteString, g)
Pure.rsaes_pkcs1_v1_5_encrypt g
g PublicKey
k ByteString
m)
rsaes_pkcs1_v1_5_decrypt :: PrivateKey -> ByteString -> ByteString
rsaes_pkcs1_v1_5_decrypt :: PrivateKey -> ByteString -> ByteString
rsaes_pkcs1_v1_5_decrypt PrivateKey
k ByteString
c = Either RSAError ByteString -> ByteString
forall e a. Exception e => Either e a -> a
throwLeft (PrivateKey -> ByteString -> Either RSAError ByteString
Pure.rsaes_pkcs1_v1_5_decrypt PrivateKey
k ByteString
c)
rsassa_pkcs1_v1_5_sign :: HashInfo ->
PrivateKey ->
ByteString ->
ByteString
rsassa_pkcs1_v1_5_sign :: HashInfo -> PrivateKey -> ByteString -> ByteString
rsassa_pkcs1_v1_5_sign HashInfo
hi PrivateKey
k ByteString
m =
Either RSAError ByteString -> ByteString
forall e a. Exception e => Either e a -> a
throwLeft (HashInfo -> PrivateKey -> ByteString -> Either RSAError ByteString
Pure.rsassa_pkcs1_v1_5_sign HashInfo
hi PrivateKey
k ByteString
m)
rsassa_pkcs1_v1_5_verify :: HashInfo ->
PublicKey ->
ByteString ->
ByteString ->
Bool
rsassa_pkcs1_v1_5_verify :: HashInfo -> PublicKey -> ByteString -> ByteString -> Bool
rsassa_pkcs1_v1_5_verify HashInfo
hi PublicKey
k ByteString
m ByteString
s =
Either RSAError Bool -> Bool
forall e a. Exception e => Either e a -> a
throwLeft (HashInfo
-> PublicKey -> ByteString -> ByteString -> Either RSAError Bool
Pure.rsassa_pkcs1_v1_5_verify HashInfo
hi PublicKey
k ByteString
m ByteString
s)
type MGF = ByteString -> Int64 -> Either RSAError ByteString
generateMGF1 :: (ByteString -> ByteString) -> MGF
generateMGF1 :: (ByteString -> ByteString) -> MGF
generateMGF1 = (ByteString -> ByteString) -> MGF
Pure.generateMGF1
i2osp :: Integral a => a -> Int -> ByteString
i2osp :: forall a. Integral a => a -> Int -> ByteString
i2osp a
x Int
len = Either RSAError ByteString -> ByteString
forall e a. Exception e => Either e a -> a
throwLeft (a -> Int -> Either RSAError ByteString
forall a. Integral a => a -> Int -> Either RSAError ByteString
Pure.i2osp a
x Int
len)
os2ip :: ByteString -> Integer
os2ip :: ByteString -> Integer
os2ip = ByteString -> Integer
Pure.os2ip
rsa_ep :: Integer -> Integer -> Integer -> Integer
rsa_ep :: Integer -> Integer -> Integer -> Integer
rsa_ep Integer
n Integer
e Integer
m = Either RSAError Integer -> Integer
forall e a. Exception e => Either e a -> a
throwLeft (Integer -> Integer -> Integer -> Either RSAError Integer
Pure.rsa_ep Integer
n Integer
e Integer
m)
rsa_dp :: Integer -> Integer -> Integer -> Integer
rsa_dp :: Integer -> Integer -> Integer -> Integer
rsa_dp Integer
n Integer
d Integer
c = Either RSAError Integer -> Integer
forall e a. Exception e => Either e a -> a
throwLeft (Integer -> Integer -> Integer -> Either RSAError Integer
Pure.rsa_dp Integer
n Integer
d Integer
c)
rsa_sp1 :: Integer -> Integer -> Integer -> Integer
rsa_sp1 :: Integer -> Integer -> Integer -> Integer
rsa_sp1 Integer
n Integer
d Integer
m = Either RSAError Integer -> Integer
forall e a. Exception e => Either e a -> a
throwLeft (Integer -> Integer -> Integer -> Either RSAError Integer
Pure.rsa_sp1 Integer
n Integer
d Integer
m)
rsa_vp1 :: Integer -> Integer -> Integer -> Integer
rsa_vp1 :: Integer -> Integer -> Integer -> Integer
rsa_vp1 Integer
n Integer
e Integer
s = Either RSAError Integer -> Integer
forall e a. Exception e => Either e a -> a
throwLeft (Integer -> Integer -> Integer -> Either RSAError Integer
Pure.rsa_vp1 Integer
n Integer
e Integer
s)
generatePQ :: CryptoRandomGen g =>
g ->
Int ->
(Integer, Integer, g)
generatePQ :: forall g. CryptoRandomGen g => g -> Int -> (Integer, Integer, g)
generatePQ g
g Int
len = Either RSAError (Integer, Integer, g) -> (Integer, Integer, g)
forall e a. Exception e => Either e a -> a
throwLeft (g -> Int -> Either RSAError (Integer, Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, Integer, g)
Pure.generatePQ g
g Int
len)
largeRandomPrime :: CryptoRandomGen g => g -> Int -> (Integer, g)
largeRandomPrime :: forall g. CryptoRandomGen g => g -> Int -> (Integer, g)
largeRandomPrime g
g Int
len = Either RSAError (Integer, g) -> (Integer, g)
forall e a. Exception e => Either e a -> a
throwLeft (g -> Int -> Either RSAError (Integer, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (Integer, g)
Pure.largeRandomPrime g
g Int
len)
randomBS :: CryptoRandomGen g => g -> Int -> (ByteString, g)
randomBS :: forall g. CryptoRandomGen g => g -> Int -> (ByteString, g)
randomBS g
g Int
n = Either RSAError (ByteString, g) -> (ByteString, g)
forall e a. Exception e => Either e a -> a
throwLeft (g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
Pure.randomBS g
g Int
n)
randomNZBS :: CryptoRandomGen g => g -> Int -> (ByteString, g)
randomNZBS :: forall g. CryptoRandomGen g => g -> Int -> (ByteString, g)
randomNZBS g
gen Int
size = Either RSAError (ByteString, g) -> (ByteString, g)
forall e a. Exception e => Either e a -> a
throwLeft (g -> Int -> Either RSAError (ByteString, g)
forall g.
CryptoRandomGen g =>
g -> Int -> Either RSAError (ByteString, g)
Pure.randomNZBS g
gen Int
size)
modular_exponentiation :: Integer -> Integer -> Integer -> Integer
modular_exponentiation :: Integer -> Integer -> Integer -> Integer
modular_exponentiation = Integer -> Integer -> Integer -> Integer
Pure.modular_exponentiation
modular_inverse :: Integer ->
Integer ->
Integer
modular_inverse :: Integer -> Integer -> Integer
modular_inverse = Integer -> Integer -> Integer
Pure.modular_inverse
hashSHA1 :: HashInfo
hashSHA1 :: HashInfo
hashSHA1 = HashInfo
Pure.hashSHA1
hashSHA224 :: HashInfo
hashSHA224 :: HashInfo
hashSHA224 = HashInfo
Pure.hashSHA224
hashSHA256 :: HashInfo
hashSHA256 :: HashInfo
hashSHA256 = HashInfo
Pure.hashSHA256
hashSHA384 :: HashInfo
hashSHA384 :: HashInfo
hashSHA384 = HashInfo
Pure.hashSHA384
hashSHA512 :: HashInfo
hashSHA512 :: HashInfo
hashSHA512 = HashInfo
Pure.hashSHA512