|
1 | 1 | package blake2b |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/binary" |
4 | 5 | "fmt" |
5 | 6 | "reflect" |
6 | 7 | "testing" |
@@ -57,3 +58,60 @@ var testVectorsF = []testVector{ |
57 | 58 | }, |
58 | 59 | }, |
59 | 60 | } |
| 61 | + |
| 62 | +func Fuzz(f *testing.F) { |
| 63 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 64 | + fuzz(data) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func fuzz(data []byte) { |
| 69 | + // Make sure the data confirms to the input model |
| 70 | + if len(data) != 211 { |
| 71 | + return |
| 72 | + } |
| 73 | + // Parse everything and call all the implementations |
| 74 | + var ( |
| 75 | + rounds = binary.BigEndian.Uint16(data[0:2]) |
| 76 | + |
| 77 | + h [8]uint64 |
| 78 | + m [16]uint64 |
| 79 | + t [2]uint64 |
| 80 | + f uint64 |
| 81 | + ) |
| 82 | + |
| 83 | + for i := 0; i < 8; i++ { |
| 84 | + offset := 2 + i*8 |
| 85 | + h[i] = binary.LittleEndian.Uint64(data[offset : offset+8]) |
| 86 | + } |
| 87 | + for i := 0; i < 16; i++ { |
| 88 | + offset := 66 + i*8 |
| 89 | + m[i] = binary.LittleEndian.Uint64(data[offset : offset+8]) |
| 90 | + } |
| 91 | + t[0] = binary.LittleEndian.Uint64(data[194:202]) |
| 92 | + t[1] = binary.LittleEndian.Uint64(data[202:210]) |
| 93 | + |
| 94 | + if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1 |
| 95 | + f = 0xFFFFFFFFFFFFFFFF |
| 96 | + } |
| 97 | + |
| 98 | + // Run the blake2b compression on all instruction sets and cross reference |
| 99 | + want := h |
| 100 | + fGeneric(&want, &m, t[0], t[1], f, uint64(rounds)) |
| 101 | + |
| 102 | + have := h |
| 103 | + fSSE4(&have, &m, t[0], t[1], f, uint64(rounds)) |
| 104 | + if have != want { |
| 105 | + panic("SSE4 mismatches generic algo") |
| 106 | + } |
| 107 | + have = h |
| 108 | + fAVX(&have, &m, t[0], t[1], f, uint64(rounds)) |
| 109 | + if have != want { |
| 110 | + panic("AVX mismatches generic algo") |
| 111 | + } |
| 112 | + have = h |
| 113 | + fAVX2(&have, &m, t[0], t[1], f, uint64(rounds)) |
| 114 | + if have != want { |
| 115 | + panic("AVX2 mismatches generic algo") |
| 116 | + } |
| 117 | +} |
0 commit comments