Пример #1
0
func NewFile(suite abstract.Suite, path string) (*File, error) {
	f, err := os.Open(path)
	if err != nil {
		log.Fatal("Failed opening file", path, err)
	}
	defer f.Close()

	fi, err := f.Stat()
	if err != nil {
		return nil, err
	}
	blocks := (fi.Size() + BlockSize - 1) / BlockSize

	x := &File{
		Name:   path,
		Hashes: make(map[string]int64, blocks),
	}

	for i := 0; int64(i) < blocks; i++ {
		tmp := make([]byte, BlockSize)
		_, err := f.Read(tmp)
		if err != nil {
			log.Fatal("Failed reading file", err)
		}
		h := suite.Hash()
		h.Write(tmp)
		x.Hashes[string(h.Sum(nil))] = int64((i * BlockSize))
	}

	return x, nil
}
Пример #2
0
func verifyMessage(suite abstract.Suite, m interface{}, hash1 []byte) error {

	// Make a copy of the signature
	x := reflect.ValueOf(m).Elem().FieldByName("Sig")
	sig := reflect.New(x.Type()).Elem()
	sig.Set(x)

	// Reset signature field
	reflect.ValueOf(m).Elem().FieldByName("Sig").Set(reflect.ValueOf(crypto.SchnorrSig{})) // XXX: hack

	// Marshal ...
	mb, err := network.MarshalRegisteredType(m)
	if err != nil {
		return err
	}

	// ... and hash message
	hash2, err := crypto.HashBytes(suite.Hash(), mb)
	if err != nil {
		return err
	}

	// Copy back original signature
	reflect.ValueOf(m).Elem().FieldByName("Sig").Set(sig) // XXX: hack

	// Compare hashes
	if !bytes.Equal(hash1, hash2) {
		return errors.New("Message has a different hash than the given one")
	}

	return nil
}
Пример #3
0
// Apply a standard set of validation tests to a ciphersuite.
func TestSuite(suite abstract.Suite) {

	// Try hashing something
	h := suite.Hash()
	l := h.Size()
	//println("HashLen: ",l)
	h.Write([]byte("abc"))
	hb := h.Sum(nil)
	//println("Hash:")
	//println(hex.Dump(hb))
	if h.Size() != l || len(hb) != l {
		panic("inconsistent hash output length")
	}

	// Generate some pseudorandom bits
	s := suite.Cipher(hb)
	sb := make([]byte, 128)
	s.XORKeyStream(sb, sb)
	//println("Stream:")
	//println(hex.Dump(sb))

	// Test if it generates two fresh keys with nil cipher
	s1 := suite.NewKey(nil)
	s2 := suite.NewKey(nil)
	if s1.Equal(s2) {
		panic("NewKey returns twice the same key given nil")
	}

	// Test if it creates the same with the same seed
	st1 := suite.Cipher(hb)
	st2 := suite.Cipher(hb)
	s3 := suite.NewKey(st1)
	s4 := suite.NewKey(st2)
	if !s3.Equal(s4) {
		panic("NewKey returns two different keys given same stream")
	}

	// Test if it creates two different with random stream
	stream := random.Stream
	s5 := suite.NewKey(stream)
	s6 := suite.NewKey(stream)
	if s5.Equal(s6) {
		panic("NewKey returns same key given random stream")
	}

	// Test the public-key group arithmetic
	TestGroup(suite)
}
Пример #4
0
// Apply a standard set of validation tests to a ciphersuite.
func TestSuite(suite abstract.Suite) {

	// Try hashing something
	h := suite.Hash()
	l := h.Size()
	//println("HashLen: ",l)
	h.Write([]byte("abc"))
	hb := h.Sum(nil)
	//println("Hash:")
	//println(hex.Dump(hb))
	if h.Size() != l || len(hb) != l {
		panic("inconsistent hash output length")
	}

	// Generate some pseudorandom bits
	s := suite.Cipher(hb)
	sb := make([]byte, 128)
	s.XORKeyStream(sb, sb)
	//println("Stream:")
	//println(hex.Dump(sb))

	// Test the public-key group arithmetic
	TestGroup(suite)
}
Пример #5
0
// HashFileSuite returns the hash of a file using the hashing function of the
// suite given.
func HashFileSuite(suite abstract.Suite, file string) ([]byte, error) {
	return HashFile(suite.Hash(), file)
}
Пример #6
0
// HashStreamSuite will hash the stream using the hashing function of the suite
func HashStreamSuite(suite abstract.Suite, stream io.Reader) ([]byte, error) {
	return HashStream(suite.Hash(), stream)
}
Пример #7
0
// HashArgsSuite makes a new hash from the suite and calls HashArgs
func HashArgsSuite(suite abstract.Suite, args ...encoding.BinaryMarshaler) ([]byte, error) {
	return HashArgs(suite.Hash(), args...)
}