diff --git a/src/crypto.ml b/src/crypto.ml index aac1d07d..99cabfea 100644 --- a/src/crypto.ml +++ b/src/crypto.ml @@ -159,37 +159,35 @@ end = struct binary-encoded objects with just the R and S values *) type t = string - (* mirage_crypto: "The result is the concatenation of r and s, as specified in RFC 8032." *) + (* mirage_crypto: + "The result is the concatenation of r and s, as specified in RFC 8032." *) let sign ~key s = Mirage_crypto_ec.Ed25519.sign ~key s let verify ~key s ~msg = let b = Mirage_crypto_ec.Ed25519.verify ~key s ~msg in match b with - | false -> Error "signature verification failure: invalid signature" + | false -> Error "EddsaSignature verification: invalid signature" | true -> Ok () let to_octets t = t + let check_size t = + match String.length t = 64 with + | false -> Error "EddsaSignature of_octets: data is not 64 bytes." + | true -> Ok () + let of_octets v = - match String.length v = 64 with - | false -> - Fmt.error "EddsaSignature.of_octets failure: data is not 64 bytes." - | true -> Ok v + let+ () = check_size v in + v let bin = let of_octets_exn t = of_octets t |> Result.get_ok in Bin.map (Bin.bytes 64) of_octets_exn to_octets - let check_size t = - match String.length t = 64 with - | false -> Error "EddsaSignature: invalid string length" - | true -> Ok () - let jsont = let of_b32 s = let* t = B32.decode s in - let+ () = check_size t in - t + of_octets t in let to_b32 = B32.encode in Jsont.of_of_string ~kind:"EddsaSignature" of_b32 ~enc:to_b32 diff --git a/src/signatures.ml b/src/signatures.ml index 80cebad6..1d57405e 100644 --- a/src/signatures.ml +++ b/src/signatures.ml @@ -1,9 +1,7 @@ (* TODO signatures - check with taler-wallet-core/src/crypto/cryptoImplementation.js + check with taler-wallet check signed/unsigned ints - check endianness - - better handling of decoding failure *) + check endianness *) open Hash module Aliases = struct @@ -177,8 +175,9 @@ end = struct type r = R.r type t = EddsaSignature.t - let sign_f ~f r = f (Bin.to_string R.bin r) - let verify_f ~f t r = f t ~msg:(Bin.to_string R.bin r) + let to_string = Bin.to_string R.bin + let sign_f ~f r = f (to_string r) + let verify_f ~f t r = f t ~msg:(to_string r) let jsont = EddsaSignature.jsont let caqti : EddsaSignature.t Caqti_type.t = EddsaSignature.caqti let to_octets t = EddsaSignature.to_octets t