From 9bc5eece92dee92a65d2aec3cf64ba3ab380322f Mon Sep 17 00:00:00 2001 From: haskal Date: Sun, 8 Nov 2020 18:13:56 -0500 Subject: [PATCH] implement bindings for sign and lock --- crossfire/not-crypto.rkt | 63 ++++++++++++++++++++++++++++++++++-- crossfire/static-support.rkt | 2 +- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/crossfire/not-crypto.rkt b/crossfire/not-crypto.rkt index dd93a74..74b9851 100644 --- a/crossfire/not-crypto.rkt +++ b/crossfire/not-crypto.rkt @@ -16,9 +16,9 @@ ;; You should have received a copy of the GNU Affero General Public License ;; along with this program. If not, see . -(require ffi/vector ffi/unsafe racket/random +(require ffi/vector ffi/unsafe racket/bool racket/random "static-support.rkt" - (for-syntax racket/base racket/string racket/syntax) + (for-syntax racket/base racket/string) (only-in racket/contract provide/contract) ;; MATTHIAS ;; WHY @@ -43,6 +43,65 @@ stx (string-replace (symbol->string (syntax->datum #'name)) "-" "_"))]) #'(define name (get-ffi-obj/runtime c-name monocypher type)))])) +;; public key signatures + (define/ffi crypto-sign-public-key (_fun (pubkey : (_u8vector o 32)) (seckey : _u8vector) -> _void -> pubkey)) (provide/contract [crypto-sign-public-key (contract:-> (bytes-len/c 32) (bytes-len/c 32))]) + +(define/ffi crypto-sign + (_fun (signature : (_u8vector o 64)) (seckey : _u8vector) (pubkey : _u8vector) + (message : _u8vector) (msgsize : _size = (u8vector-length message)) + -> _void -> signature)) +(provide/contract [crypto-sign (contract:-> (bytes-len/c 32) (bytes-len/c 32) bytes? + (bytes-len/c 64))]) + +(define/ffi crypto-check + (_fun (signature : _u8vector) (pubkey : _u8vector) (message : _u8vector) + (msgsize : _size = (u8vector-length message)) + -> (result : _int) -> (zero? result))) +(provide/contract [crypto-check (contract:-> (bytes-len/c 64) (bytes-len/c 32) bytes? boolean?)]) + +(define (crypto-sign-make-key) + (crypto-random-bytes 32)) +(provide crypto-sign-make-key) + +;; key exchange + +;; XXX : in monocypher crypto_key_exchange_public_key is actually crypto_x25519_public_key +(define/ffi crypto-x25519-public-key + (_fun (pubkey : (_u8vector o 32)) (seckey : _u8vector) -> _void -> pubkey)) +(define crypto-key-exchange-public-key crypto-x25519-public-key) +(provide/contract [crypto-key-exchange-public-key (contract:-> (bytes-len/c 32) (bytes-len/c 32))]) + +(define/ffi crypto-key-exchange + (_fun (outkey : (_u8vector o 32)) (yourseckey : _u8vector) (theirpubkey : _u8vector) + -> _void -> outkey)) +(provide/contract [crypto-key-exchange (contract:-> (bytes-len/c 32) (bytes-len/c 32) + (bytes-len/c 32))]) + +(define (crypto-key-exchange-make-key) + (crypto-random-bytes 32)) +(provide crypto-key-exchange-make-key) + +;; authenticated encryption + +(define/ffi crypto-lock + (_fun (mac : (_u8vector o 16)) (ct : (_u8vector o textsize)) (key : _u8vector) (nonce : _u8vector) + (pt : _u8vector) (textsize : _size = (u8vector-length pt)) + -> _void -> (values ct mac))) +(provide/contract [crypto-lock (contract:-> (bytes-len/c 32) (bytes-len/c 24) bytes? + (values bytes? (bytes-len/c 16)))]) + +(define/ffi crypto-unlock + (_fun (pt : (_u8vector o textsize)) (key : _u8vector) (nonce : _u8vector) (mac : _u8vector) + (ct : _u8vector) (textsize : _size = (u8vector-length ct)) + -> (res : _int) -> (if (zero? res) pt #f))) +(provide/contract [crypto-unlock (contract:-> (bytes-len/c 32) (bytes-len/c 24) (bytes-len/c 16) + bytes? (or/c false? bytes?))]) + +(define (crypto-lock-make-key) + (crypto-random-bytes 32)) +(define (crypto-lock-make-nonce) + (crypto-random-bytes 24)) +(provide crypto-lock-make-key crypto-lock-make-nonce) diff --git a/crossfire/static-support.rkt b/crossfire/static-support.rkt index 8d0d6ed..f9a2a8e 100644 --- a/crossfire/static-support.rkt +++ b/crossfire/static-support.rkt @@ -16,7 +16,7 @@ ;; You should have received a copy of the GNU Affero General Public License ;; along with this program. If not, see . -(require ffi/unsafe racket/bool racket/string) +(require ffi/unsafe racket/bool) (provide get-ffi-obj/static static-ffi-available? ffi-lib/runtime get-ffi-obj/runtime)