mte/src/secmod_rsa.ml

239 lines
6.9 KiB
OCaml
Raw Normal View History

2026-02-17 09:30:20 +01:00
let src = Logs.Src.create "mte.secmod_rsa"
module Log = (val Logs.src_log src : Logs.LOG)
(* - *)
open Syntax
open Time
2026-03-20 22:47:58 +01:00
module DenominationHash = Hash.DenominationHash
module Coin = Config.Coin
2026-03-19 23:01:04 +01:00
module Cfg = struct
2026-03-19 23:01:04 +01:00
include Config.Exchange_secmod_rsa
2026-03-21 07:38:29 +01:00
let key_dir = "/RSA"
let sm_key = "/SM_RSA"
end
2026-02-17 09:30:20 +01:00
type key = {
coin: Coin.t;
2026-03-20 22:47:58 +01:00
priv: Rsa.priv;
pub: Rsa.pub;
2026-02-24 17:23:03 +01:00
h_pub: DenominationHash.t;
2026-02-24 17:51:30 +01:00
t1: TimeAbsolute.t;
t2: TimeAbsolute.t;
2026-02-17 09:30:20 +01:00
}
type t = {
2026-03-11 14:17:52 +01:00
fs: Fat.t;
2026-03-20 22:47:58 +01:00
sm_priv: Eddsa.priv;
sm_pub: Eddsa.pub;
2026-02-24 17:23:03 +01:00
ht: (DenominationHash.t, key) Hashtbl.t;
2026-02-17 09:30:20 +01:00
}
let find_exn h_pub section_name =
Coin.all_coins
|> Iarray.find_opt (fun coin ->
String.equal section_name coin.Coin.section_name)
|> function
| Some coin -> coin
| None ->
Fmt.failwith
"Secmod_rsa denomination key loading failure on key `%a`: unknown \
section_name [%s]"
DenominationHash.pp h_pub section_name
(* ? enforce all to be of the same size instead *)
(* we use Bin.cstring + b32 binary encoding because
rsa keysize is not known and then we have to escape '\x00' *)
let rsa_private_key_bin =
let decode_exn o =
Rsa.priv_of_b32 o |> function Error e -> invalid_arg e | Ok v -> v
in
let encode o = Rsa.priv_to_b32 o in
Bin.map Bin.cstring decode_exn encode
2026-03-11 14:17:52 +01:00
let key_bin =
let open Bin in
record (fun t1 t2 section_name priv ->
2026-03-20 22:47:58 +01:00
let pub = Rsa.pub_of_priv priv in
let h_pub = DenominationHash.hash pub in
let coin = find_exn h_pub section_name in
{ coin; t1; t2; priv; pub; h_pub })
2026-03-11 14:17:52 +01:00
|+ field TimeAbsolute.bin (fun t -> t.t1)
|+ field TimeAbsolute.bin (fun t -> t.t2)
|+ field cstring (fun t -> t.coin.section_name)
|+ field rsa_private_key_bin (fun t -> t.priv)
2026-03-11 14:17:52 +01:00
|> sealr
let key_spath k =
2026-03-21 07:38:29 +01:00
let sfn = String.sub (DenominationHash.to_b32 k.h_pub) 0 8 in
Fat.Path.add Cfg.key_dir sfn
2026-03-11 14:17:52 +01:00
let read_eddsa fs spath =
2026-03-21 07:38:29 +01:00
Log.debug (fun m -> m "reading key file `%s`" spath);
let* data = Fat.read fs spath in
let* priv = Eddsa.priv_of_octets data |> Result.map_error (fun e -> `Msg e) in
2026-03-20 22:47:58 +01:00
let pub = Eddsa.pub_of_priv priv in
2026-03-11 14:17:52 +01:00
Ok (priv, pub)
let write_eddsa fs spath priv =
2026-03-21 07:38:29 +01:00
Log.debug (fun m -> m "writing key file `%s`" spath);
2026-03-20 22:47:58 +01:00
let data = Eddsa.priv_to_octets priv in
Fat.write fs spath data
2026-03-11 14:17:52 +01:00
let read_key fs spath =
2026-03-21 07:38:29 +01:00
Log.debug (fun m -> m "reading key file `%s`" spath);
let* s = Fat.read fs spath in
let+ k = Bbin.decode key_bin s in
k
2026-03-11 14:17:52 +01:00
let write_key fs k =
let spath = key_spath k in
2026-03-21 07:38:29 +01:00
Log.debug (fun m -> m "writing key file `%s`" spath);
let* s = Bbin.encode key_bin k in
Fat.write fs spath s
2026-03-11 14:17:52 +01:00
let delete_file fs spath =
2026-03-21 07:38:29 +01:00
Log.debug (fun m -> m "delete key file `%s`" spath);
let+ () = Fat.remove fs spath in
()
2026-02-17 09:30:20 +01:00
(* -- *)
let gen_key coin t1 t2 =
let bits = coin.Coin.rsa_keysize in
let priv, pub = Rsa.generate ~bits () in
let h_pub = DenominationHash.hash pub in
let k = { coin; priv; pub; h_pub; t1; t2 } in
2026-02-17 09:30:20 +01:00
Log.debug (fun m ->
m "generated key for coin [%s]: `%a`" coin.section_name
DenominationHash.pp k.h_pub);
2026-03-11 14:17:52 +01:00
k
2026-02-17 09:30:20 +01:00
2026-02-24 17:51:30 +01:00
let sort_keys l = List.sort (fun a b -> TimeAbsolute.compare a.t2 b.t2) l
2026-02-17 09:30:20 +01:00
let split_in_periodes coin ~start ~end_ =
2026-02-17 09:30:20 +01:00
assert (start < end_);
let duration_withdraw = coin.Coin.duration_withdraw in
2026-02-17 09:30:20 +01:00
(* no overlap on first periode *)
let t1 = start in
let t2 = TimeAbsolute.add start duration_withdraw in
2026-02-17 09:30:20 +01:00
let acc = [ (t1, t2) ] in
let start = t2 in
let rec go acc start end_ =
2026-02-24 17:51:30 +01:00
let t1 = TimeAbsolute.sub start Cfg.overlap_duration in
let t2 = TimeAbsolute.add start duration_withdraw in
2026-02-17 09:30:20 +01:00
if t2 > end_ then acc else go ((t1, t2) :: acc) t2 end_
in
go acc start end_
let gen_additional_keys_until_lookahead coin ~now l =
2026-02-17 09:30:20 +01:00
let start =
match List.rev (sort_keys l) with
| [] -> now
2026-02-24 17:51:30 +01:00
| hd :: _ -> TimeAbsolute.sub hd.t2 Cfg.overlap_duration
2026-02-17 09:30:20 +01:00
in
2026-02-24 17:51:30 +01:00
let end_ = TimeAbsolute.add now Cfg.lookahead_sign in
if TimeAbsolute.compare start end_ >= 0 then []
2026-02-17 09:30:20 +01:00
else
let periodes = split_in_periodes coin ~start ~end_ in
let new_keys = List.map (fun (t1, t2) -> gen_key coin t1 t2) periodes in
2026-02-17 09:30:20 +01:00
new_keys
2026-03-11 14:17:52 +01:00
let load fs =
let* () =
if Fat.exists fs Cfg.key_dir then Ok () else Fat.mkdir fs Cfg.key_dir
2026-03-11 14:17:52 +01:00
in
let* l = Fat.ls fs Cfg.key_dir in
2026-03-21 07:38:29 +01:00
let l = List.map (fun entry -> Fat.Path.add Cfg.key_dir entry.Fat.name) l in
let l = List.filter (fun spath -> not @@ String.equal Cfg.sm_key spath) l in
2026-03-11 14:17:52 +01:00
let* keys = list_map (read_key fs) l in
2026-02-17 09:30:20 +01:00
match keys with
| [] -> Ok None
| _l ->
2026-03-19 23:01:04 +01:00
let* sm_priv, sm_pub = read_eddsa fs Cfg.sm_key in
2026-02-17 09:30:20 +01:00
let ht = Hashtbl.create 0xff in
2026-02-24 17:23:03 +01:00
let () = List.iter (fun k -> Hashtbl.replace ht k.h_pub k) keys in
2026-03-11 14:17:52 +01:00
Ok (Some { fs; sm_priv; sm_pub; ht })
2026-02-17 09:30:20 +01:00
2026-03-11 14:17:52 +01:00
let init fs =
let* opt = load fs in
let now = TimeAbsolute.of_ptime (Mirage_ptime.now ()) in
2026-02-17 09:30:20 +01:00
let* t =
match opt with
| Some t -> Ok t
| None ->
2026-03-20 22:47:58 +01:00
let sm_priv, sm_pub = Eddsa.generate () in
Log.debug (fun m -> m "generated secmod key: `%a`" Eddsa.pp_pub sm_pub);
2026-03-19 23:01:04 +01:00
let* () = write_eddsa fs Cfg.sm_key sm_priv in
2026-02-17 09:30:20 +01:00
let ht = Hashtbl.create 0xff in
2026-03-11 14:17:52 +01:00
Ok { fs; sm_priv; sm_pub; ht }
2026-02-17 09:30:20 +01:00
in
let all_keys = List.of_seq @@ Hashtbl.to_seq_values t.ht in
let new_keys_l =
Coin.all_coins
|> Iarray.to_list
|> List.map (fun coin ->
let keys =
List.filter
(fun k -> String.equal coin.Coin.section_name k.coin.section_name)
all_keys
in
gen_additional_keys_until_lookahead coin ~now keys)
2026-02-17 09:30:20 +01:00
in
let new_keys = List.concat new_keys_l in
List.iter (fun k -> Hashtbl.replace t.ht k.h_pub k) new_keys;
2026-03-11 14:17:52 +01:00
let+ () = list_iter (write_key fs) new_keys in
2026-02-17 09:30:20 +01:00
t
2026-03-11 14:17:52 +01:00
module Make (Fs : Fat.FS) = struct
2026-02-17 09:30:20 +01:00
let t =
2026-03-11 14:17:52 +01:00
match init Fs.t with
| Error e ->
Fmt.failwith "secmod_rsa initialization failure: %a." Result.pp_err e
2026-02-17 09:30:20 +01:00
| Ok t -> t
let find_exn h_pub =
Log.debug (fun m -> m "find_exn: `%a`" DenominationHash.pp h_pub);
match Hashtbl.find_opt t.ht h_pub with
| Some v -> v
| None -> Fmt.failwith "secmod_rsa operation on unknown key"
2026-02-17 09:30:20 +01:00
2026-02-24 17:23:03 +01:00
let delete h_pub =
let k = find_exn h_pub in
2026-02-24 17:23:03 +01:00
Hashtbl.remove t.ht h_pub;
2026-03-11 14:17:52 +01:00
delete_file t.fs (key_spath k)
2026-02-17 09:30:20 +01:00
let _delete_outdated ~now =
2026-02-24 17:23:03 +01:00
Hashtbl.to_seq t.ht
2026-02-17 09:30:20 +01:00
|> List.of_seq
2026-02-24 17:51:30 +01:00
|> List.filter (fun (_h_pub, k) -> TimeAbsolute.compare now k.t2 >= 0)
2026-02-24 17:23:03 +01:00
|> List.map (fun (h_pub, _k) -> h_pub)
2026-02-17 09:30:20 +01:00
|> list_iter delete
let add coin t1 t2 =
let k = gen_key coin t1 t2 in
2026-03-11 14:17:52 +01:00
let+ () = write_key t.fs k in
Hashtbl.replace t.ht k.h_pub k;
2026-02-17 09:30:20 +01:00
()
let sm_pub = t.sm_pub
2026-03-20 22:47:58 +01:00
let sign_secmod s = Eddsa.sign ~key:t.sm_priv s
2026-02-17 09:30:20 +01:00
2026-02-24 19:31:39 +01:00
let sign h_pub msg =
let k = find_exn h_pub in
Rsa.sign ~key:k.priv msg
2026-02-17 09:30:20 +01:00
2026-02-24 17:23:03 +01:00
let revoke h_pub =
Log.debug (fun m -> m "revoke `%a`" DenominationHash.pp h_pub);
let k = find_exn h_pub in
2026-02-24 17:23:03 +01:00
let* () = delete h_pub in
let* () = add k.coin k.t1 k.t2 in
2026-02-17 09:30:20 +01:00
Ok ()
2026-02-23 12:29:49 +01:00
let conv k = (k.h_pub, (k.coin, k.pub, k.t1))
2026-02-23 12:29:49 +01:00
let keys () = Hashtbl.to_seq_values t.ht |> List.of_seq |> List.map conv
let find_key pub = Hashtbl.find_opt t.ht pub |> Option.map conv
2026-02-17 09:30:20 +01:00
end