encode/decode rsapublickey
This commit is contained in:
parent
e72ed3e972
commit
86154ab890
2 changed files with 45 additions and 63 deletions
|
|
@ -242,73 +242,59 @@ 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 header = {
|
|
||||||
n_len: int;
|
|
||||||
e_len: int;
|
|
||||||
}
|
|
||||||
|
|
||||||
type t = {
|
let rev_string len s = String.init len (fun i -> s.[len - 1 - i])
|
||||||
header: header;
|
|
||||||
n: 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_bits_be src pos len =
|
||||||
let s = Z.to_bits v in
|
String.sub src pos len |> rev_string len |> Z.of_bits
|
||||||
let len = String.length s in
|
|
||||||
let s = String.init len (fun i -> s.[len - 1 - i]) in
|
|
||||||
s
|
|
||||||
|
|
||||||
let header_bin =
|
let to_octets ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
|
||||||
let open Bin in
|
let n_len = Z.size n in
|
||||||
record (fun n_len e_len -> { n_len; e_len })
|
let e_len = Z.size e in
|
||||||
|+ field beint16 (fun t -> t.n_len)
|
let len = 4 + n_len + e_len in
|
||||||
|+ field beint16 (fun t -> t.e_len)
|
let b = Bytes.make len '\x00' in
|
||||||
|> sealr
|
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
|
||||||
|
|
||||||
(* note: this one has a dynamic sizeof *)
|
let of_octets =
|
||||||
let bin =
|
let check = function
|
||||||
let open Bin in
|
| false -> Error "RsaPublicKey.of_octets: invalid data"
|
||||||
record (fun header n e ->
|
| true -> Ok ()
|
||||||
let n = Z.of_bits n in
|
in
|
||||||
let e = Z.of_bits e in
|
fun s ->
|
||||||
{ header; n; e })
|
let open Syntax in
|
||||||
|+ field header_bin (fun t -> t.header)
|
let len = String.length s in
|
||||||
(* TODO
|
let* () = check (len >= 4) in
|
||||||
here it should not be [cstring] but [bytes t.header.n_len]
|
let n_len = String.get_uint16_be s 0 in
|
||||||
-> just parse this without Bin *)
|
let e_len = String.get_uint16_be s 2 in
|
||||||
|+ field cstring (fun t -> z_to_bigendian_bits t.n)
|
let* () = check (len = n_len + e_len + 4) in
|
||||||
|+ field cstring (fun t -> z_to_bigendian_bits t.e)
|
let n = z_of_bits_be s 4 n_len in
|
||||||
|> sealr
|
let e = z_of_bits_be s (4 + n_len) e_len in
|
||||||
|
Mirage_crypto_pk.Rsa.pub ~n ~e |> unwrap_err_msg
|
||||||
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
|
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 |> Bin.to_string GNUNET_RsaPublicKey.bin
|
let to_octets = RsaPublicKey.to_octets
|
||||||
end)
|
end)
|
||||||
|
|
||||||
module ExchangePublicKeyP = struct
|
module ExchangePublicKeyP = struct
|
||||||
|
|
|
||||||
12
src/types.ml
12
src/types.ml
|
|
@ -132,19 +132,15 @@ 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 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
|
|
||||||
|
|
||||||
let of_b32 s =
|
let of_b32 s =
|
||||||
let open Syntax in
|
let open Syntax in
|
||||||
|
let open Binary_formats.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