]> gitweb.factorcode.org Git - factor.git/commitdiff
sodium: the add essential crypto-box-* words and a unit-test 2086/head
authorAlexander Iljin <ajsoft@yandex.ru>
Sun, 23 Dec 2018 23:29:39 +0000 (00:29 +0100)
committerAlexander Iljin <ajsoft@yandex.ru>
Sun, 23 Dec 2018 23:51:08 +0000 (00:51 +0100)
extra/sodium/sodium-tests.factor [new file with mode: 0644]
extra/sodium/sodium.factor

diff --git a/extra/sodium/sodium-tests.factor b/extra/sodium/sodium-tests.factor
new file mode 100644 (file)
index 0000000..f0d3e38
--- /dev/null
@@ -0,0 +1,12 @@
+! Copyright (C) 2018 Alexander Ilin.
+! See http://factorcode.org/license.txt for BSD license.
+USING: arrays io.encodings.string io.encodings.utf8 kernel math
+sequences sodium tools.test ;
+IN: sodium.tests
+
+{ t } [
+    "Encrypted message" dup utf8 encode
+    crypto-box-nonce 2 [ crypto-box-keypair 2array ] times
+    [ [ first ] [ second ] bi* crypto-box-easy ] 3keep swap
+    [ first ] [ second ] bi* crypto-box-open-easy utf8 decode =
+] unit-test
index 95c68f69c4a298ab75b90836768dbc815b481e90..283e8085725ea4cc3069ea68645c26a2ff36ea77 100644 (file)
@@ -25,6 +25,11 @@ ERROR: buffer-too-small ;
 : secretbox-message-buf ( cipher-length -- byte-array )
     crypto_secretbox_macbytes message-buf ;
 
+: box-cipher-buf ( message-length -- byte-array )
+    crypto_box_macbytes cipher-buf ;
+
+: box-message-buf ( cipher-length -- byte-array )
+    crypto_box_macbytes message-buf ;
 
 PRIVATE>
 
@@ -65,4 +70,22 @@ PRIVATE>
     [ crypto_secretbox_keybytes check-length ] tri*
     crypto_secretbox_open_easy 0 = [ drop f ] unless ;
 
+: crypto-box-keypair ( -- public-key secret-key )
+    crypto_box_publickeybytes <byte-array>
+    crypto_box_secretkeybytes <byte-array>
+    2dup crypto_box_keypair check0 ;
+
+: crypto-box-nonce ( -- nonce-bytes )
+    crypto_box_noncebytes n-random-bytes ;
+
+: crypto-box-easy ( message nonce public-key private-key -- cipher-bytes )
+    [
+        dup length [ box-cipher-buf dup rot ] keep
+    ] 3dip crypto_box_easy check0 ;
+
+: crypto-box-open-easy ( cipher-bytes nonce public-key private-key -- message )
+    [
+        dup length [ box-message-buf dup rot ] keep
+    ] 3dip crypto_box_open_easy check0 ;
+
 [ sodium-init ] "sodium" add-startup-hook