From f2e94de6d62be39044b28ca61b8659cd295253c2 Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Tue, 21 Jun 2011 21:00:49 -0400 Subject: crypto/openpgp: add ElGamal support. R=bradfitz, r CC=golang-dev https://golang.org/cl/4639049 --- src/pkg/crypto/openpgp/write_test.go | 156 +++++++++++++++++++++-------------- 1 file changed, 95 insertions(+), 61 deletions(-) (limited to 'src/pkg/crypto/openpgp/write_test.go') diff --git a/src/pkg/crypto/openpgp/write_test.go b/src/pkg/crypto/openpgp/write_test.go index 028a5e087d..c542dfa45d 100644 --- a/src/pkg/crypto/openpgp/write_test.go +++ b/src/pkg/crypto/openpgp/write_test.go @@ -122,78 +122,112 @@ func TestSymmetricEncryption(t *testing.T) { } } -func testEncryption(t *testing.T, isSigned bool) { - kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) - - var signed *Entity - if isSigned { - signed = kring[0] - } +var testEncryptionTests = []struct { + keyRingHex string + isSigned bool +}{ + { + testKeys1And2PrivateHex, + false, + }, + { + testKeys1And2PrivateHex, + true, + }, + { + dsaElGamalTestKeysHex, + false, + }, + { + dsaElGamalTestKeysHex, + true, + }, +} - buf := new(bytes.Buffer) - w, err := Encrypt(buf, kring[:1], signed, nil /* no hints */ ) - if err != nil { - t.Errorf("error in Encrypt: %s", err) - return - } +func TestEncryption(t *testing.T) { + for i, test := range testEncryptionTests { + kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex)) + + passphrase := []byte("passphrase") + for _, entity := range kring { + if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { + err := entity.PrivateKey.Decrypt(passphrase) + if err != nil { + t.Errorf("#%d: failed to decrypt key", i) + } + } + for _, subkey := range entity.Subkeys { + if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { + err := subkey.PrivateKey.Decrypt(passphrase) + if err != nil { + t.Errorf("#%d: failed to decrypt subkey", i) + } + } + } + } - const message = "testing" - _, err = w.Write([]byte(message)) - if err != nil { - t.Errorf("error writing plaintext: %s", err) - return - } - err = w.Close() - if err != nil { - t.Errorf("error closing WriteCloser: %s", err) - return - } + var signed *Entity + if test.isSigned { + signed = kring[0] + } - md, err := ReadMessage(buf, kring, nil /* no prompt */ ) - if err != nil { - t.Errorf("error reading message: %s", err) - return - } + buf := new(bytes.Buffer) + w, err := Encrypt(buf, kring[:1], signed, nil /* no hints */ ) + if err != nil { + t.Errorf("#%d: error in Encrypt: %s", i, err) + continue + } - if isSigned { - expectedKeyId := kring[0].signingKey().PublicKey.KeyId - if md.SignedByKeyId != expectedKeyId { - t.Errorf("message signed by wrong key id, got: %d, want: %d", *md.SignedBy, expectedKeyId) + const message = "testing" + _, err = w.Write([]byte(message)) + if err != nil { + t.Errorf("#%d: error writing plaintext: %s", i, err) + continue } - if md.SignedBy == nil { - t.Errorf("failed to find the signing Entity") + err = w.Close() + if err != nil { + t.Errorf("#%d: error closing WriteCloser: %s", i, err) + continue } - } - - plaintext, err := ioutil.ReadAll(md.UnverifiedBody) - if err != nil { - t.Errorf("error reading encrypted contents: %s", err) - return - } - expectedKeyId := kring[0].encryptionKey().PublicKey.KeyId - if len(md.EncryptedToKeyIds) != 1 || md.EncryptedToKeyIds[0] != expectedKeyId { - t.Errorf("expected message to be encrypted to %v, but got %#v", expectedKeyId, md.EncryptedToKeyIds) - } + md, err := ReadMessage(buf, kring, nil /* no prompt */ ) + if err != nil { + t.Errorf("#%d: error reading message: %s", i, err) + continue + } - if string(plaintext) != message { - t.Errorf("got: %s, want: %s", string(plaintext), message) - } + if test.isSigned { + expectedKeyId := kring[0].signingKey().PublicKey.KeyId + if md.SignedByKeyId != expectedKeyId { + t.Errorf("#%d: message signed by wrong key id, got: %d, want: %d", i, *md.SignedBy, expectedKeyId) + } + if md.SignedBy == nil { + t.Errorf("#%d: failed to find the signing Entity", i) + } + } - if isSigned { - if md.SignatureError != nil { - t.Errorf("signature error: %s", err) + plaintext, err := ioutil.ReadAll(md.UnverifiedBody) + if err != nil { + t.Errorf("#%d: error reading encrypted contents: %s", i, err) + continue } - if md.Signature == nil { - t.Error("signature missing") + + expectedKeyId := kring[0].encryptionKey().PublicKey.KeyId + if len(md.EncryptedToKeyIds) != 1 || md.EncryptedToKeyIds[0] != expectedKeyId { + t.Errorf("#%d: expected message to be encrypted to %v, but got %#v", i, expectedKeyId, md.EncryptedToKeyIds) } - } -} -func TestEncryption(t *testing.T) { - testEncryption(t, false /* not signed */ ) -} + if string(plaintext) != message { + t.Errorf("#%d: got: %s, want: %s", i, string(plaintext), message) + } -func TestEncryptAndSign(t *testing.T) { - testEncryption(t, true /* signed */ ) + if test.isSigned { + if md.SignatureError != nil { + t.Errorf("#%d: signature error: %s", i, err) + } + if md.Signature == nil { + t.Error("signature missing") + } + } + } } -- cgit v1.3