Skip to content

Commit 9052df5

Browse files
committed
fix elgamalecc
1 parent a09a28c commit 9052df5

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

pubkey/elgamalecc/elgamalecc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (priv *PrivateKey) Public() crypto.PublicKey {
5959
return &priv.PublicKey
6060
}
6161

62+
// Decrypt decrypts ciphertext with priv.
63+
func (priv *PrivateKey) Decrypt(rand io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error) {
64+
return DecryptASN1(priv, ciphertext)
65+
}
66+
6267
// Generate the PrivateKey
6368
func GenerateKey(random io.Reader, c elliptic.Curve) (*PrivateKey, error) {
6469
d, err := randFieldElement(random, c)

pubkey/elgamalecc/elgamalecc_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/asn1"
1111

1212
"github.com/deatil/go-cryptobin/elliptic/secp256k1"
13+
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
1314
)
1415

1516
func fromHex(s string) []byte {
@@ -57,6 +58,40 @@ func Test_Encrypt_Check(t *testing.T) {
5758
t.Fatal(err)
5859
}
5960

61+
priv2, err := NewPrivateKey(c, x)
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
66+
privToPub := priv.Public()
67+
if !pub.Equal(privToPub) {
68+
t.Errorf("Test_Encrypt_Check privToPub fail")
69+
}
70+
71+
if !priv2.Equal(priv) {
72+
t.Errorf("Test_Encrypt_Check priv Equal priv2 fail")
73+
}
74+
75+
x2 := fromHex("6fbfc7f9710928bfde9398f5dab8b2a48401863a705e054d344b8db06e043373")
76+
Y2 := fromHex("0476e321b586be3cb474bd7f849a6bb17354d6346de312aa44ce646911132d3743117354253159f61cdfeaf896cbf3b6070334a003b49a18934838b3bfff98cf77")
77+
78+
priv3, err := NewPrivateKey(c, x2)
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
83+
pub3, err := NewPublicKey(c, Y2)
84+
if err != nil {
85+
t.Fatal(err)
86+
}
87+
88+
if priv3.Equal(priv) {
89+
t.Errorf("Test_Encrypt_Check priv3 should not Equal priv")
90+
}
91+
if pub3.Equal(pub) {
92+
t.Errorf("Test_Encrypt_Check pub3 should not Equal pub")
93+
}
94+
6095
data := []byte("Hello")
6196

6297
C1x, C1y, C2, err := Encrypt(rand.Reader, pub, data)
@@ -149,6 +184,90 @@ func Test_EncryptASN1_Check(t *testing.T) {
149184
}
150185
}
151186

187+
func Test_EncryptASN1_Check2(t *testing.T) {
188+
c := secp256k1.S256()
189+
190+
x := fromHex("58202239faae18806aa03e7b8cc287a2c213081c3f3631aaea63ce83077b7e6c")
191+
Y := fromHex("0408ced9c22629139d373b23209bc95d6be34ef13a8cc5c1a7504ea988bfe04702f3278908d94eb3e6b5091701e4f2f30d3494b7010afeec96c069215d8aef27f6")
192+
193+
priv, err := NewPrivateKey(c, x)
194+
if err != nil {
195+
t.Fatal(err)
196+
}
197+
198+
pub, err := NewPublicKey(c, Y)
199+
if err != nil {
200+
t.Fatal(err)
201+
}
202+
203+
data := []byte("00Hello")
204+
205+
ct, err := EncryptASN1(rand.Reader, pub, data)
206+
if err != nil {
207+
t.Fatal(err)
208+
}
209+
210+
p, _ := DecryptASN1(priv, ct)
211+
if !bytes.Equal(data, p) {
212+
t.Errorf("ElgamalDecrypt fail, got %x, want %x", p, data)
213+
}
214+
215+
C1 := fromHex("04c82264c17f181e469a1cb258b2c48300f692a65e93181a32fe8c2d43a2d3d7e9892599a2a538a64726ac12bf78f5e08b0e77ccd4fe4bf2a902e831e181f900ff")
216+
C2, _ := new(big.Int).SetString("60465485415873670516518360642899670456852166251606531814121725105989579773216426234104724728428181230636039521187393414737061049499825803369974263911235347", 10)
217+
218+
enc, err := asn1.Marshal(encryptedData{
219+
C1: C1,
220+
C2: C2,
221+
})
222+
223+
p2, _ := DecryptASN1(priv, enc)
224+
if !bytes.Equal(data, p2) {
225+
t.Errorf("Test_EncryptASN1_Check2 fail, got %x, want %x", p2, data)
226+
}
227+
}
228+
229+
func Test_EncryptASN1_Check3(t *testing.T) {
230+
c := secp256k1.S256()
231+
232+
x := fromHex("f64d526828db07a022cd6b015c0e98cadc2ebc972fa3b89c247b0542f277e7b7")
233+
Y := fromHex("04f12647c9bd5d3bf8c8169da4e32633912f8220c2eff81a9114ffb6ce8045dad0b947c83e5d57eff38af3e9f99ed40072280bac4ef14a030defca3d8e478ac910")
234+
235+
priv, err := NewPrivateKey(c, x)
236+
if err != nil {
237+
t.Fatal(err)
238+
}
239+
240+
pub, err := NewPublicKey(c, Y)
241+
if err != nil {
242+
t.Fatal(err)
243+
}
244+
245+
data := []byte("Hello")
246+
247+
ct, err := EncryptASN1(rand.Reader, pub, data)
248+
if err != nil {
249+
t.Fatal(err)
250+
}
251+
252+
p, _ := DecryptASN1(priv, ct)
253+
if !bytes.Equal(data, p) {
254+
t.Errorf("ElgamalDecrypt fail, got %x, want %x", p, data)
255+
}
256+
257+
C1 := fromHex("04dda83f84e290ee6183c99eac3ddf46a31b74b8c7bf0011688ac67afb53479384118539fab5649fb4356f8a5de6377d864985ad87399b4cd97317fc541bac3c6f")
258+
C2, _ := new(big.Int).SetString("61738521896366194598781997268525606632833970892953952697258628550069003116299643941844766144610099424143434787628598037087667609225907007597296768147814038", 10)
259+
260+
enc, err := asn1.Marshal(encryptedData{
261+
C1: C1,
262+
C2: C2,
263+
})
264+
265+
p2, _ := DecryptASN1(priv, enc)
266+
if !bytes.Equal(data, p2) {
267+
t.Errorf("Test_EncryptASN1_Check3 fail, got %x, want %x", p2, data)
268+
}
269+
}
270+
152271
func Test_key(t *testing.T) {
153272
test_key(t, elliptic.P224())
154273
test_key(t, elliptic.P256())
@@ -218,3 +337,63 @@ func test_key(t *testing.T, c elliptic.Curve) {
218337
}
219338

220339
}
340+
341+
func Test_PrivateKeyDecrypt_Check(t *testing.T) {
342+
c := elliptic.P256()
343+
344+
x := fromHex("67e87b98e8a4383098fedb448c1f3d278bebba50525dec57c0576c605bfffdda")
345+
346+
priv, err := NewPrivateKey(c, x)
347+
if err != nil {
348+
t.Fatal(err)
349+
}
350+
351+
data := []byte("Hello")
352+
353+
C1 := fromHex("04af7035184190ce72b1ee000ec8f18927a664c23358ce4d41ff757283a5846bb58c19c551753ea0af151c31c1a3698606af565c122a387dbe67d7fa5deba2393f")
354+
C2, _ := new(big.Int).SetString("64998866770800537035816591092081487793369751526287129670052291837083837454710935744289325621649282383337514803150244272041445838052262073601284819093853352", 10)
355+
356+
enc, err := asn1.Marshal(encryptedData{
357+
C1: C1,
358+
C2: C2,
359+
})
360+
361+
p2, _ := priv.Decrypt(nil, enc, nil)
362+
if !bytes.Equal(data, p2) {
363+
t.Errorf("Test_PrivateKeyDecrypt_Check fail, got %x, want %x", p2, data)
364+
}
365+
}
366+
367+
func Test_NewPrivateKey(t *testing.T) {
368+
c := elliptic.P256()
369+
370+
priv, err := GenerateKey(rand.Reader, c)
371+
if err != nil {
372+
t.Fatal(err)
373+
}
374+
pub := &priv.PublicKey
375+
376+
priv2, err := GenerateKey(rand.Reader, c)
377+
if err != nil {
378+
t.Fatal(err)
379+
}
380+
pub2 := &priv2.PublicKey
381+
382+
privBytes := PrivateKeyTo(priv)
383+
pubBytes := PublicKeyTo(pub)
384+
385+
newPriv, err := NewPrivateKey(c, privBytes)
386+
if err != nil {
387+
t.Fatal(err)
388+
}
389+
newPub, err := NewPublicKey(c, pubBytes)
390+
if err != nil {
391+
t.Fatal(err)
392+
}
393+
394+
cryptobin_test.Equal(t, priv, newPriv, "NewPrivateKey 1")
395+
cryptobin_test.Equal(t, pub, newPub, "NewPublicKey 1")
396+
397+
cryptobin_test.NotEqual(t, priv2, newPriv, "NewPrivateKey 1")
398+
cryptobin_test.NotEqual(t, pub2, newPub, "NewPublicKey 1")
399+
}

0 commit comments

Comments
 (0)