|
|
@ -58,12 +58,10 @@ func TestKDF(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
k, err := concatKDF(h, msg, nil, 64) |
|
|
|
k, err := concatKDF(h, msg, nil, 64) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if len(k) != 64 { |
|
|
|
if len(k) != 64 { |
|
|
|
t.Logf("KDF: generated key is the wrong size (%d instead of 64\n", len(k)) |
|
|
|
t.Fatalf("KDF: generated key is the wrong size (%d instead of 64\n", len(k)) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -97,32 +95,27 @@ func cmpPublic(pub1, pub2 PublicKey) bool { |
|
|
|
func TestSharedKey(t *testing.T) { |
|
|
|
func TestSharedKey(t *testing.T) { |
|
|
|
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
skLen := MaxSharedKeyLength(&prv1.PublicKey) / 2 |
|
|
|
skLen := MaxSharedKeyLength(&prv1.PublicKey) / 2 |
|
|
|
|
|
|
|
|
|
|
|
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen) |
|
|
|
sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen) |
|
|
|
sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(sk1, sk2) { |
|
|
|
if !bytes.Equal(sk1, sk2) { |
|
|
|
t.Log(ErrBadSharedKeys.Error()) |
|
|
|
t.Fatal(ErrBadSharedKeys) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -169,26 +162,22 @@ func TestSharedKeyPadding(t *testing.T) { |
|
|
|
func TestTooBigSharedKey(t *testing.T) { |
|
|
|
func TestTooBigSharedKey(t *testing.T) { |
|
|
|
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, err = prv1.GenerateShared(&prv2.PublicKey, 32, 32) |
|
|
|
_, err = prv1.GenerateShared(&prv2.PublicKey, 32, 32) |
|
|
|
if err != ErrSharedKeyTooBig { |
|
|
|
if err != ErrSharedKeyTooBig { |
|
|
|
t.Log("ecdh: shared key should be too large for curve") |
|
|
|
t.Fatal("ecdh: shared key should be too large for curve") |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, err = prv2.GenerateShared(&prv1.PublicKey, 32, 32) |
|
|
|
_, err = prv2.GenerateShared(&prv1.PublicKey, 32, 32) |
|
|
|
if err != ErrSharedKeyTooBig { |
|
|
|
if err != ErrSharedKeyTooBig { |
|
|
|
t.Log("ecdh: shared key should be too large for curve") |
|
|
|
t.Fatal("ecdh: shared key should be too large for curve") |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -196,8 +185,7 @@ func TestTooBigSharedKey(t *testing.T) { |
|
|
|
func BenchmarkGenerateKeyP256(b *testing.B) { |
|
|
|
func BenchmarkGenerateKeyP256(b *testing.B) { |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
if _, err := GenerateKey(rand.Reader, elliptic.P256(), nil); err != nil { |
|
|
|
if _, err := GenerateKey(rand.Reader, elliptic.P256(), nil); err != nil { |
|
|
|
b.Log(err.Error()) |
|
|
|
b.Fatal(err) |
|
|
|
b.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -206,15 +194,13 @@ func BenchmarkGenerateKeyP256(b *testing.B) { |
|
|
|
func BenchmarkGenSharedKeyP256(b *testing.B) { |
|
|
|
func BenchmarkGenSharedKeyP256(b *testing.B) { |
|
|
|
prv, err := GenerateKey(rand.Reader, elliptic.P256(), nil) |
|
|
|
prv, err := GenerateKey(rand.Reader, elliptic.P256(), nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
b.Log(err.Error()) |
|
|
|
b.Fatal(err) |
|
|
|
b.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
b.ResetTimer() |
|
|
|
b.ResetTimer() |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
_, err := prv.GenerateShared(&prv.PublicKey, 16, 16) |
|
|
|
_, err := prv.GenerateShared(&prv.PublicKey, 16, 16) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
b.Log(err.Error()) |
|
|
|
b.Fatal(err) |
|
|
|
b.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -223,15 +209,13 @@ func BenchmarkGenSharedKeyP256(b *testing.B) { |
|
|
|
func BenchmarkGenSharedKeyS256(b *testing.B) { |
|
|
|
func BenchmarkGenSharedKeyS256(b *testing.B) { |
|
|
|
prv, err := GenerateKey(rand.Reader, crypto.S256(), nil) |
|
|
|
prv, err := GenerateKey(rand.Reader, crypto.S256(), nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
b.Log(err.Error()) |
|
|
|
b.Fatal(err) |
|
|
|
b.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
b.ResetTimer() |
|
|
|
b.ResetTimer() |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
_, err := prv.GenerateShared(&prv.PublicKey, 16, 16) |
|
|
|
_, err := prv.GenerateShared(&prv.PublicKey, 16, 16) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
b.Log(err.Error()) |
|
|
|
b.Fatal(err) |
|
|
|
b.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -240,38 +224,32 @@ func BenchmarkGenSharedKeyS256(b *testing.B) { |
|
|
|
func TestEncryptDecrypt(t *testing.T) { |
|
|
|
func TestEncryptDecrypt(t *testing.T) { |
|
|
|
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
message := []byte("Hello, world.") |
|
|
|
message := []byte("Hello, world.") |
|
|
|
ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) |
|
|
|
ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pt, err := prv2.Decrypt(ct, nil, nil) |
|
|
|
pt, err := prv2.Decrypt(ct, nil, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(pt, message) { |
|
|
|
if !bytes.Equal(pt, message) { |
|
|
|
t.Log("ecies: plaintext doesn't match message") |
|
|
|
t.Fatal("ecies: plaintext doesn't match message") |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, err = prv1.Decrypt(ct, nil, nil) |
|
|
|
_, err = prv1.Decrypt(ct, nil, nil) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
t.Log("ecies: encryption should not have succeeded") |
|
|
|
t.Fatal("ecies: encryption should not have succeeded") |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -341,50 +319,39 @@ func TestParamSelection(t *testing.T) { |
|
|
|
func testParamSelection(t *testing.T, c testCase) { |
|
|
|
func testParamSelection(t *testing.T, c testCase) { |
|
|
|
params := ParamsFromCurve(c.Curve) |
|
|
|
params := ParamsFromCurve(c.Curve) |
|
|
|
if params == nil && c.Expected != nil { |
|
|
|
if params == nil && c.Expected != nil { |
|
|
|
t.Logf("%s (%s)\n", ErrInvalidParams.Error(), c.Name) |
|
|
|
t.Fatalf("%s (%s)\n", ErrInvalidParams.Error(), c.Name) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} else if params != nil && !cmpParams(params, c.Expected) { |
|
|
|
} else if params != nil && !cmpParams(params, c.Expected) { |
|
|
|
t.Logf("ecies: parameters should be invalid (%s)\n", |
|
|
|
t.Fatalf("ecies: parameters should be invalid (%s)\n", c.Name) |
|
|
|
c.Name) |
|
|
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Logf("%s (%s)\n", err.Error(), c.Name) |
|
|
|
t.Fatalf("%s (%s)\n", err.Error(), c.Name) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Logf("%s (%s)\n", err.Error(), c.Name) |
|
|
|
t.Fatalf("%s (%s)\n", err.Error(), c.Name) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
message := []byte("Hello, world.") |
|
|
|
message := []byte("Hello, world.") |
|
|
|
ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) |
|
|
|
ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Logf("%s (%s)\n", err.Error(), c.Name) |
|
|
|
t.Fatalf("%s (%s)\n", err.Error(), c.Name) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pt, err := prv2.Decrypt(ct, nil, nil) |
|
|
|
pt, err := prv2.Decrypt(ct, nil, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Logf("%s (%s)\n", err.Error(), c.Name) |
|
|
|
t.Fatalf("%s (%s)\n", err.Error(), c.Name) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(pt, message) { |
|
|
|
if !bytes.Equal(pt, message) { |
|
|
|
t.Logf("ecies: plaintext doesn't match message (%s)\n", |
|
|
|
t.Fatalf("ecies: plaintext doesn't match message (%s)\n", c.Name) |
|
|
|
c.Name) |
|
|
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, err = prv1.Decrypt(ct, nil, nil) |
|
|
|
_, err = prv1.Decrypt(ct, nil, nil) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
t.Logf("ecies: encryption should not have succeeded (%s)\n", |
|
|
|
t.Fatalf("ecies: encryption should not have succeeded (%s)\n", c.Name) |
|
|
|
c.Name) |
|
|
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
@ -396,23 +363,20 @@ func TestBasicKeyValidation(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
message := []byte("Hello, world.") |
|
|
|
message := []byte("Hello, world.") |
|
|
|
ct, err := Encrypt(rand.Reader, &prv.PublicKey, message, nil, nil) |
|
|
|
ct, err := Encrypt(rand.Reader, &prv.PublicKey, message, nil, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, b := range badBytes { |
|
|
|
for _, b := range badBytes { |
|
|
|
ct[0] = b |
|
|
|
ct[0] = b |
|
|
|
_, err := prv.Decrypt(ct, nil, nil) |
|
|
|
_, err := prv.Decrypt(ct, nil, nil) |
|
|
|
if err != ErrInvalidPublicKey { |
|
|
|
if err != ErrInvalidPublicKey { |
|
|
|
t.Log("ecies: validated an invalid key") |
|
|
|
t.Fatal("ecies: validated an invalid key") |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -450,19 +414,16 @@ func TestSharedKeyStatic(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen) |
|
|
|
sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen) |
|
|
|
sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Log(err.Error()) |
|
|
|
t.Fatal(err) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(sk1, sk2) { |
|
|
|
if !bytes.Equal(sk1, sk2) { |
|
|
|
t.Log(ErrBadSharedKeys.Error()) |
|
|
|
t.Fatal(ErrBadSharedKeys) |
|
|
|
t.FailNow() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sk, _ := hex.DecodeString("167ccc13ac5e8a26b131c3446030c60fbfac6aa8e31149d0869f93626a4cdf62") |
|
|
|
sk, _ := hex.DecodeString("167ccc13ac5e8a26b131c3446030c60fbfac6aa8e31149d0869f93626a4cdf62") |
|
|
|