blake2b_f_fuzz.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // +build gofuzz
  2. package blake2b
  3. import (
  4. "encoding/binary"
  5. )
  6. func Fuzz(data []byte) int {
  7. // Make sure the data confirms to the input model
  8. if len(data) != 211 {
  9. return 0
  10. }
  11. // Parse everything and call all the implementations
  12. var (
  13. rounds = binary.BigEndian.Uint16(data[0:2])
  14. h [8]uint64
  15. m [16]uint64
  16. t [2]uint64
  17. f uint64
  18. )
  19. for i := 0; i < 8; i++ {
  20. offset := 2 + i*8
  21. h[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
  22. }
  23. for i := 0; i < 16; i++ {
  24. offset := 66 + i*8
  25. m[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
  26. }
  27. t[0] = binary.LittleEndian.Uint64(data[194:202])
  28. t[1] = binary.LittleEndian.Uint64(data[202:210])
  29. if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1
  30. f = 0xFFFFFFFFFFFFFFFF
  31. }
  32. // Run the blake2b compression on all instruction sets and cross reference
  33. want := h
  34. fGeneric(&want, &m, t[0], t[1], f, uint64(rounds))
  35. have := h
  36. fSSE4(&have, &m, t[0], t[1], f, uint64(rounds))
  37. if have != want {
  38. panic("SSE4 mismatches generic algo")
  39. }
  40. have = h
  41. fAVX(&have, &m, t[0], t[1], f, uint64(rounds))
  42. if have != want {
  43. panic("AVX mismatches generic algo")
  44. }
  45. have = h
  46. fAVX2(&have, &m, t[0], t[1], f, uint64(rounds))
  47. if have != want {
  48. panic("AVX2 mismatches generic algo")
  49. }
  50. return 1
  51. }