sel-0.0.1.0: Cryptography for the casual user
Copyright(C) Hécate Moonlight 2022
LicenseBSD-3-Clause
MaintainerThe Haskell Cryptography Group
PortabilityGHC only
Safe HaskellSafe-Inferred
LanguageHaskell2010

Sel.PublicKey.Cipher

Description

 
Synopsis

Introduction

Public-key authenticated encryption allows a sender to encrypt a confidential message specifically for the recipient, using the recipient's public key.

Usage

import qualified Sel.PublicKey.Cipher as Cipher

main = do
  -- We get the sender their pair of keys:
  (senderSecretKey, senderPublicKey) <- newKeyPair
  -- We get the nonce from the other party with the message, or with 'encrypt' and our own message.
  (nonce, encryptedMessage) <- Cipher.encrypt "hello hello" secretKey
  let result = Cipher.decrypt encryptedMessage secretKey nonce
  print result
  -- "Just \"hello hello\""

Key pair generation

newKeyPair :: IO (PublicKey, SecretKey) Source #

Generate a new random secret key.

May throw KeyPairGenerationException if the generation fails.

Since: 0.0.1.0

newtype SecretKey Source #

A secret key of size cryptoBoxSecretKeyBytes.

Since: 0.0.1.0

Constructors

SecretKey (ForeignPtr CUChar) 

Instances

Instances details
Show SecretKey Source #
show secretKey == "[REDACTED]"

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Eq SecretKey Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Ord SecretKey Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Display SecretKey Source #
display secretKey == "[REDACTED]"

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

unsafeSecretKeyToHexByteString :: SecretKey -> StrictByteString Source #

Convert a SecretKey to a hexadecimal-encoded StrictByteString.

⚠️ Be prudent as to where you store it!

Since: 0.0.1.0

newtype PublicKey Source #

A public key of size cryptoBoxPublicKeyBytes.

Since: 0.0.1.0

Constructors

PublicKey (ForeignPtr CUChar) 

Instances

Instances details
Show PublicKey Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Eq PublicKey Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Ord PublicKey Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Display PublicKey Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

publicKeyToHexByteString :: PublicKey -> StrictByteString Source #

Convert a PublicKey to a hexadecimal-encoded StrictByteString.

Since: 0.0.1.0

keyPairFromHexByteStrings Source #

Arguments

:: StrictByteString

Public key

-> StrictByteString

Secret key

-> Either Text (PublicKey, SecretKey) 

Create a pair of SecretKey and PublicKey from hexadecimal-encoded StrictByteStrings that you have obtained on your own, usually from the network or disk.

The public and secret keys, once decoded from base16, must respectively be at least of length cryptoBoxPublicKeyBytes and 'cryptoBoxSecretKeyBytes.

Since: 0.0.1.0

Nonce

newtype Nonce Source #

Convert a SecretKey to a hexadecimal-encoded StrictByteString.

⚠️ Be prudent as to where you store it!

A random number that must only be used once per exchanged message. It does not have to be confidential. It is of size cryptoBoxNonceBytes.

Since: 0.0.1.0

Constructors

Nonce (ForeignPtr CUChar) 

Instances

Instances details
Show Nonce Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Methods

showsPrec :: Int -> Nonce -> ShowS #

show :: Nonce -> String #

showList :: [Nonce] -> ShowS #

Eq Nonce Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Methods

(==) :: Nonce -> Nonce -> Bool #

(/=) :: Nonce -> Nonce -> Bool #

Ord Nonce Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Methods

compare :: Nonce -> Nonce -> Ordering #

(<) :: Nonce -> Nonce -> Bool #

(<=) :: Nonce -> Nonce -> Bool #

(>) :: Nonce -> Nonce -> Bool #

(>=) :: Nonce -> Nonce -> Bool #

max :: Nonce -> Nonce -> Nonce #

min :: Nonce -> Nonce -> Nonce #

Display Nonce Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

nonceFromHexByteString :: StrictByteString -> Either Text Nonce Source #

Create a Nonce from a hexadecimal-encoded StrictByteString that you have obtained on your own, usually from the network or disk.

Since: 0.0.1.0

nonceToHexByteString :: Nonce -> StrictByteString Source #

Convert a Nonce to a hexadecimal-encoded StrictByteString.

Since: 0.0.1.0

Cipher text

data CipherText Source #

A ciphertext consisting of an encrypted message and an authentication tag.

Since: 0.0.1.0

Instances

Instances details
Show CipherText Source #

⚠️ Be prudent as to what you do with it!

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Eq CipherText Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Ord CipherText Source #

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

Display CipherText Source #

⚠️ Be prudent as to what you do with it!

Since: 0.0.1.0

Instance details

Defined in Sel.PublicKey.Cipher

cipherTextFromHexByteString :: StrictByteString -> Either Text CipherText Source #

Create a CipherText from a binary StrictByteString that you have obtained on your own, usually from the network or disk. It must be a valid cipherText built from the concatenation of the encrypted message and the authentication tag.

The input cipher text, once decoded from base16, must be at least of length cryptoBoxMACBytes.

Since: 0.0.1.0

cipherTextToHexText :: CipherText -> Text Source #

Convert a CipherText to a hexadecimal-encoded Text.

⚠️ Be prudent as to where you store it!

Since: 0.0.1.0

cipherTextToHexByteString :: CipherText -> StrictByteString Source #

Convert a CipherText to a hexadecimal-encoded StrictByteString.

⚠️ Be prudent as to where you store it!

Since: 0.0.1.0

cipherTextToBinary :: CipherText -> StrictByteString Source #

Convert a CipherText to a binary StrictByteString.

⚠️ Be prudent as to where you store it!

Since: 0.0.1.0

Encryption and Decryption

encrypt Source #

Arguments

:: StrictByteString

Message to encrypt.

-> PublicKey

Public key of the recipient

-> SecretKey

Secret key of the sender

-> IO (Nonce, CipherText) 

Create an authenticated CipherText from a message, a SecretKey, and a one-time cryptographic Nonce that must never be re-used with the same secret key to encrypt another message.

Since: 0.0.1.0

decrypt Source #

Arguments

:: CipherText

Encrypted message you want to decrypt.

-> PublicKey

Public key of the sender.

-> SecretKey

Secret key of the recipient.

-> Nonce

Nonce used for encrypting the original message.

-> Maybe StrictByteString 

Decrypt a CipherText and authenticated message with the shared secret key and the one-time cryptographic nonce.

Since: 0.0.1.0

Errors

data KeyPairGenerationException Source #

Exception thrown upon error during the generation of the key pair by newKeyPair.

Since: 0.0.1.0