You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.7 KiB

  1. package xchacha20poly1305
  2. import (
  3. "bytes"
  4. cr "crypto/rand"
  5. mr "math/rand"
  6. "testing"
  7. )
  8. // The following test is taken from
  9. // https://github.com/golang/crypto/blob/master/chacha20poly1305/chacha20poly1305_test.go#L69
  10. // It requires the below copyright notice, where "this source code" refers to the following function.
  11. // Copyright 2016 The Go Authors. All rights reserved.
  12. // Use of this source code is governed by a BSD-style
  13. // license that can be found at the bottom of this file.
  14. func TestRandom(t *testing.T) {
  15. // Some random tests to verify Open(Seal) == Plaintext
  16. for i := 0; i < 256; i++ {
  17. var nonce [24]byte
  18. var key [32]byte
  19. al := mr.Intn(128)
  20. pl := mr.Intn(16384)
  21. ad := make([]byte, al)
  22. plaintext := make([]byte, pl)
  23. _, err := cr.Read(key[:])
  24. if err != nil {
  25. t.Errorf("error on read: %w", err)
  26. }
  27. _, err = cr.Read(nonce[:])
  28. if err != nil {
  29. t.Errorf("error on read: %w", err)
  30. }
  31. _, err = cr.Read(ad)
  32. if err != nil {
  33. t.Errorf("error on read: %w", err)
  34. }
  35. _, err = cr.Read(plaintext)
  36. if err != nil {
  37. t.Errorf("error on read: %w", err)
  38. }
  39. aead, err := New(key[:])
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. ct := aead.Seal(nil, nonce[:], plaintext, ad)
  44. plaintext2, err := aead.Open(nil, nonce[:], ct, ad)
  45. if err != nil {
  46. t.Errorf("random #%d: Open failed", i)
  47. continue
  48. }
  49. if !bytes.Equal(plaintext, plaintext2) {
  50. t.Errorf("random #%d: plaintext's don't match: got %x vs %x", i, plaintext2, plaintext)
  51. continue
  52. }
  53. if len(ad) > 0 {
  54. alterAdIdx := mr.Intn(len(ad))
  55. ad[alterAdIdx] ^= 0x80
  56. if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
  57. t.Errorf("random #%d: Open was successful after altering additional data", i)
  58. }
  59. ad[alterAdIdx] ^= 0x80
  60. }
  61. alterNonceIdx := mr.Intn(aead.NonceSize())
  62. nonce[alterNonceIdx] ^= 0x80
  63. if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
  64. t.Errorf("random #%d: Open was successful after altering nonce", i)
  65. }
  66. nonce[alterNonceIdx] ^= 0x80
  67. alterCtIdx := mr.Intn(len(ct))
  68. ct[alterCtIdx] ^= 0x80
  69. if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
  70. t.Errorf("random #%d: Open was successful after altering ciphertext", i)
  71. }
  72. ct[alterCtIdx] ^= 0x80
  73. }
  74. }
  75. // AFOREMENTIONED LICENCE
  76. // Copyright (c) 2009 The Go Authors. All rights reserved.
  77. //
  78. // Redistribution and use in source and binary forms, with or without
  79. // modification, are permitted provided that the following conditions are
  80. // met:
  81. //
  82. // * Redistributions of source code must retain the above copyright
  83. // notice, this list of conditions and the following disclaimer.
  84. // * Redistributions in binary form must reproduce the above
  85. // copyright notice, this list of conditions and the following disclaimer
  86. // in the documentation and/or other materials provided with the
  87. // distribution.
  88. // * Neither the name of Google Inc. nor the names of its
  89. // contributors may be used to endorse or promote products derived from
  90. // this software without specific prior written permission.
  91. //
  92. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  93. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  94. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  95. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  96. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  98. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  99. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  100. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  101. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  102. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.