blake2b_f_fuzz.go 1.3 KB

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