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

update to mirage-crypto 0.11.0 API #3

Merged
merged 2 commits into from
Feb 14, 2023
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
42 changes: 5 additions & 37 deletions lib/flow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,44 +169,12 @@ let handshake_server ctx ?g ~password ~identity (Cfg (algorithm, arguments)) =
let+ shared_keys = Spoke.server_finalize ~server packet in
return (ciphers, shared_keys)

module type CIPHER_BLOCK = sig
type key

val authenticate_encrypt :
key:key -> nonce:Cstruct.t -> ?adata:Cstruct.t -> Cstruct.t -> Cstruct.t

val authenticate_decrypt :
key:key ->
nonce:Cstruct.t ->
?adata:Cstruct.t ->
Cstruct.t ->
Cstruct.t option

val of_secret : Cstruct.t -> key
val tag_size : int
end

type 'k cipher_block = (module CIPHER_BLOCK with type key = 'k)
type 'k cipher_block = (module Mirage_crypto.AEAD with type key = 'k)

let module_of : type k. k Spoke.aead -> k cipher_block = function
| Spoke.GCM -> (module Mirage_crypto.Cipher_block.AES.GCM)
| Spoke.CCM ->
let module M = struct
include Mirage_crypto.Cipher_block.AES.CCM

let of_secret =
of_secret ~maclen:16 (* XXX(dinosaure): [16] comes from [ocaml-tls] *)

let tag_size = 16
end in
(module M)
| Spoke.ChaCha20_Poly1305 ->
let module M = struct
include Mirage_crypto.Chacha20

let tag_size = Mirage_crypto.Poly1305.mac_size
end in
(module M)
| Spoke.CCM16 -> (module Mirage_crypto.Cipher_block.AES.CCM16)
| Spoke.ChaCha20_Poly1305 -> (module Mirage_crypto.Chacha20)

module Make (Flow : Mirage_flow.S) = struct
open Lwt.Infix
Expand Down Expand Up @@ -276,13 +244,13 @@ module Make (Flow : Mirage_flow.S) = struct
let key_len =
match aead with
| Spoke.GCM -> 32
| Spoke.CCM -> 32
| Spoke.CCM16 -> 32
| Spoke.ChaCha20_Poly1305 -> 32
in
let nonce_len =
match aead with
| Spoke.GCM -> 12
| Spoke.CCM -> 12
| Spoke.CCM16 -> 12
| Spoke.ChaCha20_Poly1305 -> 12
in
let module Cipher_block = (val module_of aead) in
Expand Down
6 changes: 3 additions & 3 deletions lib/spoke.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type a = Algorithm : 'a algorithm -> a

type _ aead =
| GCM : Mirage_crypto.Cipher_block.AES.GCM.key aead
| CCM : Mirage_crypto.Cipher_block.AES.CCM.key aead
| CCM16 : Mirage_crypto.Cipher_block.AES.CCM16.key aead
| ChaCha20_Poly1305 : Mirage_crypto.Chacha20.key aead

type cipher = AEAD : 'k aead -> cipher
Expand All @@ -114,12 +114,12 @@ let hash_of_int : int -> hash = function

let int_of_cipher = function
| AEAD GCM -> 0
| AEAD CCM -> 1
| AEAD CCM16 -> 1
| AEAD ChaCha20_Poly1305 -> 2

let cipher_of_int = function
| 0 -> AEAD GCM
| 1 -> AEAD CCM
| 1 -> AEAD CCM16
| 2 -> AEAD ChaCha20_Poly1305
| _ -> raise Invalid_cipher

Expand Down
2 changes: 1 addition & 1 deletion lib/spoke.mli
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type 'a algorithm = Pbkdf2 : int algorithm
(** The type of Authenticated Encryptions with Associated Data. *)
type _ aead =
| GCM : Mirage_crypto.Cipher_block.AES.GCM.key aead
| CCM : Mirage_crypto.Cipher_block.AES.CCM.key aead
| CCM16 : Mirage_crypto.Cipher_block.AES.CCM16.key aead
| ChaCha20_Poly1305 : Mirage_crypto.Chacha20.key aead

(** The type of ciphers. *)
Expand Down
23 changes: 13 additions & 10 deletions spoke.opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ build: [ "dune" "build" "-p" name "-j" jobs ]
run-test: [ "dune" "runtest" "-p" name "-j" jobs ]

depends: [
"ocaml" {>= "4.07.0"}
"dune" {>= "2.8.0"}
"ocaml" {>= "4.08.0"}
"dune" {>= "2.9.0"}
"fmt"
"hxd"
"logs"
"base64"
"digestif"
"encore"
"base64" {>= "3.0.0"}
"digestif" {>= "0.8.1"}
"bigstringaf" {>= "0.9.0"}
"encore" {>= "0.8"}
"ke"
"mirage-crypto"
"mirage-flow"
"mimic" {with-test}
"rresult" {with-test}
"tcpip" {with-test}
"mirage-crypto" {>= "0.11.0"}
"mirage-flow" {>= "3.0.0"}
"lwt" {>= "5.6.1"}
"result" {>= "1.5"}
"mimic" {with-test}
"rresult" {with-test}
"tcpip" {with-test}
]