HydroSecretBox

This module contains functions for encryption and decryption. Probes are short authentication tags that let a recipient reject a forged ciphertext without having to store or decrypt it; see probe and probe-verify.

Example:

(def context (Hydro.context "Examples"))
(def msg (Hydro.buf "Arbitrary data to encrypt"))

(let-do [key (HydroSecretBox.keygen)
        cipher (Maybe.unsafe-from (HydroSecretBox.encrypt &msg 0
                                                  context &key))]
  (println*
    &(Maybe.apply (HydroSecretBox.decrypt &cipher 0 context &key)
                  (fn [b] (Hydro.unbuf &b)))))

decrypt

defn

(Fn [(Ref (Array Byte) a), Int, (Ptr CChar), (Ref (Array Byte) b)] (Maybe (Array Byte)))

                        (decrypt c id ctx key)
                    

decrypts a cipher c with the ID id, using the context ctx and key key. A cipher shorter than a cipher header cannot have come from encrypt, and yields Nothing.

encrypt

defn

(Fn [(Ref (Array Byte) a), Int, (Ptr CChar), (Ref (Array Byte) b)] (Maybe (Array Byte)))

                        (encrypt m id ctx key)
                    

encrypts a message m with the ID id, using the context ctx and key key.

keygen

defn

(Fn [] (Array Byte))

                        (keygen)
                    

generates a key for use in encryption/decryption procedures.

probe

defn

(Fn [(Ref (Array Byte) a), (Ptr CChar), (Ref (Array Byte) b)] (Array Byte))

                        (probe c ctx key)
                    

creates a probe for the cipher c, using the context ctx and key key. A probe is bound to the authentication tag of c, so a recipient can call probe-verify to discard forgeries before storing or decrypting anything. c has to be a cipher produced by encrypt; anything shorter than a cipher header aborts.

probe-verify

defn

(Fn [(Ref (Array Byte) a), (Ref (Array Byte) b), (Ptr CChar), (Ref (Array Byte) c)] Bool)

                        (probe-verify p c ctx key)
                    

verifies the probe p against the cipher c, using the context ctx and key key. Returns false if p was made with a different key, context or cipher, and also if p is not the size probe produces — a probe usually arrives from somewhere else, so its size is not something the recipient gets to assume.