|
7 | 7 | "crypto/rand" |
8 | 8 | "crypto/elliptic" |
9 | 9 | "encoding/hex" |
| 10 | + "encoding/asn1" |
| 11 | + |
| 12 | + "github.com/deatil/go-cryptobin/elliptic/secp256k1" |
10 | 13 | ) |
11 | 14 |
|
12 | 15 | func fromHex(s string) []byte { |
@@ -79,3 +82,139 @@ func Test_Encrypt_Check(t *testing.T) { |
79 | 82 | t.Errorf("Test_Encrypt_Check fail, got %x, want %x", p2, data) |
80 | 83 | } |
81 | 84 | } |
| 85 | + |
| 86 | +func Test_EncryptASN1(t *testing.T) { |
| 87 | + c := elliptic.P256() |
| 88 | + |
| 89 | + priv, err := GenerateKey(rand.Reader, c) |
| 90 | + if err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + |
| 94 | + pub := &priv.PublicKey |
| 95 | + |
| 96 | + data := []byte("test-data test-data test-data test-data test-data") |
| 97 | + |
| 98 | + ct, err := EncryptASN1(rand.Reader, pub, data) |
| 99 | + if err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + |
| 103 | + p, _ := DecryptASN1(priv, ct) |
| 104 | + if !bytes.Equal(data, p) { |
| 105 | + t.Errorf("Test_Encrypt fail, got %x, want %x", p, data) |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +func Test_EncryptASN1_Check(t *testing.T) { |
| 111 | + c := elliptic.P256() |
| 112 | + |
| 113 | + x := fromHex("67e87b98e8a4383098fedb448c1f3d278bebba50525dec57c0576c605bfffdda") |
| 114 | + Y := fromHex("045ec79d828a77af85aa9c8ef3ba5afd7674376d6b134d14b10bb4afb7fd0952b0e894f696b38096ff547bbd0a0f1d14a195f2fc9ce951901fe2925560f29d98c0") |
| 115 | + |
| 116 | + priv, err := NewPrivateKey(c, x) |
| 117 | + if err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + |
| 121 | + pub, err := NewPublicKey(c, Y) |
| 122 | + if err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + |
| 126 | + data := []byte("Hello") |
| 127 | + |
| 128 | + ct, err := EncryptASN1(rand.Reader, pub, data) |
| 129 | + if err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + |
| 133 | + p, _ := DecryptASN1(priv, ct) |
| 134 | + if !bytes.Equal(data, p) { |
| 135 | + t.Errorf("ElgamalDecrypt fail, got %x, want %x", p, data) |
| 136 | + } |
| 137 | + |
| 138 | + C1 := fromHex("04af7035184190ce72b1ee000ec8f18927a664c23358ce4d41ff757283a5846bb58c19c551753ea0af151c31c1a3698606af565c122a387dbe67d7fa5deba2393f") |
| 139 | + C2, _ := new(big.Int).SetString("64998866770800537035816591092081487793369751526287129670052291837083837454710935744289325621649282383337514803150244272041445838052262073601284819093853352", 10) |
| 140 | + |
| 141 | + enc, err := asn1.Marshal(encryptedData{ |
| 142 | + C1: C1, |
| 143 | + C2: C2, |
| 144 | + }) |
| 145 | + |
| 146 | + p2, _ := DecryptASN1(priv, enc) |
| 147 | + if !bytes.Equal(data, p2) { |
| 148 | + t.Errorf("Test_Encrypt_Check fail, got %x, want %x", p2, data) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func Test_key(t *testing.T) { |
| 153 | + test_key(t, elliptic.P224()) |
| 154 | + test_key(t, elliptic.P256()) |
| 155 | + test_key(t, elliptic.P384()) |
| 156 | + test_key(t, elliptic.P521()) |
| 157 | + |
| 158 | + test_key(t, secp256k1.S256()) |
| 159 | +} |
| 160 | + |
| 161 | +func test_key(t *testing.T, c elliptic.Curve) { |
| 162 | + priv, err := GenerateKey(rand.Reader, c) |
| 163 | + if err != nil { |
| 164 | + t.Fatal(err) |
| 165 | + } |
| 166 | + |
| 167 | + pub := &priv.PublicKey |
| 168 | + |
| 169 | + { |
| 170 | + priKey, err := MarshalECPrivateKey(priv) |
| 171 | + if err != nil { |
| 172 | + t.Fatal(err) |
| 173 | + } |
| 174 | + |
| 175 | + parsekey, err := ParseECPrivateKey(priKey) |
| 176 | + if err != nil { |
| 177 | + t.Fatal(err) |
| 178 | + } |
| 179 | + |
| 180 | + if !parsekey.Equal(priv) { |
| 181 | + t.Error("MarshalECPrivateKey fail") |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + { |
| 187 | + priKey, err := MarshalPrivateKey(priv) |
| 188 | + if err != nil { |
| 189 | + t.Fatal(err) |
| 190 | + } |
| 191 | + |
| 192 | + parsekey, err := ParsePrivateKey(priKey) |
| 193 | + if err != nil { |
| 194 | + t.Fatal(err) |
| 195 | + } |
| 196 | + |
| 197 | + if !parsekey.Equal(priv) { |
| 198 | + t.Error("MarshalPrivateKey fail") |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + { |
| 204 | + pubKey, err := MarshalPublicKey(pub) |
| 205 | + if err != nil { |
| 206 | + t.Fatal(err) |
| 207 | + } |
| 208 | + |
| 209 | + parsekey, err := ParsePublicKey(pubKey) |
| 210 | + if err != nil { |
| 211 | + t.Fatal(err) |
| 212 | + } |
| 213 | + |
| 214 | + if !parsekey.Equal(pub) { |
| 215 | + t.Error("MarshalPublicKey fail") |
| 216 | + } |
| 217 | + |
| 218 | + } |
| 219 | + |
| 220 | +} |
0 commit comments