github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/crypto/blake2b/blake2b_f_fuzz.go (about)

     1  // Copyright 2019 The ebakus/go-ebakus Authors
     2  // This file is part of the ebakus/go-ebakus library.
     3  //
     4  // The ebakus/go-ebakus library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The ebakus/go-ebakus library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // +build gofuzz
    18  
    19  package blake2b
    20  
    21  import (
    22  	"encoding/binary"
    23  )
    24  
    25  func Fuzz(data []byte) int {
    26  	// Make sure the data confirms to the input model
    27  	if len(data) != 211 {
    28  		return 0
    29  	}
    30  	// Parse everything and call all the implementations
    31  	var (
    32  		rounds = binary.BigEndian.Uint16(data[0:2])
    33  
    34  		h [8]uint64
    35  		m [16]uint64
    36  		t [2]uint64
    37  		f uint64
    38  	)
    39  	for i := 0; i < 8; i++ {
    40  		offset := 2 + i*8
    41  		h[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
    42  	}
    43  	for i := 0; i < 16; i++ {
    44  		offset := 66 + i*8
    45  		m[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
    46  	}
    47  	t[0] = binary.LittleEndian.Uint64(data[194:202])
    48  	t[1] = binary.LittleEndian.Uint64(data[202:210])
    49  
    50  	if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1
    51  		f = 0xFFFFFFFFFFFFFFFF
    52  	}
    53  	// Run the blake2b compression on all instruction sets and cross reference
    54  	want := h
    55  	fGeneric(&want, &m, t[0], t[1], f, uint64(rounds))
    56  
    57  	have := h
    58  	fSSE4(&have, &m, t[0], t[1], f, uint64(rounds))
    59  	if have != want {
    60  		panic("SSE4 mismatches generic algo")
    61  	}
    62  	have = h
    63  	fAVX(&have, &m, t[0], t[1], f, uint64(rounds))
    64  	if have != want {
    65  		panic("AVX mismatches generic algo")
    66  	}
    67  	have = h
    68  	fAVX2(&have, &m, t[0], t[1], f, uint64(rounds))
    69  	if have != want {
    70  		panic("AVX2 mismatches generic algo")
    71  	}
    72  	return 1
    73  }