···2233## Documentation
4455-Comments in the single interface file, [`nocrypto.mli`][nocrypto-mli]. Also available [online][docs].
55+Comments in the single interface file, [`nocrypto.mli`][nocrypto-mli].
66+Also available [online][docs].
6778The documentation is a work in progress. :)
891010+## Build
1111+1212+```bash
1313+pkg/pkg.ml build
1414+ --with-unix BOOL
1515+ --with-lwt BOOL
1616+ --xen BOOL
1717+ --freestanding BOOL`
1818+1919+pkg/pkg.ml test
2020+```
2121+922## FAQ
10231124#### RNG seeding
12251313-You get something like `Fatal error: exception Uncommon.Boot.Unseeded_generator` and ask yourself: "Is there a simple way to forget about seeding and have the thing working?"
2626+You get something like `Fatal error: exception Uncommon.Boot.Unseeded_generator`
2727+and ask yourself: "Is there a simple way to forget about seeding and have the
2828+thing working?"
14291530```OCaml
1631(* On pure Unix: *)
···2944#0 _mm_aeskeygenassist_si128 (__C=<optimized out>, __X=...)
3045```
31463232-`Nocrypto` has CPU acceleration support (`SSE2`+`AES-NI`), but it has no run-time autodetection yet. You
3333-compiled the library with acceleration, but you are using it on a machine that does not support it.
4747+`Nocrypto` has CPU acceleration support (`SSE2`+`AES-NI`), but it has no
4848+run-time autodetection yet. You compiled the library with acceleration, but you
4949+are using it on a machine that does not support it.
34503535-`./configure --disable-modernity` disables non-portable code.
5151+`pkg/pkg.ml build --accelerate false` force-disables non-portable code.
36523737-`./configure --enable-modernity` enables non-portable code if the build machine supports it.
5353+`pkg/pkg.ml build --accelerate true` force-enables non-portable code.
38543939-The flag defaults to `enable`.
4040-4141-A second way to disable this feature is by exporting `$NOCRYPTO_NO_ACCEL`
4242-environment variable during build. This is desirable, for example, when building
4343-via `opam`.
5555+Another way to specify the flags is not the `NOCRYPTO_ACCELERATE` environment
5656+variable. When `--accelerate` is not specified, it maches the capabilities of
5757+the build machine.
44584559[docs]: http://mirleft.github.io/ocaml-nocrypto
4660[nocrypto-mli]: https://github.com/mirleft/ocaml-nocrypto/blob/master/src/nocrypto.mli
···11+true: color(always)
22+true: bin_annot, safe_string
33+true: warn(A-4-29-33-40-41-42-43-34-44-48)
44+true: package(bytes), package(cstruct)
55+66+<src>: include
77+<src/*.ml{,i}>: package(zarith), package(sexplib), package(ppx_sexp_conv)
88+<src/*.cm{x,o}> and not <src/nocrypto.cmx>: for-pack(Nocrypto)
99+<src/*.cm{,x}a>: link_stubs(src/libnocrypto_stubs)
1010+1111+<unix>: include
1212+<unix/*.ml{,i}>: package(unix), package(bytes)
1313+1414+<lwt>: include
1515+<lwt/*.ml{,i}>: package(lwt.unix), package(cstruct.lwt)
1616+1717+<mirage>: include
1818+<mirage/*.ml{,i}>: package(lwt), package(mirage-entropy)
1919+2020+<**/*.c>: ccopt(--std=c99 -Wall -Wextra -O3)
2121+2222+<bench/*>: use_nocrypto, package(zarith), package(cstruct.unix)
2323+<tests/*>: use_nocrypto, package(zarith), package(oUnit)
2424+2525+<rondom>: -traverse
+1-1
bench/speed.ml
···991010 let time ~n f a =
1111 let t1 = Sys.time () in
1212- for i = 1 to n do ignore (f a) done ;
1212+ for _ = 1 to n do ignore (f a) done ;
1313 let t2 = Sys.time () in
1414 (t2 -. t1)
1515
···11-# OASIS_START
22-# DO NOT EDIT (digest: 2bc9bac9457cbd1ffbed67ed78dcd301)
31Uncommon
42Base64
53Hash
···1513Hmac_drgb
1614Gcm
1715Ccm
1818-# OASIS_STOP
+1-1
tests/dsa_tests.ml
···2182218221832183let private_key ~p ~q ~g ~x ~y = priv_of_hex ~p ~q ~gg:g ~x ~y
2184218421852185-let test_rfc6979 ~priv:({ Dsa.q; x } as priv) ~msg ~hash ~k ~r ~s _ =
21852185+let test_rfc6979 ~priv ~msg ~hash ~k ~r ~s _ =
21862186 let h1 = Hash.digest hash msg in
21872187 let k' =
21882188 let module H = (val (Hash.module_of hash)) in
+6-6
tests/testlib.ml
···261261 a5 23 69 38 7e ec b5 fc 4b 89 42 c4 32 fa e5 58
262262 6f 39 5d a7 4e cd b5 da dc 1e 52 fe a4 33 72 c1
263263 82 48 8a 5b c1 44 bc 60 9b 38 5b 80 5f 44 14 93"
264264- and x = Cs.of_hex
264264+ and s = Cs.of_hex
265265 "f9 47 87 95 d2 a1 6d d1 7c c8 a9 c0 71 28 a2 82
266266 71 95 7e 79 87 0b fc 34 a2 42 ec 42 ac cc 42 81
267267 7b f6 c4 f5 80 a9 70 e3 35 93 9b a3 21 81 a4 e3
···290290 in
291291 let grp = Dh.Group.oakley_5 in
292292293293- match Dh.(shared grp (fst (key_of_secret grp x)) gy) with
293293+ match Dh.(shared grp (fst (key_of_secret grp ~s)) gy) with
294294 | None -> assert_failure "degenerate shared secret"
295295 | Some shared' ->
296296 assert_cs_equal ~msg:"shared secret" shared shared'
···877877 ];
878878879879 "RNG extraction" >::: [
880880- random_n_selftest "int" Fc.Rng.int 1000 [
880880+ random_n_selftest ~typ:"int" Fc.Rng.int 1000 [
881881 (1, 2); (0, 129); (7, 136); (0, 536870913);
882882 ] ;
883883- random_n_selftest "int32" Fc.Rng.int32 1000 [
883883+ random_n_selftest ~typ:"int32" Fc.Rng.int32 1000 [
884884 (7l, 136l); (0l, 536870913l);
885885 ] ;
886886- random_n_selftest "int64" Fc.Rng.int64 1000 [
886886+ random_n_selftest ~typ:"int64" Fc.Rng.int64 1000 [
887887 (7L, 136L); (0L, 536870913L); (0L, 2305843009213693953L);
888888 ] ;
889889- random_n_selftest "Z" Fc.Rng.z 1000 [
889889+ random_n_selftest ~typ:"Z" Fc.Rng.z 1000 [
890890 Z.(of_int 7, of_int 135);
891891 Z.(of_int 0, of_int 536870913);
892892 Z.(of_int 0, of_int64 2305843009213693953L)
+11-4
tests/testrunner.ml
···22open OUnit2
3344(* Gather quantum uncertainty. *)
55+(* let () = *)
66+(* let t = Unix.gettimeofday () in *)
77+(* let cs = Cstruct.create 8 in *)
88+(* Cstruct.BE.set_uint64 cs 0 Int64.(of_float (t *. 1000.)) ; *)
99+(* Nocrypto.Rng.reseed cs *)
1010+1111+let () = Nocrypto_entropy_unix.initialize ()
1212+513let () =
66- let t = Unix.gettimeofday () in
77- let cs = Cstruct.create 8 in
88- Cstruct.BE.set_uint64 cs 0 Int64.(of_float (t *. 1000.)) ;
99- Nocrypto.Rng.reseed cs
1414+ Format.printf "AES mode: %s\n%!"
1515+ (match Nocrypto.Cipher_block.AES.mode with
1616+ | `AES_NI -> "AES-NI" | `Generic -> "soft")
10171118let () =
1219(* Nocrypto.Rng.reseed @@ Cstruct.of_string "\001\002\003\004" ; *)
+1-1
unix/nocrypto_entropy_unix.ml
···2020 let buf = Bytes.create n in
2121 let k = Unix.read fd buf 0 n in
2222 let cs = Cstruct.create k in
2323- Cstruct.blit_from_string buf 0 cs 0 k ;
2323+ Cstruct.blit_from_bytes buf 0 cs 0 k ;
2424 cs
25252626let reseed ?(bytes = a_little) ?(device = sys_rng) g =