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.

101 lines
3.5 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. cr.Read(key[:])
  24. cr.Read(nonce[:])
  25. cr.Read(ad)
  26. cr.Read(plaintext)
  27. aead, err := New(key[:])
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. ct := aead.Seal(nil, nonce[:], plaintext, ad)
  32. plaintext2, err := aead.Open(nil, nonce[:], ct, ad)
  33. if err != nil {
  34. t.Errorf("Random #%d: Open failed", i)
  35. continue
  36. }
  37. if !bytes.Equal(plaintext, plaintext2) {
  38. t.Errorf("Random #%d: plaintext's don't match: got %x vs %x", i, plaintext2, plaintext)
  39. continue
  40. }
  41. if len(ad) > 0 {
  42. alterAdIdx := mr.Intn(len(ad))
  43. ad[alterAdIdx] ^= 0x80
  44. if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
  45. t.Errorf("Random #%d: Open was successful after altering additional data", i)
  46. }
  47. ad[alterAdIdx] ^= 0x80
  48. }
  49. alterNonceIdx := mr.Intn(aead.NonceSize())
  50. nonce[alterNonceIdx] ^= 0x80
  51. if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
  52. t.Errorf("Random #%d: Open was successful after altering nonce", i)
  53. }
  54. nonce[alterNonceIdx] ^= 0x80
  55. alterCtIdx := mr.Intn(len(ct))
  56. ct[alterCtIdx] ^= 0x80
  57. if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
  58. t.Errorf("Random #%d: Open was successful after altering ciphertext", i)
  59. }
  60. ct[alterCtIdx] ^= 0x80
  61. }
  62. }
  63. // AFOREMENTIONED LICENCE
  64. // Copyright (c) 2009 The Go Authors. All rights reserved.
  65. //
  66. // Redistribution and use in source and binary forms, with or without
  67. // modification, are permitted provided that the following conditions are
  68. // met:
  69. //
  70. // * Redistributions of source code must retain the above copyright
  71. // notice, this list of conditions and the following disclaimer.
  72. // * Redistributions in binary form must reproduce the above
  73. // copyright notice, this list of conditions and the following disclaimer
  74. // in the documentation and/or other materials provided with the
  75. // distribution.
  76. // * Neither the name of Google Inc. nor the names of its
  77. // contributors may be used to endorse or promote products derived from
  78. // this software without specific prior written permission.
  79. //
  80. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  81. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  82. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  83. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  84. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  85. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  86. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  87. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  88. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  89. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  90. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.