diff --git a/elgamal/elgamal.go b/elgamal/elgamal.go index cbd5d8dd..c990d992 100644 --- a/elgamal/elgamal.go +++ b/elgamal/elgamal.go @@ -138,7 +138,37 @@ func Encrypt(random io.Reader, pub *PublicKey, msg []byte) (c1, c2 *big.Int, err em[len(em)-len(msg)-1] = 0 copy(mm, msg) - m := new(big.Int).SetBytes(em) + return EncryptLegacy(random, pub, em) +} + +// Decrypt +func Decrypt(priv *PrivateKey, c1, c2 *big.Int) (msg []byte, err error) { + em, err := DecryptLegacy(priv, c1, c2) + if err != nil { + return nil, err + } + + firstByteIsTwo := subtle.ConstantTimeByteEq(em[0], 2) + + var lookingForIndex, index int + lookingForIndex = 1 + + for i := 1; i < len(em); i++ { + equals0 := subtle.ConstantTimeByteEq(em[i], 0) + index = subtle.ConstantTimeSelect(lookingForIndex&equals0, i, index) + lookingForIndex = subtle.ConstantTimeSelect(equals0, 0, lookingForIndex) + } + + if firstByteIsTwo != 1 || lookingForIndex != 0 || index < 9 { + return nil, errors.New("elgamal: decryption error") + } + + return em[index+1:], nil +} + +// EncryptLegacy +func EncryptLegacy(random io.Reader, pub *PublicKey, msg []byte) (c1, c2 *big.Int, err error) { + m := new(big.Int).SetBytes(msg) k, err := rand.Int(random, pub.P) if err != nil { @@ -153,8 +183,8 @@ func Encrypt(random io.Reader, pub *PublicKey, msg []byte) (c1, c2 *big.Int, err return } -// Decrypt -func Decrypt(priv *PrivateKey, c1, c2 *big.Int) (msg []byte, err error) { +// DecryptLegacy +func DecryptLegacy(priv *PrivateKey, c1, c2 *big.Int) (msg []byte, err error) { s := new(big.Int).Exp(c1, priv.X, priv.P) if s.ModInverse(s, priv.P) == nil { return nil, errors.New("elgamal: invalid private key") @@ -164,22 +194,7 @@ func Decrypt(priv *PrivateKey, c1, c2 *big.Int) (msg []byte, err error) { s.Mod(s, priv.P) em := s.Bytes() - firstByteIsTwo := subtle.ConstantTimeByteEq(em[0], 2) - - var lookingForIndex, index int - lookingForIndex = 1 - - for i := 1; i < len(em); i++ { - equals0 := subtle.ConstantTimeByteEq(em[i], 0) - index = subtle.ConstantTimeSelect(lookingForIndex&equals0, i, index) - lookingForIndex = subtle.ConstantTimeSelect(equals0, 0, lookingForIndex) - } - - if firstByteIsTwo != 1 || lookingForIndex != 0 || index < 9 { - return nil, errors.New("elgamal: decryption error") - } - - return em[index+1:], nil + return em, nil } // c1 and c2 data diff --git a/elgamal/elgamal_test.go b/elgamal/elgamal_test.go index c1fa54f4..50793aa4 100644 --- a/elgamal/elgamal_test.go +++ b/elgamal/elgamal_test.go @@ -71,6 +71,28 @@ func Test_Encrypt_2(t *testing.T) { assertEqual(string(de), data, "Encrypt-Dedata") } +func Test_EncryptLegacy(t *testing.T) { + assertEqual := cryptobin_test.AssertEqualT(t) + assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) + assertError := cryptobin_test.AssertErrorT(t) + + pri, err := GenerateKey(rand.Reader, testBitsize, testProbability) + pub := &pri.PublicKey + + assertError(err, "Encrypt-Error") + assertNotEmpty(pri, "Encrypt") + + data := "123tesfd!df" + + c1, c2, err := EncryptLegacy(rand.Reader, pub, []byte(data)) + assertError(err, "EncryptLegacy-Encrypt-Error") + + de, err := DecryptLegacy(pri, c1, c2) + assertError(err, "EncryptLegacy-Decrypt-Error") + + assertEqual(string(de), data, "EncryptLegacy-Dedata") +} + func Test_EncryptAsn1(t *testing.T) { assertEqual := cryptobin_test.AssertEqualT(t) assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)