| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package ecdh implements ECDH encryption, suitable for OpenPGP, |
| 6 |
// as specified in RFC 6637, section 8. |
| 7 |
package ecdh |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"crypto/rand" |
| 12 |
"io" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/ProtonMail/go-crypto/openpgp/internal/ecc" |
| 16 |
|
| 17 |
"github.com/ProtonMail/go-crypto/openpgp/aes/keywrap" |
| 18 |
"github.com/ProtonMail/go-crypto/openpgp/internal/algorithm" |
| 19 |
) |
| 20 |
|
| 21 |
func TestCurves(t *testing.T) { |
| 22 |
for _, curve := range ecc.Curves { |
| 23 |
ECDHCurve, ok := curve.Curve.(ecc.ECDHCurve) |
| 24 |
if !ok { |
| 25 |
continue |
| 26 |
} |
| 27 |
|
| 28 |
t.Run(ECDHCurve.GetCurveName(), func(t *testing.T) { |
| 29 |
testFingerprint := make([]byte, 20) |
| 30 |
_, err := io.ReadFull(rand.Reader, testFingerprint[:]) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
|
| 35 |
priv := testGenerate(t, ECDHCurve) |
| 36 |
testEncryptDecrypt(t, priv, curve.Oid.Bytes(), testFingerprint) |
| 37 |
testValidation(t, priv) |
| 38 |
testDecryptInvalidPadding(t, priv, curve.Oid.Bytes(), testFingerprint) |
| 39 |
|
| 40 |
// Needs fresh key |
| 41 |
priv = testGenerate(t, ECDHCurve) |
| 42 |
testMarshalUnmarshal(t, priv) |
| 43 |
}) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func testGenerate(t *testing.T, curve ecc.ECDHCurve) *PrivateKey { |
| 48 |
kdf := KDF{ |
| 49 |
Hash: algorithm.SHA512, |
| 50 |
Cipher: algorithm.AES256, |
| 51 |
} |
| 52 |
|
| 53 |
priv, err := GenerateKey(rand.Reader, curve, kdf) |
| 54 |
if err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
|
| 58 |
return priv |
| 59 |
} |
| 60 |
|
| 61 |
func testEncryptDecrypt(t *testing.T, priv *PrivateKey, oid, fingerprint []byte) { |
| 62 |
message := []byte("hello world") |
| 63 |
|
| 64 |
vsG, m, err := Encrypt(rand.Reader, &priv.PublicKey, message, oid, fingerprint) |
| 65 |
if err != nil { |
| 66 |
t.Errorf("error encrypting: %s", err) |
| 67 |
} |
| 68 |
|
| 69 |
message2, err := Decrypt(priv, vsG, m, oid, fingerprint) |
| 70 |
if err != nil { |
| 71 |
t.Errorf("error decrypting: %s", err) |
| 72 |
} |
| 73 |
|
| 74 |
if !bytes.Equal(message2, message) { |
| 75 |
t.Errorf("decryption failed, got: %x, want: %x", message2, message) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func testDecryptInvalidPadding(t *testing.T, priv *PrivateKey, oid, fingerprint []byte) { |
| 80 |
ephemeral, zb, err := priv.PublicKey.curve.Encaps(rand.Reader, priv.PublicKey.Point) |
| 81 |
if err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
vsG := priv.PublicKey.curve.MarshalBytePoint(ephemeral) |
| 85 |
|
| 86 |
z, err := buildKey(&priv.PublicKey, zb, oid, fingerprint, false, false) |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
|
| 91 |
malformed := []byte{0x09, 'A', 'B', 'C', 'D', 'E', 'F', 0xFF} |
| 92 |
c, err := keywrap.Wrap(z, malformed) |
| 93 |
if err != nil { |
| 94 |
t.Fatal(err) |
| 95 |
} |
| 96 |
|
| 97 |
if _, err := Decrypt(priv, vsG, c, oid, fingerprint); err == nil { |
| 98 |
t.Fatal("expected an error for invalid padding, got none") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func testValidation(t *testing.T, priv *PrivateKey) { |
| 103 |
if err := Validate(priv); err != nil { |
| 104 |
t.Fatalf("valid key marked as invalid: %s", err) |
| 105 |
} |
| 106 |
|
| 107 |
priv.D[5] ^= 1 |
| 108 |
if err := Validate(priv); err == nil { |
| 109 |
t.Fatalf("failed to detect invalid key") |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func testMarshalUnmarshal(t *testing.T, priv *PrivateKey) { |
| 114 |
p := priv.MarshalPoint() |
| 115 |
d := priv.MarshalByteSecret() |
| 116 |
|
| 117 |
parsed := NewPrivateKey(*NewPublicKey(priv.GetCurve(), priv.KDF.Hash, priv.KDF.Cipher)) |
| 118 |
|
| 119 |
if err := parsed.UnmarshalPoint(p); err != nil { |
| 120 |
t.Fatalf("unable to unmarshal point: %s", err) |
| 121 |
} |
| 122 |
|
| 123 |
if err := parsed.UnmarshalByteSecret(d); err != nil { |
| 124 |
t.Fatalf("unable to unmarshal integer: %s", err) |
| 125 |
} |
| 126 |
|
| 127 |
expectedD := make([]byte, len(priv.D)) |
| 128 |
copy(expectedD, priv.D) |
| 129 |
|
| 130 |
// Curve25519 expects keys to be saved clamped |
| 131 |
if priv.curve.GetCurveName() == "curve25519" { |
| 132 |
expectedD[0] &= 248 |
| 133 |
expectedD[31] &= 127 |
| 134 |
expectedD[31] |= 64 |
| 135 |
} |
| 136 |
|
| 137 |
if !bytes.Equal(priv.Point, parsed.Point) || !bytes.Equal(expectedD, parsed.D) { |
| 138 |
t.Fatal("failed to marshal/unmarshal correctly") |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestShortKDFHashRejected(t *testing.T) { |
| 143 |
curve := ecc.NewCurve25519() |
| 144 |
kdf := KDF{Hash: algorithm.SHA224, Cipher: algorithm.AES256} |
| 145 |
priv, err := GenerateKey(rand.Reader, curve, kdf) |
| 146 |
if err != nil { |
| 147 |
t.Fatal(err) |
| 148 |
} |
| 149 |
msg := make([]byte, 24) |
| 150 |
if _, _, err := Encrypt(rand.Reader, &priv.PublicKey, msg, []byte{}, make([]byte, 20)); err == nil { |
| 151 |
t.Error("expected error when the KDF hash is shorter than the KDF cipher key") |
| 152 |
} |
| 153 |
} |