mirror of
https://github.com/saymrwulf/swisspost-evoting-go-poc.git
synced 2026-11-18 09:54:49 +00:00
Cast-as-intended return codes: CC extraction + voter check
The return code the voter checks is now genuinely computed by the CCs from the submitted ciphertext, not looked up from the card: - returncode_extract.go: after a ballot is accepted, the server asks each CC to exponentiate E2 by its return-code key (product over CCs = Enc(vote^Σk)), then each CC contributes a partial-decryption factor; the server recovers vote^Σk, which equals the card base prime_sel^Σk, and looks up the short code. - The server returns that code to the voter, who checks it against the card for the chosen option; a mismatch aborts with a clear error. Soundness test: a malicious client that encrypts option A for the tally (E1) but option B in the return-code channel (E2) is REJECTED by the plaintext-equality proof — so the code shown always reflects the tallied vote. This closes the cast-as-intended gap (the old return codes were decorative, finding F16). Card lCC now uses a fixed tau so extraction can recompute it without learning the option up front. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
d2cfbf291f
commit
127ae61d11
5 changed files with 247 additions and 4 deletions
|
|
@ -275,10 +275,13 @@ func (c *Ceremony) assembleVotingCard(voterIdx int) (*votingCard, error) {
|
|||
confirmCombined = confirmCombined.Multiply(cShare)
|
||||
}
|
||||
|
||||
// Derive the short codes and register mapping-table entries.
|
||||
// Derive the short codes and register mapping-table entries. The lCC value
|
||||
// uses a fixed tau (not the per-option prime) so that vote-time extraction
|
||||
// can recompute it from vote^{Σk} without knowing which option was chosen —
|
||||
// the per-option base prime_i^{Σk} already makes the codes distinct.
|
||||
choiceCodes := make([]string, cfg.NumOptions)
|
||||
for i := 0; i < cfg.NumOptions; i++ {
|
||||
lCC := returncodes.ComputeLCCValue(choiceCombined[i], vcID, cfg.ElectionID, c.Setup.st.primes[i])
|
||||
lCC := returncodes.ComputeLCCValue(choiceCombined[i], vcID, cfg.ElectionID, tauFixed)
|
||||
short := fmt.Sprintf("CC-%02d", i)
|
||||
choiceCodes[i] = short
|
||||
c.Setup.st.mappingTable.Add(lCC, short)
|
||||
|
|
|
|||
|
|
@ -25,6 +25,10 @@ func (p *ControlComponent) handleCCMsg(env *transport.Envelope) (*transport.Enve
|
|||
return p.handleVerifyBallot(env)
|
||||
case MsgShuffle:
|
||||
return p.handleShuffle(env)
|
||||
case MsgRCExpReq:
|
||||
return p.handleRCExp(env)
|
||||
case MsgRCDecReq:
|
||||
return p.handleRCDec(env)
|
||||
default:
|
||||
return nil, fmt.Errorf("%s: unhandled message type %q", p.id.Name, env.Type)
|
||||
}
|
||||
|
|
|
|||
128
pkg/party/returncode_extract.go
Normal file
128
pkg/party/returncode_extract.go
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
package party
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/user/evote/pkg/elgamal"
|
||||
"github.com/user/evote/pkg/returncodes"
|
||||
"github.com/user/evote/pkg/transport"
|
||||
)
|
||||
|
||||
// Return-code extraction message types.
|
||||
const (
|
||||
MsgRCExpReq = "rc-exp-req" // server -> CCj: exponentiate E2 by k_j
|
||||
MsgRCExpResp = "rc-exp-resp"
|
||||
MsgRCDecReq = "rc-dec-req" // server -> CCj: partial-decrypt factor
|
||||
MsgRCDecResp = "rc-dec-resp"
|
||||
)
|
||||
|
||||
// tauFixed is the option-independent tau fed to ComputeLCCValue at both card
|
||||
// generation and vote-time extraction (see codes.go for why it is fixed).
|
||||
var tauFixed = big.NewInt(1)
|
||||
|
||||
type rcExpReq struct {
|
||||
VcID string `json:"vc_id"`
|
||||
E2 wireCiphertext `json:"e2"`
|
||||
}
|
||||
type rcExpResp struct {
|
||||
Exp wireCiphertext `json:"exp"` // E2^{k_j}
|
||||
}
|
||||
|
||||
type rcDecReq struct {
|
||||
Gamma string `json:"gamma"` // gamma of the combined exponentiated ciphertext
|
||||
}
|
||||
type rcDecResp struct {
|
||||
Factor string `json:"factor"` // gamma^{sk_j[0]}
|
||||
}
|
||||
|
||||
// extractChoiceReturnCode (server) recovers the voter's return code from E2 by
|
||||
// asking each CC to exponentiate E2 by its return-code key and to contribute a
|
||||
// partial-decryption factor, then combining and looking the value up in the
|
||||
// mapping table. The value recovered is vote^{Σk_j}, which equals the card base
|
||||
// prime_sel^{Σk_j} — so the code returned is the code for the ACTUALLY SUBMITTED
|
||||
// vote (cast-as-intended). Returns the short code, or "" if no match.
|
||||
func (p *VotingServer) extractChoiceReturnCode(vcID string, e2 elgamal.Ciphertext) (string, error) {
|
||||
group := p.cer.Config.Group
|
||||
|
||||
// Phase 1: each CC exponentiates E2 by its return-code key; product over CCs
|
||||
// yields Enc(vote^{Σk}, ·, returnCodesPK).
|
||||
var combined elgamal.Ciphertext
|
||||
for j := 0; j < p.cer.Config.NumCCs; j++ {
|
||||
env, err := p.cer.send(p.id, CCName(j), MsgRCExpReq, rcExpReq{VcID: vcID, E2: encodeCiphertext(e2)})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("rc-exp cc%d: %w", j, err)
|
||||
}
|
||||
var resp rcExpResp
|
||||
if err := transport.UnmarshalPayload(env.Payload, &resp); err != nil {
|
||||
return "", err
|
||||
}
|
||||
expCt, err := resp.Exp.decode(group)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("rc-exp cc%d decode: %w", j, err)
|
||||
}
|
||||
if j == 0 {
|
||||
combined = expCt
|
||||
} else {
|
||||
combined = combined.Multiply(expCt)
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: each CC contributes gamma^{sk_j[0]}; product is the decryption
|
||||
// mask returnCodesPK^{R}. vote^{Σk} = phi / mask.
|
||||
mask := group.Identity()
|
||||
for j := 0; j < p.cer.Config.NumCCs; j++ {
|
||||
env, err := p.cer.send(p.id, CCName(j), MsgRCDecReq, rcDecReq{Gamma: combined.Gamma.Value().String()})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("rc-dec cc%d: %w", j, err)
|
||||
}
|
||||
var resp rcDecResp
|
||||
if err := transport.UnmarshalPayload(env.Payload, &resp); err != nil {
|
||||
return "", err
|
||||
}
|
||||
factor, err := strToGq(resp.Factor, group)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("rc-dec cc%d decode: %w", j, err)
|
||||
}
|
||||
mask = mask.Multiply(factor)
|
||||
}
|
||||
voteToK := combined.GetPhi(0).Divide(mask)
|
||||
|
||||
// Look up the recovered value in the mapping table.
|
||||
lCC := returncodes.ComputeLCCValue(voteToK, vcID, p.cer.Config.ElectionID, tauFixed)
|
||||
code, err := p.st.mappingTable.Lookup(lCC)
|
||||
if err != nil {
|
||||
return "", nil // no match (e.g. spoiled) — not an error
|
||||
}
|
||||
return code, nil
|
||||
}
|
||||
|
||||
// handleRCExp (CC) exponentiates E2 by this CC's return-code key k_j.
|
||||
func (p *ControlComponent) handleRCExp(env *transport.Envelope) (*transport.Envelope, error) {
|
||||
var req rcExpReq
|
||||
if err := transport.UnmarshalPayload(env.Payload, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
group := p.cer.Config.Group
|
||||
e2, err := req.E2.decode(group)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cc%d rc-exp: %w", p.index, err)
|
||||
}
|
||||
k := deriveReturnCodeKey(p.st.returnCodeSecret, labelChoice, p.cer.Config.ElectionID, req.VcID, group)
|
||||
return reply(p.id, env.From, MsgRCExpResp, env.Nonce, rcExpResp{Exp: encodeCiphertext(e2.Exponentiate(k))})
|
||||
}
|
||||
|
||||
// handleRCDec (CC) returns gamma^{sk_j[0]}, its factor of the decryption mask.
|
||||
func (p *ControlComponent) handleRCDec(env *transport.Envelope) (*transport.Envelope, error) {
|
||||
var req rcDecReq
|
||||
if err := transport.UnmarshalPayload(env.Payload, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
group := p.cer.Config.Group
|
||||
gamma, err := strToGq(req.Gamma, group)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cc%d rc-dec: %w", p.index, err)
|
||||
}
|
||||
factor := gamma.Exponentiate(p.st.keyPair.SK.Get(0))
|
||||
return reply(p.id, env.From, MsgRCDecResp, env.Nonce, rcDecResp{Factor: factor.Value().String()})
|
||||
}
|
||||
|
|
@ -129,7 +129,25 @@ func (p *VoterClient) castBallot(selected []int) (*transport.Envelope, error) {
|
|||
ReturnCodeCT: encodeCiphertext(e2),
|
||||
EqProof: encodePlaintextEquality(eqProof),
|
||||
}
|
||||
return p.cer.send(p.id, NameServer, MsgCastBallot, ballot)
|
||||
env, err := p.cer.send(p.id, NameServer, MsgCastBallot, ballot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Cast-as-intended check: the return code the server computed from the
|
||||
// submission must match the code printed on the card for the chosen option.
|
||||
var resp ballotStoredResp
|
||||
if err := transport.UnmarshalPayload(env.Payload, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(selected) == 1 {
|
||||
want := p.st.card.ChoiceReturnCodes[selected[0]]
|
||||
if resp.ChoiceReturnCode != want {
|
||||
return nil, fmt.Errorf("%s: return code mismatch — server returned %q, card expects %q (vote may have been altered)",
|
||||
p.id.Name, resp.ChoiceReturnCode, want)
|
||||
}
|
||||
}
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// handleCastBallot (server) validates a ballot's structure, asks every CC to
|
||||
|
|
@ -179,7 +197,19 @@ func (p *VotingServer) handleCastBallot(env *transport.Envelope) (*transport.Env
|
|||
Ciphertext: ct,
|
||||
VcPK: vcPK,
|
||||
})
|
||||
return reply(p.id, env.From, MsgBallotStored, env.Nonce, ackPayload{Party: p.id.Name, OK: true})
|
||||
|
||||
// Cast-as-intended: recover the return code from E2 (proven equal to the
|
||||
// ballot) via the CCs, and send it back to the voter to check against the
|
||||
// card. E2 was validated by decode above.
|
||||
e2, err := b.ReturnCodeCT.decode(group)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("return-code ciphertext: %w", err)
|
||||
}
|
||||
code, err := p.extractChoiceReturnCode(b.VcID, e2)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("return-code extraction: %w", err)
|
||||
}
|
||||
return reply(p.id, env.From, MsgBallotStored, env.Nonce, ballotStoredResp{OK: true, ChoiceReturnCode: code})
|
||||
}
|
||||
|
||||
type ballotVerdict struct {
|
||||
|
|
@ -187,6 +217,13 @@ type ballotVerdict struct {
|
|||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// ballotStoredResp is the server's reply to the voter, carrying the return code
|
||||
// the CCs computed from the submitted ballot for the voter to check.
|
||||
type ballotStoredResp struct {
|
||||
OK bool `json:"ok"`
|
||||
ChoiceReturnCode string `json:"choice_return_code"`
|
||||
}
|
||||
|
||||
// handleVerifyBallot (CC) re-derives the exponentiation-proof statement and
|
||||
// verifies it. A malformed proof yields a clean reject, never a panic.
|
||||
func (p *ControlComponent) handleVerifyBallot(env *transport.Envelope) (*transport.Envelope, error) {
|
||||
|
|
|
|||
|
|
@ -4,9 +4,11 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/user/evote/pkg/elgamal"
|
||||
"github.com/user/evote/pkg/hash"
|
||||
emath "github.com/user/evote/pkg/math"
|
||||
"github.com/user/evote/pkg/returncodes"
|
||||
"github.com/user/evote/pkg/transport"
|
||||
"github.com/user/evote/pkg/zkp"
|
||||
)
|
||||
|
||||
func runToVoting(t *testing.T, voters, options int) *Ceremony {
|
||||
|
|
@ -59,6 +61,75 @@ func TestBallotWithBadProofRejected(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestCastAsIntendedCatchesVoteSubstitution simulates malware that encrypts one
|
||||
// option for the tally (E1) but a different option in the return-code channel
|
||||
// (E2), so the voter would be shown the return code for the option they intended
|
||||
// while a different vote is actually tallied. The plaintext-equality proof binds
|
||||
// E1 and E2, so the CCs reject the ballot — the substitution is caught.
|
||||
func TestCastAsIntendedCatchesVoteSubstitution(t *testing.T) {
|
||||
c := runToVoting(t, 1, 3)
|
||||
voter := c.Voters[0]
|
||||
// Tally vote = option 0; return-code channel claims option 1.
|
||||
if _, err := voter.castBallotSubstituted(t, 0, 1); err == nil {
|
||||
t.Fatal("CCs accepted a ballot whose E1 and E2 encrypt different votes")
|
||||
}
|
||||
if len(c.Server.st.ballotBox) != 0 {
|
||||
t.Fatal("substituted ballot was stored")
|
||||
}
|
||||
}
|
||||
|
||||
// castBallotSubstituted encrypts option tallyOpt in E1 and option codeOpt in E2,
|
||||
// attaching an equality proof over the mismatched pair (which cannot verify).
|
||||
func (p *VoterClient) castBallotSubstituted(t *testing.T, tallyOpt, codeOpt int) (*transport.Envelope, error) {
|
||||
t.Helper()
|
||||
cfg := p.cer.Config
|
||||
group := cfg.Group
|
||||
zq := emath.ZqGroupFromGqGroup(group)
|
||||
|
||||
tallyElem, _ := emath.NewGqElement(returncodes.EncodeVote([]int{tallyOpt}, p.st.primes), group)
|
||||
codeElem, _ := emath.NewGqElement(returncodes.EncodeVote([]int{codeOpt}, p.st.primes), group)
|
||||
|
||||
msgElems := make([]emath.GqElement, cfg.NumOptions)
|
||||
msgElems[0] = tallyElem
|
||||
for i := 1; i < cfg.NumOptions; i++ {
|
||||
msgElems[i] = group.Identity()
|
||||
}
|
||||
r := emath.RandomZqElement(zq)
|
||||
ct := elgamal.Encrypt(elgamal.NewMessage(emath.GqVectorOf(msgElems...)), r, p.st.electionPK)
|
||||
|
||||
p.st.vcSK = emath.RandomZqElement(zq)
|
||||
vcPK := group.Generator().Exponentiate(p.st.vcSK)
|
||||
gammaExp := ct.Gamma.Exponentiate(p.st.vcSK)
|
||||
phi0Exp := ct.GetPhi(0).Exponentiate(p.st.vcSK)
|
||||
bases := emath.GqVectorOf(group.Generator(), ct.Gamma, ct.GetPhi(0))
|
||||
exps := emath.GqVectorOf(vcPK, gammaExp, phi0Exp)
|
||||
expProof := zkp.GenExponentiationProof(bases, p.st.vcSK, exps, group,
|
||||
hash.HashableString{Value: cfg.ElectionID},
|
||||
hash.HashableString{Value: p.st.card.VerificationCardID})
|
||||
|
||||
// E2 encrypts a DIFFERENT option than E1.
|
||||
rcPK0 := elgamal.PublicKey{Elements: emath.GqVectorOf(p.st.returnCodePK.Get(0))}
|
||||
r2 := emath.RandomZqElement(zq)
|
||||
e2 := elgamal.Encrypt(elgamal.NewMessage(emath.GqVectorOf(codeElem)), r2, rcPK0)
|
||||
c1 := elgamal.NewCiphertext(ct.Gamma, emath.GqVectorOf(ct.GetPhi(0)))
|
||||
eqProof := zkp.GenPlaintextEqualityProof(c1, e2,
|
||||
p.st.electionPK.Get(0), p.st.returnCodePK.Get(0), r, r2, group,
|
||||
hash.HashableString{Value: cfg.ElectionID},
|
||||
hash.HashableString{Value: p.st.card.VerificationCardID})
|
||||
|
||||
return p.cer.send(p.id, NameServer, MsgCastBallot, wireBallot{
|
||||
VoterID: p.st.card.VoterID,
|
||||
VcID: p.st.card.VerificationCardID,
|
||||
Ciphertext: encodeCiphertext(ct),
|
||||
ExponentiatedG: gammaExp.Value().String(),
|
||||
ExponentiatedP: phi0Exp.Value().String(),
|
||||
VcPK: vcPK.Value().String(),
|
||||
ExpProof: encodeExponentiation(expProof),
|
||||
ReturnCodeCT: encodeCiphertext(e2),
|
||||
EqProof: encodePlaintextEquality(eqProof),
|
||||
})
|
||||
}
|
||||
|
||||
// castBallotTampered submits a structurally valid ballot (real ciphertext,
|
||||
// well-formed group elements) but with a zeroed exponentiation proof that cannot
|
||||
// verify. Used only by the test above.
|
||||
|
|
|
|||
Loading…
Reference in a new issue