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 Crypto
|
|
|
|
|
open Time
|
2026-03-11 14:17:52 +01:00
|
|
|
module Sfn = Mfat.Sfn
|
|
|
|
|
module Spath = Mfat.Spath
|
2026-02-23 05:20:24 +01:00
|
|
|
|
2026-03-19 23:01:04 +01:00
|
|
|
module Coin_config = struct
|
|
|
|
|
type t = {
|
|
|
|
|
name: string;
|
|
|
|
|
duration_withdraw: TimeRelative.t;
|
|
|
|
|
rsa_keysize: int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let of_coin (coin : Config.Coin.t) =
|
|
|
|
|
{
|
|
|
|
|
name= coin.section_name;
|
|
|
|
|
duration_withdraw= coin.duration_withdraw;
|
|
|
|
|
rsa_keysize= coin.rsa_keysize;
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2026-02-23 05:20:24 +01:00
|
|
|
module Cfg = struct
|
2026-03-19 23:01:04 +01:00
|
|
|
include Config.Exchange_secmod_rsa
|
|
|
|
|
|
|
|
|
|
let key_dir = Config.spath "/RSA"
|
|
|
|
|
let sm_key = Config.spath "/SM_RSA"
|
|
|
|
|
let coin_config_list = List.map Coin_config.of_coin Config.Coin.all_coins
|
|
|
|
|
|
|
|
|
|
let get_coin_config ~section_name =
|
|
|
|
|
coin_config_list
|
|
|
|
|
|> List.find_opt (fun (cfg : Coin_config.t) ->
|
|
|
|
|
String.equal cfg.name section_name)
|
|
|
|
|
|> function
|
|
|
|
|
| None ->
|
|
|
|
|
Fmt.failwith "secmod_rsa failure: section `%s` not found" section_name
|
|
|
|
|
| Some cfg -> cfg
|
2026-02-23 05:20:24 +01:00
|
|
|
end
|
2026-02-17 09:30:20 +01:00
|
|
|
|
|
|
|
|
type key = {
|
|
|
|
|
section_name: string;
|
|
|
|
|
priv: RsaPrivateKey.t;
|
|
|
|
|
pub: RsaPublicKey.t;
|
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;
|
|
|
|
|
sm_priv: EddsaPrivateKey.t;
|
2026-02-17 09:30:20 +01:00
|
|
|
sm_pub: EddsaPublicKey.t;
|
2026-02-24 17:23:03 +01:00
|
|
|
ht: (DenominationHash.t, key) Hashtbl.t;
|
2026-02-17 09:30:20 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-11 14:17:52 +01:00
|
|
|
let key_bin =
|
|
|
|
|
let open Bin in
|
|
|
|
|
record (fun t1 t2 section_name priv ->
|
|
|
|
|
let pub = RsaPrivateKey.pub_of_priv priv in
|
|
|
|
|
let h_pub = DenominationHash.hash_of_rsa pub in
|
|
|
|
|
{ section_name; t1; t2; priv; pub; h_pub })
|
|
|
|
|
|+ field TimeAbsolute.bin (fun t -> t.t1)
|
|
|
|
|
|+ field TimeAbsolute.bin (fun t -> t.t2)
|
|
|
|
|
|+ field cstring (fun t -> t.section_name)
|
|
|
|
|
|+ field RsaPrivateKey.bin (fun t -> t.priv)
|
|
|
|
|
|> sealr
|
|
|
|
|
|
|
|
|
|
let key_spath k =
|
|
|
|
|
let sfn_res =
|
|
|
|
|
String.sub (DenominationHash.to_b32 k.h_pub) 0 8 |> Sfn.of_string
|
2026-02-23 06:30:39 +01:00
|
|
|
in
|
2026-03-11 14:17:52 +01:00
|
|
|
match sfn_res with
|
|
|
|
|
| Error _ -> failwith "not possible"
|
|
|
|
|
| Ok sfn -> Spath.(Cfg.key_dir / sfn)
|
|
|
|
|
|
|
|
|
|
let read_eddsa fs spath =
|
|
|
|
|
Log.debug (fun m -> m "reading key file `%a`" Spath.pp spath);
|
|
|
|
|
let* data = Fat.read fs spath |> unwrap_msg in
|
|
|
|
|
let* priv = EddsaPrivateKey.of_octets data in
|
|
|
|
|
let pub = EddsaPrivateKey.pub_of_priv priv in
|
|
|
|
|
Ok (priv, pub)
|
|
|
|
|
|
|
|
|
|
let write_eddsa fs spath priv =
|
|
|
|
|
Log.debug (fun m -> m "writing key file `%a`" Spath.pp spath);
|
2026-02-17 09:30:20 +01:00
|
|
|
let data = EddsaPrivateKey.to_octets priv in
|
2026-03-11 14:17:52 +01:00
|
|
|
Fat.write fs spath data |> unwrap_msg
|
|
|
|
|
|
|
|
|
|
let read_key fs spath =
|
|
|
|
|
Log.debug (fun m -> m "reading key file `%a`" Spath.pp spath);
|
|
|
|
|
let* data = Fat.read fs spath |> unwrap_msg in
|
|
|
|
|
let k = Bin.decode key_bin data (ref 0) in
|
|
|
|
|
Ok k
|
|
|
|
|
|
|
|
|
|
let write_key fs k =
|
|
|
|
|
let spath = key_spath k in
|
|
|
|
|
Log.debug (fun m -> m "writing key file `%a`" Spath.pp spath);
|
|
|
|
|
let data = Bin.to_string key_bin k in
|
|
|
|
|
Fat.write fs spath data |> unwrap_msg
|
|
|
|
|
|
|
|
|
|
let delete_file fs spath =
|
|
|
|
|
Log.debug (fun m -> m "delete key file `%a`" Spath.pp spath);
|
|
|
|
|
let+ () = Fat.remove fs spath |> unwrap_msg in
|
2026-02-25 00:12:05 +01:00
|
|
|
()
|
2026-02-17 09:30:20 +01:00
|
|
|
|
|
|
|
|
(* -- *)
|
|
|
|
|
|
2026-03-19 23:01:04 +01:00
|
|
|
let gen_key cfg t1 t2 =
|
|
|
|
|
let priv, pub = RsaPrivateKey.generate ~bits:cfg.Coin_config.rsa_keysize () in
|
2026-02-27 23:36:05 +01:00
|
|
|
let h_pub = DenominationHash.hash_of_rsa pub in
|
2026-03-19 23:01:04 +01:00
|
|
|
let k = { section_name= cfg.name; priv; pub; h_pub; t1; t2 } in
|
2026-02-17 09:30:20 +01:00
|
|
|
Log.debug (fun m ->
|
2026-03-19 23:01:04 +01:00
|
|
|
m "generated key %s `%s`" cfg.Coin_config.name
|
|
|
|
|
(DenominationHash.to_b32 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
|
|
|
|
2026-03-19 23:01:04 +01:00
|
|
|
let split_in_periodes (cfg : Coin_config.t) ~start ~end_ =
|
2026-02-17 09:30:20 +01:00
|
|
|
assert (start < end_);
|
|
|
|
|
(* no overlap on first periode *)
|
|
|
|
|
let t1 = start in
|
2026-03-19 23:01:04 +01:00
|
|
|
let t2 = TimeAbsolute.add start cfg.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
|
2026-03-19 23:01:04 +01:00
|
|
|
let t2 = TimeAbsolute.add start cfg.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_
|
|
|
|
|
|
2026-03-19 23:01:04 +01:00
|
|
|
let gen_additional_keys_until_lookahead cfg ~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
|
2026-03-19 23:01:04 +01:00
|
|
|
let periodes = split_in_periodes cfg ~start ~end_ in
|
|
|
|
|
let new_keys = List.map (fun (t1, t2) -> gen_key cfg 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 |> unwrap_msg
|
|
|
|
|
in
|
|
|
|
|
let* l = Fat.ls fs Cfg.key_dir |> unwrap_msg in
|
2026-03-19 23:01:04 +01:00
|
|
|
let l = List.map (fun entry -> Spath.add Cfg.key_dir entry.Fat.name) l in
|
|
|
|
|
let l = List.filter (fun spath -> not @@ Spath.equal spath Cfg.sm_key) 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-11 14:17:52 +01:00
|
|
|
let sm_priv, sm_pub = EddsaPrivateKey.generate () in
|
2026-02-17 09:30:20 +01:00
|
|
|
Log.debug (fun m ->
|
|
|
|
|
m "generated secmod key: `%s`" (EddsaPublicKey.to_b32 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 =
|
2026-03-19 23:01:04 +01:00
|
|
|
Cfg.coin_config_list
|
|
|
|
|
|> List.map (fun (cfg : Coin_config.t) ->
|
|
|
|
|
let keys = List.filter (fun k -> k.section_name = cfg.name) all_keys in
|
|
|
|
|
gen_additional_keys_until_lookahead cfg ~now keys)
|
2026-02-17 09:30:20 +01:00
|
|
|
in
|
|
|
|
|
let new_keys = List.concat new_keys_l in
|
2026-02-25 00:12:05 +01:00
|
|
|
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
|
2026-02-17 09:30:20 +01:00
|
|
|
| Error e -> Fmt.failwith "secmod_rsa initialization failure: %s." e
|
|
|
|
|
| Ok t -> t
|
|
|
|
|
|
2026-02-24 17:23:03 +01:00
|
|
|
let find h_pub =
|
|
|
|
|
Hashtbl.find_opt t.ht h_pub |> Option.to_result ~none:"key not found"
|
2026-02-17 09:30:20 +01:00
|
|
|
|
2026-02-24 17:23:03 +01:00
|
|
|
let delete h_pub =
|
|
|
|
|
let* k = find h_pub in
|
|
|
|
|
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
|
|
|
|
|
|
2026-03-19 23:01:04 +01:00
|
|
|
let add cfg t1 t2 =
|
|
|
|
|
let k = gen_key cfg t1 t2 in
|
2026-03-11 14:17:52 +01:00
|
|
|
let+ () = write_key t.fs k in
|
2026-03-03 01:36:12 +01:00
|
|
|
Hashtbl.replace t.ht k.h_pub k;
|
2026-02-17 09:30:20 +01:00
|
|
|
()
|
|
|
|
|
|
|
|
|
|
let sm_pub = t.sm_pub
|
2026-03-11 14:17:52 +01:00
|
|
|
let sign_secmod s = EddsaSignature.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 =
|
2026-02-24 17:23:03 +01:00
|
|
|
let+ k = find h_pub in
|
2026-03-11 14:17:52 +01:00
|
|
|
let data = RsaSignature.sign ~key:k.priv msg in
|
2026-02-17 09:30:20 +01:00
|
|
|
data
|
|
|
|
|
|
2026-02-24 17:23:03 +01:00
|
|
|
let revoke h_pub =
|
2026-02-25 00:12:05 +01:00
|
|
|
Log.debug (fun m ->
|
|
|
|
|
m "revoke `%s`" (DenominationHash.to_octets h_pub |> B32.encode));
|
2026-02-24 17:23:03 +01:00
|
|
|
let* k = find h_pub in
|
|
|
|
|
let* () = delete h_pub in
|
2026-03-19 23:01:04 +01:00
|
|
|
let cfg = Cfg.get_coin_config ~section_name:k.section_name in
|
|
|
|
|
let* () = add cfg k.t1 k.t2 in
|
2026-02-17 09:30:20 +01:00
|
|
|
Ok ()
|
2026-02-23 12:29:49 +01:00
|
|
|
|
|
|
|
|
let conv =
|
2026-02-24 17:23:03 +01:00
|
|
|
fun { section_name; priv= _; pub; h_pub; t1; t2= _ } ->
|
|
|
|
|
(h_pub, (section_name, pub, 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
|