symmetric_key_encrypted_test.go 3.0 KB

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