Module Cryptokit.RSA
The RSA module implements RSA public-key cryptography. Public-key cryptography is asymmetric: two distinct keys are used for encrypting a message, then decrypting it. Moreover, while one of the keys must remain secret, the other can be made public, since it is computationally very hard to reconstruct the private key from the public key. This feature supports both public-key encryption (anyone can encode with the public key, but only the owner of the private key can decrypt) and digital signature (only the owner of the private key can sign, but anyone can check the signature with the public key).
type key={}The type of RSA keys. Components
size,nandedefine the public part of the key. Componentssize,nandddefine the private part of the key. To speed up private key operations through the use of the Chinese remainder theorem (CRT), additional componentsp,q,dp,dqandqinvare provided. These are part of the private key.
val wipe_key : key -> unitErase all components of a RSA key.
val new_key : ?rng:Random.rng -> ?e:int -> int -> keyGenerate a new, random RSA key. The non-optional
intargument is the desired size for the modulus, in bits (e.g. 2048). The optionalrngargument specifies a random number generator to use for generating the key; it defaults toCryptokit.Random.secure_rng. The optionaleargument specifies the public exponent desired. If not specified,eis chosen randomly. Small values ofesuch ase = 65537significantly speeds up encryption and signature checking compared with a randome. Very small values ofesuch ase = 3can weaken security and are best avoided. The result ofnew_keyis a complete RSA key with all components defined: public, private, and private for use with the CRT.
val encrypt : key -> string -> stringencrypt k msgencrypts the stringmsgwith the public part of keyk(componentsnande).msgmust be smaller thankey.nwhen both strings are viewed as natural numbers in big-endian notation. In practice,msgshould be of lengthkey.size / 8 - 1, using padding if necessary. If you need to encrypt longer plaintexts using RSA, encrypt them with a symmetric cipher, using a randomly-generated key, and encrypt only that key with RSA.
val decrypt : key -> string -> stringdecrypt k msgdecrypts the ciphertext stringmsgwith the private part of keyk(componentsnandd). The size ofmsgis limited as described forCryptokit.RSA.encrypt.
val decrypt_CRT : key -> string -> stringdecrypt_CRT k msgdecrypts the ciphertext stringmsgwith the CRT private part of keyk(componentsn,p,q,dp,dqandqinv). The use of the Chinese remainder theorem (CRT) allows significantly faster decryption thanCryptokit.RSA.decrypt, at no loss in security. The size ofmsgis limited as described forCryptokit.RSA.encrypt.
val sign : key -> string -> stringsign k msgencrypts the plaintext stringmsgwith the private part of keyk(componentsnandd), thus performing a digital signature onmsg. The size ofmsgis limited as described forCryptokit.RSA.encrypt. If you need to sign longer messages, compute a cryptographic hash of the message and sign only the hash with RSA.
val sign_CRT : key -> string -> stringsign_CRT k msgencrypts the plaintext stringmsgwith the CRT private part of keyk(componentsn,p,q,dp,dqandqinv), thus performing a digital signature onmsg. The use of the Chinese remainder theorem (CRT) allows significantly faster signature thanCryptokit.RSA.sign, at no loss in security. The size ofmsgis limited as described forCryptokit.RSA.encrypt.
val unwrap_signature : key -> string -> stringunwrap_signature k msgdecrypts the ciphertext stringmsgwith the public part of keyk(componentsnandd), thus extracting the plaintext that was signed by the sender. The size ofmsgis limited as described forCryptokit.RSA.encrypt.