Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to mirage_crypto 1.0.0 #334

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dream.opam
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ depends: [
"caqti-lwt" {>= "2.0.0"}
("conf-libev" {os != "win32"} | "ocaml" {os = "win32"})
"cstruct" {>= "6.0.0"}
"digestif" {>= "0.7"} # to_raw_string.
"dream-httpaf" {>= "1.0.0~alpha3"}
"dream-pure" {>= "1.0.0~alpha2"}
"dune" {>= "2.7.0"} # --instrument-with.
Expand All @@ -68,8 +69,8 @@ depends: [
"magic-mime"
"markup" {>= "1.0.2"}
"mirage-clock" {>= "3.0.0"} # now_d_ps : unit -> int * int64.
"mirage-crypto" {>= "0.8.1"} # AES-256-GCM.
"mirage-crypto-rng"
"mirage-crypto" {>= "1.0.0"}
"mirage-crypto-rng" {>= "1.0.0"}
"mirage-crypto-rng-lwt"
"multipart_form" {>= "0.4.0"}
"multipart_form-lwt"
Expand Down
32 changes: 12 additions & 20 deletions src/cipher/cipher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,26 @@ struct

let derive_key secret =
secret
|> Cstruct.of_string
|> Mirage_crypto.Hash.SHA256.digest
|> Mirage_crypto.Cipher_block.AES.GCM.of_secret
|> Digestif.SHA256.digest_string
|> Digestif.SHA256.to_raw_string
|> Mirage_crypto.AES.GCM.of_secret

(* TODO Memoize keys or otherwise avoid key derivation on every call. *)
let encrypt_with_nonce secret nonce plaintext associated_data =
let key = derive_key secret in
let adata = Option.map Cstruct.of_string associated_data in
let adata = associated_data in
let ciphertext =
Mirage_crypto.Cipher_block.AES.GCM.authenticate_encrypt
~key
~nonce
?adata
(Cstruct.of_string plaintext)
|> Cstruct.to_string
in
Mirage_crypto.AES.GCM.authenticate_encrypt ~key ~nonce ?adata plaintext in

"\x00" ^ (Cstruct.to_string nonce) ^ ciphertext
"\x00" ^ nonce ^ ciphertext

let encrypt ?associated_data ~secret plaintext =
encrypt_with_nonce
secret (Random.random_buffer 12) plaintext associated_data

let test_encrypt ?associated_data ~secret ~nonce plaintext =
encrypt_with_nonce
secret (Cstruct.of_string nonce) plaintext associated_data
secret nonce plaintext associated_data

let decrypt ?associated_data ~secret ciphertext =
let key = derive_key secret in
Expand All @@ -105,17 +99,15 @@ struct
if ciphertext.[0] != prefix then
None
else
let adata = Option.map Cstruct.of_string associated_data in
let adata = associated_data in
let plaintext =
Mirage_crypto.Cipher_block.AES.GCM.authenticate_decrypt
Mirage_crypto.AES.GCM.authenticate_decrypt
~key
~nonce:(Cstruct.of_string ~off:1 ~len:12 ciphertext)
~nonce:(String.sub ciphertext 1 12)
?adata
(Cstruct.of_string ciphertext ~off:13)
(String.sub ciphertext 13 (String.length ciphertext - 13))
in
match plaintext with
| None -> None
| Some plaintext -> Some (Cstruct.to_string plaintext)
plaintext
end

let secrets_field =
Expand Down
1 change: 1 addition & 0 deletions src/cipher/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(name dream__cipher)
(libraries
cstruct
digestif
dream-pure
mirage-crypto
mirage-crypto-rng
Expand Down
2 changes: 1 addition & 1 deletion src/cipher/random.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ let random_buffer n =
Mirage_crypto_rng.generate n

let random n =
Cstruct.to_string (random_buffer n)
random_buffer n
Loading