Last active
August 29, 2015 13:57
-
-
Save liliakai/9593979 to your computer and use it in GitHub Desktop.
My PLIBMTTBHGATY Project: RSA Blind Signatures in Go!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package blinding | |
import ( | |
"bytes" | |
"crypto/rsa" | |
"math/big" | |
) | |
func Blind(data []byte, publicKey *rsa.PublicKey, r *big.Int) (blinded []byte) { | |
e := new(big.Int).SetInt64(int64(publicKey.E)) | |
b := new(big.Int).Exp(r, e, publicKey.N) | |
m := new(big.Int).SetBytes(data) | |
m.Mul(m, b).Mod(m, publicKey.N) | |
return m.Bytes() | |
} | |
func Unblind(blind_sig []byte, publicKey *rsa.PublicKey, r_inv *big.Int) (sig []byte) { | |
s := new(big.Int).SetBytes(blind_sig) | |
s.Mul(s, r_inv).Mod(s, publicKey.N).Bytes() | |
return s.Bytes() | |
} | |
func Sign(data []byte, privateKey *rsa.PrivateKey) (sig []byte) { | |
m := new(big.Int).SetBytes(data) | |
s := new(big.Int).Exp(m, privateKey.D, privateKey.PublicKey.N) | |
return s.Bytes() | |
} | |
func Verify(data []byte, publicKey *rsa.PublicKey, signature []byte) bool { | |
s := new(big.Int).SetBytes(signature) | |
e := new(big.Int).SetUint64(uint64(publicKey.E)) | |
m := new(big.Int).Exp(s, e, publicKey.N) | |
return bytes.Compare(m.Bytes(), data) == 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package blinding | |
import ( | |
"io" | |
"testing" | |
"crypto/rsa" | |
"crypto/rand" | |
"math/big" | |
) | |
func randBytes(buf []byte) { | |
if _, err := io.ReadFull(rand.Reader, buf); err != nil { | |
panic(err) | |
} | |
} | |
func TestBlinding(t *testing.T) { | |
var data [32]byte | |
randBytes(data[:]) | |
priv, err := rsa.GenerateKey(rand.Reader, 1024) | |
r, _ := rand.Int(rand.Reader, priv.PublicKey.N) | |
r_inv := new(big.Int).ModInverse(r, priv.PublicKey.N) | |
blinded_data := Blind(data[:], &priv.PublicKey, r) | |
if (err != nil) { panic(err) } | |
blind_sig := Sign(blinded_data[:], priv) | |
sig := Unblind(blind_sig[:], &priv.PublicKey, r_inv) | |
good_signature := Verify(data[:], &priv.PublicKey, sig) | |
if (!good_signature) { | |
t.Fail() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment