Skip to content

Commit

Permalink
style: 🎨 formatted OCaml code and Dune stanzas
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Aurélio da Silva <[email protected]>
  • Loading branch information
marcoonroad committed Aug 4, 2019
1 parent e78c9f5 commit d4cdca8
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dune-workspace
.merlin
bisect*.out
nocoiner.install
TRASH/
node_modules/
# Ignore folders generated by Bundler
vendor
Expand Down
3 changes: 2 additions & 1 deletion bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
; (flags :standard -ccopt -static)
; (ocamlopt_flags :standard -ccopt -static)
; (link_flags :standard -cclib -static)
(modes (best exe))
(modes
(best exe))
(modules program helpers commands))
1 change: 1 addition & 0 deletions docs/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(dirs :standard \ vendor)
2 changes: 2 additions & 0 deletions dune
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
(release
(ocamlopt_flags
(:standard -O3))))

(dirs :standard \ TRASH)
17 changes: 9 additions & 8 deletions lib/encryption.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ let __kdf key =
let mac_key = Hardening.kdf ~size:32l ~salt:mac_salt key in
(aes_key, mac_key)

let hash data =
Cstruct.of_hex @@ Hashing.hash @@ Cstruct.to_string data
let hash data = Cstruct.of_hex @@ Hashing.hash @@ Cstruct.to_string data

let mac ~key data =
let key', data' = Cstruct.to_string key, Cstruct.to_string data in
let key', data' = (Cstruct.to_string key, Cstruct.to_string data) in
Cstruct.of_hex @@ Hashing.mac ~key:key' data'

let encrypt ~key ~iv ~metadata ~message:msg =
let (aes_key, mac_key) = __kdf key in
let aes_key, mac_key = __kdf key in
let aes_key' = AES.of_secret aes_key in
let plaintext = Helpers.pad ~basis:16 @@ Cstruct.to_string msg in
let ciphertext = AES.encrypt ~iv ~key:aes_key' @@ Cstruct.of_string plaintext in
let ciphertext =
AES.encrypt ~iv ~key:aes_key' @@ Cstruct.of_string plaintext
in
let secret = hash mac_key in
let payload = Cstruct.concat [ metadata; iv; ciphertext ] in
let payload = Cstruct.concat [metadata; iv; ciphertext] in
let tag = mac ~key:secret payload in
(ciphertext, tag)

let decrypt ~reason ~key ~iv ~metadata ~cipher ~tag =
let (aes_key, mac_key) = __kdf key in
let aes_key, mac_key = __kdf key in
let secret = hash mac_key in
let payload = Cstruct.concat [ metadata; iv; cipher ] in
let payload = Cstruct.concat [metadata; iv; cipher] in
let tag' = mac ~key:secret payload in
if Cstruct.equal tag tag' then
let aes_key' = AES.of_secret aes_key in
Expand Down
3 changes: 3 additions & 0 deletions lib/entropy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module RngZ = Nocrypto.Rng.Z
module NumZ = Nocrypto.Numeric.Z

let __gen_min bits = "1" ^ Core.String.init (bits - 1) ~f:(Core.const '0')

let __gen_max bits = Core.String.init bits ~f:(Core.const '1')

let __max_bits bits = Z.of_string_base 2 @@ __gen_max bits

let __min_bits bits = Z.of_string_base 2 @@ __gen_min bits

let __random_bits bits =
Expand All @@ -13,6 +15,7 @@ let __random_bits bits =
NumZ.to_cstruct_be @@ RngZ.gen_r _min_random _max_random

let max_bits bits = NumZ.to_cstruct_be @@ __max_bits bits

let min_bits bits = NumZ.to_cstruct_be @@ __min_bits bits

let key () = __random_bits 256
Expand Down
17 changes: 9 additions & 8 deletions lib/fingerprint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module List = Core.List
let hash data = Cstruct.of_hex @@ Hashing.hash data

let xor = Nocrypto.Uncommon.Cs.xor

let xor_list = List.reduce_exn ~f:xor

let id ( ) =
let timestamp = hash @@ string_of_float @@ Unix.gettimeofday ( ) in
let pid = hash @@ string_of_int @@ Unix.getpid ( ) in
let hostname = hash @@ Unix.gethostname ( ) in
let cwd = hash @@ Unix.getcwd ( ) in
let context = Cstruct.to_string @@ xor_list [
timestamp; pid; hostname; cwd
] in
let id () =
let timestamp = hash @@ string_of_float @@ Unix.gettimeofday () in
let pid = hash @@ string_of_int @@ Unix.getpid () in
let hostname = hash @@ Unix.gethostname () in
let cwd = hash @@ Unix.getcwd () in
let context =
Cstruct.to_string @@ xor_list [timestamp; pid; hostname; cwd]
in
Encoding.encode @@ Hashing.raw_hash context
9 changes: 3 additions & 6 deletions lib/hashing.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module Blake2B = Digestif.BLAKE2B

let hash data =
Blake2B.to_hex @@ Blake2B.digest_string data
let hash data = Blake2B.to_hex @@ Blake2B.digest_string data

let raw_hash data =
Blake2B.to_raw_string @@ Blake2B.digest_string data
let raw_hash data = Blake2B.to_raw_string @@ Blake2B.digest_string data

let mac ~key data =
Blake2B.to_hex @@ Blake2B.Keyed.mac_string ~key data
let mac ~key data = Blake2B.to_hex @@ Blake2B.Keyed.mac_string ~key data
2 changes: 2 additions & 0 deletions lib/hashing.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
val hash : string -> string

val raw_hash : string -> string

val mac : key:string -> string -> string
13 changes: 8 additions & 5 deletions lib/nocoiner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ module Reasons = Exceptions
module String = Core.String
module List = Core.List

let __concat_on separator left right =
left ^ separator ^ right
let __concat_on separator left right = left ^ separator ^ right

let __join ~on list =
list
Expand All @@ -16,7 +15,7 @@ let commit payload =
let metadata = Cstruct.of_string @@ Fingerprint.id () in
let message = Cstruct.of_string @@ Encoding.encode payload in
let cipher, tag = Encryption.encrypt ~key ~iv ~metadata ~message in
let commitment = __join ~on:"@" [ metadata; iv; cipher; tag ] in
let commitment = __join ~on:"@" [metadata; iv; cipher; tag] in
let opening = Encoding.encode_blob key in
(commitment, opening)

Expand All @@ -37,6 +36,10 @@ let __split ~reason ~on data =
let reveal ~commitment ~opening =
let open Reasons in
let key = __decode ~reason:InvalidOpening opening in
let metadata, iv, cipher, tag = __split ~reason:InvalidCommitment ~on:'@' commitment in
let payload = Encryption.decrypt ~reason:BindingFailure ~key ~iv ~metadata ~cipher ~tag in
let metadata, iv, cipher, tag =
__split ~reason:InvalidCommitment ~on:'@' commitment
in
let payload =
Encryption.decrypt ~reason:BindingFailure ~key ~iv ~metadata ~cipher ~tag
in
Encoding.decode @@ Cstruct.to_string payload
2 changes: 1 addition & 1 deletion test/spec/commit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let __nondeterministic_commitment _ =
let c1, o1 = Nocoiner.commit _SECRET in
let c2, o2 = Nocoiner.commit _SECRET in
let c3, o3 = Nocoiner.commit _SECRET in
let ky1, ky2, ky3 = o1, o2, o3 in
let ky1, ky2, ky3 = (o1, o2, o3) in
let mt1, iv1, cp1, tg1 = Helpers.__split ~on:'@' c1 in
let mt2, iv2, cp2, tg2 = Helpers.__split ~on:'@' c2 in
let mt3, iv3, cp3, tg3 = Helpers.__split ~on:'@' c3 in
Expand Down

0 comments on commit d4cdca8

Please sign in to comment.