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

View file

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