aez_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // aez_test.go - AEZ tests.
  2. //
  3. // To the extent possible under law, Yawning Angel has waived all copyright
  4. // and related or neighboring rights to aez, 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 aez
  8. import (
  9. "bytes"
  10. "crypto/cipher"
  11. "crypto/rand"
  12. "encoding/hex"
  13. "encoding/json"
  14. "fmt"
  15. "os"
  16. "path/filepath"
  17. "testing"
  18. )
  19. func readJsonTestdata(t *testing.T, name string, destination interface{}) {
  20. var file *os.File
  21. file, err := os.Open(filepath.Join("testdata", name))
  22. if err != nil {
  23. t.Fatalf("Failed to read test vectors in %s", name)
  24. }
  25. decoder := json.NewDecoder(file)
  26. if err := decoder.Decode(&destination); err != nil {
  27. t.Fatalf("Failed to parse test vectors in %s", name)
  28. }
  29. }
  30. // (a,b) ==> Extract(a) = b.
  31. type ExtractVector struct {
  32. A string `json:"a"`
  33. B string `json:"b"`
  34. }
  35. func TestExtract(t *testing.T) {
  36. var extractVectors []ExtractVector
  37. readJsonTestdata(t, "extract.json", &extractVectors)
  38. for i, vec := range extractVectors {
  39. var extractedKey [extractedKeySize]byte
  40. vecA, err := hex.DecodeString(vec.A)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. vecB, err := hex.DecodeString(vec.B)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. extract(vecA, &extractedKey)
  49. assertEqual(t, i, vecB, extractedKey[:])
  50. }
  51. }
  52. // {K, tau, [N, A...], V) ==> AEZ-hash{K, {[tau]_128, N, A...)) = V
  53. type HashVector struct {
  54. K string `json:"k"`
  55. Tau int `json:"tau"`
  56. Data []string `json:"data"`
  57. V string `json:"v"`
  58. }
  59. func TestHash(t *testing.T) {
  60. var e eState
  61. var hashVectors []HashVector
  62. readJsonTestdata(t, "hash.json", &hashVectors)
  63. for i, vec := range hashVectors {
  64. vecK, err := hex.DecodeString(vec.K)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. var data [][]byte
  69. for _, v := range vec.Data {
  70. d, err := hex.DecodeString(v)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. data = append(data, d)
  75. }
  76. vecV, err := hex.DecodeString(vec.V)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. var nonce []byte
  81. var ad [][]byte
  82. if len(data) > 0 {
  83. nonce = data[0]
  84. if len(data) > 1 {
  85. ad = data[1:]
  86. }
  87. }
  88. var result [blockSize]byte
  89. e.init(vecK)
  90. e.aezHash(nonce, ad, vec.Tau, result[:])
  91. assertEqual(t, i, vecV, result[:])
  92. }
  93. }
  94. // (K, delta, tau, R) ==> AEZ-prf(K, T, tau*8) = R where delta = AEZ-hash(K,T)
  95. type PrfVector struct {
  96. K string `json:"k"`
  97. Delta string `json:"delta"`
  98. Tau int `json:"tau"`
  99. R string `json:"R"`
  100. }
  101. func TestPRF(t *testing.T) {
  102. var e eState
  103. var prfVectors []PrfVector
  104. readJsonTestdata(t, "prf.json", &prfVectors)
  105. for i, vec := range prfVectors {
  106. vecK, err := hex.DecodeString(vec.K)
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. vecDelta, err := hex.DecodeString(vec.Delta)
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. vecR, err := hex.DecodeString(vec.R)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. var vDelta [blockSize]byte
  119. copy(vDelta[:], vecDelta)
  120. result := make([]byte, len(vecR))
  121. e.init(vecK)
  122. e.aezPRF(&vDelta, vec.Tau, result)
  123. assertEqual(t, i, vecR, result)
  124. }
  125. }
  126. // (K, N, A, taubytes, M, C) ==> Encrypt(K,N,A,taubytes,M) = C
  127. type EncryptVector struct {
  128. K string `json:"k"`
  129. Nonce string `json:"nonce"`
  130. Data []string `json:"data"`
  131. Tau int `json:"tau"`
  132. M string `json:"m"`
  133. C string `json:"c"`
  134. }
  135. func TestEncryptDecrypt(t *testing.T) {
  136. var encryptVectors []EncryptVector
  137. readJsonTestdata(t, "encrypt.json", &encryptVectors)
  138. assertEncrypt(t, encryptVectors)
  139. //
  140. // No AD test cases
  141. //
  142. readJsonTestdata(t, "encrypt_no_ad.json", &encryptVectors)
  143. assertEncrypt(t, encryptVectors)
  144. //
  145. // 33 bytes of AD test cases
  146. //
  147. readJsonTestdata(t, "encrypt_33_byte_ad.json", &encryptVectors)
  148. assertEncrypt(t, encryptVectors)
  149. //
  150. // 16 byte key test cases
  151. //
  152. readJsonTestdata(t, "encrypt_16_byte_key.json", &encryptVectors)
  153. assertEncrypt(t, encryptVectors)
  154. }
  155. func assertEncrypt(t *testing.T, vectors []EncryptVector) {
  156. var e eState
  157. for i, vec := range vectors {
  158. vecK, err := hex.DecodeString(vec.K)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. vecNonce, err := hex.DecodeString(vec.Nonce)
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. var vecData [][]byte
  167. for _, s := range vec.Data {
  168. d, err := hex.DecodeString(s)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. vecData = append(vecData, d)
  173. }
  174. vecM, err := hex.DecodeString(vec.M)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. vecC, err := hex.DecodeString(vec.C)
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. // Test the cipher.AEAD code as well, for applicable test vectors.
  183. var aead cipher.AEAD
  184. var ad []byte
  185. if len(vecNonce) == aeadNonceSize && vec.Tau == aeadOverhead && len(vecData) <= 1 {
  186. aead, err = New(vecK)
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. if len(vecData) == 1 {
  191. ad = vecData[0]
  192. }
  193. }
  194. e.init(vecK)
  195. c := Encrypt(vecK, vecNonce, vecData, vec.Tau, vecM, nil)
  196. assertEqual(t, i, vecC, c)
  197. if aead != nil {
  198. ac := aead.Seal(nil, vecNonce, vecM, ad)
  199. assertEqual(t, i, vecC, ac)
  200. }
  201. m, ok := Decrypt(vecK, vecNonce, vecData, vec.Tau, vecC, nil)
  202. if !ok {
  203. t.Fatalf("decrypt failed: [%d]", i)
  204. }
  205. assertEqual(t, i, vecM, m)
  206. if aead != nil {
  207. am, err := aead.Open(nil, vecNonce, vecC, ad)
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. assertEqual(t, i, vecM, am)
  212. }
  213. }
  214. }
  215. func assertEqual(t *testing.T, idx int, expected, actual []byte) {
  216. if !bytes.Equal(expected, actual) {
  217. for i, v := range actual {
  218. if expected[i] != v {
  219. t.Errorf("[%d] first mismatch at offset: %d (%02x != %02x)", idx, i, expected[i], v)
  220. break
  221. }
  222. }
  223. t.Errorf("expected: %s", hex.Dump(expected))
  224. t.Errorf("actual: %s", hex.Dump(actual))
  225. t.FailNow()
  226. }
  227. }
  228. var benchOutput []byte
  229. func doBenchEncrypt(b *testing.B, n int) {
  230. var key [extractedKeySize]byte
  231. if _, err := rand.Read(key[:]); err != nil {
  232. b.Error(err)
  233. b.Fail()
  234. }
  235. const tau = 16
  236. var nonce [16]byte
  237. src := make([]byte, n)
  238. dst := make([]byte, n+tau)
  239. check := make([]byte, n+tau)
  240. b.SetBytes(int64(n))
  241. b.ResetTimer()
  242. for i := 0; i < b.N; i++ {
  243. b.StartTimer()
  244. dst = Encrypt(key[:], nonce[:], nil, tau, src[:n], dst[:0])
  245. b.StopTimer()
  246. dec, ok := Decrypt(key[:], nonce[:], nil, tau, dst, check[:0])
  247. if !ok {
  248. b.Fatalf("decrypt failed")
  249. }
  250. if !bytes.Equal(dec, src) {
  251. b.Fatalf("decrypt produced invalid output")
  252. }
  253. copy(src, dst[:n])
  254. }
  255. benchOutput = src
  256. }
  257. func BenchmarkEncrypt(b *testing.B) {
  258. sizes := []int{1, 32, 512, 1024, 2048, 16384, 32768, 65536, 1024768}
  259. if testing.Short() {
  260. sizes = []int{1, 32, 512, 1024, 16384, 65536, 1024768}
  261. }
  262. b.SetParallelism(1) // AES-NI is a per-physical core thing.
  263. for _, sz := range sizes {
  264. n := fmt.Sprintf("%d", sz)
  265. b.Run(n, func(b *testing.B) { doBenchEncrypt(b, sz) })
  266. }
  267. }