diff options
-rw-r--r-- | src/ocaml/botan.ml | 50 | ||||
-rw-r--r-- | src/ocaml/botan.mli | 3 |
2 files changed, 42 insertions, 11 deletions
diff --git a/src/ocaml/botan.ml b/src/ocaml/botan.ml index 8abdd04dc..091f44c27 100644 --- a/src/ocaml/botan.ml +++ b/src/ocaml/botan.ml @@ -10,12 +10,16 @@ open Foreign exception Botan_Error of int -(* TODO: translate error code to string *) +(* TODO: translate error code to string +TODO: Don't evaluate res unless rc == 0 +*) let result_or_exn rc res = match rc with | 0 -> res | _ as ec -> raise (Botan_Error ec) +let to_size_t i = Unsigned.Size_t.of_int i +let to_uint32 i = Unsigned.UInt32.of_int i module Botan = struct @@ -51,10 +55,30 @@ module Botan = struct foreign "botan_hex_encode" (string @-> size_t @-> ptr char @-> uint32_t @-> returning int) in let bin_len = String.length bin in let hex_len = 2*bin_len in - let hex = allocate_n char hex_len in - let rc = hex_encode bin (Unsigned.Size_t.of_int bin_len) hex (Unsigned.UInt32.of_int 0) in + let hex = allocate_n ~count:hex_len char in + let rc = hex_encode bin (to_size_t bin_len) hex (to_uint32 0) in result_or_exn rc (string_from_ptr hex hex_len) + (* Bcrypt *) + let bcrypt pass rng work_factor = + let bcrypt = + foreign "botan_bcrypt_generate" (ptr char @-> ptr size_t @-> + string @-> ptr void @-> size_t @-> uint32_t @-> returning int) in + let bcrypt_size = 61 (* FIXME *) in + let alloc_size = allocate size_t (to_size_t bcrypt_size) in + let res = allocate_n ~count:bcrypt_size char in + let rc = bcrypt res alloc_size pass rng (to_size_t work_factor) (to_uint32 0) in + result_or_exn rc (string_from_ptr res (Unsigned.Size_t.to_int (!@ alloc_size))) + + let check_bcrypt pass hash = + let check_bcrypt = + foreign "botan_bcrypt_is_valid" (string @-> string @-> returning int) in + let rc = check_bcrypt pass hash in + match rc with + | 0 -> true + | -100 -> false + | _ as ec -> raise (Botan_Error ec) + module Hash = struct type t = unit ptr let hash_t : t typ = ptr void @@ -63,7 +87,7 @@ module Botan = struct let hash_init = foreign "botan_hash_init" (ptr hash_t @-> string @-> uint32_t @-> returning int) in let o = allocate_n ~count:1 hash_t in - let rc = hash_init o name (Unsigned.UInt32.of_int 0) in + let rc = hash_init o name (to_uint32 0) in result_or_exn rc (!@ o) let destroy hash = @@ -89,7 +113,7 @@ module Botan = struct let hash_update = foreign "botan_hash_update" (hash_t @-> string @-> size_t @-> returning int) in let input_len = (String.length input) in - let rc = hash_update hash input (Unsigned.Size_t.of_int input_len) in + let rc = hash_update hash input (to_size_t input_len) in result_or_exn rc () let final hash = @@ -110,7 +134,7 @@ module Botan = struct let rng_init = foreign "botan_rng_init" (ptr rng_t @-> string @-> uint32_t @-> returning int) in let o = allocate_n ~count:1 rng_t in - let rc = rng_init o name (Unsigned.UInt32.of_int 0) in + let rc = rng_init o name (to_uint32 0) in result_or_exn rc (!@ o) let destroy rng = @@ -123,20 +147,21 @@ module Botan = struct let rng_generate = foreign "botan_rng_get" (rng_t @-> ptr char @-> size_t @-> returning int) in let res = allocate_n ~count:out_len char in - let rc = rng_generate rng res (Unsigned.Size_t.of_int out_len) in + let rc = rng_generate rng res (to_size_t out_len) in result_or_exn rc (string_from_ptr res out_len) let reseed rng bits = let rng_reseed = foreign "botan_rng_reseed" (rng_t @-> size_t @-> returning int) in - let rc = rng_reseed rng (Unsigned.Size_t.of_int bits) in + let rc = rng_reseed rng (to_size_t bits) in + result_or_exn rc () let update rng input = let rng_update = foreign "botan_rng_update" (rng_t @-> string @-> size_t @-> returning int) in let input_len = (String.length input) in - let rc = rng_update rng input (Unsigned.Size_t.of_int input_len) in + let rc = rng_update rng input (to_size_t input_len) in result_or_exn rc () end (* RNG *) @@ -144,8 +169,11 @@ module Botan = struct end (* Botan *) let () = - let rng = Botan.RNG.create "user" in - print_string (Botan.hex_encode (Botan.RNG.generate rng 11) ^ "\n") + let rng = Botan.RNG.create "system" in + let bcrypt = Botan.bcrypt "pass" rng 10 in + let ok = Botan.check_bcrypt "pass" bcrypt in + let nok = Botan.check_bcrypt "something else" bcrypt in + print_string (Printf.sprintf "%s %B %B\n" bcrypt ok nok) let () = let (maj,min,patch) = Botan.version in diff --git a/src/ocaml/botan.mli b/src/ocaml/botan.mli index ec7e71dfe..0407177bb 100644 --- a/src/ocaml/botan.mli +++ b/src/ocaml/botan.mli @@ -22,6 +22,9 @@ module Botan : sig val reseed : t -> int -> unit end + val bcrypt : string -> RNG.t -> int -> string + val check_bcrypt : string -> string -> bool + module Hash : sig type t val create : string -> t |