Commit 71f98814 authored by Stephane Glondu's avatar Stephane Glondu

Add platform AES primitive

parent 5e468bb6
......@@ -23,6 +23,8 @@ val sha256_hex : string -> string
val sha256_b64 : string -> string
val pbkdf2_hex : iterations:int -> salt:string -> string -> string
val aes_hex : key:string -> data:string -> string
type rng
val secure_rng : rng
val pseudo_rng : string -> rng
......
......@@ -56,6 +56,13 @@ let pbkdf2_hex ~iterations ~salt x =
in
hex_fromBits derived
let aes_hex ~key ~data =
let key = hex_toBits key in
let data = hex_toBits data in
let cipher = Js.Unsafe.(new_obj (get sjcl "cipher.aes") [| key |]) in
let output = Js.Unsafe.meth_call cipher "encrypt" [| data |] in
hex_fromBits output
type rng = unit -> unit
let sjcl_random = Js.Unsafe.get sjcl "random"
......
......@@ -69,6 +69,13 @@ let pbkdf2_hex ~iterations ~salt x =
pbkdf2 ~prf:MAC.hmac_sha256 ~iterations ~size:1 ~salt x |>
transform_string (Hexa.encode ())
let aes_hex ~key ~data =
let open Cryptokit in
let key = transform_string (Hexa.decode ()) key in
let data = transform_string (Hexa.decode ()) data in
let output = transform_string (Cipher.(aes ~mode:ECB key Encrypt)) data in
transform_string (Hexa.encode ()) output
type rng = Cryptokit.Random.rng
let secure_rng =
......
......@@ -74,6 +74,7 @@ module Tests = struct
let j = Z.of_bits "\x81\xab\xd3\xed\x0b\x19\x2e\x40\x7a\xca" in
let k = Z.of_string "956173156978067279948673" in
check "of_bits" (fun () -> Z.(j =% k));
check "AES" (fun () -> aes_hex ~key:"0000000000000000000000000000000000000000000000000000000000000000" ~data:"00000000000000000000000000000000" = "dc95c078a2408989ad48a21492842087");
Printf.ksprintf alert "%d tests were successful!" !ntests
let cmds = ["do_unit_tests", unit_tests]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment