     :aes256-cbc-hmac-sha512 (bytes/slice key 0 32)))
 
 (defn- generate-authtag
-  [{:keys [algorithm input authkey iv] :as params}]
-  (let [data (bytes/concat iv input)
+  [{:keys [algorithm input authkey iv aad] :as params}]
+  (let [al (if aad
+             (aad->bytes aad)
+             (byte-array 0))
+        data (bytes/concat aad iv input al)
         fulltag (hmac/hash data authkey algorithm)
         truncatesize (quot (count fulltag) 2)]
     (bytes/slice fulltag 0 truncatesize)))

           org.bouncycastle.crypto.BlockCipher
           org.bouncycastle.crypto.StreamCipher
           org.bouncycastle.crypto.InvalidCipherTextException
           clojure.lang.IFn
           clojure.lang.Keyword))


(defmethod generate-iv :aes192-gcm [_] (nonce/random-bytes 12))
(defmethod generate-iv :aes256-gcm [_] (nonce/random-bytes 12))

(defn- aad->bytes
  [aad]
  (let [length (* (count aad) 8)
        buffer (ByteBuffer/allocate 8)]
    (.putLong buffer length)
    (.array buffer)))

(defn- extract-encryption-key
  [key algorithm]
  {:pre [(bytes/bytes? key)]}
