Skip to content

Commit

Permalink
Merge pull request #42 from artenator/master
Browse files Browse the repository at this point in the history
added box detached api
  • Loading branch information
lvh authored Jun 15, 2018
2 parents 017cf73 + 1948f55 commit 26f85ef
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/caesium/binding.clj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@
^bytes ^{Pinned {}} pk
^bytes ^{Pinned {}} sk]]

[^int crypto_box_detached
[^bytes ^{Pinned {}} c
^bytes ^{Pinned {}} mac
^bytes ^{Pinned {}} m
^long ^{LongLong {}} mlen
^bytes ^{Pinned {}} n
^bytes ^{Pinned {}} pk
^bytes ^{Pinned {}} sk]]
[^int crypto_box_open_detached
[^bytes ^{Pinned {}} m
^bytes ^{Pinned {}} c
^bytes ^{Pinned {}} mac
^long ^{LongLong {}} clen
^bytes ^{Pinned {}} n
^bytes ^{Pinned {}} pk
^bytes ^{Pinned {}} sk]]

[^long ^{size_t {}} crypto_sign_bytes []]
[^long ^{size_t {}} crypto_sign_seedbytes []]
[^long ^{size_t {}} crypto_sign_publickeybytes []]
Expand Down
35 changes: 35 additions & 0 deletions src/caesium/crypto/box.clj
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,38 @@
libsodium function."
[pk sk ctext]
(box-seal-open ctext pk sk))

(defn box-detached-to-bufs! [c mac m mlen n pk sk]
(b/call! detached c mac m mlen n pk sk))

(defn box-detached [ptext nonce pk sk]
(let [c (bb/alloc (bb/buflen ptext))
mac (bb/alloc macbytes)]
(box-detached-to-bufs!
c
mac
(bb/->indirect-byte-buf ptext)
(bb/buflen ptext)
(bb/->indirect-byte-buf nonce)
(bb/->indirect-byte-buf pk)
(bb/->indirect-byte-buf sk))
{:c (bb/->bytes c)
:mac (bb/->bytes mac)}))

(defn box-open-detached-to-bufs! [m c mac clen n pk sk]
(let [res (b/call! open_detached m c mac clen n pk sk)]
(if (zero? res)
m
(throw (RuntimeException. "Ciphertext verification failed")))))

(defn box-open-detached [ctext mac nonce pk sk]
(let [m (bb/alloc (bb/buflen ctext))]
(box-open-detached-to-bufs!
m
(bb/->indirect-byte-buf ctext)
(bb/->indirect-byte-buf mac)
(bb/buflen ctext)
(bb/->indirect-byte-buf nonce)
(bb/->indirect-byte-buf pk)
(bb/->indirect-byte-buf sk))
(bb/->bytes m)))
19 changes: 19 additions & 0 deletions test/caesium/crypto/box_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,22 @@
(is (thrown-with-msg?
RuntimeException #"Ciphertext verification failed"
(b/anonymous-decrypt bob-pk bob-sk forgery))))))

(deftest detached-test
(let [nonce (box-vector "nonce")
ptext (box-vector "plaintext")
c-kat (box-vector "ciphertext")
bob-pk (box-vector "bob-public-key")
bob-sk (box-vector "bob-secret-key")
alice-pk (box-vector "alice-public-key")
alice-sk (box-vector "alice-secret-key")
{:keys [c mac]} (b/box-detached ptext nonce alice-pk bob-sk)
open-detached (b/box-open-detached c mac nonce bob-pk alice-sk)]
(is (bb/bytes= (byte-array (drop b/macbytes c-kat)) c))
(is (bb/bytes= (byte-array (take b/macbytes c-kat)) mac))
(is (bb/bytes= ptext open-detached))
(let [forged-c (r/randombytes (- (alength ^bytes c-kat) b/macbytes))
forged-mac (r/randombytes b/macbytes)]
(is (thrown-with-msg?
RuntimeException #"Ciphertext verification failed"
(b/box-open-detached forged-c forged-mac nonce bob-pk alice-sk))))))

0 comments on commit 26f85ef

Please sign in to comment.