This commit is contained in:
parent
e72ed3e972
commit
92630b7e2b
2 changed files with 29 additions and 41 deletions
|
|
@ -257,58 +257,46 @@ module GNUNET_RsaPublicKey = struct
|
||||||
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 header = {
|
type t = {
|
||||||
n_len: int;
|
n_len: int;
|
||||||
e_len: int;
|
e_len: int;
|
||||||
}
|
|
||||||
|
|
||||||
type t = {
|
|
||||||
header: header;
|
|
||||||
n: Z.t;
|
n: Z.t;
|
||||||
e: Z.t;
|
e: Z.t;
|
||||||
}
|
}
|
||||||
|
|
||||||
(* todo: need to strip leading zeros or something? *)
|
(* todo: need to strip leading zeros or something? *)
|
||||||
(* Z.to_bits is in little endian but we need it in big endian *)
|
(* reverse bytes because Z.of_bits reads little endian *)
|
||||||
let z_to_bigendian_bits v =
|
let z_of_be_bits src pos len =
|
||||||
let s = Z.to_bits v in
|
let x = String.sub src pos len in
|
||||||
let len = String.length s in
|
let x = String.init len (fun i -> x.[len - 1 - i]) in
|
||||||
let s = String.init len (fun i -> s.[len - 1 - i]) in
|
Z.of_bits x
|
||||||
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 of_pub ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
|
||||||
let header = { n_len= Z.size n; e_len= Z.size e } in
|
{ n_len= Z.size n; e_len= Z.size e; n; e }
|
||||||
let t = { header; n; e } in
|
|
||||||
t
|
let to_octets _pub = assert false
|
||||||
|
let error = Error "GNUNET_RsaPublicKey.of_octets: invalid data"
|
||||||
|
|
||||||
|
let of_octets s =
|
||||||
|
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_be_bits s 4 n_len in
|
||||||
|
let e = z_of_be_bits s (4 + n_len) e_len in
|
||||||
|
Ok { n_len; e_len; n; e })
|
||||||
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 =
|
let to_octets pub =
|
||||||
GNUNET_RsaPublicKey.of_pub pub |> Bin.to_string GNUNET_RsaPublicKey.bin
|
GNUNET_RsaPublicKey.of_pub pub |> GNUNET_RsaPublicKey.to_octets
|
||||||
end)
|
end)
|
||||||
|
|
||||||
module ExchangePublicKeyP = struct
|
module ExchangePublicKeyP = struct
|
||||||
|
|
|
||||||
|
|
@ -136,15 +136,15 @@ module RsaPublicKey = struct
|
||||||
|
|
||||||
let to_octets ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
|
let to_octets ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
|
||||||
let open Binary_formats.GNUNET_RsaPublicKey in
|
let open Binary_formats.GNUNET_RsaPublicKey in
|
||||||
let header = { n_len= Z.size n; e_len= Z.size e } in
|
let v = { n_len= Z.size n; e_len= Z.size e; n; e } in
|
||||||
let v = { header; n; e } in
|
let s = to_octets v in
|
||||||
let s = Bin.to_string bin v in
|
|
||||||
s
|
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* s = B32.decode s 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
|
let+ v = Mirage_crypto_pk.Rsa.pub ~n:v.n ~e:v.e |> unwrap_err_msg in
|
||||||
v
|
v
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue