This commit is contained in:
parent
bc91f7357c
commit
7942050c30
2 changed files with 153 additions and 61 deletions
|
|
@ -97,67 +97,17 @@ end = struct
|
|||
end
|
||||
|
||||
module RsaPublicKey = struct
|
||||
module Binary = 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 = Z.to_bits n in
|
||||
let e = Z.to_bits e in
|
||||
let n_len = String.length n in
|
||||
let e_len = String.length 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 = rev_string n_len n in
|
||||
let e = rev_string e_len e in
|
||||
Bytes.blit_string n 0 b 4 n_len;
|
||||
Bytes.blit_string e 0 b (4 + n_len) e_len;
|
||||
Bytes.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
|
||||
|
||||
let of_octets s =
|
||||
match of_octets s with
|
||||
| Error e -> Fmt.failwith "RsaPublicKey.of_octets failure: %s@." e
|
||||
| Ok v -> v
|
||||
end
|
||||
|
||||
include Binary
|
||||
open Mirage_crypto_pk
|
||||
|
||||
type t = Rsa.pub
|
||||
|
||||
let of_octets = Util.Bin_rsa.pub_of_octets
|
||||
let to_octets = Util.Bin_rsa.pub_to_octets
|
||||
|
||||
let of_b32 s =
|
||||
let open Syntax in
|
||||
let* s = B32.decode s in
|
||||
let v = of_octets s in
|
||||
let v = Util.Bin_rsa.pub_of_octets s in
|
||||
let+ v = Rsa.pub ~n:v.n ~e:v.e |> unwrap_err_msg in
|
||||
v
|
||||
|
||||
|
|
@ -171,16 +121,13 @@ module RsaPrivateKey = struct
|
|||
type t = priv
|
||||
|
||||
let pub_of_priv = pub_of_priv
|
||||
|
||||
(* TODO rsa *)
|
||||
let to_octets _t : string = assert false
|
||||
let of_octets _t : t = assert false
|
||||
(*let bin = Bin.map (Bin.bytes 32) of_octets to_octets*)
|
||||
let of_octets = Util.Bin_rsa.priv_of_octets
|
||||
let to_octets = Util.Bin_rsa.priv_to_octets
|
||||
|
||||
let of_b32 s =
|
||||
let open Syntax in
|
||||
let* octets = B32.decode s in
|
||||
Ok (of_octets octets)
|
||||
let* s = B32.decode s in
|
||||
Ok (of_octets s)
|
||||
|
||||
let to_b32 t = B32.encode (to_octets t)
|
||||
let jsont = Jsont.of_of_string ~kind:"EddsaPrivateKey" of_b32 ~enc:to_b32
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue