Commit e42dd2fd authored by Stephane Glondu's avatar Stephane Glondu

Add a generic group parsing module

Move code specific to a group implementation there, and use it
everywhere else.
parent d34318ef
(**************************************************************************)
(* BELENIOS *)
(* *)
(* Copyright © 2012-2014 Inria *)
(* *)
(* This program is free software: you can redistribute it and/or modify *)
(* it under the terms of the GNU Affero General Public License as *)
(* published by the Free Software Foundation, either version 3 of the *)
(* License, or (at your option) any later version, with the additional *)
(* exemption that compiling, linking, and/or using OpenSSL is allowed. *)
(* *)
(* This program is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
(* Affero General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU Affero General Public *)
(* License along with this program. If not, see *)
(* <http://www.gnu.org/licenses/>. *)
(**************************************************************************)
open Serializable_j
open Signatures
(** Generic group parsing *)
(* For now, only finite fields are supported... *)
let of_string x =
let group = ff_params_of_string x in
let module G = (val Group_field.make group : Group_field.GROUP) in
(module G : GROUP)
let read state buf =
let group = read_ff_params state buf in
let module G = (val Group_field.make group : Group_field.GROUP) in
(module G : GROUP)
let public_key_of_string x =
let w = ff_pubkey_of_string x in
let group = {g=w.ffpk_g; p=w.ffpk_p; q=w.ffpk_q} in
let module X = struct
module G = (val Group_field.make group : Group_field.GROUP)
let y = w.ffpk_y
end in
(module X : PUBLIC_KEY)
let election_params_of_string x =
let params = params_of_string read_ff_pubkey x in
let {ffpk_g=g; ffpk_p=p; ffpk_q=q; ffpk_y=y} = params.e_public_key in
let group = {g; p; q} in
let module X = struct
module G = (val Group_field.make group : Group_field.GROUP)
let params = {params with e_public_key = y}
end in
(module X : ELECTION_PARAMS)
(**************************************************************************)
(* BELENIOS *)
(* *)
(* Copyright © 2012-2014 Inria *)
(* *)
(* This program is free software: you can redistribute it and/or modify *)
(* it under the terms of the GNU Affero General Public License as *)
(* published by the Free Software Foundation, either version 3 of the *)
(* License, or (at your option) any later version, with the additional *)
(* exemption that compiling, linking, and/or using OpenSSL is allowed. *)
(* *)
(* This program is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
(* Affero General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU Affero General Public *)
(* License along with this program. If not, see *)
(* <http://www.gnu.org/licenses/>. *)
(**************************************************************************)
(** Generic group parsing *)
open Signatures
val of_string : string -> (module GROUP)
(** Parse a [Serializable_t.group]. *)
val read : (module GROUP) reader
val public_key_of_string : string -> (module PUBLIC_KEY)
(** Parse a [Serializable_t.wrapped_pubkey]. *)
val election_params_of_string : string -> (module ELECTION_PARAMS)
(** Parse a [Serializable_t.params]. *)
......@@ -83,6 +83,14 @@ let unsafe_make group =
let compare = Z.compare
type group = ff_params
let group = group
type wrapped_pubkey = Serializable_t.ff_pubkey
let wrap_pubkey y =
Serializable_t.({ffpk_g=g; ffpk_p=p; ffpk_q=q; ffpk_y=y})
let write_wrapped_pubkey = write_ff_pubkey
end in (module G : GROUP)
let make group =
......
......@@ -3,4 +3,5 @@ Serializable_t
Serializable_j
Common
Group_field
Group
Election
......@@ -87,6 +87,25 @@ module type GROUP = sig
(** Serializable description of the group. *)
val group : group
type wrapped_pubkey
(** The type of a pubkey wrapped with group parameters. *)
val wrap_pubkey : t -> wrapped_pubkey
val write_wrapped_pubkey : wrapped_pubkey writer
end
(** A public key wrapped with its group. *)
module type PUBLIC_KEY = sig
module G : GROUP
val y : G.t
end
(** Election params wrapped with its group. *)
module type ELECTION_PARAMS = sig
module G : GROUP
val params : G.t params
end
(** Monad signature. *)
......
......@@ -86,7 +86,7 @@ let parse_args () = begin
let ic = open_in fname in
let ls = Yojson.init_lexer () in
let lb = Lexing.from_channel ic in
let r = Serializable_j.read_ff_params ls lb in
let r = Group.read ls lb in
close_in ic;
r
in
......@@ -107,7 +107,7 @@ let parse_args () = begin
let file = !file
let derive = !derive
let dir = !dir
module G = (val Group_field.make group : Group_field.GROUP)
module G = (val group : GROUP)
end in
(module P : PARAMS)
......
......@@ -48,11 +48,9 @@ module type PARAMS = sig
val do_finalize : bool
val do_decrypt : bool
val ballot_file : string option
val params : ff_pubkey params
val election_fingerprint : string
val group : ff_params
module G : GROUP
val y : G.t
val params : G.t params
end
......@@ -119,15 +117,13 @@ let parse_args () = begin
let params, election_fingerprint =
match (load_from_file (fun l ->
params_of_string read_ff_pubkey l,
Group.election_params_of_string l,
sha256_b64 l
) "election.json") with
| Some [e] -> e
| _ -> failwith "invalid election file"
in
let {ffpk_g = g; ffpk_p = p; ffpk_q = q; ffpk_y = y} = params.e_public_key in
let module P = struct
let sk_file = !sk_file
let do_finalize = !do_finalize
......@@ -135,9 +131,7 @@ let parse_args () = begin
let ballot_file = !ballot_file
let params = params
let election_fingerprint = election_fingerprint
let group = {g; p; q}
let y = y
module G = (val Group_field.make group : Group_field.GROUP)
include (val params : ELECTION_PARAMS)
end in
(module P : PARAMS)
......@@ -164,7 +158,7 @@ module Run (P : PARAMS) : EMPTY = struct
| Some pks ->
assert (Array.forall KG.check pks);
let y' = KG.combine pks in
assert G.(y =~ y')
assert G.(params.e_public_key =~ y')
| None -> ()
let public_keys =
......@@ -179,7 +173,7 @@ module Run (P : PARAMS) : EMPTY = struct
| None -> failwith "missing public keys"
let e = {
e_params = { params with e_public_key = P.y };
e_params = params;
e_pks = Some pks;
e_fingerprint = election_fingerprint;
}
......
......@@ -28,7 +28,6 @@ module type PARAMS = sig
val uuid : Uuidm.t
val template : template
module G : GROUP
val write_params : Bi_outbuf.t -> G.t params -> unit
end
let parse_args () = begin
......@@ -67,12 +66,10 @@ let parse_args () = begin
let ic = open_in fname in
let ls = Yojson.init_lexer () in
let lb = Lexing.from_channel ic in
let r = read_ff_params ls lb in
let r = Group.read ls lb in
r
in
let {g; p; q} = group in
let module P = struct
let uuid = match !uuid with
......@@ -91,22 +88,13 @@ let parse_args () = begin
close_in ic;
r
module G = (val Group_field.make group : Group_field.GROUP)
let params y = {
e_description = template.t_description;
e_name = template.t_name;
e_public_key = {ffpk_g = g; ffpk_p = p; ffpk_q = q; ffpk_y = y};
e_questions = template.t_questions;
e_uuid = uuid;
e_short_name = template.t_short_name;
}
module G = (val group : GROUP)
let write_params buf params =
let y = params.e_public_key in
let w = {ffpk_g=g; ffpk_p=p; ffpk_q=q; ffpk_y=y} in
let w = G.wrap_pubkey y in
let params = { params with e_public_key = w } in
write_params write_ff_pubkey buf params
write_params G.write_wrapped_pubkey buf params
end in
......@@ -148,7 +136,7 @@ module Run (P : PARAMS) : EMPTY = struct
let params = {
e_description = template.t_description;
e_name = template.t_name;
e_public_key = y;
e_public_key = G.wrap_pubkey y;
e_questions = template.t_questions;
e_uuid = uuid;
e_short_name = template.t_short_name;
......@@ -156,6 +144,7 @@ module Run (P : PARAMS) : EMPTY = struct
(* Save to disk *)
let write_params = write_params G.write_wrapped_pubkey
let () = save_to "election.json" write_params params
end
......
......@@ -60,13 +60,13 @@ let parse_args () = begin
let ic = open_in fname in
let ls = Yojson.init_lexer () in
let lb = Lexing.from_channel ic in
let r = read_ff_params ls lb in
let r = Group.read ls lb in
close_in ic;
r
in
let module P = struct
module G = (val Group_field.make group : Group_field.GROUP)
module G = (val group : GROUP)
end in
(module P : PARAMS)
......
......@@ -2,6 +2,7 @@ Serializable_builtin_j
Serializable_j
Common
Group_field
Group
Election
Web_serializable_j
......
......@@ -52,10 +52,9 @@ end
let make config =
let e_fingerprint = sha256_b64 config.raw_election in
let wrapped_params = params_of_string read_ff_pubkey config.raw_election in
let {ffpk_g = g; ffpk_p = p; ffpk_q = q; ffpk_y = y} = wrapped_params.e_public_key in
let group = {g; p; q} in
let e_params = { wrapped_params with e_public_key = y } in
let params = Group.election_params_of_string config.raw_election in
let module P = (val params : ELECTION_PARAMS) in
let e_params = P.params in
let module R : REGISTRATION = struct
......@@ -75,7 +74,7 @@ let make config =
module Auth = Web_auth.Make (N)
module W : WEB_ELECTION = struct
module G = (val Group_field.make group : Group_field.GROUP)
module G = P.G
module M = MakeLwtRandom(struct let rng = make_rng () end)
module E = Election.MakeElection(G)(M)
module H = Auth.Handlers
......@@ -142,12 +141,12 @@ let make config =
fail (Serialization (Invalid_argument "multiline ballot"))
) else return () >>
lwt ballot =
try Lwt.return (ballot_of_string read_number rawballot)
try Lwt.return (ballot_of_string G.read rawballot)
with e -> fail (Serialization e)
in
lwt credential =
match ballot.signature with
| Some s -> Lwt.return (Z.to_string s.s_public_key)
| Some s -> Lwt.return (G.to_string s.s_public_key)
| None -> fail MissingCredential
in
lwt old_cred =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment