Exemple #1
0
// Hash hashes the data in a chunk into a cas.Key.
//
// Hash makes sure to never return a Special Key.
func Hash(chunk *chunks.Chunk) cas.Key {
	var pers [blake2b.PersonSize]byte
	copy(pers[:], personalizationPrefix)
	copy(pers[len(personalizationPrefix):], chunk.Type)
	config := &blake2b.Config{
		Size:   cas.KeySize,
		Person: pers[:],
		Tree: &blake2b.Tree{
			// We are faking tree mode without any intent to actually
			// follow all the rules, to be able to feed the level
			// into the hash function. These settings are dubious, but
			// we need to do something to make having Tree legal.
			Fanout:        0,
			MaxDepth:      255,
			InnerHashSize: cas.KeySize,

			NodeDepth: chunk.Level,
		},
	}
	h, err := blake2b.New(config)
	if err != nil {
		// we don't let outside data directly influence the config, so
		// this is always localized programmer error
		panic(fmt.Errorf("blake2b config error: %v", err))
	}

	if len(chunk.Buf) == 0 {
		return cas.Empty
	}
	_, _ = h.Write(chunk.Buf)
	keybuf := h.Sum(nil)
	return makeKey(keybuf)
}
Exemple #2
0
// Init readies longHash for an output of length n.
func (lh *longHash) Init(n int) {
	lh.n = n
	lh.h.Reset()
	lh.h0 = lh.h
	lh.h1 = lh.h
	var err error
	if n < 64 {
		lh.h0, err = blake2b.New(&blake2b.Config{Size: uint8(n)})
	} else if n%64 != 0 {
		n := 33 + (n+31)%32
		lh.h1, err = blake2b.New(&blake2b.Config{Size: uint8(n)})
	}
	if err != nil {
		panic(err)
	}
	put32(lh.buf[:4], uint32(n))
	lh.Write(lh.buf[:4])
}
Exemple #3
0
// newHash returns new BLAKE2b hash keyed with RefHash key.
func newHash() hash.Hash {
	h, err := blake2b.New(&blake2b.Config{
		Size:   RefLen,
		Key:    config.Keys.RefHash[:],
		Person: []byte("hesfic"),
	})
	if err != nil {
		panic(err.Error())
	}
	return h

}
Exemple #4
0
func boxSealNonce(ephemeralPk, publicKey *[32]byte) (*[24]byte, error) {
	nonce := new([24]byte)
	hashConfig := &blake2b.Config{Size: 24}
	hashFn, err := blake2b.New(hashConfig)
	if err != nil {
		return nil, errors.New("Failed to create blake2b hash function")
	}
	hashFn.Write(ephemeralPk[0:32])
	hashFn.Write(publicKey[0:32])
	nonceSum := hashFn.Sum(nil)
	copy(nonce[:], nonceSum[0:24])
	return nonce, nil
}
Exemple #5
0
func (s *Convergent) computeBoxedKey(key []byte) []byte {
	conf := blake2b.Config{
		Size:   cas.KeySize,
		Key:    s.secret[:],
		Person: personalizeKey,
	}
	h, err := blake2b.New(&conf)
	if err != nil {
		panic(fmt.Errorf("blake2 config failure: %v", err))
	}
	// hash.Hash docs say it never fails
	_, _ = h.Write(key)
	return h.Sum(nil)
}
Exemple #6
0
// Nonce summarizes key, type and level so mismatch of e.g. type can
// be detected.
func (s *Convergent) makeNonce(key []byte) *[nonceSize]byte {
	conf := blake2b.Config{
		Size:   nonceSize,
		Person: personalizeNonce,
	}
	h, err := blake2b.New(&conf)
	if err != nil {
		panic(fmt.Errorf("blake2 config failure: %v", err))
	}
	// hash.Hash docs say it never fails
	_, _ = h.Write(key)

	var ret [nonceSize]byte
	h.Sum(ret[:0])
	return &ret
}
func hashPassword(password, salt string) (result string) {
	c := &blake2b.Config{
		Salt: []byte(salt),
		Size: blake2b.Size,
	}

	b2b, err := blake2b.New(c)
	if err != nil {
		panic(err)
	}

	b2b.Reset()

	fin := bytes.NewBufferString(password + Config.Site.Pepper)
	io.Copy(b2b, fin)

	result = fmt.Sprintf("%x", b2b.Sum(nil))

	return
}
Exemple #8
0
var (
	algoFlag = flag.String("a", "blake2b", "hash algorithm (blake2b, blake2s)")
	sizeFlag = flag.Int("s", 0, "digest size in bytes (0 = default)")
)

type hashDesc struct {
	name    string
	maxSize int
	maker   func(size uint8) (hash.Hash, error)
}

var algorithms = map[string]hashDesc{
	"blake2b": {
		"BLAKE2b",
		blake2b.Size,
		func(size uint8) (hash.Hash, error) { return blake2b.New(&blake2b.Config{Size: size}) },
	},
	"blake2s": {
		"BLAKE2s",
		blake2s.Size,
		func(size uint8) (hash.Hash, error) { return blake2s.New(&blake2s.Config{Size: size}) },
	},
}

func calcSum(f *os.File, h hash.Hash) (sum []byte, err error) {
	h.Reset()
	_, err = io.Copy(h, f)
	sum = h.Sum(nil)
	return
}
Exemple #9
0
func blakeLong(out []byte, in []byte) error {
	var (
		outlen      = len(out)
		outlenBytes = make([]byte, 4)
	)
	binary.LittleEndian.PutUint32(outlenBytes, uint32(outlen))

	if len(out) <= blakeOutBytes {
		hash, err := blake2b.New(&blake2b.Config{
			Size: uint8(outlen),
		})
		if err != nil {
			return err
		}

		if _, err = hash.Write(outlenBytes); err != nil {
			return err
		}
		if _, err = hash.Write(in); err != nil {
			return err
		}
		sum := hash.Sum(nil)
		copy(out, sum)
		return nil
	}

	var (
		toProduce uint32
		buffer    [blakeOutBytes]byte
	)

	hash, err := blake2b.New(&blake2b.Config{
		Size: blakeOutBytes,
	})
	if err != nil {
		return err
	}

	if _, err = hash.Write(outlenBytes); err != nil {
		return err
	}
	if _, err = hash.Write(in); err != nil {
		return err
	}
	sum := hash.Sum(nil)
	copy(out, sum[:blakeOutBytes/2])
	out = out[blakeOutBytes/2:]
	toProduce = uint32(outlen) - blakeOutBytes/2

	for toProduce > blakeOutBytes {
		copy(buffer[:], sum)
		hash.Reset()
		if _, err = hash.Write(buffer[:]); err != nil {
			return err
		}
		sum = hash.Sum(nil)
		copy(out, sum[:blakeOutBytes/2])
		out = out[blakeOutBytes/2:]
		toProduce -= blakeOutBytes / 2
	}

	copy(buffer[:], sum[:blakeOutBytes])
	hash, err = blake2b.New(&blake2b.Config{
		Size: uint8(toProduce),
	})
	if err != nil {
		return err
	}
	if _, err = hash.Write(buffer[:]); err != nil {
		return err
	}
	sum = hash.Sum(nil)
	copy(out, sum)

	return nil
}
Exemple #10
0
func NewImage(r *http.Request, user *User) (i *Image, err error) {
	userUpload, header, err := r.FormFile("file")
	if err != nil {
		return nil, err
	}

	data, err := ioutil.ReadAll(userUpload)
	if err != nil {
		return nil, err
	}

	c := &blake2b.Config{
		Salt: []byte("lol as if"),
		Size: blake2b.Size,
	}

	b2b, err := blake2b.New(c)
	if err != nil {
		panic(err)
	}

	b2b.Reset()
	io.Copy(b2b, bytes.NewBuffer(data))
	hash := fmt.Sprintf("%x", b2b.Sum(nil))

	tagsRaw := r.FormValue("tags")
	tags := strings.Split(tagsRaw, ",")

	for _, tag := range tags {
		if strings.HasPrefix(tag, "rating:") {
			goto ok
		}
	}

	return nil, ErrNeedRatingTag

ok:

	mime := header.Header.Get("Content-Type")

	if mime == "image/svg+xml" {
		return nil, errors.New("Unsupported image format")
	}

	if !strings.HasPrefix(mime, "image/") {
		return nil, errors.New("Unsupported image format")
	}

	i = &Image{
		UUID:        uuid.New(),
		Poster:      user,
		PosterID:    user.ID,
		Mime:        mime,
		Filename:    header.Filename,
		Hash:        hash,
		Description: r.Form.Get("description"),
	}

	for _, tag := range tags {
		t, err := NewTag(tag)
		if err != nil {
			return nil, err
		}

		i.Tags = append(i.Tags, t)
	}

	err = os.Mkdir(Config.Storage.Path+"/"+i.UUID, os.ModePerm)
	if err != nil {
		return nil, err
	}

	fout, err := os.Create(Config.Storage.Path + "/" + i.UUID + "/" + i.Filename)
	if err != nil {
		return nil, err
	}

	io.Copy(fout, bytes.NewBuffer(data))

	conn := Redis.Get()
	defer conn.Close()

	query := Db.Create(i)
	if query.Error != nil {
		return nil, query.Error
	}

	_, err = conn.Do("RPUSH", "uploads", i.UUID)
	if err != nil {
		return nil, err
	}

	return
}
Exemple #11
0
func initialHash(blockhash *[prehashSeedLength]byte, ctx *context, variant Variant) error {
	state, err := blake2b.New(&blake2b.Config{
		Size: prehashDigestLength,
	})
	if err != nil {
		return err
	}

	value := make([]byte, 4) // 32-bit expressed in 4xuint8

	binary.LittleEndian.PutUint32(value, ctx.lanes)
	if _, err = state.Write(value); err != nil {
		return err
	}

	binary.LittleEndian.PutUint32(value, uint32(len(ctx.out)))
	if _, err = state.Write(value); err != nil {
		return err
	}

	binary.LittleEndian.PutUint32(value, ctx.memoryCost)
	if _, err = state.Write(value); err != nil {
		return err
	}

	binary.LittleEndian.PutUint32(value, ctx.timeCost)
	if _, err = state.Write(value); err != nil {
		return err
	}

	binary.LittleEndian.PutUint32(value, versionNumber)
	if _, err = state.Write(value); err != nil {
		return err
	}

	binary.LittleEndian.PutUint32(value, uint32(variant))
	if _, err = state.Write(value); err != nil {
		return err
	}

	binary.LittleEndian.PutUint32(value, uint32(len(ctx.pwd)))
	if _, err = state.Write(value); err != nil {
		return err
	}

	if ctx.pwd != nil {
		if _, err = state.Write(ctx.pwd); err != nil {
			return err
		}
	}

	binary.LittleEndian.PutUint32(value, uint32(len(ctx.salt)))
	if _, err = state.Write(value); err != nil {
		return err
	}

	if ctx.salt != nil {
		if _, err = state.Write(ctx.salt); err != nil {
			return err
		}
	}

	binary.LittleEndian.PutUint32(value, uint32(len(ctx.secret)))
	if _, err = state.Write(value); err != nil {
		return err
	}

	if ctx.secret != nil {
		if _, err = state.Write(ctx.secret); err != nil {
			return err
		}
	}

	binary.LittleEndian.PutUint32(value, uint32(len(ctx.ad)))
	if _, err = state.Write(value); err != nil {
		return err
	}

	if ctx.ad != nil {
		if _, err = state.Write(ctx.ad); err != nil {
			return err
		}
	}

	result := state.Sum(nil)
	for i, v := range result {
		blockhash[i] = v
	}

	return nil
}