mte/src/crypto.ml

212 lines
5.4 KiB
OCaml
Raw Normal View History

2025-11-21 19:07:36 +01:00
(* TODO key format
- check what is the exact format in GNUNET
- endianess issue? *)
module EddsaPublicKey = struct
(* EdDSA and ECDHE public keys always point on Curve25519
and represented using the standard 256 bits Ed25519 compact format,
converted to Crockford Base32. *)
open Mirage_crypto_ec.Ed25519
type t = pub
let to_octets t = pub_to_octets t
2025-12-07 06:39:33 +01:00
let of_octets t =
pub_of_octets t |> function
2025-11-21 19:07:36 +01:00
| Error e -> Fmt.error "%a" Mirage_crypto_ec.pp_error e
2025-12-07 06:39:33 +01:00
| Ok v -> Ok v
let bin =
let of_octets o =
match of_octets o with Error e -> raise (Util.Bin_error e) | Ok v -> v
in
Bin.map (Bin.bytes 32) of_octets to_octets
2025-12-11 03:19:37 +01:00
let of_b32 s =
let open Syntax in
let* octets = B32.decode s in
let* pub = of_octets octets in
Ok pub
2025-12-07 06:39:33 +01:00
let jsont =
let to_b32 t = B32.encode (to_octets t) in
Jsont.of_of_string ~kind:"EddsaPublicKey" of_b32 ~enc:to_b32
let caqti =
Caqti_type.custom
~encode:(fun v -> Ok (to_octets v))
~decode:(fun v -> of_octets v)
Caqti_type.octets
2025-11-21 19:07:36 +01:00
end
2025-11-21 19:29:04 +01:00
module EddsaPrivateKey = struct
(* EdDSA and ECDHE public keys always point on Curve25519
and represented using the standard 256 bits Ed25519 compact format,
converted to Crockford Base32. *)
open Mirage_crypto_ec.Ed25519
type t = priv
2025-11-28 14:00:11 +01:00
let pub_of_priv = pub_of_priv
2025-11-21 19:29:04 +01:00
let to_octets t = priv_to_octets t
2025-12-15 08:56:44 +01:00
let of_octets t =
priv_of_octets t |> function
| Error err ->
let err = Fmt.str "%a" Mirage_crypto_ec.pp_error err in
Error err
| Ok v -> Ok v
let bin =
let of_octets_exn t = of_octets t |> Result.get_ok in
Bin.map (Bin.bytes 32) of_octets_exn to_octets
2025-11-21 19:29:04 +01:00
2025-12-07 06:39:33 +01:00
let jsont =
let of_b32 s =
let open Syntax in
let* octets = B32.decode s in
2025-12-15 08:56:44 +01:00
of_octets octets
2025-12-07 06:39:33 +01:00
in
let to_b32 t = B32.encode (to_octets t) in
Jsont.of_of_string ~kind:"EddsaPrivateKey" of_b32 ~enc:to_b32
2025-11-21 19:29:04 +01:00
end
2025-11-21 19:07:36 +01:00
module EddsaSignature : sig
type t
2025-12-03 16:19:11 +01:00
val sign : key:EddsaPrivateKey.t -> string -> t
2025-12-16 14:20:41 +01:00
(* Ok () on verification success *)
val verify : key:EddsaPublicKey.t -> t -> msg:string -> (unit, string) result
2025-11-21 19:29:04 +01:00
val to_octets : t -> string
2025-12-15 08:56:44 +01:00
val of_octets : string -> (t, string) result
2025-11-21 19:07:36 +01:00
val jsont : t Jsont.t
2025-11-21 19:29:04 +01:00
val bin : t Bin.t
2025-12-07 06:39:33 +01:00
val caqti : t Caqti_type.t
2025-11-21 19:07:36 +01:00
end = struct
(* TODO key format
endianess issue? *)
2025-12-07 06:29:06 +01:00
(* transmitted as 64-bytes base32
binary-encoded objects with just the R and S values *)
2025-11-21 19:07:36 +01:00
type t = string
2025-12-07 06:39:33 +01:00
(* mirage_crypto: "The result is the concatenation of r and s, as specified in RFC 8032." *)
let sign ~key s = Mirage_crypto_ec.Ed25519.sign ~key s
2025-12-16 14:20:41 +01:00
let verify ~key s ~msg =
let b = Mirage_crypto_ec.Ed25519.verify ~key s ~msg in
match b with
| false -> Error "signature verification failure: invalid signature"
| true -> Ok ()
2025-11-21 19:07:36 +01:00
let to_octets t = t
2025-11-21 19:29:04 +01:00
let of_octets v =
match String.length v = 64 with
| false ->
2025-12-15 08:56:44 +01:00
Fmt.error "EddsaSignature.of_octets failure: data is not 64 bytes."
| true -> Ok v
2025-11-21 19:29:04 +01:00
2025-12-15 08:56:44 +01:00
let bin =
let of_octets_exn t = of_octets t |> Result.get_ok in
Bin.map (Bin.bytes 64) of_octets_exn to_octets
2025-11-21 19:29:04 +01:00
2025-11-21 19:07:36 +01:00
let check_size t =
match String.length t = 64 with
| false -> Error "EddsaSignature: invalid string length"
| true -> Ok ()
2025-12-07 06:39:33 +01:00
let jsont =
let of_b32 s =
let open Syntax in
let* t = B32.decode s in
let+ () = check_size t in
t
in
let to_b32 = B32.encode in
Jsont.of_of_string ~kind:"EddsaSignature" of_b32 ~enc:to_b32
let caqti =
Caqti_type.custom
~encode:(fun v -> Ok (to_octets v))
2025-12-15 08:56:44 +01:00
~decode:(fun s -> of_octets s)
2025-12-07 06:39:33 +01:00
Caqti_type.octets
2025-11-21 19:07:36 +01:00
end
module RsaPublicKey = struct
open Mirage_crypto_pk
type t = Rsa.pub
2025-11-29 17:34:24 +01:00
let to_octets = Util.Bin_rsa.pub_to_octets
2025-12-07 06:39:33 +01:00
let of_octets = Util.Bin_rsa.pub_of_octets
2025-11-29 17:34:24 +01:00
2025-12-07 06:39:33 +01:00
let jsont =
let of_b32 s =
let open Syntax in
let* s = B32.decode s in
let+ v = of_octets s in
v
in
let to_b32 t = B32.encode (to_octets t) in
Jsont.of_of_string ~kind:"RsaPublicKey" of_b32 ~enc:to_b32
let caqti : t Caqti_type.t =
Caqti_type.custom
~encode:(fun v -> Ok (to_octets v))
~decode:(fun v -> of_octets v)
Caqti_type.octets
2025-11-21 19:07:36 +01:00
end
2025-11-29 14:03:37 +01:00
module RsaPrivateKey = struct
open Mirage_crypto_pk.Rsa
type t = priv
2025-12-15 16:43:49 +01:00
let generate ~bits () =
let priv = generate ~bits () in
let pub = pub_of_priv priv in
(priv, pub)
2025-11-29 14:03:37 +01:00
let pub_of_priv = pub_of_priv
2025-11-29 17:34:24 +01:00
let of_octets = Util.Bin_rsa.priv_of_octets
let to_octets = Util.Bin_rsa.priv_to_octets
2025-11-29 14:03:37 +01:00
2025-12-07 06:39:33 +01:00
let jsont =
let of_b32 s =
let open Syntax in
let* s = B32.decode s in
let+ v = of_octets s in
v
in
let to_b32 t = B32.encode (to_octets t) in
Jsont.of_of_string ~kind:"RsaPrivateKey" of_b32 ~enc:to_b32
2025-11-29 14:03:37 +01:00
end
2025-11-21 19:07:36 +01:00
module RsaSignature : sig
type t
val sign : key:Mirage_crypto_pk.Rsa.priv -> string -> t
val jsont : t Jsont.t
end = struct
type t = string
(* TODO rsa sign *)
let sign ~key s =
Mirage_crypto_pk.Rsa.decrypt ~crt_hardening:true ~mask:`Yes ~key s
2025-12-07 06:39:33 +01:00
let jsont =
let of_b32 s = B32.decode s in
let to_b32 t = B32.encode t in
Jsont.of_of_string ~kind:"RsaSignature" of_b32 ~enc:to_b32
2025-11-21 19:07:36 +01:00
end
2025-12-15 16:43:49 +01:00
(* some type aliases, just for prettier .mli *)
type eddsa_priv = EddsaPrivateKey.t
type eddsa_pub = EddsaPublicKey.t
type eddsa_sig = EddsaSignature.t
type rsa_priv = RsaPrivateKey.t
type rsa_pub = RsaPublicKey.t
type rsa_sig = RsaSignature.t
type denomination_hash = Bin_type.DenominationHash.t