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.
15 func TestSymmetricKeyEncrypted(t *testing.T) {
16 buf := readerFromHex(symmetricallyEncryptedHex)
17 packet, err := Read(buf)
19 t.Errorf("failed to read SymmetricKeyEncrypted: %s", err)
22 ske, ok := packet.(*SymmetricKeyEncrypted)
24 t.Error("didn't find SymmetricKeyEncrypted packet")
27 key, cipherFunc, err := ske.Decrypt([]byte("password"))
33 packet, err = Read(buf)
35 t.Errorf("failed to read SymmetricallyEncrypted: %s", err)
38 se, ok := packet.(*SymmetricallyEncrypted)
40 t.Error("didn't find SymmetricallyEncrypted packet")
43 r, err := se.Decrypt(cipherFunc, key)
49 contents, err := ioutil.ReadAll(r)
50 if err != nil && err != io.EOF {
55 expectedContents, _ := hex.DecodeString(symmetricallyEncryptedContentsHex)
56 if !bytes.Equal(expectedContents, contents) {
57 t.Errorf("bad contents got:%x want:%x", contents, expectedContents)
61 const symmetricallyEncryptedHex = "8c0d04030302371a0b38d884f02060c91cf97c9973b8e58e028e9501708ccfe618fb92afef7fa2d80ddadd93cf"
62 const symmetricallyEncryptedContentsHex = "cb1062004d14c4df636f6e74656e74732e0a"
64 func TestSerializeSymmetricKeyEncryptedCiphers(t *testing.T) {
65 tests := [...]struct {
66 cipherFunc CipherFunction
69 {Cipher3DES, "Cipher3DES"},
70 {CipherCAST5, "CipherCAST5"},
71 {CipherAES128, "CipherAES128"},
72 {CipherAES192, "CipherAES192"},
73 {CipherAES256, "CipherAES256"},
76 for _, test := range tests {
78 passphrase := []byte("testing")
80 DefaultCipher: test.cipherFunc,
83 key, err := SerializeSymmetricKeyEncrypted(&buf, passphrase, config)
85 t.Errorf("cipher(%s) failed to serialize: %s", test.name, err)
91 t.Errorf("cipher(%s) failed to reparse: %s", test.name, err)
95 ske, ok := p.(*SymmetricKeyEncrypted)
97 t.Errorf("cipher(%s) parsed a different packet type: %#v", test.name, p)
101 if ske.CipherFunc != config.DefaultCipher {
102 t.Errorf("cipher(%s) SKE cipher function is %d (expected %d)", test.name, ske.CipherFunc, config.DefaultCipher)
104 parsedKey, parsedCipherFunc, err := ske.Decrypt(passphrase)
106 t.Errorf("cipher(%s) failed to decrypt reparsed SKE: %s", test.name, err)
109 if !bytes.Equal(key, parsedKey) {
110 t.Errorf("cipher(%s) keys don't match after Decrypt: %x (original) vs %x (parsed)", test.name, key, parsedKey)
112 if parsedCipherFunc != test.cipherFunc {
113 t.Errorf("cipher(%s) cipher function doesn't match after Decrypt: %d (original) vs %d (parsed)",
114 test.name, test.cipherFunc, parsedCipherFunc)