lioness_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // lioness.go - A LIONESS-BLAKE2b-ChaCha20 implementation.
  2. //
  3. // To the extent possible under law, Yawning Angel has waived all copyright
  4. // and related or neighboring rights to lioness, 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 lioness
  8. import (
  9. "bytes"
  10. "crypto/rand"
  11. "testing"
  12. )
  13. func TestLionessBasic(t *testing.T) {
  14. var key [KeySize]byte
  15. var iv [IVSize]byte
  16. var src, dst, tmp [1024]byte
  17. if _, err := rand.Read(key[:]); err != nil {
  18. t.Error(err)
  19. t.Fail()
  20. }
  21. if _, err := rand.Read(iv[:]); err != nil {
  22. t.Error(err)
  23. t.Fail()
  24. }
  25. if _, err := rand.Read(src[:]); err != nil {
  26. t.Error(err)
  27. t.Fail()
  28. }
  29. if err := Encrypt(key, iv, dst[:], src[:]); err != nil {
  30. t.Error(err)
  31. t.Fail()
  32. }
  33. if bytes.Equal(src[:], dst[:]) {
  34. t.Error("src/dest match")
  35. t.Fail()
  36. }
  37. if err := Decrypt(key, iv, tmp[:], dst[:]); err != nil {
  38. t.Error(err)
  39. t.Fail()
  40. }
  41. if !bytes.Equal(src[:], tmp[:]) {
  42. t.Error("src/tmp mismatch")
  43. t.Fail()
  44. }
  45. // TODO: Test that each subnonce actually changes things.
  46. // TODO: Test that each subkey actually changes things.
  47. }
  48. var benchOutput []byte
  49. func doBenchEncrypt(b *testing.B, n int) {
  50. var key [KeySize]byte
  51. var iv [IVSize]byte
  52. if _, err := rand.Read(key[:]); err != nil {
  53. b.Error(err)
  54. b.Fail()
  55. }
  56. if _, err := rand.Read(iv[:]); err != nil {
  57. b.Error(err)
  58. b.Fail()
  59. }
  60. src := make([]byte, n)
  61. dst := make([]byte, n)
  62. b.SetBytes(int64(n))
  63. b.ResetTimer()
  64. for i := 0; i < b.N; i++ {
  65. if err := Encrypt(key, iv, dst, src); err != nil {
  66. b.Error(err)
  67. b.Fatal()
  68. }
  69. }
  70. benchOutput = dst
  71. }
  72. func BenchmarkLionessEncrypt_33(b *testing.B) {
  73. doBenchEncrypt(b, 33)
  74. }
  75. func BenchmarkLionessEncrypt_512(b *testing.B) {
  76. doBenchEncrypt(b, 512)
  77. }
  78. func BenchmarkLionessEncrypt_1024(b *testing.B) {
  79. doBenchEncrypt(b, 1024)
  80. }
  81. func BenchmarkLionessEncrypt_4096(b *testing.B) {
  82. doBenchEncrypt(b, 4096)
  83. }
  84. func BenchmarkLionessEncrypt_16384(b *testing.B) {
  85. doBenchEncrypt(b, 16384)
  86. }
  87. func BenchmarkLionessEncrypt_32768(b *testing.B) {
  88. doBenchEncrypt(b, 32768)
  89. }
  90. func BenchmarkLionessEncrypt_65536(b *testing.B) {
  91. doBenchEncrypt(b, 65536)
  92. }
  93. func BenchmarkLionessEncrypt_1024768(b *testing.B) {
  94. doBenchEncrypt(b, 1024768)
  95. }