diff --git a/src/base_32.ml b/src/base_32.ml index 5c111967..bbc96e96 100644 --- a/src/base_32.ml +++ b/src/base_32.ml @@ -21,4 +21,6 @@ let decode s = | c -> c) s in - Base32.decode ~alphabet ~off:0 ~len:(String.length s) s + match Base32.decode ~alphabet ~off:0 ~len:(String.length s) s with + | Error (`Msg e) -> Error e + | Ok v -> Ok v diff --git a/src/dune b/src/dune index c5d29ca9..7c13b657 100644 --- a/src/dune +++ b/src/dune @@ -1,13 +1,18 @@ (executable (public_name mte) (name mte) - (modules assets assets_crunch headers mte config util management) + (modules mte) + (libraries mte)) + +(library + (name mte) + (wrapped false) + (modules :standard \ mte base_32) (libraries - json - types - headers_lib - syntax + base_32 ; + angstrom + zarith ; mirage-crypto duration vif @@ -15,29 +20,7 @@ jsont cohttp)) -(library - (name json) - (modules json) - (libraries jsont jsont.bytesrw fmt syntax types)) - -(library - (name types) - (modules types) - (libraries - angstrom - zarith ; - fmt - syntax)) - -(library - (name headers_lib) - (modules headers_lib) - (libraries fmt syntax)) - -(library - (name syntax) - (modules syntax) - (libraries)) +; crockford base32, as a lib to use [Base32] by accident *) (library (name base_32) diff --git a/src/json.ml b/src/json.ml index 580128df..876d5f5c 100644 --- a/src/json.ml +++ b/src/json.ml @@ -75,3 +75,12 @@ end let amount_jsont = Jsont.of_of_string ~kind:"Amount" Amount.of_string ~enc:Amount.to_string + +module Eddsa = struct + open Eddsa + + let pub_jsont = + let dec = Jsont.Base.dec_result pub_of_string in + let enc = Jsont.Base.enc pub_to_string in + Jsont.Base.string (Jsont.Base.map ~kind:"EddsaPublicKey" ~dec ~enc ()) +end diff --git a/src/types.ml b/src/types.ml index d401b9bf..ef08f05f 100644 --- a/src/types.ml +++ b/src/types.ml @@ -112,20 +112,39 @@ module Amount = struct end module Eddsa = struct + (* TODO test *) (* EdDSA and ECDHE public keys always point on Curve25519 and represented using the standard 256 bits Ed25519 compact format, converted to Crockford Base32. *) - type pub = string - type priv = string + type pub = Mirage_crypto_ec.Ed25519.pub + let pub_of_string s = + let open Mirage_crypto_ec in + match Base_32.decode s with + | Error _ as err -> err + | Ok octets -> ( + match Ed25519.pub_of_octets octets with + | Error e -> Fmt.error "%a" pp_error e + | Ok pub -> Ok pub) + + let pub_to_string pub = + pub |> Mirage_crypto_ec.Ed25519.pub_to_octets |> Base_32.encode + + (*type priv = string*) (* EdDSA signatures are transmitted as 64-bytes base32 binary-encoded objects with just the R and S values (base32_ binary-only). *) - type signature = string + (*type signature = string*) end module Rsa = struct (* RSA public key converted to Crockford Base32. *) - type pub = string + type pub = Mirage_crypto_pk.Rsa.pub + + (* TODO + how to read/write pub rsa key + what format is used? *) + let pub_of_string _s = assert false + let pub_to_string _pub = assert false end module Hash_code = struct diff --git a/test/dune b/test/dune index 6d9fb164..57d2e7e5 100644 --- a/test/dune +++ b/test/dune @@ -1,4 +1,4 @@ (test (name test) (modules test) - (libraries types json)) + (libraries mte fmt))