...

Source file src/golang.org/x/crypto/openpgp/packet/symmetric_key_encrypted_test.go

Documentation: golang.org/x/crypto/openpgp/packet

     1  // Copyright 2011 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 packet
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"io"
    11  	"testing"
    12  )
    13  
    14  func TestSymmetricKeyEncrypted(t *testing.T) {
    15  	buf := readerFromHex(symmetricallyEncryptedHex)
    16  	packet, err := Read(buf)
    17  	if err != nil {
    18  		t.Errorf("failed to read SymmetricKeyEncrypted: %s", err)
    19  		return
    20  	}
    21  	ske, ok := packet.(*SymmetricKeyEncrypted)
    22  	if !ok {
    23  		t.Error("didn't find SymmetricKeyEncrypted packet")
    24  		return
    25  	}
    26  	key, cipherFunc, err := ske.Decrypt([]byte("password"))
    27  	if err != nil {
    28  		t.Error(err)
    29  		return
    30  	}
    31  
    32  	packet, err = Read(buf)
    33  	if err != nil {
    34  		t.Errorf("failed to read SymmetricallyEncrypted: %s", err)
    35  		return
    36  	}
    37  	se, ok := packet.(*SymmetricallyEncrypted)
    38  	if !ok {
    39  		t.Error("didn't find SymmetricallyEncrypted packet")
    40  		return
    41  	}
    42  	r, err := se.Decrypt(cipherFunc, key)
    43  	if err != nil {
    44  		t.Error(err)
    45  		return
    46  	}
    47  
    48  	contents, err := io.ReadAll(r)
    49  	if err != nil && err != io.EOF {
    50  		t.Error(err)
    51  		return
    52  	}
    53  
    54  	expectedContents, _ := hex.DecodeString(symmetricallyEncryptedContentsHex)
    55  	if !bytes.Equal(expectedContents, contents) {
    56  		t.Errorf("bad contents got:%x want:%x", contents, expectedContents)
    57  	}
    58  }
    59  
    60  const symmetricallyEncryptedHex = "8c0d04030302371a0b38d884f02060c91cf97c9973b8e58e028e9501708ccfe618fb92afef7fa2d80ddadd93cf"
    61  const symmetricallyEncryptedContentsHex = "cb1062004d14c4df636f6e74656e74732e0a"
    62  
    63  func TestSerializeSymmetricKeyEncryptedCiphers(t *testing.T) {
    64  	tests := [...]struct {
    65  		cipherFunc CipherFunction
    66  		name       string
    67  	}{
    68  		{Cipher3DES, "Cipher3DES"},
    69  		{CipherCAST5, "CipherCAST5"},
    70  		{CipherAES128, "CipherAES128"},
    71  		{CipherAES192, "CipherAES192"},
    72  		{CipherAES256, "CipherAES256"},
    73  	}
    74  
    75  	for _, test := range tests {
    76  		var buf bytes.Buffer
    77  		passphrase := []byte("testing")
    78  		config := &Config{
    79  			DefaultCipher: test.cipherFunc,
    80  		}
    81  
    82  		key, err := SerializeSymmetricKeyEncrypted(&buf, passphrase, config)
    83  		if err != nil {
    84  			t.Errorf("cipher(%s) failed to serialize: %s", test.name, err)
    85  			continue
    86  		}
    87  
    88  		p, err := Read(&buf)
    89  		if err != nil {
    90  			t.Errorf("cipher(%s) failed to reparse: %s", test.name, err)
    91  			continue
    92  		}
    93  
    94  		ske, ok := p.(*SymmetricKeyEncrypted)
    95  		if !ok {
    96  			t.Errorf("cipher(%s) parsed a different packet type: %#v", test.name, p)
    97  			continue
    98  		}
    99  
   100  		if ske.CipherFunc != config.DefaultCipher {
   101  			t.Errorf("cipher(%s) SKE cipher function is %d (expected %d)", test.name, ske.CipherFunc, config.DefaultCipher)
   102  		}
   103  		parsedKey, parsedCipherFunc, err := ske.Decrypt(passphrase)
   104  		if err != nil {
   105  			t.Errorf("cipher(%s) failed to decrypt reparsed SKE: %s", test.name, err)
   106  			continue
   107  		}
   108  		if !bytes.Equal(key, parsedKey) {
   109  			t.Errorf("cipher(%s) keys don't match after Decrypt: %x (original) vs %x (parsed)", test.name, key, parsedKey)
   110  		}
   111  		if parsedCipherFunc != test.cipherFunc {
   112  			t.Errorf("cipher(%s) cipher function doesn't match after Decrypt: %d (original) vs %d (parsed)",
   113  				test.name, test.cipherFunc, parsedCipherFunc)
   114  		}
   115  	}
   116  }
   117  

View as plain text