salsa20_amd64_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2019 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. // +build amd64,!appengine,!gccgo
  5. package salsa
  6. import (
  7. "bytes"
  8. "testing"
  9. )
  10. func TestCounterOverflow(t *testing.T) {
  11. in := make([]byte, 4096)
  12. key := &[32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
  13. 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}
  14. for n, counter := range []*[16]byte{
  15. &[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0}, // zero counter
  16. &[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff}, // counter about to overflow 32 bits
  17. &[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 0xff, 0xff, 0xff, 0xff}, // counter above 32 bits
  18. } {
  19. out := make([]byte, 4096)
  20. XORKeyStream(out, in, counter, key)
  21. outGeneric := make([]byte, 4096)
  22. genericXORKeyStream(outGeneric, in, counter, key)
  23. if !bytes.Equal(out, outGeneric) {
  24. t.Errorf("%d: assembly and go implementations disagree", n)
  25. }
  26. }
  27. }