1-- | 2-- Module : Crypto.PubKey.RSA.Types 3-- License : BSD-style 4-- Maintainer : Vincent Hanquez <vincent@snarc.org> 5-- Stability : experimental 6-- Portability : Good 7-- 8{-# LANGUAGE DeriveDataTypeable #-} 9{-# LANGUAGE GeneralizedNewtypeDeriving #-} 10module Crypto.PubKey.RSA.Types 11 ( Error(..) 12 , Blinder(..) 13 , PublicKey(..) 14 , PrivateKey(..) 15 , KeyPair(..) 16 , toPublicKey 17 , toPrivateKey 18 , private_size 19 , private_n 20 , private_e 21 ) where 22 23import Data.Data 24import Crypto.Internal.Imports 25 26-- | Blinder which is used to obfuscate the timing 27-- of the decryption primitive (used by decryption and signing). 28data Blinder = Blinder !Integer !Integer 29 deriving (Show,Eq) 30 31-- | error possible during encryption, decryption or signing. 32data Error = 33 MessageSizeIncorrect -- ^ the message to decrypt is not of the correct size (need to be == private_size) 34 | MessageTooLong -- ^ the message to encrypt is too long 35 | MessageNotRecognized -- ^ the message decrypted doesn't have a PKCS15 structure (0 2 .. 0 msg) 36 | SignatureTooLong -- ^ the message's digest is too long 37 | InvalidParameters -- ^ some parameters lead to breaking assumptions. 38 deriving (Show,Eq) 39 40-- | Represent a RSA public key 41data PublicKey = PublicKey 42 { public_size :: Int -- ^ size of key in bytes 43 , public_n :: Integer -- ^ public p*q 44 , public_e :: Integer -- ^ public exponent e 45 } deriving (Show,Read,Eq,Data) 46 47instance NFData PublicKey where 48 rnf (PublicKey sz n e) = rnf n `seq` rnf e `seq` sz `seq` () 49 50-- | Represent a RSA private key. 51-- 52-- Only the pub, d fields are mandatory to fill. 53-- 54-- p, q, dP, dQ, qinv are by-product during RSA generation, 55-- but are useful to record here to speed up massively 56-- the decrypt and sign operation. 57-- 58-- implementations can leave optional fields to 0. 59-- 60data PrivateKey = PrivateKey 61 { private_pub :: PublicKey -- ^ public part of a private key (size, n and e) 62 , private_d :: Integer -- ^ private exponent d 63 , private_p :: Integer -- ^ p prime number 64 , private_q :: Integer -- ^ q prime number 65 , private_dP :: Integer -- ^ d mod (p-1) 66 , private_dQ :: Integer -- ^ d mod (q-1) 67 , private_qinv :: Integer -- ^ q^(-1) mod p 68 } deriving (Show,Read,Eq,Data) 69 70instance NFData PrivateKey where 71 rnf (PrivateKey pub d p q dp dq qinv) = 72 rnf pub `seq` rnf d `seq` rnf p `seq` rnf q `seq` rnf dp `seq` rnf dq `seq` qinv `seq` () 73 74-- | get the size in bytes from a private key 75private_size :: PrivateKey -> Int 76private_size = public_size . private_pub 77 78-- | get n from a private key 79private_n :: PrivateKey -> Integer 80private_n = public_n . private_pub 81 82-- | get e from a private key 83private_e :: PrivateKey -> Integer 84private_e = public_e . private_pub 85 86-- | Represent RSA KeyPair 87-- 88-- note the RSA private key contains already an instance of public key for efficiency 89newtype KeyPair = KeyPair PrivateKey 90 deriving (Show,Read,Eq,Data,NFData) 91 92-- | Public key of a RSA KeyPair 93toPublicKey :: KeyPair -> PublicKey 94toPublicKey (KeyPair priv) = private_pub priv 95 96-- | Private key of a RSA KeyPair 97toPrivateKey :: KeyPair -> PrivateKey 98toPrivateKey (KeyPair priv) = priv 99