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.

139 lines
4.5 KiB

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