norx_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // norx_test.go - NORX tests
  2. //
  3. // To the extent possible under law, Yawning Angel has waived all copyright
  4. // and related or neighboring rights to the software, using the Creative
  5. // Commons "CC0" public domain dedication. See LICENSE or
  6. // <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
  7. package norx
  8. import (
  9. "bytes"
  10. "crypto/rand"
  11. "fmt"
  12. "testing"
  13. "github.com/stretchr/testify/require"
  14. )
  15. var (
  16. katCipherTexts = map[string][]byte{
  17. "NORX-64-4-1": kat6441,
  18. "NORX-64-6-1": kat6461,
  19. }
  20. testVectorCipherTexts = map[string][]byte{
  21. "NORX-64-4-1": vec6441,
  22. "NORX-64-6-1": vec6461,
  23. }
  24. canAccelerate bool
  25. )
  26. func mustInitHardwareAcceleration() {
  27. // initHardwareAcceleration()
  28. if !IsHardwareAccelerated() {
  29. panic("initHardwareAcceleration() failed")
  30. }
  31. }
  32. func TestF(t *testing.T) {
  33. forceDisableHardwareAcceleration()
  34. doTestF(t)
  35. if !canAccelerate {
  36. t.Log("Hardware acceleration not supported on this host.")
  37. return
  38. }
  39. mustInitHardwareAcceleration()
  40. doTestF(t)
  41. }
  42. func doTestF(t *testing.T) {
  43. impl := "_" + hardwareAccelImpl.name
  44. t.Run("F"+impl, func(t *testing.T) {
  45. require := require.New(t)
  46. s := &state{}
  47. for i := range s.s {
  48. s.s[i] = uint64(i)
  49. }
  50. hardwareAccelImpl.permuteFn(s, 2)
  51. require.Equal(initializationConstants, s.s, "pre-generated vs calculated")
  52. })
  53. }
  54. func TestKAT(t *testing.T) {
  55. forceDisableHardwareAcceleration()
  56. doTestKAT(t)
  57. if !canAccelerate {
  58. t.Log("Hardware acceleration not supported on this host.")
  59. return
  60. }
  61. mustInitHardwareAcceleration()
  62. doTestKAT(t)
  63. }
  64. func doTestKAT(t *testing.T) {
  65. testRounds := []int{4, 6}
  66. impl := "_" + hardwareAccelImpl.name
  67. for _, l := range testRounds {
  68. n := fmt.Sprintf("NORX-64-%d-1", l)
  69. t.Run(n+"_Vector"+impl, func(t *testing.T) { doTestVector(t, n, l) })
  70. t.Run(n+"_KAT"+impl, func(t *testing.T) { doTestKATFull(t, n, l) })
  71. }
  72. }
  73. func doTestVector(t *testing.T, vn string, l int) {
  74. require := require.New(t)
  75. var k, n [32]byte
  76. var a, m, z [128]byte
  77. for i := range k {
  78. k[i] = byte(i)
  79. n[i] = byte(i + 32)
  80. }
  81. for i := range a {
  82. a[i] = byte(i)
  83. m[i] = byte(i)
  84. z[i] = byte(i)
  85. }
  86. ctVec := testVectorCipherTexts[vn]
  87. if ctVec == nil {
  88. panic("missing test vector cipher text")
  89. }
  90. ct := aeadEncrypt(l, nil, a[:], m[:], z[:], n[:], k[:])
  91. require.Equal(ctVec, ct, "aeadEncrypt()")
  92. pt, ok := aeadDecrypt(l, nil, a[:], ct, z[:], n[:], k[:])
  93. require.True(ok, "aeadDecrypt(): ok")
  94. require.Equal(m[:], pt, "aeadDecrupt()")
  95. }
  96. func doTestKATFull(t *testing.T, vn string, l int) {
  97. require := require.New(t)
  98. var w, h [256]byte
  99. var k, n [32]byte
  100. for i := range w {
  101. w[i] = byte(255 & (i*197 + 123))
  102. }
  103. for i := range h {
  104. h[i] = byte(255 & (i*193 + 123))
  105. }
  106. for i := range k {
  107. k[i] = byte(255 & (i*191 + 123))
  108. }
  109. for i := range n {
  110. n[i] = byte(255 & (i*181 + 123))
  111. }
  112. var katAcc []byte
  113. kat := katCipherTexts[vn]
  114. katOff := 0
  115. if kat == nil {
  116. panic("missing KAT cipher texts")
  117. }
  118. for i := range w {
  119. katAcc = aeadEncrypt(l, katAcc, h[:i], w[:i], nil, n[:], k[:])
  120. c := katAcc[katOff:]
  121. require.Len(c, i+TagSize, "aeadEncrypt(): len(c) %d", i)
  122. require.Equal(kat[katOff:katOff+len(c)], c)
  123. m, ok := aeadDecrypt(l, nil, h[:i], c, nil, n[:], k[:])
  124. require.True(ok, "aeadDecrypt(): ok %d", i)
  125. require.Len(m, i, "aeadDecrypt(): len(m) %d", i)
  126. if len(m) != 0 {
  127. require.Equal(m, w[:i], "aeadDecrupt(): m %d", i)
  128. }
  129. katOff += len(c)
  130. }
  131. require.Equal(kat, katAcc, "Final concatenated cipher texts.")
  132. }
  133. func BenchmarkNORX(b *testing.B) {
  134. forceDisableHardwareAcceleration()
  135. doBenchmarkNORX(b)
  136. if !canAccelerate {
  137. b.Log("Hardware acceleration not supported on this host.")
  138. return
  139. }
  140. mustInitHardwareAcceleration()
  141. doBenchmarkNORX(b)
  142. }
  143. func doBenchmarkNORX(b *testing.B) {
  144. benchRounds := []int{4, 6}
  145. benchSizes := []int{8, 64, 576, 1536, 4096, 1024768}
  146. impl := "_" + hardwareAccelImpl.name
  147. for _, l := range benchRounds {
  148. n := fmt.Sprintf("NORX-64-%d-1", l)
  149. for _, sz := range benchSizes {
  150. bn := n + impl + "_"
  151. sn := fmt.Sprintf("_%d", sz)
  152. b.Run(bn+"Encrypt"+sn, func(b *testing.B) { doBenchmarkAEAD(b, l, sz, true) })
  153. b.Run(bn+"Decrypt"+sn, func(b *testing.B) { doBenchmarkAEAD(b, l, sz, false) })
  154. }
  155. }
  156. }
  157. func doBenchmarkAEAD(b *testing.B, l, sz int, isEncrypt bool) {
  158. b.StopTimer()
  159. b.SetBytes(int64(sz))
  160. nonce, key := make([]byte, NonceSize), make([]byte, KeySize)
  161. m, c, d := make([]byte, sz), make([]byte, 0, sz+TagSize), make([]byte, 0, sz)
  162. rand.Read(nonce)
  163. rand.Read(key)
  164. rand.Read(m)
  165. for i := 0; i < b.N; i++ {
  166. c = c[:0]
  167. d = d[:0]
  168. if isEncrypt {
  169. b.StartTimer()
  170. }
  171. c = aeadEncrypt(l, c, nil, m, nil, nonce, key)
  172. if isEncrypt {
  173. b.StopTimer()
  174. } else {
  175. b.StartTimer()
  176. }
  177. var ok bool
  178. d, ok = aeadDecrypt(l, d, nil, c, nil, nonce, key)
  179. if !isEncrypt {
  180. b.StopTimer()
  181. }
  182. if !ok {
  183. b.Fatalf("aeadDecrypt failed")
  184. }
  185. if !bytes.Equal(m, d) {
  186. b.Fatalf("aeadDecrypt output mismatch")
  187. }
  188. }
  189. }
  190. func init() {
  191. canAccelerate = IsHardwareAccelerated()
  192. }