Skip to content

Pad blind RSA protocol messages to fixed modulus width #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions blindsign/blindrsa/blindrsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func (v RSAVerifier) fixedBlind(message, salt []byte, r, rInv *big.Int) ([]byte,
z.Mul(z, x)
z.Mod(z, v.pk.N)

blindedMsg := z.Bytes()
kLen := (v.pk.N.BitLen() + 7) / 8
blindedMsg := make([]byte, kLen)
z.FillBytes(blindedMsg)

return blindedMsg, RSAVerifierState{
encodedMsg: encodedMsg,
Expand Down Expand Up @@ -152,7 +154,8 @@ func (state RSAVerifierState) Finalize(data []byte) ([]byte, error) {
s.Mul(s, z)
s.Mod(s, state.verifier.pk.N)

sig := s.Bytes()
sig := make([]byte, kLen)
s.FillBytes(sig)

err := verifyBlindSignature(state.verifier.pk, state.encodedMsg, sig)
if err != nil {
Expand Down Expand Up @@ -197,7 +200,10 @@ func (signer RSASigner) BlindSign(data []byte) ([]byte, error) {
return nil, err
}

return s.Bytes(), nil
blindSig := make([]byte, kLen)
s.FillBytes(blindSig)

return blindSig, nil
}

var (
Expand Down
14 changes: 14 additions & 0 deletions blindsign/blindrsa/blindrsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"math/big"
Expand Down Expand Up @@ -91,11 +92,20 @@ func runSignatureProtocol(signer RSASigner, verifier RSAVerifier, message []byte
return nil, err
}

kLen := (signer.sk.N.BitLen() + 7) / 8
if len(blindedMsg) != kLen {
return nil, fmt.Errorf("Protocol message (blind message) length mismatch, expected %d, got %d", kLen, len(blindedMsg))
}

blindedSig, err := signer.BlindSign(blindedMsg)
if err != nil {
return nil, err
}

if len(blindedSig) != kLen {
return nil, fmt.Errorf("Protocol message (blind signature) length mismatch, expected %d, got %d", kLen, len(blindedMsg))
}

sig, err := state.Finalize(blindedSig)
if err != nil {
return nil, err
Expand Down Expand Up @@ -342,6 +352,10 @@ func verifyTestVector(t *testing.T, vector testVector) {
t.Fatal(err)
}

if !bytes.Equal(state.(RSAVerifierState).encodedMsg, vector.encodedMessage) {
t.Errorf("Encoded message mismatch: expected %x, got %x", state.(RSAVerifierState).encodedMsg, vector.encodedMessage)
}

if !bytes.Equal(sig, vector.sig) {
t.Errorf("Signature mismatch: expected %x, got %x", sig, vector.sig)
}
Expand Down