encode/decode rsapublickey

This commit is contained in:
swrup 2025-10-18 15:28:04 +02:00
parent e72ed3e972
commit d5abae41b6
2 changed files with 53 additions and 73 deletions

View file

@ -38,9 +38,56 @@
? only for "HashCode" and "ShortHashCode"
*)
module Taler_signatures = Include.Taler_signatures
let int32_size = 4
let int64_size = 8
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
no 0-termination *)
let rev_string len s = String.init len (fun i -> s.[len - 1 - i])
(* todo: need to strip leading zeros or something? *)
(* reverse bytes because Z.of_bits reads bytes in little endian *)
let z_of_bits_be src pos len =
String.sub src pos len |> rev_string len |> Z.of_bits
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;
Bytes.set_uint16_be b 2 e_len;
let n = Z.to_bits n |> rev_string n_len in
let e = Z.to_bits e |> rev_string e_len in
Bytes.blit_string n 0 b 4 n_len;
Bytes.blit_string e 0 b (4 + n_len) e_len;
Bytes.unsafe_to_string b
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
(* -- Time -- *)
(* microseconds since the UNIX Epoch
@ -238,77 +285,14 @@ module UTIL = struct
end
include UTIL
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
integer in big-endian format (MSB first).
Leading zeroes are stripped unless they are required to keep a value positive.
*)
type header = {
n_len: int;
e_len: int;
}
type t = {
header: header;
n: Z.t;
e: Z.t;
}
(* todo: need to strip leading zeros or something? *)
(* Z.to_bits is in little endian but we need it in big endian *)
let z_to_bigendian_bits v =
let s = Z.to_bits v in
let len = String.length s in
let s = String.init len (fun i -> s.[len - 1 - i]) in
s
let header_bin =
let open Bin in
record (fun n_len e_len -> { n_len; e_len })
|+ field beint16 (fun t -> t.n_len)
|+ field beint16 (fun t -> t.e_len)
|> sealr
(* note: this one has a dynamic sizeof *)
let bin =
let open Bin in
record (fun header n e ->
let n = Z.of_bits n in
let e = Z.of_bits e in
{ header; n; e })
|+ field header_bin (fun t -> t.header)
(* TODO
here it should not be [cstring] but [bytes t.header.n_len]
-> just parse this without Bin *)
|+ field cstring (fun t -> z_to_bigendian_bits t.n)
|+ field cstring (fun t -> z_to_bigendian_bits t.e)
|> sealr
let of_pub ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
let header = { n_len= Z.size n; e_len= Z.size e } in
let t = { header; n; e } in
t
end
module DenominationHash = MK_SRC_HASH_32 (struct
type src = Mirage_crypto_pk.Rsa.pub
let to_octets pub =
GNUNET_RsaPublicKey.of_pub pub |> Bin.to_string GNUNET_RsaPublicKey.bin
(* function to convert src type to a string for hashing *)
let to_octets = RsaPublicKey.to_octets
end)
module ExchangePublicKeyP = struct

View file

@ -132,19 +132,15 @@ 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 header = { n_len= Z.size n; e_len= Z.size e } in
let v = { header; n; e } in
let s = Bin.to_string bin v in
s
type t = Mirage_crypto_pk.Rsa.pub
let of_b32 s =
let open Syntax in
let open Binary_formats.RsaPublicKey in
let* s = B32.decode s in
let* v = Util.bin_of_string Binary_formats.GNUNET_RsaPublicKey.bin 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
v