poly1305_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2012 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 poly1305
  5. import (
  6. "bytes"
  7. "testing"
  8. "unsafe"
  9. )
  10. var testData = []struct {
  11. in, k, correct []byte
  12. }{
  13. {
  14. []byte("Hello world!"),
  15. []byte("this is 32-byte key for Poly1305"),
  16. []byte{0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0},
  17. },
  18. {
  19. make([]byte, 32),
  20. []byte("this is 32-byte key for Poly1305"),
  21. []byte{0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07},
  22. },
  23. {
  24. make([]byte, 2007),
  25. []byte("this is 32-byte key for Poly1305"),
  26. []byte{0xda, 0x84, 0xbc, 0xab, 0x02, 0x67, 0x6c, 0x38, 0xcd, 0xb0, 0x15, 0x60, 0x42, 0x74, 0xc2, 0xaa},
  27. },
  28. {
  29. make([]byte, 2007),
  30. make([]byte, 32),
  31. make([]byte, 16),
  32. },
  33. {
  34. // This test triggers an edge-case. See https://go-review.googlesource.com/#/c/30101/.
  35. []byte{0x81, 0xd8, 0xb2, 0xe4, 0x6a, 0x25, 0x21, 0x3b, 0x58, 0xfe, 0xe4, 0x21, 0x3a, 0x2a, 0x28, 0xe9, 0x21, 0xc1, 0x2a, 0x96, 0x32, 0x51, 0x6d, 0x3b, 0x73, 0x27, 0x27, 0x27, 0xbe, 0xcf, 0x21, 0x29},
  36. []byte{0x3b, 0x3a, 0x29, 0xe9, 0x3b, 0x21, 0x3a, 0x5c, 0x5c, 0x3b, 0x3b, 0x05, 0x3a, 0x3a, 0x8c, 0x0d},
  37. []byte{0x6d, 0xc1, 0x8b, 0x8c, 0x34, 0x4c, 0xd7, 0x99, 0x27, 0x11, 0x8b, 0xbe, 0x84, 0xb7, 0xf3, 0x14},
  38. },
  39. {
  40. // This test generates a result of (2^130-1) % (2^130-5).
  41. []byte{
  42. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. },
  46. []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  47. []byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  48. },
  49. {
  50. // This test generates a result of (2^130-6) % (2^130-5).
  51. []byte{
  52. 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. },
  56. []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  57. []byte{0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  58. },
  59. {
  60. // This test generates a result of (2^130-5) % (2^130-5).
  61. []byte{
  62. 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. },
  66. []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  67. []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  68. },
  69. }
  70. func testSum(t *testing.T, unaligned bool) {
  71. var out [16]byte
  72. var key [32]byte
  73. for i, v := range testData {
  74. in := v.in
  75. if unaligned {
  76. in = unalignBytes(in)
  77. }
  78. copy(key[:], v.k)
  79. Sum(&out, in, &key)
  80. if !bytes.Equal(out[:], v.correct) {
  81. t.Errorf("%d: expected %x, got %x", i, v.correct, out[:])
  82. }
  83. }
  84. }
  85. func TestSum(t *testing.T) { testSum(t, false) }
  86. func TestSumUnaligned(t *testing.T) { testSum(t, true) }
  87. func benchmark(b *testing.B, size int, unaligned bool) {
  88. var out [16]byte
  89. var key [32]byte
  90. in := make([]byte, size)
  91. if unaligned {
  92. in = unalignBytes(in)
  93. }
  94. b.SetBytes(int64(len(in)))
  95. b.ResetTimer()
  96. for i := 0; i < b.N; i++ {
  97. Sum(&out, in, &key)
  98. }
  99. }
  100. func Benchmark64(b *testing.B) { benchmark(b, 64, false) }
  101. func Benchmark1K(b *testing.B) { benchmark(b, 1024, false) }
  102. func Benchmark64Unaligned(b *testing.B) { benchmark(b, 64, true) }
  103. func Benchmark1KUnaligned(b *testing.B) { benchmark(b, 1024, true) }
  104. func unalignBytes(in []byte) []byte {
  105. out := make([]byte, len(in)+1)
  106. if uintptr(unsafe.Pointer(&out[0]))&(unsafe.Alignof(uint32(0))-1) == 0 {
  107. out = out[1:]
  108. } else {
  109. out = out[:len(in)]
  110. }
  111. copy(out, in)
  112. return out
  113. }