crypton-2.2.5: Cryptography Primitives
LicenseBSD-style
MaintainerCarlos Rodriguez-Vega <crodveg@yahoo.es>
Stabilityexperimental
Portabilityunknown
Safe HaskellNone
LanguageHaskell2010

Crypto.PubKey.Rabin.Basic

Description

Rabin cryptosystem for public-key cryptography and digital signature.

What is kept from the clock, and what is not

The square roots modulo the secret primes are taken with expSafe, which does not read the exponent it is given. Two things here do read what they are given.

Signing asks for the Jacobi symbol of the hash modulo each of the two private primes, and the Jacobi symbol is computed by a sequence of reductions whose number follows both of its arguments -- so the work done per signature follows the primes. Key generation runs the extended Euclidean algorithm on the two primes for the same reason. Neither has a drop-in replacement here: a Jacobi symbol that does not read its arguments is a different algorithm, not a different call.

Around all of that is Integer arithmetic, whose cost follows the size of the numbers; see Crypto.PubKey.DSA for that note at more length.

The hash algorithm is passed as a value, and that is not going to change

sign, signWith and verify take a hash argument whose value they never read. Every HashAlgorithm instance is a nullary constructor, and the algorithm comes from the type; the value is there to carry the type and nothing else. A caller holding a value loses nothing by it, and a caller that is itself polymorphic in the algorithm has no value to pass.

Elsewhere in crypton that is answered by taking a Digest instead: the digest carries the algorithm in its type and the bytes in its value, so nothing is passed only to name a type. Crypto.PubKey.RSA.PKCS15, Crypto.PubKey.DSA, Crypto.PubKey.ElGamal, Crypto.PubKey.Rabin.RW and Crypto.PubKey.Rabin.Modified all do that.

This module cannot. What is hashed here is not the message:

h = os2ip $ hashWith hashAlg $ B.append padding m

and the padding is not the caller's either. sign searches for one, drawing eight bytes at a time until the first octet is non-zero and the Jacobi symbols of the hash modulo each private prime are both 1. Which bytes get hashed is therefore decided inside the signing operation, after the caller has handed over the message, so there is no digest for a caller to compute in advance.

A proxy argument would work where a digest cannot. It is deliberately not added: it would be the one exception to a rule the rest of the library now follows, and this module has no callers asking for it. See the survey in https://github.com/kazu-yamamoto/crypton/issues/304.

Synopsis

Documentation

data PublicKey Source #

Represent a Rabin public key.

Constructors

PublicKey 

Fields

Instances

Instances details
Data PublicKey Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> PublicKey -> c PublicKey #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c PublicKey #

toConstr :: PublicKey -> Constr #

dataTypeOf :: PublicKey -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c PublicKey) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PublicKey) #

gmapT :: (forall b. Data b => b -> b) -> PublicKey -> PublicKey #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> PublicKey -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> PublicKey -> r #

gmapQ :: (forall d. Data d => d -> u) -> PublicKey -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> PublicKey -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> PublicKey -> m PublicKey #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> PublicKey -> m PublicKey #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> PublicKey -> m PublicKey #

Read PublicKey Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Show PublicKey Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Eq PublicKey Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

data PrivateKey Source #

Represent a Rabin private key.

Constructors

PrivateKey 

Fields

Instances

Instances details
Data PrivateKey Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> PrivateKey -> c PrivateKey #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c PrivateKey #

toConstr :: PrivateKey -> Constr #

dataTypeOf :: PrivateKey -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c PrivateKey) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PrivateKey) #

gmapT :: (forall b. Data b => b -> b) -> PrivateKey -> PrivateKey #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> PrivateKey -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> PrivateKey -> r #

gmapQ :: (forall d. Data d => d -> u) -> PrivateKey -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> PrivateKey -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> PrivateKey -> m PrivateKey #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> PrivateKey -> m PrivateKey #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> PrivateKey -> m PrivateKey #

Read PrivateKey Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Show PrivateKey Source #

The public part is shown; the secret fields are not. Use debugShow to see them.

Instance details

Defined in Crypto.PubKey.Rabin.Basic

DebugShow PrivateKey Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Eq PrivateKey Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

data Signature Source #

Rabin Signature.

Constructors

Signature (Integer, Integer) 

Instances

Instances details
Data Signature Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Signature -> c Signature #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Signature #

toConstr :: Signature -> Constr #

dataTypeOf :: Signature -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Signature) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Signature) #

gmapT :: (forall b. Data b => b -> b) -> Signature -> Signature #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Signature -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Signature -> r #

gmapQ :: (forall d. Data d => d -> u) -> Signature -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Signature -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Signature -> m Signature #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Signature -> m Signature #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Signature -> m Signature #

Read Signature Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Show Signature Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

Eq Signature Source # 
Instance details

Defined in Crypto.PubKey.Rabin.Basic

generate :: MonadRandom m => Int -> m (PublicKey, PrivateKey) Source #

Generate a pair of (private, public) key of size in bytes. Primes p and q are both congruent 3 mod 4.

See algorithm 8.11 in "Handbook of Applied Cryptography" by Alfred J. Menezes et al.

encrypt Source #

Arguments

:: (HashAlgorithm hash, MonadRandom m) 
=> OAEPParams hash ByteString ByteString

OAEP padding parameters

-> PublicKey

public key

-> ByteString

plaintext

-> m (Either Error ByteString) 

Encrypt plaintext using public key.

encryptWithSeed Source #

Arguments

:: HashAlgorithm hash 
=> ByteString

Seed

-> OAEPParams hash ByteString ByteString

OAEP padding

-> PublicKey

public key

-> ByteString

plaintext

-> Either Error ByteString 

Encrypt plaintext using public key an a predefined OAEP seed.

See algorithm 8.11 in "Handbook of Applied Cryptography" by Alfred J. Menezes et al.

decrypt Source #

Arguments

:: HashAlgorithm hash 
=> OAEPParams hash ByteString ByteString

OAEP padding parameters

-> PrivateKey

private key

-> ByteString

ciphertext

-> Maybe ByteString 

Decrypt ciphertext using private key.

The ciphertext has to be what encrypt produces: the big-endian encoding, with no leading zero octet, of a value below the modulus. Squaring and the square roots that undo it work modulo n, so without that condition c and c + n -- and c with a zero octet in front of it -- would all decrypt to the same message, and a ciphertext would not be unique to its plaintext.

See algorithm 8.12 in "Handbook of Applied Cryptography" by Alfred J. Menezes et al.

sign Source #

Arguments

:: (MonadRandom m, HashAlgorithm hash) 
=> PrivateKey

private key

-> hash

hash function

-> ByteString

message to sign

-> m (Either Error Signature) 

Sign message using hash algorithm and private key.

See https://en.wikipedia.org/wiki/Rabin_signature_algorithm.

signWith Source #

Arguments

:: HashAlgorithm hash 
=> ByteString

padding

-> PrivateKey

private key

-> hash

hash function

-> ByteString

message to sign

-> Either Error Signature 

Sign message using padding, hash algorithm and private key.

See https://en.wikipedia.org/wiki/Rabin_signature_algorithm.

verify Source #

Arguments

:: HashAlgorithm hash 
=> PublicKey

private key

-> hash

hash function

-> ByteString

message

-> Signature

signature

-> Bool 

Verify signature using hash algorithm and public key.

See https://en.wikipedia.org/wiki/Rabin_signature_algorithm.