aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-02-13 21:30:31 +0100
committerpukkamustard <pukkamustard@posteo.net>2021-02-13 21:30:31 +0100
commit9510d6d45023f5292f8c4fc43edba9fd66c5454d (patch)
treee5a50907ad7a08d092056dab2405e19410a8d82e
parentfda4420f090f617c9d1685134bff1eaa8f8be470 (diff)
Add Monocypher.Advanced.IETF_ChaCha20 module
-rw-r--r--ffi/bindings/monocypher_bindings.ml4
-rw-r--r--guix.scm3
-rw-r--r--lib/dune2
-rw-r--r--lib/monocypher.ml16
-rw-r--r--lib/monocypher.mli14
5 files changed, 37 insertions, 2 deletions
diff --git a/ffi/bindings/monocypher_bindings.ml b/ffi/bindings/monocypher_bindings.ml
index e04b0af..c1dc472 100644
--- a/ffi/bindings/monocypher_bindings.ml
+++ b/ffi/bindings/monocypher_bindings.ml
@@ -10,4 +10,8 @@ module M(F: Ctypes.FOREIGN) = struct
let crypto_blake2b_general =
foreign "crypto_blake2b_general"
C.(ptr char @-> size_t @-> string_opt @-> size_t @-> string @-> size_t @-> returning void)
+
+ let crypto_ietf_chacha20_ctr =
+ foreign "crypto_ietf_chacha20_ctr"
+ C.(ptr char @-> string @-> size_t @-> string @-> string @-> uint32_t @-> returning uint32_t)
end
diff --git a/guix.scm b/guix.scm
index 3446b19..afaf04c 100644
--- a/guix.scm
+++ b/guix.scm
@@ -14,7 +14,8 @@
(build-system dune-build-system)
(arguments '())
(propagated-inputs
- `(("ocaml-ctypes" ,ocaml-ctypes)))
+ `(("ocaml-integers" ,ocaml-integers)
+ ("ocaml-ctypes" ,ocaml-ctypes)))
(native-inputs
`(("merlin" ,ocaml-merlin)
("dot-merlin-reader" ,ocaml-dot-merlin-reader)
diff --git a/lib/dune b/lib/dune
index 89be42c..c9540a8 100644
--- a/lib/dune
+++ b/lib/dune
@@ -1,3 +1,3 @@
(library
(name monocypher)
- (libraries monocypher.ffi ctypes))
+ (libraries monocypher.ffi ctypes integers))
diff --git a/lib/monocypher.ml b/lib/monocypher.ml
index a62fb42..b3efdd3 100644
--- a/lib/monocypher.ml
+++ b/lib/monocypher.ml
@@ -28,3 +28,19 @@ module Hashing = struct
string_from_ptr hash_ptr ~length:size
end
end
+
+module Advanced = struct
+ module IETF_ChaCha20 = struct
+ let crypt ~key ~nonce ?(ctr=0) msg =
+ let cipher_ptr = allocate_n char ~count:(String.length msg) in
+ let msg_size = String.length msg in
+ let _next_ctr = M.crypto_ietf_chacha20_ctr
+ cipher_ptr
+ msg (Unsigned.Size_t.of_int msg_size)
+ key
+ nonce
+ (Unsigned.UInt32.of_int ctr)
+ in
+ string_from_ptr cipher_ptr ~length:msg_size
+ end
+end
diff --git a/lib/monocypher.mli b/lib/monocypher.mli
index 805d64a..5dd6ee1 100644
--- a/lib/monocypher.mli
+++ b/lib/monocypher.mli
@@ -5,8 +5,22 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*)
+
module Hashing : sig
module Blake2b : sig
+ (** BLAKE2b is a fast cryptographically secure hash, based on the ideas of
+ Chacha20. It is faster than MD5, yet just as secure as SHA-3. *)
+
+ (** [digest ~key ~size msg] return the Blake2b hash of [msg] of size [size] bytes (defaults to 64). If [key] is not specified no key is used.*)
val digest : ?key:string -> ?size:int -> string -> string
end
end
+
+module Advanced : sig
+ module IETF_ChaCha20 : sig
+ (** These functions provide an interface for the Chacha20 encryption primitive as specified by the IETF in RFC 8439. *)
+
+ (** [crypt ~key ~nonce ~ctr data] returns the XOR of the IETF ChaCha20 applied to [data] using key [key] (32-byte) nonce [nonce] (12-byte). Counter [ctr] (defaults to 0) may be used to specify position in ChaCha20 stream.*)
+ val crypt : key:string -> nonce:string -> ?ctr:int -> string -> string
+ end
+end