This commit is contained in:
swrup 2025-10-18 17:03:38 +02:00
parent 5fa63ee4ee
commit 1419ac7da7
2 changed files with 31 additions and 46 deletions

View file

@ -242,27 +242,16 @@ module Taler_signatures = Include.Taler_signatures
(* -- Cryptographic primitives -- *)
(* GNUNET_CRYPTO format *)
module GNUNET_RsaPublicKey = struct
(* libgnuutil format:
https://docs.gnunet.org/doxygen/d9/dbe/structGNUNET__CRYPTO__RsaPublicKeyHeaderP.html
https://docs.gnunet.org/doxygen/d6/d70/group__libgnunetutil.html#ga9c99a81e8cd649c1c925d23211a0738f
https://www.gnupg.org/documentation/manuals/gcrypt/MPI-formats.html
format:
- rsa header
- modulus
- public_exponent
module RsaPublicKey = struct
(* https://www.gnupg.org/documentation/manuals/gcrypt/MPI-formats.html
format:
- uint16_be: n size
- uint16_be: e size
- n
- e
integer in big-endian format (MSB first).
Leading zeroes are stripped unless they are required to keep a value positive.
*)
type t = {
n_len: int;
e_len: int;
n: Z.t;
e: Z.t;
}
Leading zeroes are stripped unless they are required to keep a value positive. *)
let rev_string len s = String.init len (fun i -> s.[len - 1 - i])
@ -271,10 +260,9 @@ module GNUNET_RsaPublicKey = struct
let z_of_bits_be src pos len =
String.sub src pos len |> rev_string len |> Z.of_bits
let of_pub ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
{ n_len= Z.size n; e_len= Z.size e; n; e }
let to_octets { n_len; e_len; n; e } =
let to_octets ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
let n_len = Z.size n in
let e_len = Z.size e in
let len = 4 + n_len + e_len in
let b = Bytes.make len '\x00' in
Bytes.set_uint16_be b 0 n_len;
@ -285,27 +273,28 @@ module GNUNET_RsaPublicKey = struct
Bytes.blit_string e 0 b (4 + n_len) e_len;
Bytes.unsafe_to_string b
let of_octets s =
let error = Error "GNUNET_RsaPublicKey.of_octets: invalid data" in
let len = String.length s in
match len >= 4 with
| false -> error
| true -> (
let n_len = String.get_uint16_be s 0 in
let e_len = String.get_uint16_be s 2 in
match len = n_len + e_len + 4 with
| false -> error
| true ->
let n = z_of_bits_be s 4 n_len in
let e = z_of_bits_be s (4 + n_len) e_len in
Ok { n_len; e_len; n; e })
let of_octets =
let check = function
| false -> Error "RsaPublicKey.of_octets: invalid data"
| true -> Ok ()
in
fun s ->
let open Syntax in
let len = String.length s in
let* () = check (len >= 4) in
let n_len = String.get_uint16_be s 0 in
let e_len = String.get_uint16_be s 2 in
let* () = check (len = n_len + e_len + 4) in
let n = z_of_bits_be s 4 n_len in
let e = z_of_bits_be s (4 + n_len) e_len in
Mirage_crypto_pk.Rsa.pub ~n ~e |> unwrap_err_msg
end
module DenominationHash = MK_SRC_HASH_32 (struct
type src = Mirage_crypto_pk.Rsa.pub
let to_octets pub =
GNUNET_RsaPublicKey.of_pub pub |> GNUNET_RsaPublicKey.to_octets
(* function to convert src type to a string for hashing *)
let to_octets = RsaPublicKey.to_octets
end)
module ExchangePublicKeyP = struct

View file

@ -132,17 +132,13 @@ end = struct
end
module RsaPublicKey = struct
type t = Mirage_crypto_pk.Rsa.pub
open Binary_formats.RsaPublicKey
let to_octets ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
let open Binary_formats.GNUNET_RsaPublicKey in
let v = { n_len= Z.size n; e_len= Z.size e; n; e } in
let s = to_octets v in
s
type t = Mirage_crypto_pk.Rsa.pub
let of_b32 s =
let open Syntax in
let open Binary_formats.GNUNET_RsaPublicKey in
let open Binary_formats.RsaPublicKey in
let* s = B32.decode s in
let* v = of_octets s in
let+ v = Mirage_crypto_pk.Rsa.pub ~n:v.n ~e:v.e |> unwrap_err_msg in