symmetrically_encrypted_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "crypto/sha1"
  8. "encoding/hex"
  9. "golang.org/x/crypto/openpgp/errors"
  10. "io"
  11. "io/ioutil"
  12. "testing"
  13. )
  14. // TestReader wraps a []byte and returns reads of a specific length.
  15. type testReader struct {
  16. data []byte
  17. stride int
  18. }
  19. func (t *testReader) Read(buf []byte) (n int, err error) {
  20. n = t.stride
  21. if n > len(t.data) {
  22. n = len(t.data)
  23. }
  24. if n > len(buf) {
  25. n = len(buf)
  26. }
  27. copy(buf, t.data)
  28. t.data = t.data[n:]
  29. if len(t.data) == 0 {
  30. err = io.EOF
  31. }
  32. return
  33. }
  34. func testMDCReader(t *testing.T) {
  35. mdcPlaintext, _ := hex.DecodeString(mdcPlaintextHex)
  36. for stride := 1; stride < len(mdcPlaintext)/2; stride++ {
  37. r := &testReader{data: mdcPlaintext, stride: stride}
  38. mdcReader := &seMDCReader{in: r, h: sha1.New()}
  39. body, err := ioutil.ReadAll(mdcReader)
  40. if err != nil {
  41. t.Errorf("stride: %d, error: %s", stride, err)
  42. continue
  43. }
  44. if !bytes.Equal(body, mdcPlaintext[:len(mdcPlaintext)-22]) {
  45. t.Errorf("stride: %d: bad contents %x", stride, body)
  46. continue
  47. }
  48. err = mdcReader.Close()
  49. if err != nil {
  50. t.Errorf("stride: %d, error on Close: %s", stride, err)
  51. }
  52. }
  53. mdcPlaintext[15] ^= 80
  54. r := &testReader{data: mdcPlaintext, stride: 2}
  55. mdcReader := &seMDCReader{in: r, h: sha1.New()}
  56. _, err := ioutil.ReadAll(mdcReader)
  57. if err != nil {
  58. t.Errorf("corruption test, error: %s", err)
  59. return
  60. }
  61. err = mdcReader.Close()
  62. if err == nil {
  63. t.Error("corruption: no error")
  64. } else if _, ok := err.(*errors.SignatureError); !ok {
  65. t.Errorf("corruption: expected SignatureError, got: %s", err)
  66. }
  67. }
  68. const mdcPlaintextHex = "a302789c3b2d93c4e0eb9aba22283539b3203335af44a134afb800c849cb4c4de10200aff40b45d31432c80cb384299a0655966d6939dfdeed1dddf980"
  69. func TestSerialize(t *testing.T) {
  70. buf := bytes.NewBuffer(nil)
  71. c := CipherAES128
  72. key := make([]byte, c.KeySize())
  73. w, err := SerializeSymmetricallyEncrypted(buf, c, key, nil)
  74. if err != nil {
  75. t.Errorf("error from SerializeSymmetricallyEncrypted: %s", err)
  76. return
  77. }
  78. contents := []byte("hello world\n")
  79. w.Write(contents)
  80. w.Close()
  81. p, err := Read(buf)
  82. if err != nil {
  83. t.Errorf("error from Read: %s", err)
  84. return
  85. }
  86. se, ok := p.(*SymmetricallyEncrypted)
  87. if !ok {
  88. t.Errorf("didn't read a *SymmetricallyEncrypted")
  89. return
  90. }
  91. r, err := se.Decrypt(c, key)
  92. if err != nil {
  93. t.Errorf("error from Decrypt: %s", err)
  94. return
  95. }
  96. contentsCopy := bytes.NewBuffer(nil)
  97. _, err = io.Copy(contentsCopy, r)
  98. if err != nil {
  99. t.Errorf("error from io.Copy: %s", err)
  100. return
  101. }
  102. if !bytes.Equal(contentsCopy.Bytes(), contents) {
  103. t.Errorf("contents not equal got: %x want: %x", contentsCopy.Bytes(), contents)
  104. }
  105. }