Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Mar 14, 2024
1 parent 1c814e1 commit da81c08
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
53 changes: 34 additions & 19 deletions elgamal/elgamal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions elgamal/elgamal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit da81c08

Please sign in to comment.